2017-05-21 23:39:26 +02:00
|
|
|
from itertools import chain
|
|
|
|
|
2017-05-26 21:26:37 +02:00
|
|
|
from rest_framework.decorators import list_route
|
2017-05-21 23:39:26 +02:00
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
from rest_framework.generics import get_object_or_404
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.viewsets import ViewSet
|
|
|
|
from shapely.ops import cascaded_union
|
|
|
|
|
2017-06-11 12:56:37 +02:00
|
|
|
from c3nav.mapdata.models import Section, Space
|
2017-05-21 23:39:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditorViewSet(ViewSet):
|
|
|
|
"""
|
|
|
|
Editor API
|
|
|
|
/geometries/ returns a list of geojson features, you have to specify ?section=<id> or ?space=<id>
|
|
|
|
/geometrystyles/ returns styling information for all geometry types
|
|
|
|
"""
|
2017-06-10 14:58:13 +02:00
|
|
|
def _get_section_geometries(self, section: Section):
|
|
|
|
buildings = section.buildings.all()
|
|
|
|
buildings_geom = cascaded_union([building.geometry for building in buildings])
|
|
|
|
spaces = {space.id: space for space in section.spaces.all()}
|
|
|
|
holes_geom = []
|
|
|
|
for space in spaces.values():
|
|
|
|
if space.outside:
|
|
|
|
space.geometry = space.geometry.difference(buildings_geom)
|
|
|
|
columns_geom = cascaded_union([column.geometry for column in space.columns.all()])
|
|
|
|
space.geometry = space.geometry.difference(columns_geom)
|
|
|
|
space_holes_geom = cascaded_union([hole.geometry for hole in space.holes.all()])
|
|
|
|
holes_geom.append(space_holes_geom.intersection(space.geometry))
|
|
|
|
space.geometry = space.geometry.difference(space_holes_geom)
|
|
|
|
holes_geom = cascaded_union(holes_geom)
|
|
|
|
|
|
|
|
for building in buildings:
|
|
|
|
building.original_geometry = building.geometry
|
|
|
|
for obj in chain(buildings, spaces.values()):
|
|
|
|
obj.geometry = obj.geometry.difference(holes_geom)
|
|
|
|
|
|
|
|
results = []
|
|
|
|
results.extend(buildings)
|
|
|
|
for door in section.doors.all():
|
|
|
|
results.append(door)
|
|
|
|
|
|
|
|
results.extend(spaces.values())
|
|
|
|
return results
|
|
|
|
|
2017-06-11 13:26:35 +02:00
|
|
|
def _get_sections_pk(self, section):
|
|
|
|
sections_under = ()
|
|
|
|
sections_on_top = ()
|
|
|
|
lower_section = section.lower().first()
|
|
|
|
primary_sections = (section,) + ((lower_section,) if lower_section else ())
|
|
|
|
secondary_sections = Section.objects.filter(on_top_of__in=primary_sections).values_list('pk', 'on_top_of')
|
|
|
|
if lower_section:
|
|
|
|
sections_under = tuple(pk for pk, on_top_of in secondary_sections if on_top_of == lower_section.pk)
|
|
|
|
if True:
|
|
|
|
sections_on_top = tuple(pk for pk, on_top_of in secondary_sections if on_top_of == section.pk)
|
|
|
|
sections = chain([section.pk], sections_under, sections_on_top)
|
|
|
|
return sections, sections_on_top, sections_under
|
|
|
|
|
2017-05-21 23:39:26 +02:00
|
|
|
@list_route(methods=['get'])
|
|
|
|
def geometries(self, request, *args, **kwargs):
|
|
|
|
section = request.GET.get('section')
|
|
|
|
space = request.GET.get('space')
|
|
|
|
if section is not None:
|
|
|
|
if space is not None:
|
|
|
|
raise ValidationError('Only section or space can be specified.')
|
|
|
|
section = get_object_or_404(Section, pk=section)
|
2017-06-10 14:58:13 +02:00
|
|
|
|
2017-06-11 13:26:35 +02:00
|
|
|
sections, sections_on_top, sections_under = self._get_sections_pk(section)
|
2017-06-10 14:58:13 +02:00
|
|
|
sections = Section.objects.filter(pk__in=sections).prefetch_related('buildings', 'spaces', 'doors',
|
|
|
|
'spaces__groups', 'spaces__holes',
|
|
|
|
'spaces__columns')
|
|
|
|
sections = {s.pk: s for s in sections}
|
|
|
|
|
|
|
|
section = sections[section.pk]
|
|
|
|
sections_under = [sections[pk] for pk in sections_under]
|
|
|
|
sections_on_top = [sections[pk] for pk in sections_on_top]
|
|
|
|
|
|
|
|
results = chain(
|
|
|
|
*(self._get_section_geometries(s) for s in sections_under),
|
|
|
|
self._get_section_geometries(section),
|
|
|
|
*(self._get_section_geometries(s) for s in sections_on_top)
|
|
|
|
)
|
|
|
|
|
2017-05-21 23:39:26 +02:00
|
|
|
return Response([obj.to_geojson() for obj in results])
|
|
|
|
elif space is not None:
|
2017-06-11 13:33:00 +02:00
|
|
|
space = get_object_or_404(Space.objects.select_related('section', 'section__on_top_of'), pk=space)
|
2017-05-21 23:39:26 +02:00
|
|
|
section = space.section
|
|
|
|
|
|
|
|
doors = [door for door in section.doors.all() if door.geometry.intersects(space.geometry)]
|
2017-06-09 15:40:52 +02:00
|
|
|
doors_space_geom = cascaded_union([door.geometry for door in doors]+[space.geometry])
|
2017-05-21 23:39:26 +02:00
|
|
|
|
2017-06-11 13:33:00 +02:00
|
|
|
sections, sections_on_top, sections_under = self._get_sections_pk(section.primary_section)
|
2017-06-11 13:26:35 +02:00
|
|
|
other_spaces = Space.objects.filter(section__pk__in=sections).prefetch_related('groups')
|
|
|
|
other_spaces = [s for s in other_spaces
|
2017-06-09 15:40:52 +02:00
|
|
|
if s.geometry.intersects(doors_space_geom) and s.pk != space.pk]
|
2017-05-21 23:39:26 +02:00
|
|
|
|
2017-05-27 16:19:49 +02:00
|
|
|
space.bounds = True
|
|
|
|
|
2017-06-09 15:40:52 +02:00
|
|
|
buildings = section.buildings.all()
|
|
|
|
buildings_geom = cascaded_union([building.geometry for building in buildings])
|
|
|
|
for other_space in other_spaces:
|
|
|
|
if other_space.outside:
|
|
|
|
other_space.geometry = other_space.geometry.difference(buildings_geom)
|
2017-06-11 13:26:35 +02:00
|
|
|
other_space.opacity = 0.4
|
|
|
|
other_space.color = '#ffffff'
|
|
|
|
for building in buildings:
|
|
|
|
building.opacity = 0.5
|
2017-06-09 15:40:52 +02:00
|
|
|
|
2017-05-21 23:55:12 +02:00
|
|
|
results = chain(
|
2017-06-09 15:40:52 +02:00
|
|
|
buildings,
|
2017-05-21 23:55:12 +02:00
|
|
|
doors,
|
|
|
|
[space],
|
2017-05-29 16:36:17 +02:00
|
|
|
space.areas.all().prefetch_related('groups'),
|
2017-06-08 15:19:12 +02:00
|
|
|
space.holes.all(),
|
2017-05-21 23:39:26 +02:00
|
|
|
space.stairs.all(),
|
|
|
|
space.obstacles.all(),
|
|
|
|
space.lineobstacles.all(),
|
2017-06-09 15:22:30 +02:00
|
|
|
space.columns.all(),
|
2017-05-29 16:36:17 +02:00
|
|
|
space.points.all().prefetch_related('groups'),
|
2017-06-09 15:40:52 +02:00
|
|
|
other_spaces,
|
2017-05-21 23:55:12 +02:00
|
|
|
)
|
2017-05-22 00:12:49 +02:00
|
|
|
return Response(sum([self._get_geojsons(obj) for obj in results], ()))
|
2017-05-21 23:39:26 +02:00
|
|
|
else:
|
|
|
|
raise ValidationError('No section or space specified.')
|
|
|
|
|
2017-05-22 00:12:49 +02:00
|
|
|
def _get_geojsons(self, obj):
|
|
|
|
return ((obj.to_shadow_geojson(),) if hasattr(obj, 'to_shadow_geojson') else ()) + (obj.to_geojson(),)
|
|
|
|
|
2017-05-21 23:39:26 +02:00
|
|
|
@list_route(methods=['get'])
|
|
|
|
def geometrystyles(self, request, *args, **kwargs):
|
|
|
|
return Response({
|
|
|
|
'building': '#929292',
|
|
|
|
'space': '#d1d1d1',
|
|
|
|
'hole': 'rgba(255, 0, 0, 0.3)',
|
|
|
|
'door': '#ffffff',
|
2017-05-27 19:01:38 +02:00
|
|
|
'area': 'rgba(85, 170, 255, 0.2)',
|
2017-05-22 00:12:49 +02:00
|
|
|
'stair': 'rgba(160, 0, 160, 0.5)',
|
2017-05-21 23:39:26 +02:00
|
|
|
'obstacle': '#999999',
|
|
|
|
'lineobstacle': '#999999',
|
2017-06-09 15:22:30 +02:00
|
|
|
'column': '#888888',
|
2017-05-21 23:39:26 +02:00
|
|
|
'point': '#4488cc',
|
2017-05-22 00:12:49 +02:00
|
|
|
'shadow': '#000000',
|
2017-05-21 23:39:26 +02:00
|
|
|
})
|