-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
52 lines (41 loc) · 1.35 KB
/
utils.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
import subprocess
def checkDependencies():
deps = ["cargo", "tippecanoe", "curl", "ogr2ogr", "osmium", "gunzip"]
# If you're using Docker, uncomment the line below
# deps = ["curl", "ogr2ogr", "osmium", "gunzip"]
for dep in deps:
if subprocess.run(["which", dep], capture_output=True).returncode != 0:
raise Exception(
f"You're missing {dep}. See https://github.com/Urban-Analytics-Technology-Platform/od2net/blob/main/docs/tutorial_examples.md#setup for installation links. If you're using Docker, go edit utils.py and see the comment there."
)
# Run a command, verifying success
def run(args):
print(">", " ".join(args))
subprocess.run(args, check=True)
def download(url, outputFilename):
run(
[
"curl",
"-L",
url,
"-o",
outputFilename,
]
)
def extractCentroids(osmInput, geojsonOutput, where="building IS NOT NULL"):
run(
[
"ogr2ogr",
"-f",
"GeoJSON",
"-dialect",
"sqlite",
"-sql",
f"SELECT ST_Centroid(geometry) FROM multipolygons WHERE {where}",
geojsonOutput,
osmInput,
]
)
def writeFixedOutputFile(path, contents):
with open(path, "w") as f:
f.write(contents)