move clip_altitudes and altitudes management to SVGEngine

This commit is contained in:
Laura Klünder 2017-11-08 23:16:41 +01:00
parent 426d838b13
commit 5774910a25
2 changed files with 17 additions and 17 deletions

View file

@ -4,7 +4,6 @@ from functools import lru_cache
from typing import Optional
from shapely.geometry import JOIN_STYLE, box
from shapely.ops import unary_union
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))
# keep track which area of the image has which altitude currently
self.altitudes = {}
self.last_altitude = None
@abstractmethod
def get_png(self) -> bytes:
# 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)
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,
altitude=None, height=None, shape_cache_key=None):
# draw a shapely geometry with a given style

View file

@ -11,8 +11,9 @@ from django.core import checks
from PIL import Image
from shapely.affinity import translate
from shapely.geometry import LineString, Polygon
# 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
if settings.SVG_RENDERER == 'rsvg':
@ -53,6 +54,10 @@ class SVGEngine(RenderEngine):
# keep track of created blur filters to avoid duplicates
self.blurs = set()
# keep track which area of the image has which altitude currently
self.altitudes = {}
self.last_altitude = None
self._create_geometry_cache = {}
def get_xml(self, buffer=False):
@ -196,6 +201,17 @@ class SVGEngine(RenderEngine):
shadow = self._create_geometry(shadow_geom, attribs)
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,
altitude=None, height=None, shape_cache_key=None):
geometry = self.buffered_bbox.intersection(geometry.geom)