mirror of
https://github.com/enzet/map-machine.git
synced 2025-07-27 21:39:17 +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";
|
version: "0.1";
|
||||||
link: "https://github.com/enzet/Roentgen";
|
link: "https://github.com/enzet/Roentgen";
|
||||||
}
|
}
|
||||||
|
|
||||||
%CONTENT%
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ import svgwrite
|
||||||
from roentgen import server, tile
|
from roentgen import server, tile
|
||||||
from roentgen.constructor import Constructor
|
from roentgen.constructor import Constructor
|
||||||
from roentgen.flinger import Flinger
|
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.icon import ShapeExtractor
|
||||||
from roentgen.mapper import (
|
from roentgen.mapper import (
|
||||||
AUTHOR_MODE,
|
AUTHOR_MODE,
|
||||||
|
@ -219,6 +219,7 @@ if __name__ == "__main__":
|
||||||
elif options.command == "icons":
|
elif options.command == "icons":
|
||||||
draw_icons()
|
draw_icons()
|
||||||
elif options.command == "mapcss":
|
elif options.command == "mapcss":
|
||||||
|
from roentgen.mapcss import write_mapcss
|
||||||
write_mapcss()
|
write_mapcss()
|
||||||
elif options.command == "element":
|
elif options.command == "element":
|
||||||
draw_element(options)
|
draw_element(options)
|
||||||
|
|
|
@ -8,5 +8,6 @@ __description__ = (
|
||||||
"intended to display as many tags as possible"
|
"intended to display as many tags as possible"
|
||||||
)
|
)
|
||||||
__url__ = "https://github.com/enzet/Roentgen"
|
__url__ = "https://github.com/enzet/Roentgen"
|
||||||
|
__doc_url__ = __url__ + "/blob/main/README.md"
|
||||||
__author__ = "Sergey Vartanov"
|
__author__ = "Sergey Vartanov"
|
||||||
__email__ = "me@enzet.ru"
|
__email__ = "me@enzet.ru"
|
||||||
|
|
|
@ -24,7 +24,6 @@ class IconCollection:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
icons: List[Icon]
|
icons: List[Icon]
|
||||||
selectors: Dict[str, Icon] = field(default_factory=dict)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_scheme(
|
def from_scheme(
|
||||||
|
@ -46,7 +45,6 @@ class IconCollection:
|
||||||
tags
|
tags
|
||||||
"""
|
"""
|
||||||
icons: List[Icon] = []
|
icons: List[Icon] = []
|
||||||
selectors: Dict[str, Icon] = {}
|
|
||||||
|
|
||||||
def add() -> Icon:
|
def add() -> Icon:
|
||||||
"""
|
"""
|
||||||
|
@ -67,9 +65,7 @@ class IconCollection:
|
||||||
matcher: NodeMatcher
|
matcher: NodeMatcher
|
||||||
if matcher.shapes:
|
if matcher.shapes:
|
||||||
current_set = matcher.shapes
|
current_set = matcher.shapes
|
||||||
icon = add()
|
add()
|
||||||
if not matcher.location_restrictions:
|
|
||||||
selectors[matcher.get_mapcss_selector()] = icon
|
|
||||||
if matcher.add_shapes:
|
if matcher.add_shapes:
|
||||||
current_set = matcher.add_shapes
|
current_set = matcher.add_shapes
|
||||||
add()
|
add()
|
||||||
|
@ -116,7 +112,7 @@ class IconCollection:
|
||||||
icon.recolor(color)
|
icon.recolor(color)
|
||||||
icons.append(icon)
|
icons.append(icon)
|
||||||
|
|
||||||
return cls(icons, selectors)
|
return cls(icons)
|
||||||
|
|
||||||
def draw_icons(
|
def draw_icons(
|
||||||
self,
|
self,
|
||||||
|
@ -189,24 +185,6 @@ class IconCollection:
|
||||||
with file_name.open("w") as output_file:
|
with file_name.open("w") as output_file:
|
||||||
svg.write(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:
|
def __len__(self) -> int:
|
||||||
return len(self.icons)
|
return len(self.icons)
|
||||||
|
|
||||||
|
@ -240,33 +218,3 @@ def draw_icons() -> None:
|
||||||
logging.info(
|
logging.info(
|
||||||
f"Icons are written to {icons_by_name_path} and {icons_by_id_path}."
|
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])
|
color = self.get_color(self.material_colors[value])
|
||||||
processed.add("material")
|
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"):
|
if tag_key.endswith(":color") or tag_key.endswith(":colour"):
|
||||||
color = self.get_color(tags[tag_key])
|
color = self.get_color(tags[tag_key])
|
||||||
processed.add(tag_key)
|
processed.add(tag_key)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue