First complete backend

This commit is contained in:
Chandler Swift 2025-02-11 00:14:58 -06:00
parent 754a0c425c
commit ee147fea3c
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F
18 changed files with 949 additions and 178 deletions

View file

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

View file

@ -1,7 +1,16 @@
#!/usr/bin/env nix-shell
#! nix-shell -i bash --pure
#! nix-shell -p bash curl cacert
#! nix-shell -p bash curl cacert jq
# 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
# curl -s https://overpass-api.de/api/interpreter --data-urlencode 'data=[out:json];area[name="Northern Arizona University"]->.searchArea;(way["highway"="footway"](area.searchArea);way["highway"="cycleway"](area.searchArea););out geom;' -o data/sidewalks.json
curl -s "https://overpass-api.de/api/interpreter" --data '
[out:json];
area[name="Northern Arizona University"]->.searchArea;
(
way["highway"="footway"](area.searchArea);
way["highway"="cycleway"](area.searchArea);
);
out geom;' \
| jq '[ .elements[] | { OSMID: .id, Geometry: [ .geometry[] | [ .lat, .lon ] ], Tags: .tags } ]' > data/sidewalks.json

View file

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