Fixing random turns
Browse files- backend/api/routes.py +29 -4
backend/api/routes.py
CHANGED
|
@@ -12,10 +12,35 @@ DISTANCE_BUDGET_FACTOR_OVERALL = 1.5
|
|
| 12 |
|
| 13 |
|
| 14 |
def route_to_geojson(G, route):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return {
|
| 20 |
"type": "Feature",
|
| 21 |
"geometry": {
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def route_to_geojson(G, route):
|
| 15 |
+
"""
|
| 16 |
+
Build GeoJSON coordinates from the route node list.
|
| 17 |
+
|
| 18 |
+
For each edge, use the OSM edge geometry (intermediate curve points)
|
| 19 |
+
if available — otherwise fall back to straight start→end node coords.
|
| 20 |
+
This prevents phantom turns caused by straight-line interpolation
|
| 21 |
+
between widely-spaced nodes on curved roads.
|
| 22 |
+
"""
|
| 23 |
+
if len(route) == 0:
|
| 24 |
+
coordinates = []
|
| 25 |
+
elif len(route) == 1:
|
| 26 |
+
n = route[0]
|
| 27 |
+
coordinates = [[G.nodes[n]["x"], G.nodes[n]["y"]]]
|
| 28 |
+
else:
|
| 29 |
+
coordinates = [[G.nodes[route[0]]["x"], G.nodes[route[0]]["y"]]]
|
| 30 |
+
for i in range(len(route) - 1):
|
| 31 |
+
u, v = route[i], route[i + 1]
|
| 32 |
+
edge = list(G[u][v].values())[0]
|
| 33 |
+
geom = edge.get("geometry")
|
| 34 |
+
if geom is not None:
|
| 35 |
+
# geom is a Shapely LineString — coords are (lon, lat)
|
| 36 |
+
# Skip the first point (already added as previous node)
|
| 37 |
+
pts = list(geom.coords)
|
| 38 |
+
for lon, lat in pts[1:]:
|
| 39 |
+
coordinates.append([lon, lat])
|
| 40 |
+
else:
|
| 41 |
+
# No intermediate geometry — just add the end node
|
| 42 |
+
coordinates.append([G.nodes[v]["x"], G.nodes[v]["y"]])
|
| 43 |
+
|
| 44 |
return {
|
| 45 |
"type": "Feature",
|
| 46 |
"geometry": {
|