-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreen_index.py
110 lines (99 loc) · 3.28 KB
/
green_index.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Calculates index of highways for usage openrouteservice as extended storage"""
__author__ = "Christina Ludwig, GIScience Research Group, Heidelberg University"
__email__ = "christina.ludwig@uni-heidelberg.de"
import os
import sys
import argparse
from modules.download import download_features
from modules.green_index import calc_green_index
from modules.utils import load_config, init_logger
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculates the index of each OSM highway based on provided raster or "
"vector file. The highways are downloaded using the ohsome API."
)
parser.add_argument(
"--bbox",
"-b",
required=True,
dest="bbox",
type=str,
help="Bounding box in geographic coordinates as string without whitespace e.g. 'minx,miny,maxx,maxy')",
)
parser.add_argument(
"--timestamp",
"-t",
required=False,
dest="timestamp",
type=str,
default=None,
help="ISO formatted timestamp for download of highways from OSM.",
)
parser.add_argument(
"--width",
"-w",
required=True,
dest="width",
type=float,
help="Width of the buffer around highway segments in meters",
)
parser.add_argument(
"--vector",
"-v",
required=False,
dest="vector_file",
type=str,
help="Path to vector file containing features counted nearby highways",
)
parser.add_argument(
"--raster",
"-r",
required=False,
dest="raster_file",
type=str,
help="Path to raster file used to calculate mean value within area nearby highway",
)
parser.add_argument(
"--out_dir",
"-o",
required=True,
dest="output_dir",
type=str,
help="Path to output directory.",
)
args = parser.parse_args()
logger = init_logger("calculate index")
output_dir = os.path.join(args.output_dir, "green_index")
os.makedirs(output_dir, exist_ok=True)
if not os.path.exists(os.path.join(output_dir, "highways.geojson")):
logger.info("Downloading highway features...")
config_file_tags = "./config/config_tags.json"
config_tags = load_config(config_file_tags)
try:
download_features(
bbox=args.bbox,
timestamp=args.timestamp,
layers=config_tags["highways"],
outdir=output_dir,
)
except Exception:
logger.exception("Error during download of highways:")
sys.exit(1)
try:
assert (
args.vector_file is not None or args.raster_file is not None
), "Either raster or vector file must be given."
assert not (
(args.vector_file is not None) and (args.raster_file is not None)
), "Either raster or vector file must be given."
except AssertionError as e:
logger.critical(e)
sys.exit(1)
try:
logger.info("Calculating green index...")
calc_green_index(args.width, output_dir, raster_file=args.raster_file)
except Exception:
logger.exception("Error during green index calculation:")
sys.exit(1)