diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..0a722d7 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix; diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fce603 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +data/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd4f6c1 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +.PHONY: all +all: data/sidewalks.json + +data/raw.osm.pbf: data scripts/fetch_osm_data.sh + ./scripts/fetch_osm_data.sh + +data/sidewalks.unmin.json: data/raw.osm.pbf ./scripts/parse_sidewalks.py + ./scripts/parse_sidewalks.py data/raw.osm.pbf > data/sidewalks.unmin.json + +# data/sidewalks.json: data/sidewalks.unmin.json +# data/sidewalks.json + +data/sidewalks.json: data scripts/fetch_sidewalk_data.sh + ./scripts/fetch_sidewalk_data.sh + +data: + mkdir -p data diff --git a/scripts/fetch_osm_data.sh b/scripts/fetch_osm_data.sh new file mode 100755 index 0000000..d2adff3 --- /dev/null +++ b/scripts/fetch_osm_data.sh @@ -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 diff --git a/scripts/fetch_sidewalk_data.sh b/scripts/fetch_sidewalk_data.sh new file mode 100755 index 0000000..8dfe4bd --- /dev/null +++ b/scripts/fetch_sidewalk_data.sh @@ -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 diff --git a/scripts/parse_sidewalks.py b/scripts/parse_sidewalks.py new file mode 100755 index 0000000..021d81c --- /dev/null +++ b/scripts/parse_sidewalks.py @@ -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 \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)) diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..59172a8 --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +let + pkgs = import {}; +in +pkgs.mkShellNoCC { + packages = with pkgs; [ + gnumake + ]; +} diff --git a/test.html b/test.html new file mode 100644 index 0000000..6130ae3 --- /dev/null +++ b/test.html @@ -0,0 +1,82 @@ + + + + + + Leaflet OSM Ways + + + + + +
+ + + + + +