From 93b7883f99593aeabcb9c851b147727331a2d97d Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Tue, 13 Oct 2020 02:51:02 +0300 Subject: [PATCH] Rename text; add voltage and frequency processing. --- roentgen/point.py | 16 ++++++----- roentgen/{address.py => text.py} | 46 +++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 7 deletions(-) rename roentgen/{address.py => text.py} (51%) diff --git a/roentgen/point.py b/roentgen/point.py index 8723b89..ecce3c7 100644 --- a/roentgen/point.py +++ b/roentgen/point.py @@ -5,7 +5,7 @@ import numpy as np import svgwrite from colour import Color -from roentgen.address import get_address +from roentgen.text import get_address, get_text from roentgen.color import is_bright from roentgen.icon import Icon from roentgen.osm_reader import Tagged @@ -91,6 +91,10 @@ def construct_text( if draw_captions == "main": return texts + for text in get_text(tags): # type: str + if text: + texts.append(TextStruct(text)) + if "route_ref" in tags: texts.append(TextStruct(tags["route_ref"].replace(";", " "))) tags.pop("route_ref", None) @@ -167,8 +171,8 @@ class Point(Tagged): if painted: self.y += 16 - if not self.icon_set.extra_icons or \ - (self.icon_set.main_icon and not painted): + if (not self.icon_set.extra_icons or + (self.icon_set.main_icon and not painted)): return is_place_for_extra: bool = True @@ -189,7 +193,8 @@ class Point(Tagged): svg, shape_ids, self.point + np.array((left, self.y)), Color("#888888"), occupied) left += 16 - self.y += 16 + if self.icon_set.extra_icons: + self.y += 16 def draw_point_shape( self, svg: svgwrite.Drawing, icons: List[Icon], position, @@ -260,7 +265,7 @@ class Point(Tagged): if occupied: is_occupied: bool = False - for i in range(-int(length / 2), int(length/ 2)): + for i in range(-int(length / 2), int(length / 2)): if occupied.check((int(point[0] + i), int(point[1] - 4))): is_occupied = True break @@ -290,4 +295,3 @@ class Point(Tagged): font_family=DEFAULT_FONT, fill=fill.hex)) self.y += 11 - diff --git a/roentgen/address.py b/roentgen/text.py similarity index 51% rename from roentgen/address.py rename to roentgen/text.py index a0268d3..51872a9 100644 --- a/roentgen/address.py +++ b/roentgen/text.py @@ -15,7 +15,7 @@ def get_address(tags: Dict[str, Any], draw_captions_mode: str) -> List[str]: """ address: List[str] = [] - if draw_captions_mode != "main": + if draw_captions_mode == "address": if "addr:postcode" in tags: address.append(tags["addr:postcode"]) tags.pop("addr:postcode", None) @@ -37,3 +37,47 @@ def get_address(tags: Dict[str, Any], draw_captions_mode: str) -> List[str]: tags.pop("addr:housenumber", None) return address + + +def format_voltage(value: str) -> str: + """ + Format voltage value to more human-readable form. + + :param value: presumably string representation of integer, in Volts + """ + try: + int_value: int = int(value) + if int_value % 1000 == 0: + return f"{int(int_value / 1000)} kV" + return f"{value} V" + except ValueError: + return value + + +def format_frequency(value: str) -> str: + try: + int_value: int = int(value) + return f"{value} Hz" + except ValueError: + return value + + +def get_text(tags: Dict[str, Any]) -> List[str]: + + texts: List[str] = [] + + values: List[str] = [] + if "voltage:primary" in tags: + values.append(tags["voltage:primary"]) + if "voltage:secondary" in tags: + values.append(tags["voltage:secondary"]) + if "voltage" in tags: + values = tags["voltage"].split(";") + texts.append(", ".join(map(format_voltage, values))) + + if "frequency" in tags: + texts.append(", ".join(map( + format_frequency, tags["frequency"].split(";")))) + + return texts +