-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_osmium.py
40 lines (33 loc) · 1.21 KB
/
test_osmium.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
"""
Compute the total length of highways in an osm file.
Shows how to extract the geometry of a way.
"""
import osmium as o
import sys
class RoadLengthHandler(o.SimpleHandler):
def __init__(self):
super(RoadLengthHandler, self).__init__()
self.length = 0.0
def way(self, w):
print(w)
if 'highway' in w.tags:
try:
added_length = o.geom.haversine_distance(w.nodes)
self.length += added_length
print('added_length: %.2f km' % (added_length/1000))
except o.InvalidLocationError:
# A location error might occur if the osm file is an extract
# where nodes of ways near the boundary are missing.
print("WARNING: way %d incomplete. Ignoring." % w.id)
def main(osmfile):
h = RoadLengthHandler()
# As we need the geometry, the node locations need to be cached. Therefore
# set 'locations' to true.
h.apply_file(osmfile, locations=True)
print('Total way length: %.2f km' % (h.length/1000))
return 0
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python %s <osmfile>" % sys.argv[0])
sys.exit(-1)
exit(main(sys.argv[1]))