Commit 59b7689f authored by Eric Duminil's avatar Eric Duminil
Browse files

Current status: add trees to open street map

parent f6e6eeb4
*.pyc
\ No newline at end of file
cache
*.html
*.csv
*.png
\ No newline at end of file
PROJCS["ETRS89.UTM-32N",GEOGCS["LL-ETRF89",DATUM["ETRF89",SPHEROID["GRS1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["false_easting",500000.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",9.0],PARAMETER["scale_factor",0.9996],PARAMETER["latitude_of_origin",0.0],UNIT["Meter",1.0]]
\ No newline at end of file
<metadata xml:lang="de"><Esri><CreaDate>20240227</CreaDate><CreaTime>14401500</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>FALSE</SyncOnce><DataProperties><itemProps><itemLocation><linkage Sync="TRUE">file://\\shares01.ad.hft-stuttgart.de\fkb$\staff\kschulze\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\05_Grünszenarien\Trees_ideal_2_20240227\Trees_ideal_2_20240227.shp</linkage><protocol Sync="TRUE">Local Area Network</protocol></itemLocation><itemName Sync="TRUE">Trees_ideal_2_20240227</itemName><imsContentType Sync="TRUE">002</imsContentType><itemSize Sync="TRUE">0.000</itemSize></itemProps><copyHistory><copy source="C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\Baum" dest="\\IAF-KSR001-NB\C$\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\Baum_1" date="20231208" time="10524500"></copy></copyHistory><lineage><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\Copy" Date="20230807" Time="161016">Copy "C:\Users\Katja.Schulze\Documents\01_CircularGreenSimCity\07_Datenbeschaffung\WÜ\Daten_Aktueller Stand\2023-01-31 - Daten Bäume-Baujahre an HFT_TUM\Baum.shp" C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\Baum.shp Shapefile #</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\UpdateSchema" Date="20231124" Time="144601">UpdateSchema "CIMDATA=&lt;CIMStandardDataConnection xsi:type='typens:CIMStandardDataConnection' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/3.1.0'&gt;&lt;WorkspaceConnectionString&gt;DATABASE=C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU&lt;/WorkspaceConnectionString&gt;&lt;WorkspaceFactory&gt;Shapefile&lt;/WorkspaceFactory&gt;&lt;Dataset&gt;Baum.shp&lt;/Dataset&gt;&lt;DatasetType&gt;esriDTFeatureClass&lt;/DatasetType&gt;&lt;/CIMStandardDataConnection&gt;" &lt;operationSequence&gt;&lt;workflow&gt;&lt;AddField&gt;&lt;field_name&gt;Kroneradi&lt;/field_name&gt;&lt;field_type&gt;FLOAT&lt;/field_type&gt;&lt;field_is_nullable&gt;False&lt;/field_is_nullable&gt;&lt;field_is_required&gt;False&lt;/field_is_required&gt;&lt;/AddField&gt;&lt;/workflow&gt;&lt;/operationSequence&gt;</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\CalculateField" Date="20231124" Time="144645">CalculateField WÜ_23_01_31\Baum Kroneradi "!Kronenbrei! * 0.5" "Python 3" # Text NO_ENFORCE_DOMAINS</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\Copy" Date="20231208" Time="105245">Copy C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\Baum.shp C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\Baum_1.shp Shapefile #</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Conversion Tools.tbx\ExportFeatures" Date="20231208" Time="110432">ExportFeatures CaseStudy_TreeScenarios\Baum_ideal_ALL C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\CaseStudy_TreeScenario_ideal_20231208_ALL.shp # NOT_USE_ALIAS "Ident "Ident" true true false 8 Text 0 0,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Ident,0,8;Bezeichnun "Bezeichnun" true true false 32 Text 0 0,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Bezeichnun,0,32;Baumart "Baumart" true true false 31 Text 0 0,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Baumart,0,31;Alter am S "Alter am S" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Alter am S,-1,-1;Baumhöhe "Baumhöhe" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Baumhöhe,-1,-1;Kronenbrei "Kronenbrei" true true false 5 Float 0 0,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Kronenbrei,-1,-1;Stammumfan "Stammumfan" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Stammumfan,-1,-1;PRIMARYIND "PRIMARYIND" true true false 11 Double 0 11,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,PRIMARYIND,-1,-1;Kroneradi "Kroneradi" true true false 13 Float 0 0,First,#,CaseStudy_TreeScenarios\Baum_ideal_ALL,Kroneradi,-1,-1" #</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\CalculateField" Date="20231208" Time="151216">CalculateField CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL Kroneradi "!Kronenbrei! / 2" "Python 3" # Text NO_ENFORCE_DOMAINS</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\CalculateField" Date="20231208" Time="151303">CalculateField CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL Kroneradi "!Kronenbrei! / 2" "Python 3" # Text NO_ENFORCE_DOMAINS</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Conversion Tools.tbx\ExportFeatures" Date="20231208" Time="152313">ExportFeatures CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL C:\Users\Katja.Schulze\Documents\ArcGIS\Projects\Würzburg_Grombühl_NEU\CaseStudy_TreeScenario_ideal_20231208_2_ALL.shp # NOT_USE_ALIAS "Ident "Ident" true true false 8 Text 0 0,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Ident,0,8;Bezeichnun "Bezeichnun" true true false 32 Text 0 0,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Bezeichnun,0,32;Baumart "Baumart" true true false 31 Text 0 0,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Baumart,0,31;Alter_am_S "Alter_am_S" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Alter_am_S,-1,-1;Baumhöhe "Baumhöhe" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Baumhöhe,-1,-1;Kronenbrei "Kronenbrei" true true false 13 Float 0 0,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Kronenbrei,-1,-1;Stammumfan "Stammumfan" true true false 6 Long 0 6,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Stammumfan,-1,-1;PRIMARYIND "PRIMARYIND" true true false 11 Double 0 11,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,PRIMARYIND,-1,-1;Kroneradi "Kroneradi" true true false 13 Float 0 0,First,#,CaseStudy_TreeScenarios\CaseStudy_TreeScenario_ideal_20231208_ALL,Kroneradi,-1,-1" #</Process><Process ToolSource="c:\users\kschulze\appdata\local\programs\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\CopyFeatures" Date="20240201" Time="094958">CopyFeatures Trees_ideal_20240201 P:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\Grombuehl_Geodatabase_240201.gdb\Trees_ideal_20240201 # # # #</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\Toolboxes\Data Management Tools.tbx\CopyMultiple" Date="20240227" Time="144016">CopyMultiple "P:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\Grombuehl_Geodatabase_240201.gdb\Trees_ideal_20240201 FeatureClass" P:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\Grombuehl_Geodatabase_240201.gdb Trees_ideal_20240201_1 "Trees_ideal_20240201 FeatureClass Trees_ideal_20240201_1 #"</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\Toolboxes\Data Management Tools.tbx\Rename" Date="20240227" Time="144034">Rename P:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\Grombuehl_Geodatabase_240201.gdb\Trees_ideal_20240201_1 P:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\Grombuehl_Geodatabase_240201.gdb\Trees_ideal_2_20240227 FeatureClass</Process><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Conversion Tools.tbx\ExportFeatures" Date="20240227" Time="145416">ExportFeatures Trees_ideal_2_20240227 Z:\01_CircularGreenSimCity\AP3_Grün+Wasserworkflow\05_Grünszenarien\Trees_ideal_2_20240227\Trees_ideal_2_20240227.shp # NOT_USE_ALIAS "Ident "Ident" true true false 8 Text 0 0,First,#,Trees_ideal_2_20240227,Ident,0,7;Bezeichnun "Bezeichnun" true true false 32 Text 0 0,First,#,Trees_ideal_2_20240227,Bezeichnun,0,31;Baumart "Baumart" true true false 31 Text 0 0,First,#,Trees_ideal_2_20240227,Baumart,0,30;Alter_am_S "Alter_am_S" true true false 4 Long 0 0,First,#,Trees_ideal_2_20240227,Alter_am_S,-1,-1;Baumhöhe "Baumhöhe" true true false 4 Long 0 0,First,#,Trees_ideal_2_20240227,Baumhöhe,-1,-1;Kronenbrei "Kronenbrei" true true false 4 Float 0 0,First,#,Trees_ideal_2_20240227,Kronenbrei,-1,-1;Stammumfan "Stammumfan" true true false 4 Long 0 0,First,#,Trees_ideal_2_20240227,Stammumfan,-1,-1;PRIMARYIND "PRIMARYIND" true true false 8 Double 0 0,First,#,Trees_ideal_2_20240227,PRIMARYIND,-1,-1;Kroneradi "Kroneradi" true true false 4 Float 0 0,First,#,Trees_ideal_2_20240227,Kroneradi,-1,-1" #</Process></lineage><coordRef><type Sync="TRUE">Projected</type><geogcsn Sync="TRUE">LL-ETRF89</geogcsn><csUnits Sync="TRUE">Linear Unit: Meter (1.000000)</csUnits><projcsn Sync="TRUE">ETRS89.UTM-32N</projcsn><peXml Sync="TRUE">&lt;ProjectedCoordinateSystem xsi:type='typens:ProjectedCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/3.2.0'&gt;&lt;WKT&gt;PROJCS[&amp;quot;ETRS89.UTM-32N&amp;quot;,GEOGCS[&amp;quot;LL-ETRF89&amp;quot;,DATUM[&amp;quot;ETRF89&amp;quot;,SPHEROID[&amp;quot;GRS1980&amp;quot;,6378137.0,298.257222101]],PRIMEM[&amp;quot;Greenwich&amp;quot;,0.0],UNIT[&amp;quot;Degree&amp;quot;,0.0174532925199433]],PROJECTION[&amp;quot;Transverse_Mercator&amp;quot;],PARAMETER[&amp;quot;false_easting&amp;quot;,500000.0],PARAMETER[&amp;quot;false_northing&amp;quot;,0.0],PARAMETER[&amp;quot;central_meridian&amp;quot;,9.0],PARAMETER[&amp;quot;scale_factor&amp;quot;,0.9996],PARAMETER[&amp;quot;latitude_of_origin&amp;quot;,0.0],UNIT[&amp;quot;Meter&amp;quot;,1.0]]&lt;/WKT&gt;&lt;XOrigin&gt;-5120900&lt;/XOrigin&gt;&lt;YOrigin&gt;-9998100&lt;/YOrigin&gt;&lt;XYScale&gt;450445547.3910538&lt;/XYScale&gt;&lt;ZOrigin&gt;-100000&lt;/ZOrigin&gt;&lt;ZScale&gt;10000&lt;/ZScale&gt;&lt;MOrigin&gt;-100000&lt;/MOrigin&gt;&lt;MScale&gt;10000&lt;/MScale&gt;&lt;XYTolerance&gt;0.001&lt;/XYTolerance&gt;&lt;ZTolerance&gt;0.001&lt;/ZTolerance&gt;&lt;MTolerance&gt;0.001&lt;/MTolerance&gt;&lt;HighPrecision&gt;true&lt;/HighPrecision&gt;&lt;/ProjectedCoordinateSystem&gt;</peXml></coordRef></DataProperties><SyncDate>20240227</SyncDate><SyncTime>14541600</SyncTime><ModDate>20240227</ModDate><ModTime>14541600</ModTime></Esri><dataIdInfo><envirDesc Sync="TRUE">Microsoft Windows 10 Version 10.0 (Build 22631) ; Esri ArcGIS 13.2.0.49743</envirDesc><dataLang><languageCode value="deu" Sync="TRUE"></languageCode><countryCode value="DEU" Sync="TRUE"></countryCode></dataLang><idCitation><resTitle Sync="TRUE">Trees_ideal_2_20240227</resTitle><presForm><PresFormCd value="005" Sync="TRUE"></PresFormCd></presForm></idCitation><spatRpType><SpatRepTypCd value="001" Sync="TRUE"></SpatRepTypCd></spatRpType></dataIdInfo><mdLang><languageCode value="deu" Sync="TRUE"></languageCode><countryCode value="DEU" Sync="TRUE"></countryCode></mdLang><mdChar><CharSetCd value="004" Sync="TRUE"></CharSetCd></mdChar><distInfo><distFormat><formatName Sync="TRUE">Shapefile</formatName></distFormat><distTranOps><transSize Sync="TRUE">0.000</transSize></distTranOps></distInfo><mdHrLv><ScopeCd value="005" Sync="TRUE"></ScopeCd></mdHrLv><mdHrLvName Sync="TRUE">dataset</mdHrLvName><refSysInfo><RefSystem><refSysID><identCode code="0" Sync="TRUE"></identCode></refSysID></RefSystem></refSysInfo><spatRepInfo><VectSpatRep><geometObjs Name="Trees_ideal_2_20240227"><geoObjTyp><GeoObjTypCd value="004" Sync="TRUE"></GeoObjTypCd></geoObjTyp><geoObjCnt Sync="TRUE">0</geoObjCnt></geometObjs><topLvl><TopoLevCd value="001" Sync="TRUE"></TopoLevCd></topLvl></VectSpatRep></spatRepInfo><spdoinfo><ptvctinf><esriterm Name="Trees_ideal_2_20240227"><efeatyp Sync="TRUE">Simple</efeatyp><efeageom code="1" Sync="TRUE"></efeageom><esritopo Sync="TRUE">FALSE</esritopo><efeacnt Sync="TRUE">0</efeacnt><spindex Sync="TRUE">FALSE</spindex><linrefer Sync="TRUE">TRUE</linrefer></esriterm></ptvctinf></spdoinfo><eainfo><detailed Name="Trees_ideal_2_20240227"><enttyp><enttypl Sync="TRUE">Trees_ideal_2_20240227</enttypl><enttypt Sync="TRUE">Feature Class</enttypt><enttypc Sync="TRUE">0</enttypc></enttyp><attr><attrlabl Sync="TRUE">FID</attrlabl><attalias Sync="TRUE">FID</attalias><attrtype Sync="TRUE">OID</attrtype><attwidth Sync="TRUE">4</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale><attrdef Sync="TRUE">Internal feature number.</attrdef><attrdefs Sync="TRUE">Esri</attrdefs><attrdomv><udom Sync="TRUE">Sequential unique whole numbers that are automatically generated.</udom></attrdomv></attr><attr><attrlabl Sync="TRUE">Shape</attrlabl><attalias Sync="TRUE">Shape</attalias><attrtype Sync="TRUE">Geometry</attrtype><attwidth Sync="TRUE">0</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale><attrdef Sync="TRUE">Feature geometry.</attrdef><attrdefs Sync="TRUE">Esri</attrdefs><attrdomv><udom Sync="TRUE">Coordinates defining the features.</udom></attrdomv></attr><attr><attrlabl Sync="TRUE">Ident</attrlabl><attalias Sync="TRUE">Ident</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">8</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Bezeichnun</attrlabl><attalias Sync="TRUE">Bezeichnun</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">32</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Baumart</attrlabl><attalias Sync="TRUE">Baumart</attalias><attrtype Sync="TRUE">String</attrtype><attwidth Sync="TRUE">31</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Alter_am_S</attrlabl><attalias Sync="TRUE">Alter_am_S</attalias><attrtype Sync="TRUE">Integer</attrtype><attwidth Sync="TRUE">10</attwidth><atprecis Sync="TRUE">10</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Baumhöhe</attrlabl><attalias Sync="TRUE">Baumhöhe</attalias><attrtype Sync="TRUE">Integer</attrtype><attwidth Sync="TRUE">10</attwidth><atprecis Sync="TRUE">10</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Kronenbrei</attrlabl><attalias Sync="TRUE">Kronenbrei</attalias><attrtype Sync="TRUE">Single</attrtype><attwidth Sync="TRUE">13</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Stammumfan</attrlabl><attalias Sync="TRUE">Stammumfan</attalias><attrtype Sync="TRUE">Integer</attrtype><attwidth Sync="TRUE">10</attwidth><atprecis Sync="TRUE">10</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">PRIMARYIND</attrlabl><attalias Sync="TRUE">PRIMARYIND</attalias><attrtype Sync="TRUE">Double</attrtype><attwidth Sync="TRUE">19</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr><attr><attrlabl Sync="TRUE">Kroneradi</attrlabl><attalias Sync="TRUE">Kroneradi</attalias><attrtype Sync="TRUE">Single</attrtype><attwidth Sync="TRUE">13</attwidth><atprecis Sync="TRUE">0</atprecis><attscale Sync="TRUE">0</attscale></attr></detailed></eainfo><mdDateSt Sync="TRUE">20240227</mdDateSt></metadata>
"""
Script to automatically add potential trees in a given region, along roads and paths.
Road information is downloaded from OpenStreetMap.
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
import folium
import kdtree
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
import numpy as np
import overpy
from pyproj import Transformer
from shapely import LineString, geometry, wkt
from shapely.ops import transform
from import_existing_trees import get_existing_trees
# TODO: Use Args
# TODO: Document
# TODO: Write issue
# TODO: Write tests?
# TODO: Export shapefile?
# 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))"
EXISTING_TREES = 'Trees_ideal_2_20240227/Trees_ideal_2_20240227.shp'
EXISTING_TREES = None
# WKT = "POLYGON((9.170419 48.782366, 9.170032 48.780825, 9.169904 48.780401, 9.170440 48.778733, 9.176877 48.780118, 9.177006 48.781193, 9.177049 48.782564, 9.176298 48.782593, 9.175440 48.782409, 9.174646 48.783399, 9.170419 48.782366))"
# Fellbach
# WKT = "POLYGON((9.271353 48.811327, 9.271911 48.809010, 9.272147 48.807187, 9.275838 48.807173, 9.275602 48.806749, 9.276138 48.806325, 9.277683 48.806424, 9.277319 48.812514, 9.275581 48.811991, 9.271353 48.811327))"
EPSG_ID = 25832
# Trees will be planted every TREE_DISTANCE along roads:
TREE_DISTANCE = 10 # [m]
# Unless there's already another tree closer than MIN_DISTANCE away:
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'])
SCRIPT_DIR = Path(__file__).resolve().parent
def load_region(wkt_polygon):
region = wkt.loads(wkt_polygon)
bounds = namedtuple("Bounds", "W S E N")(*region.bounds)
return region, bounds
def get_basename(bounds):
return f'{bounds.S}__{bounds.N}__{bounds.W}__{bounds.E}_{TREE_DISTANCE}m'.replace('.', '_')
def get_osm_roads(bounds):
cache_dir = SCRIPT_DIR / 'cache'
cache_dir.mkdir(exist_ok=True)
cache_file = (cache_dir / get_basename(bounds)).with_suffix('.pickle')
if cache_file.exists():
print("Cache has been found. Parsing...")
with open(cache_file, 'rb') as cache:
ways = pickle.load(cache)
else:
print("Downloading data...")
# TODO: Could add trees from OSM or Bäumekataster too.
api = overpy.Overpass()
result = api.query(f"""
way({bounds.S},{bounds.W},{bounds.N},{bounds.E}) ["highway"];
(._;>;);
out body;
""")
ways = result.ways
print("Caching data...")
with open(cache_file, 'wb') as cache:
pickle.dump(ways, cache)
return ways
def set_plot(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()
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
ax.set_aspect(1)
x_grid = plticker.MultipleLocator(base=GRID)
y_grid = plticker.MultipleLocator(base=GRID)
ax.xaxis.set_major_locator(x_grid)
ax.yaxis.set_major_locator(y_grid)
return ax
def place_trees(existing_trees_coords, ways, region, to_local, tree_distance, min_distance_2):
local_region = transform(to_local.transform, region)
existing_trees = kdtree.create(existing_trees_coords)
tree_xs = []
tree_ys = []
for x, y in existing_trees_coords:
tree_xs.append(x)
tree_ys.append(y)
for way in ways:
width = float(way.tags.get("width", 0))
highway = way.tags.get("highway")
if highway in IGNORE_ROADS:
color = 'orange'
else:
color = 'gray'
local_xy_s = [to_local.transform(node.lon, node.lat)
for node in way.nodes]
tree_path = LineString(local_xy_s)
if width:
road_as_polygon = tree_path.buffer(width / 2, cap_style='flat')
tree_path = road_as_polygon.exterior
displayed_width = width
else:
# NOTE: Could try to guess width depending on highway type.
displayed_width = 1
xs, ys = zip(*local_xy_s)
plt.plot(xs, ys, linewidth=displayed_width,
c=color, alpha=0.8, zorder=-1)
distances = np.arange(0, tree_path.length, tree_distance)
potential_trees = [tree_path.interpolate(
distance) for distance in distances]
if tree_path.boundary:
potential_trees += [tree_path.boundary.geoms[-1]]
for potential_tree in potential_trees:
x = potential_tree.x
y = potential_tree.y
if local_region.contains(geometry.Point(x, y)):
_nearest_tree, distance_2 = existing_trees.search_nn((x, y))
if distance_2 > min_distance_2:
existing_trees.add((x, y))
tree_xs.append(x)
tree_ys.append(y)
return tree_xs, tree_ys
def plot_trees(bounds, tree_xs, tree_ys, tree_distance):
plt.scatter(tree_xs, tree_ys, s=2, c='green')
plt.grid(True)
plt.title(f"{bounds}\nTree distance : {tree_distance} m")
plt.gcf().set_size_inches(15, 10)
plt.savefig(
SCRIPT_DIR / f"{get_basename(bounds)}.png", bbox_inches='tight', dpi=300)
def export_map(bounds, tree_xs, tree_ys, epsg_id):
to_wgs84 = Transformer.from_crs(f"EPSG:{epsg_id}", "EPSG:4326", always_xy=True)
interactive_map = folium.Map()
interactive_map.fit_bounds([(bounds.S, bounds.W), (bounds.N, bounds.E)])
radius = 2 # [m]
for x, y in zip(tree_xs, tree_ys):
lon, lat = to_wgs84.transform(x, y)
folium.Circle(
location=[lat, lon],
radius=radius,
color="black",
weight=1,
fill_opacity=0.9,
opacity=1,
fill_color="#00ff15",
fill=False, # gets overridden by fill_color
popup="{} meters".format(radius),
tooltip="I am a tree in street STREET",
).add_to(interactive_map)
folium.TileLayer(
tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
attr='Esri',
name='Esri Satellite',
overlay=False,
control=True
).add_to(interactive_map)
interactive_map.save(f"{get_basename(bounds)}_trees.html")
def export_csv(bounds, tree_xs, tree_ys, wkt_polygon, tree_distance, min_distance, epsg_id):
with open(SCRIPT_DIR / f"{get_basename(bounds)}_trees.csv", "w") as csv:
csv.write(f"# Fake trees for; {wkt_polygon}\n")
csv.write(f"# Tree distance along roads; {tree_distance}; [m]\n")
csv.write(f"# Minimum allowed distance between trees; {min_distance}; [m]\n")
csv.write(f"# EPSG; {epsg_id}\n")
csv.write("# X; Y\n")
csv.write("# [m]; [m]\n")
for x, y in zip(tree_xs, tree_ys):
csv.write(f"{x};{y}\n")
print("DONE!")
def main(wkt_polygon, epsg_id, tree_distance, min_distance, import_tree_shp):
region, bounds = load_region(wkt_polygon)
ways = get_osm_roads(bounds)
if import_tree_shp:
existing_trees = get_existing_trees(import_tree_shp)
else:
existing_trees = []
to_local = Transformer.from_crs("EPSG:4326", f"EPSG:{epsg_id}", always_xy=True)
set_plot(bounds, to_local)
tree_xs, tree_ys = place_trees(existing_trees, ways, region,
to_local, tree_distance, min_distance**2)
plot_trees(bounds, tree_xs, tree_ys, tree_distance)
export_map(bounds, tree_xs, tree_ys, epsg_id)
export_csv(bounds, tree_xs, tree_ys, wkt_polygon,
tree_distance, min_distance, epsg_id)
if __name__ == "__main__":
main(WKT, EPSG_ID, TREE_DISTANCE, MIN_DISTANCE, EXISTING_TREES)
import geopandas as gpd
def get_existing_trees(shp_input):
df = gpd.read_file(shp_input)
coords = [(p.x, p.y) for p in df.geometry]
return coords
folium==0.15.1
kdtree==0.16
matplotlib==3.7.2
numpy==1.24.3
overpy==0.7
pyproj==3.6.1
Shapely==2.0.2
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