Issue #39: support flipping icons horizontally.

If tags has "direction" key, direction is mostly to the left (to the
West), and shape for the icon is right directed, we flip the shape
horizontally.
This commit is contained in:
Sergey Vartanov 2021-05-21 03:04:49 +03:00
parent 2f957e880b
commit 1b40087b18
2 changed files with 15 additions and 4 deletions

View file

@ -39,6 +39,7 @@ class Shape:
offset: np.array # vector that should be used to shift the path offset: np.array # vector that should be used to shift the path
id_: str # shape identifier id_: str # shape identifier
name: Optional[str] = None # icon description name: Optional[str] = None # icon description
is_right_directed: bool = False
def is_default(self) -> bool: def is_default(self) -> bool:
""" """
@ -98,6 +99,7 @@ class ShapeExtractor:
:param svg_file_name: input SVG file name with icons. File may contain :param svg_file_name: input SVG file name with icons. File may contain
any other irrelevant graphics. any other irrelevant graphics.
""" """
self.configuration = ShapeConfiguration(configuration_file_name)
self.shapes: Dict[str, Shape] = {} self.shapes: Dict[str, Shape] = {}
with open(svg_file_name) as input_file: with open(svg_file_name) as input_file:
@ -109,8 +111,6 @@ class ShapeExtractor:
if isinstance(node, Element): if isinstance(node, Element):
self.parse(node) self.parse(node)
self.configuration = ShapeConfiguration(configuration_file_name)
def parse(self, node: Element) -> None: def parse(self, node: Element) -> None:
""" """
Extract icon paths into a map. Extract icon paths into a map.
@ -154,7 +154,10 @@ class ShapeExtractor:
name = child_node.childNodes[0].nodeValue name = child_node.childNodes[0].nodeValue
break break
self.shapes[id_] = Shape(path, point, id_, name) is_right_directed: bool = (
id_ in self.configuration.right_directed
)
self.shapes[id_] = Shape(path, point, id_, name, is_right_directed)
else: else:
error(f"not standard ID {id_}") error(f"not standard ID {id_}")

View file

@ -10,6 +10,7 @@ from typing import Any, Dict, List, Optional, Set, Tuple, Union
import yaml import yaml
from colour import Color from colour import Color
from roentgen.direction import DirectionSet
from roentgen.icon import ( from roentgen.icon import (
DEFAULT_COLOR, DEFAULT_SHAPE_ID, Icon, IconSet, ShapeExtractor, DEFAULT_COLOR, DEFAULT_SHAPE_ID, Icon, IconSet, ShapeExtractor,
ShapeSpecification ShapeSpecification
@ -184,6 +185,7 @@ class Scheme:
:param icon_extractor: extractor with icon specifications :param icon_extractor: extractor with icon specifications
:param tags: OpenStreetMap element tags dictionary :param tags: OpenStreetMap element tags dictionary
:param for_: target (node, way, area or relation) :param for_: target (node, way, area or relation)
:return (icon set, icon priority)
""" """
tags_hash: str = ( tags_hash: str = (
",".join(tags.keys()) + ":" + ",".join(map(str, tags.values())) ",".join(tags.keys()) + ":" + ",".join(map(str, tags.values()))
@ -191,7 +193,7 @@ class Scheme:
if tags_hash in self.cache: if tags_hash in self.cache:
return self.cache[tags_hash] return self.cache[tags_hash]
main_icon: Icon = None main_icon: Optional[Icon] = None
extra_icons: List[Icon] = [] extra_icons: List[Icon] = []
processed: Set[str] = set() processed: Set[str] = set()
priority: int = 0 priority: int = 0
@ -264,6 +266,12 @@ class Scheme:
returned: IconSet = IconSet(main_icon, extra_icons, processed) returned: IconSet = IconSet(main_icon, extra_icons, processed)
self.cache[tags_hash] = returned, priority self.cache[tags_hash] = returned, priority
if "direction" in tags:
if DirectionSet(tags["direction"]).is_right() is False:
for specification in main_icon.shape_specifications:
if specification.shape.is_right_directed:
specification.flip_horizontally = True
return returned, priority return returned, priority
def get_style(self, tags: Dict[str, Any], scale): def get_style(self, tags: Dict[str, Any], scale):