Refactor drawing process.

This commit is contained in:
Sergey Vartanov 2022-05-16 03:13:21 +03:00
parent 82859b4ea0
commit 1ea38aab97
4 changed files with 30 additions and 6 deletions

View file

@ -1,6 +1,7 @@
[MASTER] [MASTER]
disable= disable=
E0401,
C0415, C0415,
R0902, R0902,
R0903, R0903,

View file

@ -311,6 +311,7 @@ def draw_text(
stroke: Optional[Color] = None, stroke: Optional[Color] = None,
opacity: float = 1.0, opacity: float = 1.0,
): ):
"""Add text element to the canvas."""
text_element = svg.text( text_element = svg.text(
text, text,
point, point,

View file

@ -14,6 +14,7 @@ from svgwrite.container import Group
from svgwrite.path import Path as SVGPath from svgwrite.path import Path as SVGPath
from svgwrite.shapes import Rect from svgwrite.shapes import Rect
from map_machine import __project__
from map_machine.constructor import Constructor from map_machine.constructor import Constructor
from map_machine.drawing import draw_text from map_machine.drawing import draw_text
from map_machine.feature.building import Building, draw_walls, BUILDING_SCALE from map_machine.feature.building import Building, draw_walls, BUILDING_SCALE
@ -33,6 +34,8 @@ from map_machine.workspace import workspace
__author__ = "Sergey Vartanov" __author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru" __email__ = "me@enzet.ru"
OPENSTREETMAP_CREDIT: str = "© OpenStreetMap contributors"
class Map: class Map:
"""Map drawing.""" """Map drawing."""
@ -190,21 +193,37 @@ class Map:
intersection.draw(self.svg, True) intersection.draw(self.svg, True)
def draw_credits(self, size: np.ndarray): def draw_credits(self, size: np.ndarray):
"""
Add OpenStreetMap credit and the link to the project itself.
OpenStreetMap requires to use the credit © OpenStreetMap contributors.
See https://www.openstreetmap.org/copyright
"""
right_margin: float = 15.0
bottom_margin: float = 15.0
font_size: float = 10.0
vertical_spacing: float = 2.0
text_color: Color = Color("#888888")
outline_color: Color = Color("#FFFFFF")
for text, point in ( for text, point in (
("Data: © OpenStreetMap contributors", np.array((15, 27))), (
("Rendering: Map Machine", np.array((15, 15))), f"Data: {OPENSTREETMAP_CREDIT}",
(right_margin, bottom_margin + font_size + vertical_spacing),
),
(f"Rendering: {__project__}", (right_margin, bottom_margin)),
): ):
for stroke_width, stroke, opacity in ( for stroke_width, stroke, opacity in (
(3.0, Color("white"), 0.7), (3.0, outline_color, 0.7),
(1.0, None, 1.0), (1.0, None, 1.0),
): ):
draw_text( draw_text(
self.svg, self.svg,
text, text,
size - point, size - np.array(point),
10, font_size,
Color("#888888"), text_color,
anchor="end", anchor="end",
stroke_width=stroke_width, stroke_width=stroke_width,
stroke=stroke, stroke=stroke,
@ -213,6 +232,7 @@ class Map:
def fatal(message: str) -> None: def fatal(message: str) -> None:
"""Print error message and exit with non-zero exit code."""
logging.fatal(message) logging.fatal(message)
sys.exit(1) sys.exit(1)

View file

@ -73,6 +73,8 @@ def format_frequency(value: str) -> str:
@dataclass @dataclass
class TextConstructor: class TextConstructor:
"""Constructs map labels out of OpenStreetMap tags."""
def __init__(self, scheme: Scheme) -> None: def __init__(self, scheme: Scheme) -> None:
self.scheme: Scheme = scheme self.scheme: Scheme = scheme
self.default_color: Color = self.scheme.get_color("text_color") self.default_color: Color = self.scheme.get_color("text_color")