Adición del modo mapa satelital
This commit is contained in:
70
pdf/views.py
70
pdf/views.py
@@ -12,9 +12,61 @@ from zoneinfo import ZoneInfo
|
||||
from functools import reduce
|
||||
from operator import getitem
|
||||
from pypdf import PdfReader, PdfWriter
|
||||
import urllib.parse
|
||||
import requests
|
||||
import base64
|
||||
import json
|
||||
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):
|
||||
medios = []
|
||||
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
|
||||
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_data = resource.to_json()
|
||||
|
||||
# Corrección de medios relacionados, se normalizan claves y se adicionan propiedades
|
||||
ruta_medios = find_key_path( resource_data, "Medio de información")
|
||||
|
||||
if ruta_medios is not None:
|
||||
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')
|
||||
@@ -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)
|
||||
|
||||
# Cambio de datos gis en texto a formato json
|
||||
|
||||
ruta_gis = find_key_path( resource_data, "Coordenadas / Geometría")
|
||||
if ruta_gis is not None:
|
||||
valor_gis=reduce(lambda dict_layer, key: dict_layer[key], ruta_gis, resource_data)["@value"]
|
||||
@@ -112,6 +166,12 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
||||
sprpcb_url=request.build_absolute_uri('/')[:-1]
|
||||
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 = [{
|
||||
'resourceinstanceid': str(resource.resourceinstanceid),
|
||||
'graph_id': str(resource.graph_id),
|
||||
@@ -120,8 +180,10 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
||||
'sprpcb_url': sprpcb_url,
|
||||
'fecha': fecha,
|
||||
'eta_group': eta_group,
|
||||
'print_ubic': bool(int(printubic)),
|
||||
'print_imgs': bool(int(printimgs)),
|
||||
'print_pdfs': bool(int(printpdfs))
|
||||
'print_pdfs': bool(int(printpdfs)),
|
||||
'map_img': map_img
|
||||
}]
|
||||
|
||||
context = {'resources': resources}
|
||||
@@ -151,8 +213,9 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
||||
|
||||
root_path = Path(__file__).resolve().parent.parent
|
||||
|
||||
if ruta_medios is not None and bool(int(printpdfs)) :
|
||||
for medio in reduce(getitem, ruta_medios[:-1], resource_data)[ruta_medios[-1]] :
|
||||
if medio['formato'] == 'application/pdf' and bool(int(printpdfs)) :
|
||||
if medio['formato'] == 'application/pdf' :
|
||||
merger.append(PdfReader(f'{root_path}/uploadedfiles/{medio["file"]}'))
|
||||
output_buffer = io.BytesIO()
|
||||
merger.write(output_buffer)
|
||||
@@ -161,6 +224,7 @@ def export_resource_pdf(request, resourceid, printubic, printimgs, printpdfs):
|
||||
output_buffer = io.BytesIO()
|
||||
merger.write(output_buffer)
|
||||
merger.close()
|
||||
|
||||
response = HttpResponse(output_buffer.getvalue(), content_type='application/pdf')
|
||||
response['Content-Disposition'] = f'attachment; filename="{nombre_pdf}.pdf"'
|
||||
return response
|
||||
Reference in New Issue
Block a user