-
Notifications
You must be signed in to change notification settings - Fork 0
/
greenness.py
229 lines (208 loc) · 7.97 KB
/
greenness.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Calculates green index from OSM and Sentinal-2 data to generate green routes with openrouteservice"""
__author__ = "Christina Ludwig, GIScience Research Group, Heidelberg University"
__email__ = "christina.ludwig@uni-heidelberg.de"
import os
import sys
import argparse
from shutil import copyfile
from modules.download import download_features
from modules.green_osm import greenness_of_osm_tags, green_from_osm
from modules.green_ndvi import green_from_ndvi
from modules.lu_polygons import generate_landuse_polygons
from modules.ndvi import ndvi
from modules.fusion import fuse
from modules.trees import integrate_trees
from modules.utils import (
create_subfolder,
check_config,
load_config,
init_logger,
rasterize,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculates the greenness based on OSM and Sentinel-2 data."
)
parser.add_argument(
"--config",
"-c",
required=True,
dest="config_file",
type=str,
help="Path to configuration file (.json) describing the area of interest (aoi)",
)
parser.add_argument(
"--google_cred",
"-g",
required=True,
dest="google_cred_file",
type=str,
help="Path to json file containing credentials for using google earth engine",
)
args = parser.parse_args()
# Set modules to be executed to True
run_download_traffic = True
run_download_landuse = True
run_download_buildings = True
run_download_trees = True
run_landuse_polygons = True
run_ndvi = True
run_green_osm_tags = True
run_green_osm = True
run_green_ndvi = True
run_fusion = True
run_integrate_trees = True
run_rasterization = True
# Load configuration parameters
config = load_config(args.config_file)
check_config(config)
# Create output folders
aoi_name = config["name"]
out_dir_aoi = os.path.join(config["output_dir"], aoi_name)
os.makedirs(out_dir_aoi, exist_ok=True)
# Copy config file to output folder
copyfile(
args.config_file, os.path.join(out_dir_aoi, os.path.basename(args.config_file))
)
# Set up logger
log_file = os.path.join(out_dir_aoi, "log.log")
logger = init_logger("calculate green", log_file)
# Load configuration file
config_tags = load_config("./config/config_tags.json")
# Start running processing modules
if run_download_landuse:
if os.path.exists(os.path.join(out_dir_aoi, "osm/landuse")):
logger.info(
"Folder for land use features already exists. Download is skipped. Delete this folder if "
"you want to download the data again."
)
else:
logger.info("Downloading land use features...")
try:
landuse_feature_dir = create_subfolder(out_dir_aoi, "osm/landuse")
download_features(
bbox=config["bbox"],
timestamp=config["timestamp"],
layers=config_tags["landuse"],
outdir=landuse_feature_dir,
)
except Exception:
logger.exception("Error during download of landuse features:")
sys.exit(1)
if run_download_traffic:
if os.path.exists(os.path.join(out_dir_aoi, "osm/traffic")):
logger.info(
"Folder for traffic features already exists. Download is skipped. Delete this folder if "
"you want to download the data again."
)
else:
logger.info("Downloading traffic features...")
try:
traffic_feature_dir = create_subfolder(out_dir_aoi, "osm/traffic")
download_features(
bbox=config["bbox"],
timestamp=config["timestamp"],
layers=config_tags["traffic"],
outdir=traffic_feature_dir,
)
except Exception:
logger.exception("Error during download of traffic features:")
sys.exit(1)
if run_download_buildings:
if os.path.exists(os.path.join(out_dir_aoi, "osm/buildings")):
logger.info(
"Folder for building features already exists. Download is skipped. Delete this folder if "
"you want to download the data again."
)
else:
logger.info("Downloading building features...")
try:
building_feature_dir = create_subfolder(out_dir_aoi, "osm/buildings")
download_features(
bbox=config["bbox"],
timestamp=config["timestamp"],
layers=config_tags["buildings"],
outdir=building_feature_dir,
)
except Exception:
logger.exception("Error during download of buildings:")
sys.exit(1)
if run_download_trees:
if os.path.exists(os.path.join(out_dir_aoi, "osm/trees")):
logger.info(
"Folder for tree features already exists. Download is skipped. Delete this folder if "
"you want to download the data again."
)
else:
logger.info("Downloading tree features...")
try:
trees_dir = create_subfolder(out_dir_aoi, "osm/trees")
download_features(
bbox=config["bbox"],
timestamp=config["timestamp"],
layers=config_tags["trees"],
outdir=trees_dir,
)
except Exception:
logger.exception("Error during download of trees:")
sys.exit(1)
if run_landuse_polygons:
logger.info("Generating land use polygons ...")
try:
generate_landuse_polygons(config)
except Exception:
logger.exception("Error during generation of land use polygons:")
sys.exit(1)
if run_ndvi:
logger.info("Calculating NDVI ...")
try:
credentials = load_config(args.google_cred_file)
ndvi(config, credentials)
except Exception:
logger.exception("Error during generation of NDVI calculation:")
sys.exit(1)
if run_green_osm_tags:
logger.info("Calculating greenness of OSM tags...")
try:
greenness_of_osm_tags(config)
except Exception:
logger.exception("Error while calculating greenness of OSM tags:")
sys.exit(1)
if run_green_osm:
logger.info("Calculating greenness of polygons based on OSM data...")
try:
green_from_osm(config)
except Exception:
logger.exception("Error during greenness calculation from OSM data:")
sys.exit(1)
if run_green_ndvi:
logger.info("Calculating greenness of polygons based on NDVI...")
try:
green_from_ndvi(config)
except Exception:
logger.exception("Error during greenness calculation from NDVI data:")
sys.exit(3)
if run_fusion:
logger.info("Fusing greenness from OSM and NDVI beliefs...")
try:
class_ndvi_osm_geo = fuse(config)
except Exception:
logger.exception("Error during greenness fusion:")
sys.exit(3)
if run_integrate_trees:
logger.info("Integrating trees in greenness map...")
try:
class_ndvi_osm_geo = integrate_trees(config)
except Exception:
logger.exception("Error during integration of trees:")
sys.exit(3)
if run_rasterization:
logger.info("Rasterizing greenness map...")
try:
lu_polygons_file = os.path.join(out_dir_aoi, f"{aoi_name}_greenness.shp")
rasterize(lu_polygons_file, column="green")
except Exception:
logger.exception("Error during green index calculation:")
sys.exit(3)