Commit 0842edf1 authored by Eric Duminil's avatar Eric Duminil
Browse files

OSM: Trying to fix widths with units

fixing some types too
parent 06421a92
...@@ -9,6 +9,7 @@ Trees are exported in a CSV table, a PNG diagram and an HTML interactive map. ...@@ -9,6 +9,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, Counter from collections import namedtuple, Counter
import re
import folium import folium
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -30,7 +31,9 @@ from import_existing_trees import get_existing_forest ...@@ -30,7 +31,9 @@ from import_existing_trees import get_existing_forest
# TODO: Write tests? # TODO: Write tests?
# From RegionChooser, or https://transfer.hft-stuttgart.de/gitlab/circulargreensimcity/circulargreensimcity/-/wikis/Fallstudien/Gromb%C3%BChl # From RegionChooser, or https://transfer.hft-stuttgart.de/gitlab/circulargreensimcity/circulargreensimcity/-/wikis/Fallstudien/Gromb%C3%BChl
WKT = "POLYGON((9.947021 49.803063, 9.947011 49.800917, 9.955025 49.800810, 9.955110 49.803019, 9.947021 49.803063))" # WKT = "POLYGON((9.947021 49.803063, 9.947011 49.800917, 9.955025 49.800810, 9.955110 49.803019, 9.947021 49.803063))"
# Grafenbühl
WKT = "POLYGON((9.147551 48.908059, 9.148635 48.907953, 9.149525 48.907819, 9.151177 48.907819, 9.151413 48.907840, 9.153226 48.908087, 9.153387 48.906705, 9.149160 48.906634, 9.148999 48.906620, 9.147551 48.908059))"
# Replace with None if no existing tree should be imported # Replace with None if no existing tree should be imported
EXISTING_TREES = 'existing_trees/Trees_ideal_2_20240227.shp' EXISTING_TREES = 'existing_trees/Trees_ideal_2_20240227.shp'
# EXISTING_TREES = 'existing_trees/baumkataster/Baum.shp' # EXISTING_TREES = 'existing_trees/baumkataster/Baum.shp'
...@@ -109,13 +112,18 @@ def set_plot(bounds: Bounds, to_local_coordinates): ...@@ -109,13 +112,18 @@ def set_plot(bounds: Bounds, to_local_coordinates):
return ax return ax
def get_width(way: overpy.Way) -> float:
width_str = way.tags.get("width", '0')
# NOTE: Some widths are written with units, so try to remove trailing [m] first.
width_str = re.sub(' ?m$', '', width_str)
return float(width_str)
def get_default_widths(ways: list) -> dict[str, float]: def get_default_widths(ways: list) -> dict[str, float]:
"""Check existing OSM highways, and extract the most common width for each type""" """Check existing OSM highways, and extract the most common width for each type"""
width_counters = {} width_counters: dict[str, Counter] = {}
for way in ways: for way in ways:
# NOTE: Some widths are written with units, so try to remove [m] first. width = get_width(way)
# TODO: use regex instead
width = float(way.tags.get("width", '0').replace('m', ''))
way_type = way.tags.get("highway") way_type = way.tags.get("highway")
if width: if width:
if way_type not in width_counters: if way_type not in width_counters:
...@@ -133,7 +141,7 @@ def place_trees(forest: Forest, ways: list, region: str, to_local, tree_distance ...@@ -133,7 +141,7 @@ def place_trees(forest: Forest, ways: list, region: str, to_local, tree_distance
for way in ways: for way in ways:
way_type = way.tags.get("highway") way_type = way.tags.get("highway")
width = float(way.tags.get("width", 0)) width = get_width(way)
default_width = default_widths.get(way_type, 0) default_width = default_widths.get(way_type, 0)
if default_width < 0: if default_width < 0:
# Ignore this type of streets # Ignore this type of streets
...@@ -199,7 +207,7 @@ def plot_trees(bounds: Bounds, forest: Forest, tree_distance: float) -> None: ...@@ -199,7 +207,7 @@ def plot_trees(bounds: Bounds, forest: Forest, tree_distance: float) -> None:
print(" DONE!") print(" DONE!")
def export_map(bounds: Bounds, forest: Forest, epsg_id: str) -> None: def export_map(bounds: Bounds, forest: Forest, epsg_id: int) -> None:
print("Exporting Map...") print("Exporting Map...")
to_wgs84 = Transformer.from_crs(f"EPSG:{epsg_id}", "EPSG:4326", always_xy=True) to_wgs84 = Transformer.from_crs(f"EPSG:{epsg_id}", "EPSG:4326", always_xy=True)
interactive_map = folium.Map() interactive_map = folium.Map()
...@@ -232,7 +240,7 @@ def export_map(bounds: Bounds, forest: Forest, epsg_id: str) -> None: ...@@ -232,7 +240,7 @@ def export_map(bounds: Bounds, forest: Forest, epsg_id: str) -> None:
print(" DONE!") print(" DONE!")
def export_csv(bounds: Bounds, forest: Forest, wkt_polygon: str, tree_distance: float, min_distance: float, epsg_id: str) -> None: def export_csv(bounds: Bounds, forest: Forest, wkt_polygon: str, tree_distance: float, min_distance: float, epsg_id: int) -> None:
print("Exporting CSV...") print("Exporting CSV...")
with open(OUTPUT_DIR / f"{get_basename(bounds)}_trees.csv", "w") as csv: with open(OUTPUT_DIR / f"{get_basename(bounds)}_trees.csv", "w") as csv:
csv.write(f"# Fake trees for; {wkt_polygon}\n") csv.write(f"# Fake trees for; {wkt_polygon}\n")
...@@ -247,7 +255,7 @@ def export_csv(bounds: Bounds, forest: Forest, wkt_polygon: str, tree_distance: ...@@ -247,7 +255,7 @@ def export_csv(bounds: Bounds, forest: Forest, wkt_polygon: str, tree_distance:
print(" DONE!") print(" DONE!")
def export_shapefile(bounds: Bounds, forest: Forest, epsg_id: str) -> None: def export_shapefile(bounds: Bounds, forest: Forest, epsg_id: int) -> None:
print("Exporting shapefile") print("Exporting shapefile")
data = [{ data = [{
...@@ -274,7 +282,7 @@ def export_shapefile(bounds: Bounds, forest: Forest, epsg_id: str) -> None: ...@@ -274,7 +282,7 @@ def export_shapefile(bounds: Bounds, forest: Forest, epsg_id: str) -> None:
print(" DONE!") print(" DONE!")
def main(wkt_polygon: str, epsg_id: str, tree_distance: float, min_distance: float, import_tree_shp) -> None: def main(wkt_polygon: str, epsg_id: int, tree_distance: float, min_distance: float, import_tree_shp) -> None:
region, bounds = load_region(wkt_polygon) region, bounds = load_region(wkt_polygon)
ways = get_osm_roads(bounds) ways = get_osm_roads(bounds)
......
...@@ -10,8 +10,8 @@ class Tree: ...@@ -10,8 +10,8 @@ class Tree:
diameter: float diameter: float
height: float height: float
z: float = 0 z: float = 0
trunk_diameter: float = None trunk_diameter: float|None = None
type: str = None type: str|None = None
description: str = '?' description: str = '?'
source: str = '?' source: str = '?'
color: str = 'green' color: str = 'green'
......
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