mirror of
https://github.com/enzet/map-machine.git
synced 2025-05-23 22:16:25 +02:00
Issue #84: support more than two roads connection.
This commit is contained in:
parent
51b9d3ec0f
commit
6c160b9eca
1 changed files with 72 additions and 41 deletions
|
@ -492,21 +492,17 @@ class Connector:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
road_1: Road,
|
connections: list[tuple[Road, int]],
|
||||||
index_1: int,
|
flinger: Flinger,
|
||||||
road_2: Road,
|
|
||||||
index_2: int,
|
|
||||||
_flinger: Flinger,
|
|
||||||
scale: float,
|
scale: float,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.road_1: Road = road_1
|
self.connections: list[tuple[Road, int]] = connections
|
||||||
self.road_2: Road = road_2
|
self.road_1: Road = connections[0][0]
|
||||||
|
self.index_1: int = connections[0][1]
|
||||||
|
|
||||||
self.index_1: int = index_1
|
self.layer: float = min(x[0].layer for x in connections)
|
||||||
self.index_2: int = index_2
|
|
||||||
|
|
||||||
self.layer: float = min(road_1.layer, road_2.layer)
|
|
||||||
self.scale: float = scale
|
self.scale: float = scale
|
||||||
|
self.flinger: Flinger = flinger
|
||||||
|
|
||||||
def draw(self, svg: Drawing) -> None:
|
def draw(self, svg: Drawing) -> None:
|
||||||
"""Draw connection fill."""
|
"""Draw connection fill."""
|
||||||
|
@ -524,20 +520,21 @@ class SimpleConnector(Connector):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
road_1: Road,
|
connections: list[tuple[Road, int]],
|
||||||
index_1: int,
|
|
||||||
road_2: Road,
|
|
||||||
index_2: int,
|
|
||||||
flinger: Flinger,
|
flinger: Flinger,
|
||||||
scale: float,
|
scale: float,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(road_1, index_1, road_2, index_2, flinger, scale)
|
super().__init__(connections, flinger, scale)
|
||||||
node: OSMNode = road_1.nodes[index_1]
|
|
||||||
|
self.road_2: Road = connections[1][0]
|
||||||
|
self.index_2: int = connections[1][1]
|
||||||
|
|
||||||
|
node: OSMNode = self.road_1.nodes[self.index_1]
|
||||||
self.point: np.ndarray = flinger.fling(node.coordinates)
|
self.point: np.ndarray = flinger.fling(node.coordinates)
|
||||||
|
|
||||||
def draw(self, svg: Drawing) -> None:
|
def draw(self, svg: Drawing) -> None:
|
||||||
"""Draw connection fill."""
|
"""Draw connection fill."""
|
||||||
circle = svg.circle(
|
circle: Circle = svg.circle(
|
||||||
self.point,
|
self.point,
|
||||||
self.road_1.width * self.scale / 2,
|
self.road_1.width * self.scale / 2,
|
||||||
fill=self.road_1.matcher.color.hex,
|
fill=self.road_1.matcher.color.hex,
|
||||||
|
@ -546,7 +543,7 @@ class SimpleConnector(Connector):
|
||||||
|
|
||||||
def draw_border(self, svg: Drawing) -> None:
|
def draw_border(self, svg: Drawing) -> None:
|
||||||
"""Draw connection outline."""
|
"""Draw connection outline."""
|
||||||
circle = svg.circle(
|
circle: Circle = svg.circle(
|
||||||
self.point,
|
self.point,
|
||||||
self.road_1.width * self.scale / 2 + 1,
|
self.road_1.width * self.scale / 2 + 1,
|
||||||
fill=self.road_1.matcher.border_color.hex,
|
fill=self.road_1.matcher.border_color.hex,
|
||||||
|
@ -561,27 +558,27 @@ class ComplexConnector(Connector):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
road_1: Road,
|
connections: list[tuple[Road, int]],
|
||||||
index_1: int,
|
|
||||||
road_2: Road,
|
|
||||||
index_2: int,
|
|
||||||
flinger: Flinger,
|
flinger: Flinger,
|
||||||
scale: float,
|
scale: float,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(road_1, index_1, road_2, index_2, flinger, scale)
|
super().__init__(connections, flinger, scale)
|
||||||
|
|
||||||
length: float = abs(road_2.width - road_1.width) * scale
|
self.road_2: Road = connections[1][0]
|
||||||
road_1.line.shorten(index_1, length)
|
self.index_2: int = connections[1][1]
|
||||||
road_2.line.shorten(index_2, length)
|
|
||||||
|
|
||||||
node: OSMNode = road_1.nodes[index_1]
|
length: float = abs(self.road_2.width - self.road_1.width) * scale
|
||||||
|
self.road_1.line.shorten(self.index_1, length)
|
||||||
|
self.road_2.line.shorten(self.index_2, length)
|
||||||
|
|
||||||
|
node: OSMNode = self.road_1.nodes[self.index_1]
|
||||||
point: np.ndarray = flinger.fling(node.coordinates)
|
point: np.ndarray = flinger.fling(node.coordinates)
|
||||||
|
|
||||||
points_1: list[np.ndarray] = get_curve_points(
|
points_1: list[np.ndarray] = get_curve_points(
|
||||||
road_1, scale, point, road_1.line.points[index_1]
|
self.road_1, scale, point, self.road_1.line.points[self.index_1]
|
||||||
)
|
)
|
||||||
points_2: list[np.ndarray] = get_curve_points(
|
points_2: list[np.ndarray] = get_curve_points(
|
||||||
road_2, scale, point, road_2.line.points[index_2]
|
self.road_2, scale, point, self.road_2.line.points[self.index_2]
|
||||||
)
|
)
|
||||||
# fmt: off
|
# fmt: off
|
||||||
self.curve_1: PathCommands = [
|
self.curve_1: PathCommands = [
|
||||||
|
@ -622,6 +619,42 @@ class ComplexConnector(Connector):
|
||||||
svg.add(path)
|
svg.add(path)
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleIntersection(Connector):
|
||||||
|
"""
|
||||||
|
Connection between more than two roads.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
connections: list[tuple[Road, int]],
|
||||||
|
flinger: Flinger,
|
||||||
|
scale: float,
|
||||||
|
) -> None:
|
||||||
|
super().__init__(connections, flinger, scale)
|
||||||
|
|
||||||
|
def draw(self, svg: Drawing) -> None:
|
||||||
|
"""Draw connection fill."""
|
||||||
|
for road, index in self.connections:
|
||||||
|
node: OSMNode = self.road_1.nodes[self.index_1]
|
||||||
|
point: np.ndarray = self.flinger.fling(node.coordinates)
|
||||||
|
circle: Circle = svg.circle(
|
||||||
|
point, road.width * self.scale / 2, fill=road.matcher.color.hex
|
||||||
|
)
|
||||||
|
svg.add(circle)
|
||||||
|
|
||||||
|
def draw_border(self, svg: Drawing) -> None:
|
||||||
|
"""Draw connection outline."""
|
||||||
|
for road, index in self.connections:
|
||||||
|
node: OSMNode = self.road_1.nodes[self.index_1]
|
||||||
|
point: np.ndarray = self.flinger.fling(node.coordinates)
|
||||||
|
circle: Circle = svg.circle(
|
||||||
|
point,
|
||||||
|
road.width * self.scale / 2 + 1,
|
||||||
|
fill=road.matcher.border_color.hex,
|
||||||
|
)
|
||||||
|
svg.add(circle)
|
||||||
|
|
||||||
|
|
||||||
class Roads:
|
class Roads:
|
||||||
"""
|
"""
|
||||||
Whole road structure.
|
Whole road structure.
|
||||||
|
@ -655,20 +688,18 @@ class Roads:
|
||||||
layered_roads[road.layer].append(road)
|
layered_roads[road.layer].append(road)
|
||||||
|
|
||||||
for id_ in self.connections:
|
for id_ in self.connections:
|
||||||
if len(self.connections[id_]) != 2:
|
|
||||||
continue
|
|
||||||
connected: list[tuple[Road, int]] = self.connections[id_]
|
connected: list[tuple[Road, int]] = self.connections[id_]
|
||||||
road_1, index_1 = connected[0]
|
|
||||||
road_2, index_2 = connected[1]
|
|
||||||
connector: Connector
|
connector: Connector
|
||||||
|
|
||||||
|
if len(self.connections[id_]) == 2:
|
||||||
|
road_1, _ = connected[0]
|
||||||
|
road_2, _ = connected[1]
|
||||||
if road_1.width == road_2.width:
|
if road_1.width == road_2.width:
|
||||||
connector = SimpleConnector(
|
connector = SimpleConnector(connected, flinger, scale)
|
||||||
road_1, index_1, road_2, index_2, flinger, scale
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
connector = ComplexConnector(
|
connector = ComplexConnector(connected, flinger, scale)
|
||||||
road_1, index_1, road_2, index_2, flinger, scale
|
else:
|
||||||
)
|
connector = SimpleIntersection(connected, flinger, scale)
|
||||||
|
|
||||||
if connector.layer not in layered_connectors:
|
if connector.layer not in layered_connectors:
|
||||||
layered_connectors[connector.layer] = []
|
layered_connectors[connector.layer] = []
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue