mirror of
https://github.com/enzet/map-machine.git
synced 2025-06-05 12:21:54 +02:00
Issue #67: refactor text processing.
This commit is contained in:
parent
a73006e67e
commit
7eaf3678f9
5 changed files with 95 additions and 75 deletions
|
@ -296,7 +296,7 @@ class Constructor:
|
|||
)
|
||||
if icon_set is not None:
|
||||
labels: list[Label] = self.scheme.construct_text(
|
||||
line.tags, "all", processed
|
||||
line.tags, processed, self.configuration.label_mode
|
||||
)
|
||||
point: Point = Point(
|
||||
icon_set,
|
||||
|
@ -334,7 +334,7 @@ class Constructor:
|
|||
)
|
||||
if icon_set is not None:
|
||||
labels: list[Label] = self.scheme.construct_text(
|
||||
line.tags, "all", processed
|
||||
line.tags, processed, self.configuration.label_mode
|
||||
)
|
||||
point: Point = Point(
|
||||
icon_set,
|
||||
|
@ -445,7 +445,9 @@ class Constructor:
|
|||
)
|
||||
if icon_set is None:
|
||||
return
|
||||
labels: list[Label] = self.scheme.construct_text(tags, "all", processed)
|
||||
labels: list[Label] = self.scheme.construct_text(
|
||||
tags, processed, self.configuration.label_mode
|
||||
)
|
||||
self.scheme.process_ignored(tags, processed)
|
||||
|
||||
if node.get_tag("natural") == "tree" and (
|
||||
|
|
|
@ -23,6 +23,7 @@ class LabelMode(Enum):
|
|||
NO = "no"
|
||||
MAIN = "main"
|
||||
ALL = "all"
|
||||
ADDRESS = "address"
|
||||
|
||||
|
||||
class BuildingMode(Enum):
|
||||
|
|
|
@ -22,8 +22,8 @@ from map_machine.pictogram.icon import (
|
|||
ShapeExtractor,
|
||||
ShapeSpecification,
|
||||
)
|
||||
from map_machine.map_configuration import MapConfiguration
|
||||
from map_machine.text import Label, get_address, get_text
|
||||
from map_machine.map_configuration import MapConfiguration, LabelMode
|
||||
from map_machine.text import Label, construct_text
|
||||
|
||||
__author__ = "Sergey Vartanov"
|
||||
__email__ = "me@enzet.ru"
|
||||
|
@ -565,75 +565,11 @@ class Scheme:
|
|||
return None
|
||||
|
||||
def construct_text(
|
||||
self, tags: dict[str, str], draw_captions: str, processed: set[str]
|
||||
self, tags: dict[str, str], processed: set[str], label_mode: LabelMode
|
||||
) -> list[Label]:
|
||||
"""Construct labels for not processed tags."""
|
||||
texts: list[Label] = []
|
||||
texts: list[Label] = construct_text(tags, processed, label_mode)
|
||||
|
||||
name = None
|
||||
alt_name = None
|
||||
if "name" in tags:
|
||||
name = tags["name"]
|
||||
processed.add("name")
|
||||
elif "name:en" in tags:
|
||||
if not name:
|
||||
name = tags["name:en"]
|
||||
processed.add("name:en")
|
||||
processed.add("name:en")
|
||||
if "alt_name" in tags:
|
||||
if alt_name:
|
||||
alt_name += ", "
|
||||
else:
|
||||
alt_name = ""
|
||||
alt_name += tags["alt_name"]
|
||||
processed.add("alt_name")
|
||||
if "old_name" in tags:
|
||||
if alt_name:
|
||||
alt_name += ", "
|
||||
else:
|
||||
alt_name = ""
|
||||
alt_name += "ex " + tags["old_name"]
|
||||
|
||||
address: list[str] = get_address(tags, draw_captions, processed)
|
||||
|
||||
if name:
|
||||
texts.append(Label(name, Color("black")))
|
||||
if alt_name:
|
||||
texts.append(Label(f"({alt_name})"))
|
||||
if address:
|
||||
texts.append(Label(", ".join(address)))
|
||||
|
||||
if draw_captions == "main":
|
||||
return texts
|
||||
|
||||
texts += get_text(tags, processed)
|
||||
|
||||
if "route_ref" in tags:
|
||||
texts.append(Label(tags["route_ref"].replace(";", " ")))
|
||||
processed.add("route_ref")
|
||||
if "cladr:code" in tags:
|
||||
texts.append(Label(tags["cladr:code"], size=7))
|
||||
processed.add("cladr:code")
|
||||
if "website" in tags:
|
||||
link = tags["website"]
|
||||
if link[:7] == "http://":
|
||||
link = link[7:]
|
||||
if link[:8] == "https://":
|
||||
link = link[8:]
|
||||
if link[:4] == "www.":
|
||||
link = link[4:]
|
||||
if link[-1] == "/":
|
||||
link = link[:-1]
|
||||
link = link[:25] + ("..." if len(tags["website"]) > 25 else "")
|
||||
texts.append(Label(link, Color("#000088")))
|
||||
processed.add("website")
|
||||
for key in ["phone"]:
|
||||
if key in tags:
|
||||
texts.append(Label(tags[key], Color("#444444")))
|
||||
processed.add(key)
|
||||
if "height" in tags:
|
||||
texts.append(Label(f"↕ {tags['height']} m"))
|
||||
processed.add("height")
|
||||
for tag in tags:
|
||||
if self.is_writable(tag, tags[tag]) and tag not in processed:
|
||||
texts.append(Label(tags[tag]))
|
||||
|
|
|
@ -6,6 +6,8 @@ from typing import Any
|
|||
|
||||
from colour import Color
|
||||
|
||||
from map_machine.map_configuration import LabelMode
|
||||
|
||||
__author__ = "Sergey Vartanov"
|
||||
__email__ = "me@enzet.ru"
|
||||
|
||||
|
@ -25,19 +27,19 @@ class Label:
|
|||
|
||||
|
||||
def get_address(
|
||||
tags: dict[str, Any], draw_captions_mode: str, processed: set[str]
|
||||
tags: dict[str, Any], processed: set[str], label_mode: LabelMode
|
||||
) -> list[str]:
|
||||
"""
|
||||
Construct address text list from the tags.
|
||||
|
||||
:param tags: OSM node, way or relation tags
|
||||
:param draw_captions_mode: captions mode ("all", "main", or "no")
|
||||
:param processed: set of processed tag keys
|
||||
:param label_mode: captions mode
|
||||
"""
|
||||
address: list[str] = []
|
||||
|
||||
tag_names: list[str] = ["housenumber"]
|
||||
if draw_captions_mode == "address":
|
||||
if label_mode == LabelMode.ADDRESS:
|
||||
tag_names += ["postcode", "country", "city", "street"]
|
||||
|
||||
for tag_name in tag_names:
|
||||
|
@ -97,3 +99,81 @@ def get_text(tags: dict[str, Any], processed: set[str]) -> list[Label]:
|
|||
processed.add("frequency")
|
||||
|
||||
return texts
|
||||
|
||||
|
||||
def construct_text(
|
||||
tags: dict[str, str], processed: set[str], label_mode: LabelMode
|
||||
) -> list[Label]:
|
||||
|
||||
texts: list[Label] = []
|
||||
|
||||
name = None
|
||||
alt_name = None
|
||||
if "name" in tags:
|
||||
name = tags["name"]
|
||||
processed.add("name")
|
||||
elif "name:en" in tags:
|
||||
if not name:
|
||||
name = tags["name:en"]
|
||||
processed.add("name:en")
|
||||
processed.add("name:en")
|
||||
if "alt_name" in tags:
|
||||
if alt_name:
|
||||
alt_name += ", "
|
||||
else:
|
||||
alt_name = ""
|
||||
alt_name += tags["alt_name"]
|
||||
processed.add("alt_name")
|
||||
if "old_name" in tags:
|
||||
if alt_name:
|
||||
alt_name += ", "
|
||||
else:
|
||||
alt_name = ""
|
||||
alt_name += "ex " + tags["old_name"]
|
||||
|
||||
address: list[str] = get_address(tags, processed, label_mode)
|
||||
|
||||
if name:
|
||||
texts.append(Label(name, Color("black")))
|
||||
if alt_name:
|
||||
texts.append(Label(f"({alt_name})"))
|
||||
if address:
|
||||
texts.append(Label(", ".join(address)))
|
||||
|
||||
if label_mode == LabelMode.MAIN:
|
||||
return texts
|
||||
|
||||
texts += get_text(tags, processed)
|
||||
|
||||
if "route_ref" in tags:
|
||||
texts.append(Label(tags["route_ref"].replace(";", " ")))
|
||||
processed.add("route_ref")
|
||||
|
||||
if "cladr:code" in tags:
|
||||
texts.append(Label(tags["cladr:code"], size=7))
|
||||
processed.add("cladr:code")
|
||||
|
||||
if "website" in tags:
|
||||
link = tags["website"]
|
||||
if link[:7] == "http://":
|
||||
link = link[7:]
|
||||
if link[:8] == "https://":
|
||||
link = link[8:]
|
||||
if link[:4] == "www.":
|
||||
link = link[4:]
|
||||
if link[-1] == "/":
|
||||
link = link[:-1]
|
||||
link = link[:25] + ("..." if len(tags["website"]) > 25 else "")
|
||||
texts.append(Label(link, Color("#000088")))
|
||||
processed.add("website")
|
||||
|
||||
for key in ["phone"]:
|
||||
if key in tags:
|
||||
texts.append(Label(tags[key], Color("#444444")))
|
||||
processed.add(key)
|
||||
|
||||
if "height" in tags:
|
||||
texts.append(Label(f"↕ {tags['height']} m"))
|
||||
processed.add("height")
|
||||
|
||||
return texts
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""
|
||||
Test label generation for nodes.
|
||||
"""
|
||||
from map_machine.map_configuration import LabelMode
|
||||
from map_machine.text import Label
|
||||
from tests import SCHEME
|
||||
|
||||
|
@ -11,7 +12,7 @@ __email__ = "me@enzet.ru"
|
|||
def construct_labels(tags: dict[str, str]) -> list[Label]:
|
||||
"""Construct labels from OSM node tags."""
|
||||
processed: set[str] = set()
|
||||
return SCHEME.construct_text(tags, "all", processed)
|
||||
return SCHEME.construct_text(tags, processed, LabelMode.ALL)
|
||||
|
||||
|
||||
def test_1_label() -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue