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-11-21 00:15:49 +01:00
|
|
|
from c3nav.mapdata.models import Level
|
2017-11-05 12:32:07 +01:00
|
|
|
from c3nav.mapdata.render.engines.base import FillAttribs, StrokeAttribs
|
2017-11-20 02:38:35 +01:00
|
|
|
from c3nav.mapdata.render.geometry import hybrid_union
|
|
|
|
from c3nav.mapdata.render.renderdata import LevelRenderData
|
2024-01-06 13:26:34 +01:00
|
|
|
from c3nav.mapdata.render.theme import ColorManager
|
2018-12-03 23:39:28 +01:00
|
|
|
from c3nav.mapdata.render.utils import get_full_levels, get_min_altitude
|
2019-12-22 21:07:33 +01:00
|
|
|
from c3nav.mapdata.utils.color import color_to_rgb, rgb_to_color
|
2017-10-10 14:39:11 +02:00
|
|
|
|
2023-12-11 18:47:48 +01:00
|
|
|
|
2017-11-06 11:18:45 +01:00
|
|
|
class MapRenderer:
|
2018-12-06 21:00:55 +01:00
|
|
|
def __init__(self, level, minx, miny, maxx, maxy, scale=1, access_permissions=None, full_levels=False,
|
|
|
|
min_width=None):
|
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
|
2018-12-06 22:23:42 +01:00
|
|
|
self.min_width = min_width/self.scale if min_width else None
|
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
|
|
|
|
2024-01-06 13:26:34 +01:00
|
|
|
def render(self, engine_cls, theme, center=True):
|
|
|
|
color_manager = ColorManager.for_theme(theme)
|
2017-10-19 17:55:41 +02:00
|
|
|
# add no access restriction to “unlocked“ access restrictions so lookup gets easier
|
2022-04-03 20:19:41 +02:00
|
|
|
access_permissions = self.access_permissions | {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
|
|
|
|
2024-01-06 13:26:34 +01:00
|
|
|
level_render_data = LevelRenderData.get(self.level, theme)
|
2017-11-21 00:15:49 +01:00
|
|
|
|
2017-12-05 18:13:06 +01:00
|
|
|
engine = engine_cls(self.width, self.height, self.minx, self.miny, float(level_render_data.base_altitude),
|
2024-01-06 13:26:34 +01:00
|
|
|
scale=self.scale, buffer=1, background=color_manager.background,
|
2023-12-11 18:47:48 +01:00
|
|
|
center=center, min_width=self.min_width)
|
2017-12-05 18:13:06 +01:00
|
|
|
|
2018-12-03 23:39:28 +01:00
|
|
|
if hasattr(engine, 'custom_render'):
|
2018-12-06 17:48:50 +01:00
|
|
|
engine.custom_render(level_render_data, access_permissions, self.full_levels)
|
2018-12-03 23:39:28 +01:00
|
|
|
return engine
|
|
|
|
|
2017-11-09 23:25:08 +01:00
|
|
|
if self.full_levels:
|
2018-12-03 23:39:28 +01:00
|
|
|
levels = get_full_levels(level_render_data)
|
2017-11-09 23:25:08 +01:00
|
|
|
else:
|
2017-11-21 00:15:49 +01:00
|
|
|
levels = level_render_data.levels
|
2017-11-09 23:25:08 +01:00
|
|
|
|
2018-12-03 23:39:28 +01:00
|
|
|
min_altitude = get_min_altitude(levels, default=level_render_data.base_altitude)
|
2017-11-10 00:25:27 +01:00
|
|
|
|
2017-11-25 15:16:15 +01:00
|
|
|
not_full_levels = engine.is_3d # always do non-full-levels until after the first primary level
|
2017-11-14 17:23:45 +01:00
|
|
|
full_levels = self.full_levels and engine.is_3d
|
2017-11-25 15:16:15 +01:00
|
|
|
for geoms in levels:
|
2017-12-20 12:21:44 +01:00
|
|
|
engine.add_group('level_%s' % geoms.short_label)
|
|
|
|
|
|
|
|
if geoms.pk == level_render_data.lowest_important_level:
|
|
|
|
engine.darken(level_render_data.darken_area)
|
|
|
|
|
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-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()
|
2017-11-21 00:15:49 +01:00
|
|
|
if access_restriction not in access_permissions))
|
2017-11-08 17:52:27 +01:00
|
|
|
crop_areas = hybrid_union(
|
2017-10-19 17:48:12 +02:00
|
|
|
tuple(area for access_restriction, area in geoms.restricted_spaces_outdoors.items()
|
2017-11-21 00:15:49 +01:00
|
|
|
if access_restriction not in access_permissions)
|
2017-10-19 17:48:12 +02:00
|
|
|
).union(add_walls)
|
|
|
|
|
2017-11-14 17:23:45 +01:00
|
|
|
if not_full_levels:
|
2024-01-06 13:26:34 +01:00
|
|
|
engine.add_geometry(geoms.walls_base, fill=FillAttribs(color_manager.wall_fill), 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)),
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.wall_fill), category='walls')
|
2017-11-14 18:18:22 +01:00
|
|
|
for i, altitudearea in enumerate(geoms.altitudeareas):
|
2017-11-19 00:30:08 +01:00
|
|
|
base = altitudearea.base.difference(crop_areas)
|
|
|
|
bottom = altitudearea.bottom.difference(crop_areas)
|
2024-01-06 13:26:34 +01:00
|
|
|
engine.add_geometry(base, fill=FillAttribs(color_manager.ground_fill), category='ground', item=i)
|
2017-11-19 00:30:08 +01:00
|
|
|
engine.add_geometry(bottom.fit(scale=geoms.min_altitude - min_altitude,
|
|
|
|
offset=min_altitude - int(0.7 * 1000)),
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.wall_fill), category='ground')
|
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)
|
2023-12-11 19:02:19 +01:00
|
|
|
engine.add_geometry(geometry, altitude=altitudearea.altitude,
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.ground_fill), category='ground', item=i)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-25 15:16:15 +01:00
|
|
|
j = 0
|
2018-12-21 19:06:29 +01:00
|
|
|
for (order, 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()
|
2017-11-21 00:15:49 +01:00
|
|
|
if access_restriction in access_permissions)
|
2017-10-19 17:20:55 +02:00
|
|
|
if areas:
|
2017-11-25 15:16:15 +01:00
|
|
|
j += 1
|
2017-11-26 13:10:31 +01:00
|
|
|
hexcolor = ''.join(hex(int(i*255))[2:].zfill(2) for i in engine.color_to_rgb(color)).upper()
|
2017-11-14 18:18:22 +01:00
|
|
|
engine.add_geometry(hybrid_union(areas), fill=FillAttribs(color),
|
2017-11-26 13:13:20 +01:00
|
|
|
category='ground_%s' % hexcolor, item=j)
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2023-12-14 20:07:25 +01:00
|
|
|
# add obstacles after everything related to ground for the nice right order
|
|
|
|
for i, altitudearea in enumerate(geoms.altitudeareas):
|
|
|
|
for height, height_obstacles in altitudearea.obstacles.items():
|
|
|
|
for color, color_obstacles in height_obstacles.items():
|
|
|
|
for obstacle in color_obstacles:
|
2023-12-25 17:40:25 +01:00
|
|
|
obstacle_geom = obstacle.difference(crop_areas)
|
2023-12-14 20:07:25 +01:00
|
|
|
if color:
|
|
|
|
fill_rgb = color_to_rgb(color)
|
|
|
|
stroke_color = rgb_to_color((*((0.75*i) for i in fill_rgb[:3]), fill_rgb[3]))
|
|
|
|
engine.add_geometry(
|
2023-12-25 17:40:25 +01:00
|
|
|
obstacle_geom,
|
2023-12-14 20:07:25 +01:00
|
|
|
fill=FillAttribs(color),
|
|
|
|
stroke=StrokeAttribs(stroke_color, 0.05, min_px=0.2),
|
|
|
|
category='obstacles'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
engine.add_geometry(
|
2023-12-25 17:40:25 +01:00
|
|
|
obstacle_geom,
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.obstacles_default_fill),
|
|
|
|
stroke=StrokeAttribs(color_manager.obstacles_default_border, 0.05, min_px=0.2),
|
2023-12-14 20:07:25 +01:00
|
|
|
category='obstacles'
|
|
|
|
)
|
2017-11-14 22:18:53 +01: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-29 09:32:15 +01:00
|
|
|
walls = None
|
2023-12-18 00:19:17 +01:00
|
|
|
# we use all_walls instead of walls, because the short wall rendering stuff doesn't work
|
|
|
|
if not add_walls.is_empty or not geoms.all_walls.is_empty:
|
|
|
|
walls = geoms.all_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:
|
2023-12-11 18:47:48 +01:00
|
|
|
engine.add_geometry(
|
|
|
|
walls.filter(bottom=not not_full_levels,
|
|
|
|
top=not walls_extended),
|
2024-09-17 22:25:12 +02:00
|
|
|
height=geoms.default_height, shadow_color=color_manager.wall_border, fill=FillAttribs(color_manager.wall_fill), category='walls'
|
2023-12-11 18:47:48 +01:00
|
|
|
)
|
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),
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.wall_fill), category='walls')
|
2017-11-17 20:07:00 +01:00
|
|
|
|
2017-11-14 16:28:44 +01:00
|
|
|
if walls_extended:
|
2024-01-06 13:26:34 +01:00
|
|
|
engine.add_geometry(geoms.walls_extended, fill=FillAttribs(color_manager.wall_fill), 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),
|
2024-01-06 13:26:34 +01:00
|
|
|
fill=FillAttribs(color_manager.door_fill),
|
|
|
|
stroke=StrokeAttribs(color_manager.door_fill, 0.05, min_px=0.2),
|
2017-11-14 17:23:45 +01:00
|
|
|
category='doors')
|
|
|
|
|
|
|
|
if doors_extended:
|
2024-01-06 13:26:34 +01:00
|
|
|
engine.add_geometry(geoms.doors_extended, fill=FillAttribs(color_manager.wall_fill), category='doors')
|
2017-10-29 09:32:15 +01:00
|
|
|
|
|
|
|
if walls is not None:
|
2023-12-11 18:47:48 +01:00
|
|
|
engine.add_geometry(walls,
|
2024-01-06 13:26:34 +01:00
|
|
|
stroke=StrokeAttribs(color_manager.wall_border, 0.1, min_px=1),
|
2023-12-11 18:47:48 +01:00
|
|
|
category='walls')
|
2017-10-19 17:20:55 +02:00
|
|
|
|
2017-11-25 15:16:15 +01:00
|
|
|
if geoms.on_top_of_id is None:
|
|
|
|
not_full_levels = not self.full_levels and engine.is_3d
|
|
|
|
|
2017-11-06 11:18:45 +01:00
|
|
|
return engine
|