forked from osmcode/pyosmium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamenity_list.py
More file actions
47 lines (31 loc) · 1.16 KB
/
Copy pathamenity_list.py
File metadata and controls
47 lines (31 loc) · 1.16 KB
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
"""
Extract objects with an amenity tag from an osm file and list them
with their name and position.
Points and areas will be processed, unclosed ways will be skipped.
This example shows how geometries from osmium objects can be imported
into shapely using the WKBFactory.
"""
import osmium
import sys
import shapely.wkb as wkblib
wkbfab = osmium.geom.WKBFactory()
class AmenityListHandler(osmium.SimpleHandler):
def print_amenity(self, tags, lon, lat):
name = tags.get('name', '')
print("%f %f %-15s %s" % (lon, lat, tags['amenity'], name))
def node(self, n):
self.print_amenity(n.tags, n.location.lon, n.location.lat)
def area(self, a):
wkb = wkbfab.create_multipolygon(a)
poly = wkblib.loads(wkb, hex=True)
centroid = poly.representative_point()
self.print_amenity(a.tags, centroid.x, centroid.y)
def main(osmfile):
handler = AmenityListHandler()
handler.apply_file(osmfile, filters=[osmium.filter.KeyFilter('amenity')])
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]))