Commit b10a37bd authored by Eric Duminil's avatar Eric Duminil
Browse files

Trying to recognize standard widths

parent 1711eae1
...@@ -8,7 +8,7 @@ Trees are exported in a CSV table, a PNG diagram and an HTML interactive map. ...@@ -8,7 +8,7 @@ Trees are exported in a CSV table, a PNG diagram and an HTML interactive map.
""" """
import pickle import pickle
from pathlib import Path from pathlib import Path
from collections import namedtuple from collections import namedtuple, Counter
import folium import folium
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -42,8 +42,7 @@ MIN_DISTANCE = TREE_DISTANCE * 0.5 # [m] ...@@ -42,8 +42,7 @@ MIN_DISTANCE = TREE_DISTANCE * 0.5 # [m]
# For display purposes only: # For display purposes only:
GRID = 100 # [m] GRID = 100 # [m]
IGNORE_ROADS = set(['primary', 'unclassified', 'secondary', DEFAULT_WIDTHS = {'unclassified': 0}
'secondary_link', 'trunk', 'trunk_link', 'primary_link'])
SCRIPT_DIR = Path(__file__).resolve().parent SCRIPT_DIR = Path(__file__).resolve().parent
...@@ -88,7 +87,7 @@ def get_osm_roads(bounds: Bounds): ...@@ -88,7 +87,7 @@ def get_osm_roads(bounds: Bounds):
return ways return ways
def set_plot(bounds, to_local_coordinates): def set_plot(bounds: Bounds, to_local_coordinates):
x_min, y_min = to_local_coordinates.transform(bounds.W, bounds.S) x_min, y_min = to_local_coordinates.transform(bounds.W, bounds.S)
x_max, y_max = to_local_coordinates.transform(bounds.E, bounds.N) x_max, y_max = to_local_coordinates.transform(bounds.E, bounds.N)
ax = plt.axes() ax = plt.axes()
...@@ -103,15 +102,40 @@ def set_plot(bounds, to_local_coordinates): ...@@ -103,15 +102,40 @@ def set_plot(bounds, to_local_coordinates):
return ax return ax
def get_default_widths(ways: list) -> dict[str, float] :
"""Check existing OSM highways, and extract the most common width for each type"""
width_counters = {}
for way in ways:
width = float(way.tags.get("width", 0))
way_type = way.tags.get("highway")
print(way_type, width)
if width:
if way_type not in width_counters:
width_counters[way_type] = Counter()
width_counters[way_type][width] += 1
return {w:c.most_common(1)[0][0] for w, c in width_counters.items()}
def place_trees(forest: Forest, ways: list, region: str, to_local, tree_distance: float, min_distance_2: float) -> Forest: def place_trees(forest: Forest, ways: list, region: str, to_local, tree_distance: float, min_distance_2: float) -> Forest:
local_region = transform(to_local.transform, region) local_region = transform(to_local.transform, region)
default_widths = {**get_default_widths(ways), **DEFAULT_WIDTHS}
print(f"Default widths: {default_widths}")
for way in ways: for way in ways:
way_type = way.tags.get("highway")
width = float(way.tags.get("width", 0)) width = float(way.tags.get("width", 0))
highway = way.tags.get("highway") if width:
if highway in IGNORE_ROADS: # Defined in OSM
color = 'blue'
else:
width = default_widths.get(way_type, 0)
if width:
# From OSM most common width
color = 'orange' color = 'orange'
else: else:
# Unknown
color = 'gray' color = 'gray'
road_xy_s = [to_local.transform(node.lon, node.lat) for node in way.nodes] road_xy_s = [to_local.transform(node.lon, node.lat) for node in way.nodes]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment