Merge main.

This commit is contained in:
Sergey Vartanov 2022-05-16 03:14:56 +03:00
commit 5b626deee4
8 changed files with 63 additions and 28 deletions

View file

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

View file

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

View file

@ -47,7 +47,7 @@ def main() -> None:
mapcss.generate_mapcss(arguments)
elif arguments.command == "element":
from map_machine.element.element import draw_element
from map_machine.element.single import draw_element
draw_element(arguments)

View file

@ -14,6 +14,7 @@ from svgwrite.container import Group
from svgwrite.path import Path as SVGPath
from svgwrite.shapes import Rect
from map_machine import __project__
from map_machine.constructor import Constructor
from map_machine.drawing import draw_text
from map_machine.feature.building import Building, draw_walls, BUILDING_SCALE
@ -33,6 +34,8 @@ from map_machine.workspace import workspace
__author__ = "Sergey Vartanov"
__email__ = "me@enzet.ru"
OPENSTREETMAP_CREDIT: str = "© OpenStreetMap contributors"
class Map:
"""Map drawing."""
@ -190,21 +193,37 @@ class Map:
intersection.draw(self.svg, True)
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 (
("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 (
(3.0, Color("white"), 0.7),
(3.0, outline_color, 0.7),
(1.0, None, 1.0),
):
draw_text(
self.svg,
text,
size - point,
10,
Color("#888888"),
size - np.array(point),
font_size,
text_color,
anchor="end",
stroke_width=stroke_width,
stroke=stroke,
@ -213,6 +232,7 @@ class Map:
def fatal(message: str) -> None:
"""Print error message and exit with non-zero exit code."""
logging.fatal(message)
sys.exit(1)

View file

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

View file

@ -22,6 +22,8 @@ setup(
version=__version__,
packages=[
"map_machine",
"map_machine.doc",
"map_machine.element",
"map_machine.feature",
"map_machine.geometry",
"map_machine.osm",

View file

@ -1,6 +1,7 @@
"""
Test command line commands.
"""
import argparse
from pathlib import Path
from subprocess import PIPE, Popen
@ -12,7 +13,7 @@ from typing import List
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from map_machine.ui.cli import COMMAND_LINES
from map_machine.ui.cli import COMMAND_LINES, parse_arguments
LOG: bytes = (
b"INFO Constructing ways...\n"
@ -22,22 +23,23 @@ LOG: bytes = (
b"INFO Drawing extra icons...\n"
b"INFO Drawing texts...\n"
)
OUTPUT_PATH: Path = Path("out")
def error_run(arguments: List[str], message: bytes) -> None:
"""Run command that should fail and check error message."""
with Popen(["map-machine"] + arguments, stderr=PIPE) as pipe:
_, error = pipe.communicate()
_, output = pipe.communicate()
assert output == message
assert pipe.returncode != 0
assert error == message
def run(arguments: List[str], message: bytes) -> None:
"""Run command that should fail and check error message."""
"""Run command that should not fail and check output."""
with Popen(["map-machine"] + arguments, stderr=PIPE) as pipe:
_, error = pipe.communicate()
_, output = pipe.communicate()
assert output == message
assert pipe.returncode == 0
assert error == message
def test_wrong_render_arguments() -> None:
@ -55,10 +57,10 @@ def test_render() -> None:
COMMAND_LINES["render"] + ["--cache", "tests/data"],
LOG + b"INFO Writing output SVG to out/map.svg...\n",
)
with Path("out/map.svg").open(encoding="utf-8") as output_file:
with (OUTPUT_PATH / "map.svg").open(encoding="utf-8") as output_file:
root: Element = ElementTree.parse(output_file).getroot()
# 4 expected elements: `defs`, `rect` (background), `g` (outline),
# 8 expected elements: `defs`, `rect` (background), `g` (outline),
# `g` (icon), 4 `text` elements (credits).
assert len(root) == 8
assert len(root[3][0]) == 0
@ -72,10 +74,10 @@ def test_render_with_tooltips() -> None:
COMMAND_LINES["render_with_tooltips"] + ["--cache", "tests/data"],
LOG + b"INFO Writing output SVG to out/map.svg...\n",
)
with Path("out/map.svg").open(encoding="utf-8") as output_file:
with (OUTPUT_PATH / "map.svg").open(encoding="utf-8") as output_file:
root: Element = ElementTree.parse(output_file).getroot()
# 4 expected elements: `defs`, `rect` (background), `g` (outline),
# 8 expected elements: `defs`, `rect` (background), `g` (outline),
# `g` (icon), 4 `text` elements (credits).
assert len(root) == 8
assert len(root[3][0]) == 1
@ -92,12 +94,11 @@ def test_icons() -> None:
b"INFO Icon grid is written to out/icon_grid.svg.\n"
b"INFO Icon grid is written to doc/grid.svg.\n",
)
assert (Path("out") / "icon_grid.svg").is_file()
assert (Path("out") / "icons_by_name").is_dir()
assert (Path("out") / "icons_by_id").is_dir()
assert (Path("out") / "icons_by_name" / "Röntgen apple.svg").is_file()
assert (Path("out") / "icons_by_id" / "apple.svg").is_file()
assert (OUTPUT_PATH / "icon_grid.svg").is_file()
assert (OUTPUT_PATH / "icons_by_name").is_dir()
assert (OUTPUT_PATH / "icons_by_id").is_dir()
assert (OUTPUT_PATH / "icons_by_name" / "Röntgen apple.svg").is_file()
assert (OUTPUT_PATH / "icons_by_id" / "apple.svg").is_file()
def test_mapcss() -> None:
@ -106,9 +107,8 @@ def test_mapcss() -> None:
COMMAND_LINES["mapcss"],
b"INFO MapCSS 0.2 scheme is written to out/map_machine_mapcss.\n",
)
out_path: Path = Path("out") / "map_machine_mapcss"
out_path: Path = OUTPUT_PATH / "map_machine_mapcss"
assert out_path.is_dir()
assert out_path.is_dir()
assert (out_path / "icons" / "apple.svg").is_file()
assert (out_path / "map_machine.mapcss").is_file()
@ -121,7 +121,16 @@ def test_element() -> None:
COMMAND_LINES["element"],
b"INFO Element is written to out/element.svg.\n",
)
assert (Path("out") / "element.svg").is_file()
assert (OUTPUT_PATH / "element.svg").is_file()
def test_unwrapped_element() -> None:
arguments: argparse.Namespace = parse_arguments(
["map_machine"] + COMMAND_LINES["element"]
)
from map_machine.element.single import draw_element
draw_element(arguments)
def test_tile() -> None:
@ -132,5 +141,5 @@ def test_tile() -> None:
b"INFO SVG file is rasterized to out/tiles/tile_18_160199_88904.png.\n",
)
assert (Path("out") / "tiles" / "tile_18_160199_88904.svg").is_file()
assert (Path("out") / "tiles" / "tile_18_160199_88904.png").is_file()
assert (OUTPUT_PATH / "tiles" / "tile_18_160199_88904.svg").is_file()
assert (OUTPUT_PATH / "tiles" / "tile_18_160199_88904.png").is_file()