53 lines
1.7 KiB
Python
Executable file
53 lines
1.7 KiB
Python
Executable file
#!/usr/bin/env nix-shell
|
|
#! nix-shell -i python
|
|
#! nix-shell -p python3 python3Packages.pyosmium python3Packages.requests
|
|
import osmium
|
|
import json
|
|
import sys
|
|
|
|
class SidewalkHandler(osmium.SimpleHandler):
|
|
def __init__(self):
|
|
super(SidewalkHandler, self).__init__()
|
|
self.sidewalks = []
|
|
|
|
def way(self, w):
|
|
# Convert OSM tags to a normal dict for easier use
|
|
tags = dict(w.tags)
|
|
is_sidewalk = False
|
|
|
|
# Check for a dedicated footway sidewalk
|
|
if tags.get('highway') == 'footway':
|
|
is_sidewalk = True
|
|
# # Alternatively, check if the 'sidewalk' tag is present and not set to no/none
|
|
# elif 'sidewalk' in tags and tags['sidewalk'].lower() not in ['no', 'none']:
|
|
# is_sidewalk = True
|
|
|
|
if is_sidewalk:
|
|
sidewalk = {
|
|
"id": w.id,
|
|
"nodes": [],
|
|
"tags": tags
|
|
}
|
|
# Gather node coordinates if available
|
|
for n in w.nodes:
|
|
# Ensure the node location is valid
|
|
if n.location.valid():
|
|
sidewalk["nodes"].append({
|
|
"lat": n.location.lat,
|
|
"lon": n.location.lon
|
|
})
|
|
self.sidewalks.append(sidewalk)
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
sys.stderr.write("Usage: python script.py <path-to-osm-pbf-file>\n")
|
|
sys.exit(1)
|
|
|
|
pbf_file = sys.argv[1]
|
|
handler = SidewalkHandler()
|
|
# The locations=True flag makes sure node coordinates are available.
|
|
handler.apply_file(pbf_file, locations=True)
|
|
|
|
# Output the results as an indented JSON list
|
|
print(json.dumps(handler.sidewalks, indent=2))
|