Skip to content

Instantly share code, notes, and snippets.

@russbiggs
Created February 16, 2022 15:48
Show Gist options
  • Save russbiggs/b0bf36e0252f7f7aa8146b41db35b88f to your computer and use it in GitHub Desktop.
Save russbiggs/b0bf36e0252f7f7aa8146b41db35b88f to your computer and use it in GitHub Desktop.
Create shapefile from OpenAQ API V2 locations endpoint
from urllib import request
import requests
import fiona
from fiona.crs import from_epsg
def result_to_dict(result):
geom = {
"type": "Point",
"coordinates": [
float(result["coordinates"]["latitude"]),
float(result["coordinates"]["longitude"]),
],
}
return {"type": "Feature", "geometry": geom, "properties": {"Name": result["name"]}}
def main():
url = "https://api.openaq.org/v2/locations?limit=5000&page=1&offset=0&sort=desc&coordinates=34.0522%2C-118.2437&radius=40000&order_by=lastUpdated&dumpRaw=false"
res = requests.get(url)
data = res.json()
records = [result_to_dict(result) for result in data["results"]]
schema = {"geometry": "Point", "properties": {"Name": "str:48"}}
with fiona.open(
"output.shp", "w", crs=from_epsg(4326), driver="ESRI Shapefile", schema=schema
) as output:
output.writerecords(records)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment