#!/usr/bin/env nix-shell #! nix-shell -i python3 -p python3 python3Packages.requests python3Packages.shapely import requests import json from collections import defaultdict from shapely.geometry import shape, mapping from shapely.ops import unary_union # https://services.arcgis.com/9OIuDHbyhmH91RfZ/arcgis/rest/services/EMSRB_Statewide/FeatureServer/0/query?f=pbf&cacheHint=true&maxRecordCountFactor=4&resultOffset=0&resultRecordCount=8000&where=1%3D1&orderByFields=OBJECTID%20ASC&outFields=AmbServNam%2CCounty%2COBJECTID&outSR=102100&spatialRel=esriSpatialRelIntersects url = "https://services.arcgis.com/9OIuDHbyhmH91RfZ/arcgis/rest/services/EMSRB_Statewide/FeatureServer/0/query" params = { "f": "geojson", "cacheHint": "true", "maxRecordCountFactor": "4", "resultOffset": "0", "resultRecordCount": "8000", "where": "1=1", "outFields": "AmbServNam,County", "outSR": "102100", "spatialRel": "esriSpatialRelIntersects", } response = requests.get(url, params=params) response.raise_for_status() data = json.loads(response.text) services = defaultdict(list) for feature in data["features"]: service_name = feature["properties"].get("AmbServNam", "Unknown") services[service_name].append(feature) merged_features = [] for service_name, features in services.items(): merged_features.append( { "type": "Feature", "properties": { "AmbServNam": service_name, }, "geometry": mapping(unary_union([shape(feature["geometry"]) for feature in features])), } ) merged_data = { "type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:3857"}}, "features": merged_features, } with open("mn-ambulance-service-areas.geojson", "w", encoding="utf-8") as f: json.dump(merged_data, f)