Adición del modo mapa satelital
This commit is contained in:
90
pdf/views.py
90
pdf/views.py
@@ -12,9 +12,61 @@ from zoneinfo import ZoneInfo
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
from operator import getitem
|
from operator import getitem
|
||||||
from pypdf import PdfReader, PdfWriter
|
from pypdf import PdfReader, PdfWriter
|
||||||
|
import urllib.parse
|
||||||
|
import requests
|
||||||
|
import base64
|
||||||
import json
|
import json
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
|
def static_map_view(geojson_colec, sat_map):
|
||||||
|
|
||||||
|
geojson_dict = ast.literal_eval(geojson_colec)
|
||||||
|
if sat_map:
|
||||||
|
for geo in geojson_dict['features'] :
|
||||||
|
if geo['geometry'].get('type') == 'Polygon':
|
||||||
|
geo['properties'] = {
|
||||||
|
"stroke": "#00ffff",
|
||||||
|
"stroke-width": 2,
|
||||||
|
"stroke-opacity": 0.8,
|
||||||
|
"fill": "#00ffff",
|
||||||
|
"fill-opacity": 0.3
|
||||||
|
}
|
||||||
|
elif geo['geometry'].get('type') == 'Point':
|
||||||
|
geo['properties'] = {
|
||||||
|
"marker-color": "#ff0000",
|
||||||
|
"marker-size": "small"
|
||||||
|
}
|
||||||
|
elif geo['geometry'].get('type') == 'LineString':
|
||||||
|
geo['properties'] = {
|
||||||
|
"stroke": "#0000ff",
|
||||||
|
"stroke-width": 2,
|
||||||
|
"stroke-opacity": 0.8
|
||||||
|
}
|
||||||
|
|
||||||
|
geojson_string = json.dumps(geojson_dict, separators=(',', ':'))
|
||||||
|
encoded_geojson = urllib.parse.quote(geojson_string)
|
||||||
|
|
||||||
|
username = "mapbox"
|
||||||
|
if sat_map:
|
||||||
|
style_id = "satellite-streets-v12" # streets-v12 | outdoors-v11 | satellite-streets-v12
|
||||||
|
else:
|
||||||
|
style_id = "light-v11"
|
||||||
|
|
||||||
|
width, height = 340, 340
|
||||||
|
|
||||||
|
# Procedimiento pendiente para extraer el token desde la configuración del sistema
|
||||||
|
access_token = 'pk.eyJ1IjoianNidXJnb3N2IiwiYSI6ImNsaWhoN3ZtMTBxeHUzZm1xaGM0cGpxOXIifQ.ki3vldVB5MwZMqDSpLSjLg'
|
||||||
|
|
||||||
|
mapbox_url = (
|
||||||
|
f"https://api.mapbox.com/styles/v1/{username}/{style_id}/static/"
|
||||||
|
f"geojson({encoded_geojson})/auto/{width}x{height}"
|
||||||
|
f"?access_token={access_token}"
|
||||||
|
)
|
||||||
|
|
||||||
|
response = requests.get(mapbox_url)
|
||||||
|
|
||||||
|
return base64.b64encode(response.content).decode('utf-8')
|
||||||
|
|
||||||
def ajustar_medios(resource_media_rel, medios_list):
|
def ajustar_medios(resource_media_rel, medios_list):
|
||||||
medios = []
|
medios = []
|
||||||
img=["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/png", "image/tif", "image/tiff"]
|
img=["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/png", "image/tif", "image/tiff"]
|
||||||
@@ -83,12 +135,13 @@ def strdict_to_dict(strdict):
|
|||||||
pass
|
pass
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
def export_resource_pdf(request, resourceid, printubic, satmap, printimgs, printpdfs):
|
||||||
resource = Resource.objects.get(pk=resourceid)
|
resource = Resource.objects.get(pk=resourceid)
|
||||||
resource_data = resource.to_json()
|
resource_data = resource.to_json()
|
||||||
|
|
||||||
# Corrección de medios relacionados, se normalizan claves y se adicionan propiedades
|
# Corrección de medios relacionados, se normalizan claves y se adicionan propiedades
|
||||||
ruta_medios = find_key_path( resource_data, "Medio de información")
|
ruta_medios = find_key_path( resource_data, "Medio de información")
|
||||||
|
|
||||||
if ruta_medios is not None:
|
if ruta_medios is not None:
|
||||||
valor_medios=reduce(lambda dict_layer, key: dict_layer[key], ruta_medios, resource_data)
|
valor_medios=reduce(lambda dict_layer, key: dict_layer[key], ruta_medios, resource_data)
|
||||||
resource_rel=resource.get_related_resources(user=request.user,resourceinstance_graphid='6afc4e9e-727e-4152-8084-309267752650')
|
resource_rel=resource.get_related_resources(user=request.user,resourceinstance_graphid='6afc4e9e-727e-4152-8084-309267752650')
|
||||||
@@ -96,6 +149,7 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
|||||||
reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] = ajustar_medios(resource_rel,valor_medios)
|
reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] = ajustar_medios(resource_rel,valor_medios)
|
||||||
|
|
||||||
# Cambio de datos gis en texto a formato json
|
# Cambio de datos gis en texto a formato json
|
||||||
|
|
||||||
ruta_gis = find_key_path( resource_data, "Coordenadas / Geometría")
|
ruta_gis = find_key_path( resource_data, "Coordenadas / Geometría")
|
||||||
if ruta_gis is not None:
|
if ruta_gis is not None:
|
||||||
valor_gis=reduce(lambda dict_layer, key: dict_layer[key], ruta_gis, resource_data)["@value"]
|
valor_gis=reduce(lambda dict_layer, key: dict_layer[key], ruta_gis, resource_data)["@value"]
|
||||||
@@ -111,7 +165,13 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
|||||||
# Fecha del informe
|
# Fecha del informe
|
||||||
sprpcb_url=request.build_absolute_uri('/')[:-1]
|
sprpcb_url=request.build_absolute_uri('/')[:-1]
|
||||||
fecha=datetime.now(ZoneInfo("America/La_Paz"))
|
fecha=datetime.now(ZoneInfo("America/La_Paz"))
|
||||||
|
|
||||||
|
map_img = None
|
||||||
|
|
||||||
|
if ruta_gis is not None and bool(int(printubic)) :
|
||||||
|
if reduce(getitem, ruta_gis[:-1], resource_data)[ruta_gis[-1]]["@value"] :
|
||||||
|
map_img = static_map_view(reduce(getitem, ruta_gis[:-1], resource_data)[ruta_gis[-1]]["@value"], bool(int(satmap)))
|
||||||
|
|
||||||
resources = [{
|
resources = [{
|
||||||
'resourceinstanceid': str(resource.resourceinstanceid),
|
'resourceinstanceid': str(resource.resourceinstanceid),
|
||||||
'graph_id': str(resource.graph_id),
|
'graph_id': str(resource.graph_id),
|
||||||
@@ -120,8 +180,10 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
|||||||
'sprpcb_url': sprpcb_url,
|
'sprpcb_url': sprpcb_url,
|
||||||
'fecha': fecha,
|
'fecha': fecha,
|
||||||
'eta_group': eta_group,
|
'eta_group': eta_group,
|
||||||
|
'print_ubic': bool(int(printubic)),
|
||||||
'print_imgs': bool(int(printimgs)),
|
'print_imgs': bool(int(printimgs)),
|
||||||
'print_pdfs': bool(int(printpdfs))
|
'print_pdfs': bool(int(printpdfs)),
|
||||||
|
'map_img': map_img
|
||||||
}]
|
}]
|
||||||
|
|
||||||
context = {'resources': resources}
|
context = {'resources': resources}
|
||||||
@@ -151,16 +213,18 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
|||||||
|
|
||||||
root_path = Path(__file__).resolve().parent.parent
|
root_path = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
for medio in reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] :
|
if ruta_medios is not None and bool(int(printpdfs)) :
|
||||||
if medio['formato'] == 'application/pdf' and bool(int(printpdfs)) :
|
for medio in reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] :
|
||||||
merger.append(PdfReader(f'{root_path}/uploadedfiles/{medio["file"]}'))
|
if medio['formato'] == 'application/pdf' :
|
||||||
output_buffer = io.BytesIO()
|
merger.append(PdfReader(f'{root_path}/uploadedfiles/{medio["file"]}'))
|
||||||
merger.write(output_buffer)
|
output_buffer = io.BytesIO()
|
||||||
merger.close()
|
merger.write(output_buffer)
|
||||||
else:
|
merger.close()
|
||||||
output_buffer = io.BytesIO()
|
else:
|
||||||
merger.write(output_buffer)
|
output_buffer = io.BytesIO()
|
||||||
merger.close()
|
merger.write(output_buffer)
|
||||||
|
merger.close()
|
||||||
|
|
||||||
response = HttpResponse(output_buffer.getvalue(), content_type='application/pdf')
|
response = HttpResponse(output_buffer.getvalue(), content_type='application/pdf')
|
||||||
response['Content-Disposition'] = f'attachment; filename="{nombre_pdf}.pdf"'
|
response['Content-Disposition'] = f'attachment; filename="{nombre_pdf}.pdf"'
|
||||||
return response
|
return response
|
||||||
Reference in New Issue
Block a user