-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery-peaks-from-osm.py
executable file
·105 lines (71 loc) · 1.99 KB
/
query-peaks-from-osm.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
#!/usr/bin/python3
r'''Query OSM data for peak annotation
SYNOPSIS
$ ./query-peaks-from-osm.py 34. -118 100000 > socal-peaks.h
# generates peak data from lat,lon 34,-118 in a radius of 100000m
'''
import sys
import argparse
import re
import os
def parse_args():
parser = \
argparse.ArgumentParser(description = __doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('lat',
type=float,
help='''Latitude''')
parser.add_argument('lon',
type=float,
help='''Longitude''')
parser.add_argument('radius',
type=float,
help='''Radius, in m''')
args = parser.parse_args()
return args
args = parse_args()
import requests
import json
api = 'http://overpass-api.de/api/interpreter'
query = f'''
[out:json];
node
["natural" = "peak" ]
(around: {args.radius},{args.lat},{args.lon});
(._;>;);
out;
'''
r = requests.post(api,
data = query)
if r.status_code != 200:
print(f"Overpass api error: {r.status_code} {r.reason=}",
file = sys.stderr)
sys.exit(1)
l = json.loads(r.text)
# with open("peaks.json") as f:
# l = json.load(f)
def name_from_element(t,
*,
ele):
keys = ('name:en', 'name', 'name:th')
for k in keys:
if k in t:
return t[k]
# No name available. Use the elevation
return f"{ele}m"
try:
E = l['elements']
except:
print("Expected 'elements' in the json output", sys.stderr)
sys.exit(1);
for e in E:
try:
t = e['tags']
except:
print("Expected 'tags' for each element", sys.stderr)
sys.exit(1);
try: ele = float(t['ele'])
except: continue
name = name_from_element(t, ele = ele)
if name is None: continue
print(f'{{ "{name}", {e["lat"]}, {e["lon"]}, {ele} }},')