Issue #45: support width tag for roads.

Compute road width from width=* tag if specified.
This commit is contained in:
Sergey Vartanov 2021-05-28 04:52:15 +03:00
parent 3abd2a1769
commit 4b88b64f86
3 changed files with 37 additions and 30 deletions

View file

@ -192,6 +192,13 @@ class Road(Figure):
super().__init__(tags, inners, outers) super().__init__(tags, inners, outers)
self.matcher: RoadMatcher = matcher self.matcher: RoadMatcher = matcher
self.width: Optional[float] = None
if "width" in tags:
try:
self.width = float(tags["width"])
except ValueError:
pass
def line_center(nodes: List[OSMNode], flinger: Flinger) -> np.array: def line_center(nodes: List[OSMNode], flinger: Flinger) -> np.array:
""" """

View file

@ -11,7 +11,7 @@ from svgwrite.path import Path
from svgwrite.shapes import Rect from svgwrite.shapes import Rect
from roentgen import ui from roentgen import ui
from roentgen.constructor import Building, Constructor, Segment from roentgen.constructor import Building, Constructor, Segment, Road
from roentgen.direction import DirectionSet, Sector from roentgen.direction import DirectionSet, Sector
from roentgen.flinger import Flinger from roentgen.flinger import Flinger
from roentgen.icon import ShapeExtractor from roentgen.icon import ShapeExtractor
@ -78,31 +78,9 @@ class Painter:
constructor.roads, key=lambda x: x.matcher.priority constructor.roads, key=lambda x: x.matcher.priority
) )
for road in roads: for road in roads:
path_commands: str = road.get_path(self.flinger) self.draw_road(road, road.matcher.border_color, 2)
path = Path(d=path_commands)
path.update({
"fill": "none",
"stroke": road.matcher.border_color.hex,
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width":
road.matcher.default_width
* self.flinger.get_scale(road.outers[0][0].coordinates) + 2
})
self.svg.add(path)
for road in roads: for road in roads:
path_commands: str = road.get_path(self.flinger) self.draw_road(road, road.matcher.color)
path = Path(d=path_commands)
path.update({
"fill": "none",
"stroke": road.matcher.color.hex,
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width":
road.matcher.default_width
* self.flinger.get_scale(road.outers[0][0].coordinates)
})
self.svg.add(path)
# Trees # Trees
@ -291,6 +269,26 @@ class Painter:
ui.progress_bar(-1, len(nodes), step=10, text="Drawing nodes") ui.progress_bar(-1, len(nodes), step=10, text="Drawing nodes")
def draw_road(
self, road: Road, color: Color, extra_width: float = 0
) -> None:
self.flinger.get_scale()
if road.width is not None:
width = road.width
else:
width = road.matcher.default_width
scale = self.flinger.get_scale(road.outers[0][0].coordinates)
path_commands: str = road.get_path(self.flinger)
path = Path(d=path_commands)
path.update({
"fill": "none",
"stroke": color.hex,
"stroke-linecap": "round",
"stroke-linejoin": "round",
"stroke-width": scale * width + extra_width
})
self.svg.add(path)
def check_level_number(tags: Dict[str, Any], level: float): def check_level_number(tags: Dict[str, Any], level: float):
""" """

View file

@ -149,14 +149,14 @@ class NodeMatcher(Matcher):
class WayMatcher(Matcher): class WayMatcher(Matcher):
def __init__(self, structure: Dict[str, Any], colors): def __init__(self, structure: Dict[str, Any], scheme: "Scheme"):
super().__init__(structure) super().__init__(structure)
self.style: Dict[str, Any] = {"fill": "none"} self.style: Dict[str, Any] = {"fill": "none"}
if "style" in structure: if "style" in structure:
style: Dict[str, Any] = structure["style"] style: Dict[str, Any] = structure["style"]
for key in style: for key in style:
if str(style[key]).endswith("_color"): if str(style[key]).endswith("_color"):
self.style[key] = colors[style[key]] self.style[key] = scheme.get_color(style[key])
else: else:
self.style[key] = style[key] self.style[key] = style[key]
self.priority = 0 self.priority = 0
@ -165,9 +165,11 @@ class WayMatcher(Matcher):
class RoadMatcher(Matcher): class RoadMatcher(Matcher):
def __init__(self, structure: Dict[str, Any], scheme): def __init__(self, structure: Dict[str, Any], scheme: "Scheme"):
super().__init__(structure) super().__init__(structure)
self.border_color: Color = Color(scheme.get_color(structure["border_color"])) self.border_color: Color = Color(
scheme.get_color(structure["border_color"])
)
self.color: Color = Color("white") self.color: Color = Color("white")
if "color" in structure: if "color" in structure:
self.color = Color(scheme.get_color(structure["color"])) self.color = Color(scheme.get_color(structure["color"]))
@ -200,7 +202,7 @@ class Scheme:
self.colors: Dict[str, str] = content["colors"] self.colors: Dict[str, str] = content["colors"]
self.way_matchers: List[WayMatcher] = [ self.way_matchers: List[WayMatcher] = [
WayMatcher(x, self.colors) for x in content["ways"] WayMatcher(x, self) for x in content["ways"]
] ]
self.road_matchers: List[RoadMatcher] = [ self.road_matchers: List[RoadMatcher] = [
RoadMatcher(x, self) for x in content["roads"] RoadMatcher(x, self) for x in content["roads"]