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-19 16:33:32 +02:00
|
|
|
from shapely.ops import unary_union
|
2017-10-10 17:49:53 +02:00
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
from c3nav.mapdata.cache import MapHistory
|
|
|
|
from c3nav.mapdata.models import MapUpdate
|
2017-10-19 17:20:55 +02:00
|
|
|
from c3nav.mapdata.render.base import get_level_render_data
|
2017-10-10 14:39:11 +02:00
|
|
|
from c3nav.mapdata.utils.svg import SVGImage
|
|
|
|
|
|
|
|
|
2017-10-19 17:20:55 +02:00
|
|
|
class SVGRenderer:
|
|
|
|
def __init__(self, level, miny, minx, maxy, maxx, scale=1, user=None):
|
|
|
|
self.level = level
|
|
|
|
self.miny = miny
|
|
|
|
self.minx = minx
|
|
|
|
self.maxy = maxy
|
|
|
|
self.maxx = maxx
|
|
|
|
self.scale = scale
|
|
|
|
self.user = user
|
2017-10-10 14:39:11 +02:00
|
|
|
|
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):
|
|
|
|
return get_level_render_data(self.level)
|
2017-10-19 15:31:30 +02:00
|
|
|
|
2017-10-24 18:12:46 +02:00
|
|
|
@cached_property
|
|
|
|
def last_update(self):
|
|
|
|
return MapHistory.open_level_cached(self.level, 'render').last_update(self.minx, self.miny,
|
|
|
|
self.maxx, self.maxy)
|
|
|
|
|
|
|
|
@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):
|
|
|
|
# todo access_restriction
|
|
|
|
return set(access_restriction for access_restriction in self.affected_access_restrictions
|
|
|
|
if self.user is not None and self.user.is_superuser)
|
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-10-19 17:20:55 +02:00
|
|
|
def render(self):
|
|
|
|
svg = SVGImage(bounds=((self.miny, self.minx), (self.maxy, self.maxx)), scale=self.scale, buffer=1)
|
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-10-20 22:13:46 +02:00
|
|
|
bbox = self.bbox
|
|
|
|
bbox_prep = prepared.prep(bbox)
|
|
|
|
|
2017-10-20 22:02:51 +02:00
|
|
|
for geoms, default_height in self.level_render_data.levels:
|
2017-10-20 22:13:46 +02:00
|
|
|
if not bbox_prep.intersects(geoms.affected_area):
|
|
|
|
continue
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# hide indoor and outdoor rooms if their access restriction was not unlocked
|
2017-10-19 17:48:12 +02:00
|
|
|
add_walls = unary_union(tuple(area for access_restriction, area in geoms.restricted_spaces_indoors.items()
|
|
|
|
if access_restriction not in unlocked_access_restrictions))
|
|
|
|
crop_areas = unary_union(
|
|
|
|
tuple(area for access_restriction, area in geoms.restricted_spaces_outdoors.items()
|
|
|
|
if access_restriction not in unlocked_access_restrictions)
|
|
|
|
).union(add_walls)
|
|
|
|
|
2017-10-19 17:55:41 +02:00
|
|
|
# render altitude areas in default ground color and add ground colors to each one afterwards
|
|
|
|
# shadows are directly calculated and added by the SVGImage class
|
2017-10-19 17:20:55 +02:00
|
|
|
for altitudearea in geoms.altitudeareas:
|
2017-10-20 22:13:46 +02:00
|
|
|
svg.add_geometry(bbox.intersection(altitudearea.geometry.difference(crop_areas)),
|
2017-10-19 17:20:55 +02:00
|
|
|
fill_color='#eeeeee', altitude=altitudearea.altitude)
|
|
|
|
|
|
|
|
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-10-20 22:13:46 +02:00
|
|
|
svg.add_geometry(bbox.intersection(unary_union(areas)), fill_color=color)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
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-20 22:02:51 +02:00
|
|
|
if not add_walls.is_empty or not geoms.walls.is_empty:
|
2017-10-20 22:13:46 +02:00
|
|
|
svg.add_geometry(bbox.intersection(geoms.walls.union(add_walls)),
|
2017-10-20 22:02:51 +02:00
|
|
|
fill_color='#aaaaaa', stroke_px=0.5, stroke_color='#aaaaaa', elevation=default_height)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-10-20 22:02:51 +02:00
|
|
|
if not geoms.doors.is_empty:
|
2017-10-20 22:13:46 +02:00
|
|
|
svg.add_geometry(bbox.intersection(geoms.doors.difference(add_walls)),
|
2017-10-20 22:02:51 +02:00
|
|
|
fill_color='#ffffff', stroke_px=0.5, stroke_color='#ffffff')
|
2017-10-19 17:20:55 +02:00
|
|
|
|
|
|
|
return svg
|