se optimiza consulta de rutas de mapa
parent
53849df320
commit
10b5741e6d
|
@ -2,6 +2,9 @@
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from rest_framework.decorators import action, api_view, schema
|
from rest_framework.decorators import action, api_view, schema
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.db.models import F, Subquery, Value
|
||||||
|
from django.db.models.functions import Coalesce
|
||||||
|
|
||||||
from ..models import Paradero, ParaderoImagen
|
from ..models import Paradero, ParaderoImagen
|
||||||
from ..models import Linea, GtfsShape, GtfsTrips
|
from ..models import Linea, GtfsShape, GtfsTrips
|
||||||
from .paradero import url_image_paradero
|
from .paradero import url_image_paradero
|
||||||
|
@ -45,19 +48,18 @@ def paraderos(request):
|
||||||
def rutas(request):
|
def rutas(request):
|
||||||
id_linea = request.GET.get('id_linea')
|
id_linea = request.GET.get('id_linea')
|
||||||
|
|
||||||
sectores = GtfsTrips.objects.filter(id_linea = id_linea).all()
|
# Subquery para obtener los id_shape distintos de gtfs_trips
|
||||||
data = []
|
subquery = GtfsTrips.objects.filter(id_linea = id_linea).values('id_shape').distinct()
|
||||||
for s in sectores:
|
|
||||||
rutas = GtfsShape.objects.filter(id_shape = s.id_shape).order_by('shape_pt_sequence')
|
# Consulta principal con inner join y ordenamiento
|
||||||
for r in rutas:
|
queryset = GtfsShape.objects.annotate(
|
||||||
data.append({
|
id_shape_subquery=Subquery(subquery),
|
||||||
'shape_pt_lat': r.shape_pt_lat,
|
shape_pt_sequence_coalesce=Coalesce('shape_pt_sequence', Value(0))
|
||||||
'shape_pt_lon': r.shape_pt_lon,
|
).filter(id_shape=F('id_shape_subquery')).order_by('shape_pt_sequence_coalesce')
|
||||||
'shape_pt_sequence': r.shape_pt_sequence
|
|
||||||
})
|
result = queryset.values('shape_pt_lat', 'shape_pt_lon', 'shape_pt_sequence')
|
||||||
data_ordenada = sorted(data, key=lambda k: k['shape_pt_sequence'])
|
|
||||||
|
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
'google_api_key': google_api_key,
|
'google_api_key': google_api_key,
|
||||||
'positions': data_ordenada
|
'positions': list(result)
|
||||||
})
|
})
|
Loading…
Reference in New Issue