mirror of
https://github.com/enzet/map-machine.git
synced 2025-07-28 05:48:57 +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
|
@ -9,5 +9,3 @@ meta {
|
|||
version: "0.1";
|
||||
link: "https://github.com/enzet/Roentgen";
|
||||
}
|
||||
|
||||
%CONTENT%
|
||||
|
|
|
@ -15,7 +15,7 @@ import svgwrite
|
|||
from roentgen import server, tile
|
||||
from roentgen.constructor import Constructor
|
||||
from roentgen.flinger import Flinger
|
||||
from roentgen.grid import draw_icons, write_mapcss
|
||||
from roentgen.grid import draw_icons
|
||||
from roentgen.icon import ShapeExtractor
|
||||
from roentgen.mapper import (
|
||||
AUTHOR_MODE,
|
||||
|
@ -219,6 +219,7 @@ if __name__ == "__main__":
|
|||
elif options.command == "icons":
|
||||
draw_icons()
|
||||
elif options.command == "mapcss":
|
||||
from roentgen.mapcss import write_mapcss
|
||||
write_mapcss()
|
||||
elif options.command == "element":
|
||||
draw_element(options)
|
||||
|
|
|
@ -8,5 +8,6 @@ __description__ = (
|
|||
"intended to display as many tags as possible"
|
||||
)
|
||||
__url__ = "https://github.com/enzet/Roentgen"
|
||||
__doc_url__ = __url__ + "/blob/main/README.md"
|
||||
__author__ = "Sergey Vartanov"
|
||||
__email__ = "me@enzet.ru"
|
||||
|
|
|
@ -24,7 +24,6 @@ class IconCollection:
|
|||
"""
|
||||
|
||||
icons: List[Icon]
|
||||
selectors: Dict[str, Icon] = field(default_factory=dict)
|
||||
|
||||
@classmethod
|
||||
def from_scheme(
|
||||
|
@ -46,7 +45,6 @@ class IconCollection:
|
|||
tags
|
||||
"""
|
||||
icons: List[Icon] = []
|
||||
selectors: Dict[str, Icon] = {}
|
||||
|
||||
def add() -> Icon:
|
||||
"""
|
||||
|
@ -67,9 +65,7 @@ class IconCollection:
|
|||
matcher: NodeMatcher
|
||||
if matcher.shapes:
|
||||
current_set = matcher.shapes
|
||||
icon = add()
|
||||
if not matcher.location_restrictions:
|
||||
selectors[matcher.get_mapcss_selector()] = icon
|
||||
add()
|
||||
if matcher.add_shapes:
|
||||
current_set = matcher.add_shapes
|
||||
add()
|
||||
|
@ -116,7 +112,7 @@ class IconCollection:
|
|||
icon.recolor(color)
|
||||
icons.append(icon)
|
||||
|
||||
return cls(icons, selectors)
|
||||
return cls(icons)
|
||||
|
||||
def draw_icons(
|
||||
self,
|
||||
|
@ -189,24 +185,6 @@ class IconCollection:
|
|||
with file_name.open("w") as output_file:
|
||||
svg.write(output_file)
|
||||
|
||||
def get_mapcss_selectors(self) -> str:
|
||||
"""
|
||||
Construct MapCSS 0.2 style scheme.
|
||||
"""
|
||||
s = ""
|
||||
for selector in self.selectors:
|
||||
for target in ["node", "area"]:
|
||||
s += target + selector + " {\n"
|
||||
s += (
|
||||
' icon-image: "icons/'
|
||||
+ "___".join(self.selectors[selector].get_shape_ids())
|
||||
+ '.svg";\n'
|
||||
)
|
||||
s += " set icon_z17;\n"
|
||||
s += " icon-width: 16;\n"
|
||||
s += "}\n"
|
||||
return s
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.icons)
|
||||
|
||||
|
@ -240,33 +218,3 @@ def draw_icons() -> None:
|
|||
logging.info(
|
||||
f"Icons are written to {icons_by_name_path} and {icons_by_id_path}."
|
||||
)
|
||||
|
||||
|
||||
def write_mapcss() -> None:
|
||||
"""
|
||||
Write MapCSS 0.2 scheme.
|
||||
"""
|
||||
out_path: Path = Path("out")
|
||||
directory: Path = out_path / "roentgen_icons_mapcss"
|
||||
directory.mkdir(exist_ok=True)
|
||||
icons_with_outline_path: Path = directory / "icons"
|
||||
|
||||
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:
|
||||
with (directory / "roentgen_icons.mapcss").open("w+") as output_file:
|
||||
for line in input_file.readlines():
|
||||
if line == "%CONTENT%\n":
|
||||
output_file.write(collection.get_mapcss_selectors())
|
||||
else:
|
||||
output_file.write(line)
|
||||
|
||||
logging.info(f"MapCSS 0.2 scheme is written to {directory}.")
|
||||
|
|
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}.")
|
|
@ -367,7 +367,7 @@ class Scheme:
|
|||
color = self.get_color(self.material_colors[value])
|
||||
processed.add("material")
|
||||
|
||||
for tag_key in tags: # type: str
|
||||
for tag_key in tags:
|
||||
if tag_key.endswith(":color") or tag_key.endswith(":colour"):
|
||||
color = self.get_color(tags[tag_key])
|
||||
processed.add(tag_key)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue