diff --git a/python_scripts/add_trees_to_open_street_map/add_trees.py b/python_scripts/add_trees_to_open_street_map/add_trees.py index 35fa8a5fdae96f5fd04fd69438bc4eca73727cca..7df812c66280caafdfdaf5976970d0a1cdeb6841 100644 --- a/python_scripts/add_trees_to_open_street_map/add_trees.py +++ b/python_scripts/add_trees_to_open_street_map/add_trees.py @@ -8,7 +8,7 @@ Trees are exported in a CSV table, a PNG diagram and an HTML interactive map. """ import pickle from pathlib import Path -from collections import namedtuple +from collections import namedtuple, Counter import folium import matplotlib.pyplot as plt @@ -42,8 +42,7 @@ MIN_DISTANCE = TREE_DISTANCE * 0.5 # [m] # For display purposes only: GRID = 100 # [m] -IGNORE_ROADS = set(['primary', 'unclassified', 'secondary', - 'secondary_link', 'trunk', 'trunk_link', 'primary_link']) +DEFAULT_WIDTHS = {'unclassified': 0} SCRIPT_DIR = Path(__file__).resolve().parent @@ -88,7 +87,7 @@ def get_osm_roads(bounds: Bounds): 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_max, y_max = to_local_coordinates.transform(bounds.E, bounds.N) ax = plt.axes() @@ -103,16 +102,41 @@ def set_plot(bounds, to_local_coordinates): 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: 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: + way_type = way.tags.get("highway") width = float(way.tags.get("width", 0)) - highway = way.tags.get("highway") - if highway in IGNORE_ROADS: - color = 'orange' + if width: + # Defined in OSM + color = 'blue' else: - color = 'gray' + width = default_widths.get(way_type, 0) + if width: + # From OSM most common width + color = 'orange' + else: + # Unknown + color = 'gray' road_xy_s = [to_local.transform(node.lon, node.lat) for node in way.nodes]