mirror of
https://github.com/enzet/map-machine.git
synced 2025-07-25 20:39:22 +02:00
Issue #62: refactor MapCSS generation.
This commit is contained in:
parent
886f041a41
commit
253f59d878
6 changed files with 74 additions and 58 deletions
68
roentgen/mapcss.py
Normal file
68
roentgen/mapcss.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
"""
|
||||
MapCSS scheme creation.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
import logging
|
||||
from colour import Color
|
||||
|
||||
from roentgen.grid import IconCollection
|
||||
from roentgen.icon import ShapeExtractor
|
||||
from roentgen.scheme import Scheme
|
||||
|
||||
|
||||
def construct_selectors(scheme: Scheme, icon_directory_name: str):
|
||||
"""
|
||||
Construct icon selectors for MapCSS 0.2 scheme.
|
||||
"""
|
||||
selectors: Dict[str, List[str]] = {}
|
||||
for matcher in scheme.node_matchers:
|
||||
if matcher.shapes and not matcher.location_restrictions:
|
||||
# TODO: support location restrictions
|
||||
selectors[matcher.get_mapcss_selector()] = [
|
||||
(x if isinstance(x, str) else x["shape"]) for x in matcher.shapes
|
||||
]
|
||||
|
||||
s = ""
|
||||
for selector in selectors:
|
||||
for target in ["node", "area"]:
|
||||
s += (
|
||||
target + selector + " {\n"
|
||||
f' icon-image: "{icon_directory_name}/'
|
||||
+ "___".join(selectors[selector])
|
||||
+ '.svg";\n}\n'
|
||||
)
|
||||
return s
|
||||
|
||||
|
||||
def write_mapcss() -> None:
|
||||
"""
|
||||
Write MapCSS 0.2 scheme.
|
||||
"""
|
||||
icon_directory_name: str = "icons"
|
||||
|
||||
out_path: Path = Path("out")
|
||||
directory: Path = out_path / "roentgen_icons_mapcss"
|
||||
directory.mkdir(exist_ok=True)
|
||||
icons_with_outline_path: Path = directory / icon_directory_name
|
||||
|
||||
icons_with_outline_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
scheme: Scheme = Scheme(Path("scheme/default.yml"))
|
||||
extractor: ShapeExtractor = ShapeExtractor(
|
||||
Path("icons/icons.svg"), Path("icons/config.json")
|
||||
)
|
||||
collection: IconCollection = IconCollection.from_scheme(scheme, extractor)
|
||||
collection.draw_icons(
|
||||
icons_with_outline_path, color=Color("black"), outline=True
|
||||
)
|
||||
with Path("data/roentgen_icons_part.mapcss").open() as input_file:
|
||||
header = input_file.read()
|
||||
|
||||
with (directory / "roentgen_icons.mapcss").open("w+") as output_file:
|
||||
output_file.write(header)
|
||||
output_file.write("\n")
|
||||
output_file.write(construct_selectors(scheme, icon_directory_name))
|
||||
|
||||
logging.info(f"MapCSS 0.2 scheme is written to {directory}.")
|
Loading…
Add table
Add a link
Reference in a new issue