Geospatial queries
Country and ISO code filtering provide a straightforward way to filter results
to a country, which is a broad administrative area. For workflows that need
more precision, a city, district, or custom region, geospatial filtering is
often a better fit. The .list() method on the Locations, Parameters and
Providers resources support two approaches to spatial querying:
- Point and Radius
- Bounding Box
Querying within the radius of a point
Section titled “Querying within the radius of a point”To query for locations within the radius of a point, you will need the coordinates in WGS84 format (EPSG:4326) of the center point and the radius (in meters) within which you want to search. The query below is an example of how you can query locations that are within 10,000 meters from Accra’s city center. The API supports a query radius up to 25,000 meters (25 km.).
client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
# Fetch locations within 10,000 meters of Accra's centeraccra_locations_radius = client.locations.list( coordinates=(5.556031, -0.204461), # (Y: latitude, X: longitude) radius=10000, # in meters limit=1000)
# Close the connection when doneclient.close()Querying within a bounding box
Section titled “Querying within a bounding box”To query for locations within a specified bounding box, you will need the WGS84 (EPSG:4326) coordinates of the four points that make up the bounding box in the form minX, minY, maxX, maxY. The example below is an example of how you can query locations from a bounding box that roughly covers the City of Accra, Ghana.
from openaq import OpenAQ
client = OpenAQ(api_key="replace-with-valid-openaq-api-key")
accra_locations_bbox = client.locations.list( bbox=(-0.271464, 5.513141, -0.131388, 5.600960), # minX, minY, maxX, maxY (X: longitude, Y: latitude) limit=1000)
# Close the connection when doneclient.close()