2017-11-09 23:25:08 +01:00
|
|
|
from itertools import chain
|
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
from django.core.cache import cache
|
2017-10-19 17:20:55 +02:00
|
|
|
from django.utils.functional import cached_property
|
2017-10-20 22:13:46 +02:00
|
|
|
from shapely import prepared
|
2017-10-10 17:49:53 +02:00
|
|
|
from shapely.geometry import box
|
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
from c3nav.mapdata.cache import MapHistory
|
2017-11-09 21:00:20 +01:00
|
|
|
from c3nav.mapdata.models import Level, MapUpdate
|
2017-11-08 18:21:54 +01:00
|
|
|
from c3nav.mapdata.render.data import LevelRenderData, hybrid_union
|
2017-11-05 12:32:07 +01:00
|
|
|
from c3nav.mapdata.render.engines.base import FillAttribs, StrokeAttribs
|
2017-10-10 14:39:11 +02:00
|
|
|
|
|
|
|
|
2017-11-06 11:18:45 +01:00
|
|
|
class MapRenderer:
|
2017-11-09 23:25:08 +01:00
|
|
|
def __init__(self, level, minx, miny, maxx, maxy, scale=1, access_permissions=None, full_levels=False):
|
2017-11-09 20:14:23 +01:00
|
|
|
self.level = level.pk if isinstance(level, Level) else level
|
2017-10-19 17:20:55 +02:00
|
|
|
self.minx = minx
|
2017-10-29 11:32:44 +01:00
|
|
|
self.miny = miny
|
2017-10-19 17:20:55 +02:00
|
|
|
self.maxx = maxx
|
2017-10-29 11:32:44 +01:00
|
|
|
self.maxy = maxy
|
2017-10-19 17:20:55 +02:00
|
|
|
self.scale = scale
|
2017-11-09 20:14:23 +01:00
|
|
|
self.access_permissions = set(access_permissions) if access_permissions else set()
|
2017-11-09 23:25:08 +01:00
|
|
|
self.full_levels = full_levels
|
2017-10-10 14:39:11 +02:00
|
|
|
|
2017-11-04 23:21:36 +01:00
|
|
|
self.width = int(round((maxx - minx) * scale))
|
|
|
|
self.height = int(round((maxy - miny) * scale))
|
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
@cached_property
|
|
|
|
def bbox(self):
|
|
|
|
return box(self.minx-1, self.miny-1, self.maxx+1, self.maxy+1)
|
2017-10-10 17:49:53 +02:00
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
@cached_property
|
|
|
|
def level_render_data(self):
|
2017-11-08 18:21:54 +01:00
|
|
|
return LevelRenderData.get(self.level)
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
@cached_property
|
|
|
|
def last_update(self):
|
2017-11-16 02:03:36 +01:00
|
|
|
return MapHistory.open_level_cached(self.level, 'composite').last_update(self.minx, self.miny,
|
|
|
|
self.maxx, self.maxy)
|
2017-10-24 18:12:46 +02:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def update_cache_key(self):
|
|
|
|
return MapUpdate.build_cache_key(*self.last_update)
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
@cached_property
|
|
|
|
def affected_access_restrictions(self):
|
2017-10-24 18:12:46 +02:00
|
|
|
cache_key = 'mapdata:affected-ars-%.2f-%.2f-%.2f-%.2f:%s' % (self.minx, self.miny, self.maxx, self.maxy,
|
|
|
|
self.update_cache_key)
|
|
|
|
result = cache.get(cache_key, None)
|
|
|
|
if result is None:
|
|
|
|
result = set(ar for ar, area in self.level_render_data.access_restriction_affected.items()
|
|
|
|
if area.intersects(self.bbox))
|
|
|
|
cache.set(cache_key, result, 120)
|
|
|
|
return result
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
@cached_property
|
|
|
|
def unlocked_access_restrictions(self):
|
2017-10-24 22:45:57 +02:00
|
|
|
return self.affected_access_restrictions & self.access_permissions
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
@cached_property
|
|
|
|
def access_cache_key(self):
|
|
|
|
return '_'.join(str(i) for i in sorted(self.unlocked_access_restrictions)) or '0'
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
@cached_property
|
|
|
|
def cache_key(self):
|
|
|
|
return self.update_cache_key + ':' + self.access_cache_key
|
|
|
|
|
2017-11-14 13:40:19 +01:00
|
|
|
def render(self, engine_cls, center=True):
|
2017-11-06 11:18:45 +01:00
|
|
|
engine = engine_cls(self.width, self.height, self.minx, self.miny,
|
2017-11-14 13:40:19 +01:00
|
|
|
scale=self.scale, buffer=1, background='#DCDCDC', center=center)
|
2017-10-19 16:33:32 +02:00
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# add no access restriction to “unlocked“ access restrictions so lookup gets easier
|
2017-10-19 17:20:55 +02:00
|
|
|
unlocked_access_restrictions = self.unlocked_access_restrictions | set([None])
|
2017-10-16 17:10:32 +02:00
|
|
|
|
2017-11-08 17:52:27 +01:00
|
|
|
bbox = prepared.prep(self.bbox)
|
2017-10-20 22:13:46 +02:00
|
|
|
|
2017-11-09 23:25:08 +01:00
|
|
|
if self.full_levels:
|
|
|
|
levels = tuple(chain(*(
|
|
|
|
tuple(sublevel for sublevel in LevelRenderData.get(level.pk).levels
|
|
|
|
if sublevel.pk == level.pk or sublevel.on_top_of_id == level.pk)
|
|
|
|
for level in self.level_render_data.levels if level.on_top_of_id is None
|
|
|
|
)))
|
|
|
|
else:
|
|
|
|
levels = self.level_render_data.levels
|
|
|
|
|
2017-11-14 02:49:02 +01:00
|
|
|
min_altitude = min(chain(*(tuple(area.altitude for area in geoms.altitudeareas)
|
2017-11-14 18:02:17 +01:00
|
|
|
for geoms in levels)))
|
2017-11-10 00:25:27 +01:00
|
|
|
|
2017-11-14 17:23:45 +01:00
|
|
|
not_full_levels = not self.full_levels and engine.is_3d
|
|
|
|
full_levels = self.full_levels and engine.is_3d
|
2017-11-09 23:25:08 +01:00
|
|
|
for geoms in levels:
|
2017-11-08 17:52:27 +01:00
|
|
|
if not bbox.intersects(geoms.affected_area):
|
2017-10-20 22:13:46 +02:00
|
|
|
continue
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-14 14:37:32 +01:00
|
|
|
engine.add_group('level_%s' % geoms.short_label)
|
2017-11-14 14:27:50 +01:00
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# hide indoor and outdoor rooms if their access restriction was not unlocked
|
2017-11-08 17:52:27 +01:00
|
|
|
add_walls = hybrid_union(tuple(area for access_restriction, area in geoms.restricted_spaces_indoors.items()
|
|
|
|
if access_restriction not in unlocked_access_restrictions))
|
|
|
|
crop_areas = hybrid_union(
|
2017-10-19 17:48:12 +02:00
|
|
|
tuple(area for access_restriction, area in geoms.restricted_spaces_outdoors.items()
|
|
|
|
if access_restriction not in unlocked_access_restrictions)
|
|
|
|
).union(add_walls)
|
|
|
|
|
2017-11-14 17:23:45 +01:00
|
|
|
if not_full_levels:
|
2017-11-14 14:27:50 +01:00
|
|
|
engine.add_geometry(geoms.walls_base, fill=FillAttribs('#aaaaaa'), category='walls')
|
2017-11-14 18:02:17 +01:00
|
|
|
engine.add_geometry(geoms.walls_bottom.fit(scale=geoms.min_altitude-min_altitude,
|
|
|
|
offset=min_altitude-int(0.7*1000)),
|
2017-11-14 17:55:35 +01:00
|
|
|
fill=FillAttribs('#aaaaaa'), category='walls')
|
2017-11-14 18:18:22 +01:00
|
|
|
for i, altitudearea in enumerate(geoms.altitudeareas):
|
2017-11-14 18:02:17 +01:00
|
|
|
scale = (altitudearea.altitude - min_altitude) / int(0.7 * 1000)
|
|
|
|
offset = (min_altitude - int(0.7*1000)) - (altitudearea.altitude - int(0.7*1000)) * scale
|
2017-11-14 17:55:35 +01:00
|
|
|
geometry = altitudearea.geometry.difference(crop_areas)
|
|
|
|
engine.add_geometry(geometry.fit(scale=scale, offset=offset).filter(top=False),
|
2017-11-14 18:18:22 +01:00
|
|
|
fill=FillAttribs('#eeeeee'), category='ground', item=i)
|
2017-11-10 00:25:27 +01:00
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# render altitude areas in default ground color and add ground colors to each one afterwards
|
2017-11-07 22:16:52 +01:00
|
|
|
# shadows are directly calculated and added by the engine
|
2017-11-14 18:18:22 +01:00
|
|
|
for i, altitudearea in enumerate(geoms.altitudeareas):
|
2017-11-14 01:29:17 +01:00
|
|
|
geometry = altitudearea.geometry.difference(crop_areas)
|
2017-11-14 17:55:35 +01:00
|
|
|
if not_full_levels:
|
2017-11-14 01:29:17 +01:00
|
|
|
geometry = geometry.filter(bottom=False)
|
2017-11-14 14:27:50 +01:00
|
|
|
engine.add_geometry(geometry, altitude=altitudearea.altitude, fill=FillAttribs('#eeeeee'),
|
2017-11-14 18:18:22 +01:00
|
|
|
category='ground', item=i)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-14 15:06:50 +01:00
|
|
|
i = 0
|
2017-10-19 17:20:55 +02:00
|
|
|
for color, areas in altitudearea.colors.items():
|
2017-10-19 17:55:41 +02:00
|
|
|
# only select ground colors if their access restriction is unlocked
|
2017-10-19 17:20:55 +02:00
|
|
|
areas = tuple(area for access_restriction, area in areas.items()
|
|
|
|
if access_restriction in unlocked_access_restrictions)
|
|
|
|
if areas:
|
2017-11-14 15:06:50 +01:00
|
|
|
i += 1
|
2017-11-14 18:18:22 +01:00
|
|
|
engine.add_geometry(hybrid_union(areas), fill=FillAttribs(color),
|
|
|
|
category='groundcolor%s' % i, item=i)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-14 22:18:53 +01:00
|
|
|
for height, obstacle in altitudearea.obstacles.items():
|
|
|
|
engine.add_geometry(obstacle, fill=FillAttribs('#cccccc'), category='obstacles')
|
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# add walls, stroke_px makes sure that all walls are at least 1px thick on all zoom levels,
|
2017-10-29 09:32:15 +01:00
|
|
|
walls = None
|
2017-10-20 22:02:51 +02:00
|
|
|
if not add_walls.is_empty or not geoms.walls.is_empty:
|
2017-11-08 17:52:27 +01:00
|
|
|
walls = geoms.walls.union(add_walls)
|
2017-10-29 09:32:15 +01:00
|
|
|
|
2017-11-14 17:23:45 +01:00
|
|
|
walls_extended = geoms.walls_extended and full_levels
|
2017-10-29 09:32:15 +01:00
|
|
|
if walls is not None:
|
2017-11-14 17:23:45 +01:00
|
|
|
engine.add_geometry(walls.filter(bottom=not not_full_levels,
|
2017-11-14 16:28:44 +01:00
|
|
|
top=not walls_extended),
|
2017-11-14 14:27:50 +01:00
|
|
|
height=geoms.default_height, fill=FillAttribs('#aaaaaa'), category='walls')
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-17 20:07:00 +01:00
|
|
|
for short_wall in geoms.short_walls:
|
|
|
|
engine.add_geometry(short_wall.filter(bottom=not not_full_levels),
|
|
|
|
height=geoms.default_height, fill=FillAttribs('#aaaaaa'), category='walls')
|
|
|
|
|
2017-11-14 16:28:44 +01:00
|
|
|
if walls_extended:
|
2017-11-14 17:23:45 +01:00
|
|
|
engine.add_geometry(geoms.walls_extended, fill=FillAttribs('#aaaaaa'), category='walls')
|
2017-11-10 01:47:55 +01:00
|
|
|
|
2017-11-14 17:23:45 +01:00
|
|
|
doors_extended = geoms.doors_extended and full_levels
|
2017-10-20 22:02:51 +02:00
|
|
|
if not geoms.doors.is_empty:
|
2017-11-14 17:23:45 +01:00
|
|
|
engine.add_geometry(geoms.doors.difference(add_walls).filter(top=not doors_extended),
|
|
|
|
fill=FillAttribs('#ffffff'),
|
|
|
|
stroke=StrokeAttribs('#ffffff', 0.05, min_px=0.2),
|
|
|
|
category='doors')
|
|
|
|
|
|
|
|
if doors_extended:
|
|
|
|
engine.add_geometry(geoms.doors_extended, fill=FillAttribs('#aaaaaa'), category='doors')
|
2017-10-29 09:32:15 +01:00
|
|
|
|
|
|
|
if walls is not None:
|
2017-11-14 14:27:50 +01:00
|
|
|
engine.add_geometry(walls, stroke=StrokeAttribs('#666666', 0.05, min_px=0.2), category='walls')
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-06 11:18:45 +01:00
|
|
|
return engine
|