Change collections JSON format.

Use dictionaries instead of lists.
This commit is contained in:
Sergey Vartanov 2021-11-28 18:36:18 +03:00
parent 1d0e9aea0d
commit 5e7cb1ce38
2 changed files with 94 additions and 81 deletions

View file

@ -1,78 +1,80 @@
[ [
[ {
"Tag:man_made=mast", "page": "Tag:man_made=mast",
{"man_made": "mast"}, "id": "mast",
"tower:construction", "tags": {"man_made": "mast"},
["freestanding", "lattice", "guyed_tube", "guyed_lattice"], "row_key": "tower:construction",
"tower:type", "row_values": ["freestanding", "lattice", "guyed_tube", "guyed_lattice"],
["", "communication", "lighting", "monitoring", "siren"] "column_key": "tower:type",
], "column_values": ["", "communication", "lighting", "monitoring", "siren"]
[ },
"Tag:natural=volcano", {
{"natural": "volcano"}, "page": "Tag:natural=volcano",
"volcano:type", "id": "volcano",
["stratovolcano", "shield", "scoria"], "tags": {"natural": "volcano"},
"volcano:status", "row_key": "volcano:type",
[ "", "active", "dormant", "extinct"] "row_values": ["stratovolcano", "shield", "scoria"],
], "column_key": "volcano:status",
[ "column_values": [ "", "active", "dormant", "extinct"]
"Tag:tower:construction=guyed_tube", },
{"man_made": "mast", "tower:construction": "guyed_tube"}, {
"tower:type", "page": "Tag:tower:construction=guyed_tube",
["", "communication", "lighting", "monitoring", "siren"] "tags": {"man_made": "mast", "tower:construction": "guyed_tube"},
], "row_key": "tower:type",
[ "row_values": ["", "communication", "lighting", "monitoring", "siren"]
"Tag:tower:construction=guyed_lattice", },
{"man_made": "mast", "tower:construction": "guyed_lattice"}, {
"tower:type", "page": "Tag:tower:construction=guyed_lattice",
["", "communication", "lighting", "monitoring", "siren"] "tags": {"man_made": "mast", "tower:construction": "guyed_lattice"},
], "row_key": "tower:type",
[ "row_values": ["", "communication", "lighting", "monitoring", "siren"]
"Key:communication:mobile_phone", },
{"communication:mobile_phone": "yes"} {
], "page": "Key:communication:mobile_phone",
[ "tags": {"communication:mobile_phone": "yes"}
"Key:traffic_calming", },
{}, {
"traffic_calming", "page": "Key:traffic_calming",
[ "tags": {},
"column_key": "traffic_calming",
"column_values": [
"bump", "mini_bumps", "hump", "table", "cushion", "rumble_strip", "bump", "mini_bumps", "hump", "table", "cushion", "rumble_strip",
"dip", "double_dip" "dip", "double_dip"
] ]
], },
[ {
"Key:crane:type", "page": "Key:crane:type",
{"man_made": "crane"}, "tags": {"man_made": "crane"},
"crane:type", "column_key": "crane:type",
[ "column_values": [
"gantry_crane", "floor-mounted_crane", "portal_crane", "gantry_crane", "floor-mounted_crane", "portal_crane",
"travel_lift", "tower_crane" "travel_lift", "tower_crane"
] ]
], },
[ {
"Tag:tower:type=diving", "page": "Tag:tower:type=diving",
{"man_made": "tower", "tower:type": "diving"} "tags": {"man_made": "tower", "tower:type": "diving"}
], },
[ {
"Key:design", "page": "Key:design",
{}, "tags": {},
"power", "row_key": "power",
["tower", "pole"], "row_values": ["tower", "pole"],
"design", "column_key": "design",
[ "column_values": [
"one-level", "two-level", "three-level", "four-level", "asymmetric", "one-level", "two-level", "three-level", "four-level", "asymmetric",
"triangle", "flag", "delta", "delta_two-level", "delta_three-level" "triangle", "flag", "delta", "delta_two-level", "delta_three-level"
] ]
], },
[ {
"Key:design_2", "page": "Key:design_2",
{}, "tags": {},
"power", "row_key": "power",
["tower"], "row_values": ["tower"],
"design", "column_key": "design",
[ "column_values": [
"donau", "donau_inverse", "barrel", "y-frame", "x-frame", "h-frame", "donau", "donau_inverse", "barrel", "y-frame", "x-frame", "h-frame",
"guyed_h-frame", "portal", "portal_two-level", "portal_three-level" "guyed_h-frame", "portal", "portal_two-level", "portal_three-level"
] ]
] }
] ]

View file

@ -4,7 +4,7 @@ Special icon collections for documentation.
import json import json
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import Optional from typing import Any, Optional
import numpy as np import numpy as np
import svgwrite import svgwrite
@ -46,19 +46,22 @@ class Collection:
column_values: list[str] = field(default_factory=list) column_values: list[str] = field(default_factory=list)
@classmethod @classmethod
def deserialize(cls, structure): def deserialize(cls, structure: dict[str, Any]):
"""Deserialize icon collection from structure.""" """Deserialize icon collection from structure."""
row_key = structure[2] if len(structure) > 2 else None row_key: Optional[str] = (
row_values = structure[3] if len(structure) > 3 else [] structure["row_key"] if "row_key" in structure else None
column_key = structure[4] if len(structure) > 4 else None )
column_values = structure[5] if len(structure) > 5 else [] row_values: list[str] = (
structure["row_values"] if "row_values" in structure else []
)
column_key: Optional[str] = (
structure["column_key"] if "column_key" in structure else None
)
column_values: list[str] = (
structure["column_values"] if "column_values" in structure else []
)
return cls( return cls(
structure[1], structure["tags"], row_key, row_values, column_key, column_values
row_key,
row_values,
column_key,
column_values,
) )
@ -98,12 +101,13 @@ class SVGTable:
max(map(len, self.collection.row_values)) * self.font_width, max(map(len, self.collection.row_values)) * self.font_width,
len(self.collection.row_key) * self.font_width len(self.collection.row_key) * self.font_width
+ (self.offset if self.collection.column_values else 0), + (self.offset if self.collection.column_values else 0),
170.0,
) )
if self.collection.row_values if self.collection.row_values
else 25.0, else 0.0,
max(map(len, self.collection.column_values)) * self.font_width max(map(len, self.collection.column_values)) * self.font_width
if self.collection.column_values if self.collection.column_values
else 25.0, else 0.0,
] ]
self.start_point: np.ndarray = ( self.start_point: np.ndarray = (
2 * self.border + np.array(self.size) + self.half_step 2 * self.border + np.array(self.size) + self.half_step
@ -285,6 +289,7 @@ class SVGTable:
) )
) )
* self.step * self.step
- self.half_step
+ self.border + self.border
) )
@ -293,10 +298,14 @@ def draw_svg_tables(output_path: Path, html_file_path: Path) -> None:
"""Draw SVG tables of icon collections.""" """Draw SVG tables of icon collections."""
with Path("data/collections.json").open() as input_file: with Path("data/collections.json").open() as input_file:
collections: list[list] = json.load(input_file) collections: list[dict[str, Any]] = json.load(input_file)
with html_file_path.open("w+") as html_file: with html_file_path.open("w+") as html_file:
for structure in collections: for structure in collections:
path: Path = output_path / f"{structure[0]}.svg" if "id" not in structure:
continue
path: Path = output_path / f"{structure['id']}.svg"
svg: Drawing = svgwrite.Drawing(path.name) svg: Drawing = svgwrite.Drawing(path.name)
collection: Collection = Collection.deserialize(structure) collection: Collection = Collection.deserialize(structure)
@ -306,7 +315,9 @@ def draw_svg_tables(output_path: Path, html_file_path: Path) -> None:
with path.open("w+") as output_file: with path.open("w+") as output_file:
svg.write(output_file) svg.write(output_file)
html_file.write(f'<img src="{path}" />\n') html_file.write(
f'<img src="{path}" style="border: 1px solid #DDD;" />\n'
)
if __name__ == "__main__": if __name__ == "__main__":