First night hacking

This commit is contained in:
Chandler Swift 2025-02-10 19:46:31 -06:00
parent 73f023d269
commit 754a0c425c
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F
8 changed files with 209 additions and 0 deletions

41
scripts/fetch_osm_data.sh Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash --pure
#! nix-shell -p bash curl cacert jq
if [ $# -eq 2 ]; then
OUTPUT=$1
else
OUTPUT=data/raw.osm.pbf
fi
WAY_DATA=$(curl -s https://overpass-api.de/api/interpreter --data-urlencode 'data=[out:json];way["name"="Northern Arizona University"];out geom;')
# Extract coordinates from the response
COORDINATES=$(echo "$WAY_DATA" | jq '[.elements[] | select(.type == "way") | .geometry[] | [.lon, .lat]]')
echo $COORDINATES
# Ensure we have coordinates
if [ -z "$COORDINATES" ]; then
echo "Error: Could not find coordinates for NAU"
exit 1
fi
# Construct the GeoJSON payload
PAYLOAD=$(jq -n --argjson coords "$COORDINATES" '{
"Name": "none",
"RegionType": "geojson",
"RegionData": {
"type": "Polygon",
"coordinates": [$coords]
}
}')
# Send the request to the Slice API
UUID=$(curl -X POST https://slice.openstreetmap.us/api/ -H "Content-Type: application/json" -d "$PAYLOAD")
while ! [ $(curl https://slice.openstreetmap.us/api/$UUID | jq -r '.Complete') = 'true' ]; do
sleep 1
done
curl https://slice.openstreetmap.us/files/$UUID.osm.pbf -o $OUTPUT

7
scripts/fetch_sidewalk_data.sh Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash --pure
#! nix-shell -p bash curl cacert
# TODO: process out extraneous data
curl -s https://overpass-api.de/api/interpreter --data-urlencode 'data=[out:json];{{geocodeArea:Northern Arizona University}}->.searchArea;(way["highway"="footway"](area.searchArea);way["highway"="cycleway"](area.searchArea););out geom;' -o data/sidewalks.json

52
scripts/parse_sidewalks.py Executable file
View file

@ -0,0 +1,52 @@
#!/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))