Select Git revision
create_db.py
Forked from
uffd / uffd
Source project has a limited visibility.
maps.py 2.79 KiB
import abc
import json
from django.contrib.gis.gdal import SpatialReference, CoordTransform
from rest_framework.views import APIView
from core.models.assemblies import Assembly
from core.models.conference import ConferenceExportCache
from .mixins import ConferenceSlugMixin
class AssembliesExportView(ConferenceSlugMixin, APIView, metaclass=abc.ABCMeta):
geometry_field = None
_cts = {} # cache of CoordTransforms (if needed)
def get_queryset(self):
exportable_states = Assembly.PLACED_STATES + [Assembly.State.HIDDEN]
return Assembly.objects.filter(conference=self.conference, state_assembly__in=exportable_states)
@staticmethod
def get_field_geojson(value, srid: int, ct_cache: dict = None):
if value.srid != srid:
if ct_cache is None:
ct_cache = {}
if value.srid not in ct_cache:
srs = SpatialReference(srid)
ct_cache[value.srid] = CoordTransform(value.srs, srs)
value.transform(ct_cache[value.srid])
return json.loads(value.geojson)
def get_geometry_field(self, obj):
return getattr(obj, self.geometry_field)
def get_geojson(self):
srid = 4326 # RFC7946 mandates WGS84
ct_cache = {}
features = []
result = {
'type': 'FeatureCollection',
# 'crs': {'type': 'name', 'properties': {'name': f'EPSG:{srid}'}}, # deprecated, not in RFC7946
'features': features,
}
for assembly in self.get_queryset().prefetch_related('parent').all():
geometry = self.get_geometry_field(assembly)
if geometry is None:
continue
feature = {
'type': 'Feature',
'id': assembly.slug,
'geometry': self.get_field_geojson(geometry, srid=srid, ct_cache=ct_cache),
'properties': {
'name': assembly.name,
'official': assembly.is_official,
'cluster': assembly.is_cluster,
'parent': assembly.parent.slug if assembly.parent else None,
},
}
features.append(feature)
return result
def get(self, request, *args, **kwargs):
cache_id = 'geojson-' + self.geometry_field
return ConferenceExportCache.handle_http_request(
request=request,
conference=self.conference,
type=ConferenceExportCache.Type.MAP,
ident=cache_id,
content_type='application/geo+json',
result_func=lambda: json.dumps(self.get_geojson()),
)
class AssembliesPoiExportView(AssembliesExportView):
geometry_field = 'location_point'
class AssembliesAreasExportView(AssembliesExportView):
geometry_field = 'location_boundaries'