move clip_altitudes and altitudes management to SVGEngine
This commit is contained in:
parent
426d838b13
commit
5774910a25
2 changed files with 17 additions and 17 deletions
|
@ -4,7 +4,6 @@ from functools import lru_cache
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from shapely.geometry import JOIN_STYLE, box
|
from shapely.geometry import JOIN_STYLE, box
|
||||||
from shapely.ops import unary_union
|
|
||||||
|
|
||||||
|
|
||||||
class FillAttribs:
|
class FillAttribs:
|
||||||
|
@ -48,10 +47,6 @@ class RenderEngine(ABC):
|
||||||
|
|
||||||
self.background_rgb = tuple(int(background[i:i + 2], 16)/255 for i in range(1, 6, 2))
|
self.background_rgb = tuple(int(background[i:i + 2], 16)/255 for i in range(1, 6, 2))
|
||||||
|
|
||||||
# keep track which area of the image has which altitude currently
|
|
||||||
self.altitudes = {}
|
|
||||||
self.last_altitude = None
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_png(self) -> bytes:
|
def get_png(self) -> bytes:
|
||||||
# render the image to png.
|
# render the image to png.
|
||||||
|
@ -67,17 +62,6 @@ class RenderEngine(ABC):
|
||||||
return (*(i/255 for i in color[:3]), color[3] if alpha is None else alpha)
|
return (*(i/255 for i in color[:3]), color[3] if alpha is None else alpha)
|
||||||
raise ValueError('invalid color string!')
|
raise ValueError('invalid color string!')
|
||||||
|
|
||||||
def clip_altitudes(self, new_geometry, new_altitude=None):
|
|
||||||
# register new geometry with an altitude
|
|
||||||
# a geometry with no altitude will reset the altitude information of its area as if nothing was ever there
|
|
||||||
if self.last_altitude is not None and self.last_altitude > new_altitude:
|
|
||||||
raise ValueError('Altitudes have to be ascending.')
|
|
||||||
|
|
||||||
if new_altitude in self.altitudes:
|
|
||||||
self.altitudes[new_altitude] = unary_union([self.altitudes[new_altitude], new_geometry])
|
|
||||||
else:
|
|
||||||
self.altitudes[new_altitude] = new_geometry
|
|
||||||
|
|
||||||
def add_geometry(self, geometry, fill: Optional[FillAttribs] = None, stroke: Optional[StrokeAttribs] = None,
|
def add_geometry(self, geometry, fill: Optional[FillAttribs] = None, stroke: Optional[StrokeAttribs] = None,
|
||||||
altitude=None, height=None, shape_cache_key=None):
|
altitude=None, height=None, shape_cache_key=None):
|
||||||
# draw a shapely geometry with a given style
|
# draw a shapely geometry with a given style
|
||||||
|
|
|
@ -11,8 +11,9 @@ from django.core import checks
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from shapely.affinity import translate
|
from shapely.affinity import translate
|
||||||
from shapely.geometry import LineString, Polygon
|
from shapely.geometry import LineString, Polygon
|
||||||
|
|
||||||
# import gobject-inspect, cairo and rsvg if the native rsvg SVG_RENDERER should be used
|
# import gobject-inspect, cairo and rsvg if the native rsvg SVG_RENDERER should be used
|
||||||
|
from shapely.ops import unary_union
|
||||||
|
|
||||||
from c3nav.mapdata.render.engines.base import FillAttribs, RenderEngine, StrokeAttribs
|
from c3nav.mapdata.render.engines.base import FillAttribs, RenderEngine, StrokeAttribs
|
||||||
|
|
||||||
if settings.SVG_RENDERER == 'rsvg':
|
if settings.SVG_RENDERER == 'rsvg':
|
||||||
|
@ -53,6 +54,10 @@ class SVGEngine(RenderEngine):
|
||||||
# keep track of created blur filters to avoid duplicates
|
# keep track of created blur filters to avoid duplicates
|
||||||
self.blurs = set()
|
self.blurs = set()
|
||||||
|
|
||||||
|
# keep track which area of the image has which altitude currently
|
||||||
|
self.altitudes = {}
|
||||||
|
self.last_altitude = None
|
||||||
|
|
||||||
self._create_geometry_cache = {}
|
self._create_geometry_cache = {}
|
||||||
|
|
||||||
def get_xml(self, buffer=False):
|
def get_xml(self, buffer=False):
|
||||||
|
@ -196,6 +201,17 @@ class SVGEngine(RenderEngine):
|
||||||
shadow = self._create_geometry(shadow_geom, attribs)
|
shadow = self._create_geometry(shadow_geom, attribs)
|
||||||
self.g += shadow
|
self.g += shadow
|
||||||
|
|
||||||
|
def clip_altitudes(self, new_geometry, new_altitude=None):
|
||||||
|
# register new geometry with an altitude
|
||||||
|
# a geometry with no altitude will reset the altitude information of its area as if nothing was ever there
|
||||||
|
if self.last_altitude is not None and self.last_altitude > new_altitude:
|
||||||
|
raise ValueError('Altitudes have to be ascending.')
|
||||||
|
|
||||||
|
if new_altitude in self.altitudes:
|
||||||
|
self.altitudes[new_altitude] = unary_union([self.altitudes[new_altitude], new_geometry])
|
||||||
|
else:
|
||||||
|
self.altitudes[new_altitude] = new_geometry
|
||||||
|
|
||||||
def _add_geometry(self, geometry, fill: Optional[FillAttribs] = None, stroke: Optional[StrokeAttribs] = None,
|
def _add_geometry(self, geometry, fill: Optional[FillAttribs] = None, stroke: Optional[StrokeAttribs] = None,
|
||||||
altitude=None, height=None, shape_cache_key=None):
|
altitude=None, height=None, shape_cache_key=None):
|
||||||
geometry = self.buffered_bbox.intersection(geometry.geom)
|
geometry = self.buffered_bbox.intersection(geometry.geom)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue