Skip to content

Commit

Permalink
Issue #140: put scheme inside map configuration.
Browse files Browse the repository at this point in the history
Refactoring of `Scheme` and `MapConfiguration` classes.
  • Loading branch information
enzet committed Aug 15, 2022
1 parent 596910f commit c76e1f0
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 70 deletions.
22 changes: 12 additions & 10 deletions map_machine/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,12 @@ def __init__(
self,
osm_data: OSMData,
flinger: Flinger,
scheme: Scheme,
extractor: ShapeExtractor,
configuration: MapConfiguration,
) -> None:
self.osm_data: OSMData = osm_data
self.flinger: Flinger = flinger
self.scheme: Scheme = scheme
self.scheme: Scheme = configuration.scheme
self.extractor: ShapeExtractor = extractor
self.configuration: MapConfiguration = configuration
self.text_constructor: TextConstructor = TextConstructor(self.scheme)
Expand Down Expand Up @@ -306,8 +305,8 @@ def construct_line(

priority: int
icon_set: IconSet
icon_set, priority = self.scheme.get_icon(
self.extractor, line.tags, processed, self.configuration
icon_set, priority = self.configuration.get_icon(
self.extractor, line.tags, processed
)
if icon_set is not None:
labels: list[Label] = self.text_constructor.construct_text(
Expand Down Expand Up @@ -347,8 +346,8 @@ def add_point_for_line(self, center_point, inners, line, outers) -> None:
processed: set[str] = set()
priority: int
icon_set: IconSet
icon_set, priority = self.scheme.get_icon(
self.extractor, line.tags, processed, self.configuration
icon_set, priority = self.configuration.get_icon(
self.extractor, line.tags, processed
)
if icon_set is not None:
labels: list[Label] = self.text_constructor.construct_text(
Expand Down Expand Up @@ -413,6 +412,8 @@ def construct_nodes(self) -> None:
"""Draw nodes."""
logging.info("Constructing nodes...")

# Sort node vertically (using latitude values) to draw them from top to
# bottom.
sorted_node_ids: Iterator[int] = sorted(
self.osm_data.nodes.keys(),
key=lambda x: -self.osm_data.nodes[x].coordinates[0],
Expand All @@ -423,6 +424,7 @@ def construct_nodes(self) -> None:
def construct_node(self, node: OSMNode) -> None:
"""Draw one node."""
tags: dict[str, str] = node.tags

if not tags:
return
if not self.check_level(tags):
Expand Down Expand Up @@ -472,8 +474,8 @@ def construct_node(self, node: OSMNode) -> None:
color = Color("#CCCCCC")
if self.configuration.drawing_mode == DrawingMode.BLACK:
color = Color("#444444")
icon_set, priority = self.scheme.get_icon(
self.extractor, tags, processed, self.configuration
icon_set, priority = self.configuration.get_icon(
self.extractor, tags, processed
)
icon_set.main_icon.recolor(color)
point: Point = Point(
Expand All @@ -487,8 +489,8 @@ def construct_node(self, node: OSMNode) -> None:
self.points.append(point)
return

icon_set, priority = self.scheme.get_icon(
self.extractor, tags, processed, self.configuration
icon_set, priority = self.configuration.get_icon(
self.extractor, tags, processed
)
if icon_set is None:
return
Expand Down
4 changes: 2 additions & 2 deletions map_machine/doc/doc_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def draw_table(self) -> None:
if column_value:
current_tags |= {self.collection.column_key: column_value}
processed: set[str] = set()
icon, _ = SCHEME.get_icon(
EXTRACTOR, current_tags, processed, MapConfiguration()
icon, _ = MapConfiguration(SCHEME).get_icon(
EXTRACTOR, current_tags, processed
)
processed = icon.processed
if not icon:
Expand Down
29 changes: 20 additions & 9 deletions map_machine/doc/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ def draw(
input_file_name: Path,
output_file_name: Path,
boundary_box: BoundaryBox,
configuration: MapConfiguration = MapConfiguration(),
configuration: Optional[MapConfiguration] = None,
) -> None:
"""Draw file."""
if configuration is None:
configuration = MapConfiguration(SCHEME)

osm_data: OSMData = OSMData()
osm_data.parse_osm_file(input_file_name)
flinger: Flinger = Flinger(
boundary_box, configuration.zoom_level, osm_data.equator_length
)
constructor: Constructor = Constructor(
osm_data, flinger, SCHEME, EXTRACTOR, configuration
osm_data, flinger, EXTRACTOR, configuration
)
constructor.construct()

svg: svgwrite.Drawing = svgwrite.Drawing(
str(output_file_name), size=flinger.size
)
map_: Map = Map(flinger, svg, SCHEME, configuration)
map_: Map = Map(flinger, svg, configuration)
map_.draw(constructor)

svg.write(output_file_name.open("w"))
Expand All @@ -63,11 +66,14 @@ def draw(
def draw_around_point(
point: np.ndarray,
name: str,
configuration: MapConfiguration = MapConfiguration(),
configuration: Optional[MapConfiguration] = None,
size: np.ndarray = np.array((600, 400)),
get: Optional[BoundaryBox] = None,
) -> None:
"""Draw around point."""
if configuration is None:
configuration = MapConfiguration(SCHEME)

input_path: Path = doc_path / f"{name}.svg"

boundary_box: BoundaryBox = BoundaryBox.from_coordinates(
Expand All @@ -90,22 +96,22 @@ def main(id_: str) -> None:
draw_around_point(
np.array((55.75277, 37.40856)),
"fitness",
MapConfiguration(zoom_level=20.2),
MapConfiguration(SCHEME, zoom_level=20.2),
np.array((300, 200)),
)

if id_ is None or id_ == "power":
draw_around_point(
np.array((52.5622, 12.94)),
"power",
configuration=MapConfiguration(zoom_level=15),
configuration=MapConfiguration(SCHEME, zoom_level=15),
)

if id_ is None or id_ == "playground":
draw_around_point(
np.array((52.47388, 13.43826)),
"playground",
configuration=MapConfiguration(zoom_level=19),
configuration=MapConfiguration(SCHEME, zoom_level=19),
)

# Playground: (59.91991/10.85535), (59.83627/10.83017), Oslo
Expand All @@ -116,6 +122,7 @@ def main(id_: str) -> None:
np.array((52.50892, 13.3244)),
"surveillance",
MapConfiguration(
SCHEME,
zoom_level=18.5,
ignore_level_matching=True,
),
Expand All @@ -126,6 +133,7 @@ def main(id_: str) -> None:
np.array((52.421, 13.101)),
"viewpoints",
MapConfiguration(
SCHEME,
label_mode=LabelMode.NO,
zoom_level=15.7,
ignore_level_matching=True,
Expand All @@ -136,15 +144,15 @@ def main(id_: str) -> None:
draw_around_point(
np.array((-26.19049, 28.05605)),
"buildings",
MapConfiguration(building_mode=BuildingMode.ISOMETRIC),
MapConfiguration(SCHEME, building_mode=BuildingMode.ISOMETRIC),
)

if id_ is None or id_ == "trees":
draw_around_point(
np.array((55.751, 37.628)),
"trees",
MapConfiguration(
label_mode=LabelMode(LabelMode.ALL), zoom_level=18.1
SCHEME, label_mode=LabelMode(LabelMode.ALL), zoom_level=18.1
),
get=BoundaryBox(37.624, 55.749, 37.633, 55.753),
)
Expand All @@ -158,6 +166,7 @@ def main(id_: str) -> None:
np.array((55.7655, 37.6055)),
"time",
MapConfiguration(
SCHEME,
DrawingMode.TIME,
zoom_level=16.5,
ignore_level_matching=True,
Expand All @@ -169,6 +178,7 @@ def main(id_: str) -> None:
np.array((55.7655, 37.6055)),
"author",
MapConfiguration(
SCHEME,
DrawingMode.AUTHOR,
seed="a",
zoom_level=16.5,
Expand All @@ -181,6 +191,7 @@ def main(id_: str) -> None:
np.array((48.87422, 2.377)),
"colors",
configuration=MapConfiguration(
SCHEME,
zoom_level=17.6,
building_mode=BuildingMode.ISOMETRIC,
ignore_level_matching=True,
Expand Down
17 changes: 9 additions & 8 deletions map_machine/doc/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ def generate_wiki_table(self) -> tuple[str, list[Icon]]:
text += f"{{{{Tag|{key}|{value}}}}}<br />"
text = text[:-6]
text += "\n"
icon, _ = SCHEME.get_icon(
EXTRACTOR,
current_tags | self.collection.tags,
set(),
MapConfiguration(ignore_level_matching=True),
icon, _ = MapConfiguration(
SCHEME, ignore_level_matching=True
).get_icon(
EXTRACTOR, current_tags | self.collection.tags, set()
)
icons.append(icon.main_icon)
text += (
Expand Down Expand Up @@ -103,7 +102,9 @@ def generate_wiki_table(self) -> tuple[str, list[Icon]]:
}
if column_value:
current_tags |= {self.collection.column_key: column_value}
icon, _ = SCHEME.get_icon(EXTRACTOR, current_tags, set())
icon, _ = MapConfiguration(SCHEME).get_icon(
EXTRACTOR, current_tags, set()
)
if not icon:
print("Icon was not constructed.")
text += (
Expand Down Expand Up @@ -135,8 +136,8 @@ def generate_new_text(
wiki_text, icons = table.generate_wiki_table()
else:
processed = set()
icon, _ = SCHEME.get_icon(
EXTRACTOR, table.collection.tags, processed, MapConfiguration()
icon, _ = MapConfiguration(SCHEME).get_icon(
EXTRACTOR, table.collection.tags, processed
)
if not icon.main_icon.is_default():
wiki_text = (
Expand Down
6 changes: 3 additions & 3 deletions map_machine/element/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,17 @@ def get_boundary_box(self) -> BoundaryBox:
def draw(self, output_path: Path, zoom: float = DEFAULT_ZOOM) -> None:
"""Draw grid."""
configuration: MapConfiguration = MapConfiguration(
level="all", credit=None
SCHEME, level="all", credit=None
)
flinger: Flinger = Flinger(
self.get_boundary_box(), zoom, self.osm_data.equator_length
)
svg: Drawing = Drawing(output_path.name, flinger.size)
constructor: Constructor = Constructor(
self.osm_data, flinger, SCHEME, SHAPE_EXTRACTOR, configuration
self.osm_data, flinger, SHAPE_EXTRACTOR, configuration
)
constructor.construct()
map_: Map = Map(flinger, svg, SCHEME, configuration)
map_: Map = Map(flinger, svg, configuration)
map_.draw(constructor)

for text, i, j in self.texts:
Expand Down
4 changes: 2 additions & 2 deletions map_machine/element/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from svgwrite.path import Path as SVGPath

from map_machine.element.grid import Grid
from map_machine.map_configuration import LabelMode
from map_machine.map_configuration import LabelMode, MapConfiguration
from map_machine.osm.osm_reader import Tags
from map_machine.pictogram.icon import ShapeExtractor
from map_machine.pictogram.point import Point
Expand Down Expand Up @@ -49,7 +49,7 @@ def draw_element(options: argparse.Namespace) -> None:
workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH
)
processed: set[str] = set()
icon, _ = scheme.get_icon(extractor, tags, processed)
icon, _ = MapConfiguration(scheme).get_icon(extractor, tags, processed)
is_for_node: bool = target == "node"
text_constructor: TextConstructor = TextConstructor(scheme)
labels: list[Label] = text_constructor.construct_text(
Expand Down
25 changes: 23 additions & 2 deletions map_machine/map_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import argparse
from dataclasses import dataclass
from enum import Enum
from typing import Optional
from typing import Optional, Any

from colour import Color

from map_machine.pictogram.icon import ShapeExtractor, IconSet
from map_machine.scheme import Scheme

__author__ = "Sergey Vartanov"
__email__ = "[email protected]"

Expand Down Expand Up @@ -42,6 +45,7 @@ class BuildingMode(Enum):
class MapConfiguration:
"""Map drawing configuration."""

scheme: Scheme
drawing_mode: DrawingMode = DrawingMode.NORMAL
building_mode: BuildingMode = BuildingMode.FLAT
label_mode: LabelMode = LabelMode.MAIN
Expand All @@ -59,10 +63,11 @@ class MapConfiguration:

@classmethod
def from_options(
cls, options: argparse.Namespace, zoom_level: float
cls, scheme: Scheme, options: argparse.Namespace, zoom_level: float
) -> "MapConfiguration":
"""Initialize from command-line options."""
return cls(
scheme,
DrawingMode(options.mode),
BuildingMode(options.buildings),
LabelMode(options.label_mode),
Expand All @@ -87,3 +92,19 @@ def background_color(self) -> Optional[Color]:
if self.drawing_mode not in (DrawingMode.NORMAL, DrawingMode.BLACK):
return Color("#111111")
return None

def get_icon(
self,
extractor: ShapeExtractor,
tags: dict[str, Any],
processed: set[str],
) -> tuple[Optional[IconSet], int]:
return self.scheme.get_icon(
extractor,
tags,
processed,
self.country,
self.zoom_level,
self.ignore_level_matching,
self.show_overlapped,
)
15 changes: 7 additions & 8 deletions map_machine/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ def __init__(
self,
flinger: Flinger,
svg: svgwrite.Drawing,
scheme: Scheme,
configuration: MapConfiguration,
) -> None:
self.flinger: Flinger = flinger
self.svg: svgwrite.Drawing = svg
self.scheme: Scheme = scheme
self.scheme: Scheme = configuration.scheme
self.configuration = configuration

self.background_color: Color = self.scheme.get_color("background_color")
Expand Down Expand Up @@ -263,8 +262,12 @@ def render_map(arguments: argparse.Namespace) -> None:
:param arguments: command-line arguments
"""
scheme: Scheme = Scheme.from_file(
workspace.find_scheme_path(arguments.scheme)
)

configuration: MapConfiguration = MapConfiguration.from_options(
arguments, float(arguments.zoom)
scheme, arguments, float(arguments.zoom)
)
cache_path: Path = Path(arguments.cache)
cache_path.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -347,19 +350,15 @@ def render_map(arguments: argparse.Namespace) -> None:
workspace.ICONS_PATH, workspace.ICONS_CONFIG_PATH
)

scheme: Scheme = Scheme.from_file(workspace.DEFAULT_SCHEME_PATH)
constructor: Constructor = Constructor(
osm_data=osm_data,
flinger=flinger,
scheme=scheme,
extractor=icon_extractor,
configuration=configuration,
)
constructor.construct()

map_: Map = Map(
flinger=flinger, svg=svg, scheme=scheme, configuration=configuration
)
map_: Map = Map(flinger=flinger, svg=svg, configuration=configuration)
map_.draw(constructor)

logging.info(f"Writing output SVG to {arguments.output_file_name}...")
Expand Down
Loading

0 comments on commit c76e1f0

Please sign in to comment.