add_trees.py 9.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""
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 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
Eric Duminil's avatar
Eric Duminil committed
20
21
import pandas as pd
import geopandas as gpd
22

23
from tree import Forest
Eric Duminil's avatar
Eric Duminil committed
24
from import_existing_trees import get_existing_forest
25
26
27
28
29
30
31
32
33

# 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))"
34
# Replace with None if no existing tree should be imported
35
EXISTING_TREES = 'existing_trees/Trees_ideal_2_20240227.shp'
Eric Duminil's avatar
Eric Duminil committed
36
# EXISTING_TREES = 'existing_trees/baumkataster/Baum.shp'
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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
Eric Duminil's avatar
Eric Duminil committed
52
53
OUTPUT_DIR = SCRIPT_DIR / 'output'
Bounds = namedtuple("Bounds", "W S E N")
54
55
56
57


def load_region(wkt_polygon):
    region = wkt.loads(wkt_polygon)
Eric Duminil's avatar
Eric Duminil committed
58
    bounds = Bounds(*region.bounds)
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    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


Eric Duminil's avatar
Eric Duminil committed
107
def place_trees(forest, ways, region, to_local, tree_distance, min_distance_2) -> Forest:
108
109
110
111
112
113
114
115
116
117
    local_region = transform(to_local.transform, region)

    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'

Eric Duminil's avatar
Eric Duminil committed
118
        road_xy_s = [to_local.transform(node.lon, node.lat) for node in way.nodes]
119

Eric Duminil's avatar
Eric Duminil committed
120
        tree_path = LineString(road_xy_s)
121
122
123
124
125
126
127
128
129

        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

Eric Duminil's avatar
Eric Duminil committed
130
131
        road_xs, road_ys = zip(*road_xy_s)
        plt.plot(road_xs, road_ys, linewidth=displayed_width, c=color, alpha=0.8, zorder=-1)
132
133
134
135
136
137
138
139
140
141
142

        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)):
Eric Duminil's avatar
Eric Duminil committed
143
                forest.add_tree_if_possible(min_distance_2, x, y,
Eric Duminil's avatar
Eric Duminil committed
144
                                            color='#DFFF00',
Eric Duminil's avatar
Eric Duminil committed
145
                                            type='Fake Tree',
Eric Duminil's avatar
Eric Duminil committed
146
                                            description='Tilia tomentosa',
Eric Duminil's avatar
Eric Duminil committed
147
                                            diameter=6,
Eric Duminil's avatar
Eric Duminil committed
148
                                            height=10,
Eric Duminil's avatar
Eric Duminil committed
149
                                            source='add_trees.py'
Eric Duminil's avatar
Eric Duminil committed
150
                                            )
Eric Duminil's avatar
Eric Duminil committed
151

Eric Duminil's avatar
Eric Duminil committed
152
    return forest
153
154


Eric Duminil's avatar
Eric Duminil committed
155
def plot_trees(bounds: Bounds, forest: Forest, tree_distance: float) -> None:
Eric Duminil's avatar
Eric Duminil committed
156
    print("Exporting diagram...")
Eric Duminil's avatar
Eric Duminil committed
157
    tree_xs, tree_ys, colors = forest.xs_ys_cs
Eric Duminil's avatar
Eric Duminil committed
158
    plt.scatter(tree_xs, tree_ys, s=2, c=colors)
159
160
161
162
163

    plt.grid(True)
    plt.title(f"{bounds}\nTree distance : {tree_distance} m")
    plt.gcf().set_size_inches(15, 10)
    plt.savefig(
Eric Duminil's avatar
Eric Duminil committed
164
        OUTPUT_DIR / f"{get_basename(bounds)}.png", bbox_inches='tight', dpi=300)
Eric Duminil's avatar
Eric Duminil committed
165
    print("  DONE!")
166
167


Eric Duminil's avatar
Eric Duminil committed
168
169
def export_map(bounds, forest, epsg_id) -> None:
    print("Exporting Map...")
170
171
172
173
    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)])

Eric Duminil's avatar
Eric Duminil committed
174
175
    for tree in forest:
        lon, lat = to_wgs84.transform(tree.x, tree.y)
176
177
        folium.Circle(
            location=[lat, lon],
Eric Duminil's avatar
Eric Duminil committed
178
            radius=tree.radius,  # [m]
179
180
181
182
            color="black",
            weight=1,
            fill_opacity=0.9,
            opacity=1,
Eric Duminil's avatar
Eric Duminil committed
183
            fill_color=tree.color,
184
            fill=False,  # gets overridden by fill_color
Eric Duminil's avatar
Eric Duminil committed
185
186
            popup=repr(tree),
            tooltip=str(tree),
187
188
189
190
191
192
193
194
195
196
        ).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)

Eric Duminil's avatar
Eric Duminil committed
197
    interactive_map.save(OUTPUT_DIR / f"{get_basename(bounds)}_trees.html")
Eric Duminil's avatar
Eric Duminil committed
198
    print("  DONE!")
199
200


Eric Duminil's avatar
Eric Duminil committed
201
202
def export_csv(bounds, forest, wkt_polygon, tree_distance, min_distance, epsg_id) -> None:
    print("Exporting CSV...")
Eric Duminil's avatar
Eric Duminil committed
203
    with open(OUTPUT_DIR / f"{get_basename(bounds)}_trees.csv", "w") as csv:
204
205
206
207
        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")
Eric Duminil's avatar
Eric Duminil committed
208
209
        csv.write("# X; Y; Type; Description; Radius; Source\n")
        csv.write("# [m]; [m]; [-]; [-]; [m]; [-]\n")
Eric Duminil's avatar
Eric Duminil committed
210
        for tree in forest:
Eric Duminil's avatar
Eric Duminil committed
211
            csv.write(f"{tree.x};{tree.y};{tree.type};{tree.description};{tree.radius};{tree.source}\n")
212

Eric Duminil's avatar
Eric Duminil committed
213
    print("  DONE!")
214
215


Eric Duminil's avatar
Eric Duminil committed
216
def export_shapefile(bounds: Bounds, forest: Forest, tree_distance: float, epsg_id: str) -> None:
Eric Duminil's avatar
Eric Duminil committed
217
    print("Exporting shapefile")
Eric Duminil's avatar
Eric Duminil committed
218
219
220
221
222

    data = [{
        'x': t.x, 'y': t.y,
        'Bezeichnun': t.description, 'Baumart': t.type,
        'Baumhöhe': t.height, 'Kronenbrei': t.diameter,
Eric Duminil's avatar
Eric Duminil committed
223
        'Stammumfan': t.trunk_diameter, 'Quelle': t.source
Eric Duminil's avatar
Eric Duminil committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        }
        for t in forest]

    df = pd.DataFrame.from_dict(data)
    gdf = gpd.GeoDataFrame(
        df.drop(columns=['x', 'y']),
        geometry=gpd.points_from_xy(df.x, df.y), crs=epsg_id
    )

    print(gdf)

    basename = get_basename(bounds)
    shp_dir = OUTPUT_DIR / basename
    shp_dir.mkdir(exist_ok=True)
Eric Duminil's avatar
Eric Duminil committed
238
    gdf.to_file(shp_dir / f"trees.shp", encoding='UTF-8')
Eric Duminil's avatar
Eric Duminil committed
239

Eric Duminil's avatar
Eric Duminil committed
240
241
242
    print("  DONE!")


Eric Duminil's avatar
Eric Duminil committed
243
def main(wkt_polygon, epsg_id, tree_distance, min_distance, import_tree_shp) -> None:
244
245
246
247
    region, bounds = load_region(wkt_polygon)
    ways = get_osm_roads(bounds)

    if import_tree_shp:
Eric Duminil's avatar
Eric Duminil committed
248
        existing_forest = get_existing_forest(import_tree_shp)
249
    else:
Eric Duminil's avatar
Eric Duminil committed
250
251
252
        existing_forest = Forest()

    print(existing_forest)
253
254
255
256

    to_local = Transformer.from_crs("EPSG:4326", f"EPSG:{epsg_id}", always_xy=True)

    set_plot(bounds, to_local)
Eric Duminil's avatar
Eric Duminil committed
257
258
    forest = place_trees(existing_forest, ways, region, to_local, tree_distance, min_distance**2)
    print(existing_forest)
259

Eric Duminil's avatar
Eric Duminil committed
260
261
    plot_trees(bounds, forest, tree_distance)
    export_map(bounds, forest, epsg_id)
Eric Duminil's avatar
Eric Duminil committed
262
263
    export_csv(bounds, forest, wkt_polygon, tree_distance, min_distance, epsg_id)
    export_shapefile(bounds, forest, tree_distance, epsg_id)
264
265
266
267


if __name__ == "__main__":
    main(WKT, EPSG_ID, TREE_DISTANCE, MIN_DISTANCE, EXISTING_TREES)