AQI location bound list of sensors limited?

Hi!

I’m trying to pull a list of sensors within a given lat/lon bounding box (Puget Sound, to be specific), and while the code (Python) runs fine and I’m getting a 200 response with data from the server, the list is suspiciously small.

There’s no way that there are only 36 sensors within Puget Sound that have pm2.5 data. Is there any way I can get a complete list of sensor_index for the given lat/lon box? I’ve tested this both in the code below and in the API documentation, and the result is the same (36 stations).

url string of the request: https://api.purpleair.com/v1/sensors?fields=pm2.5&location_type=0&nwlng=48.1162201&nwlat=-122.755863&selng=46.8555182&selat=-121.7631845

code sample:

import requests
import json
import pandas as pd

with open('api_key.txt') as f:
    api = f.read()

headers= {'X-API-Key': api}

# get list of sensors within lat/long bounding box (defined by NW/SE corners)

# Port Townsend City Hall
NWlng = 48.1162201
NWlat = -122.755863

# Peak of Mt. Rainier
SElng = 46.8555182
SElat = -121.7631845

url = 'https://api.purpleair.com/v1/sensors?fields=pm2.5&location_type=0&nwlng=' + str(NWlng) + '&nwlat='+ str(NWlat) + '&selng=' + str(SElng) + '&selat=' + str(SElat)

response = requests.get(url, headers=headers)
print("Status Code: ", response.status_code)

content = json.loads(response.content)
data = content["data"]
columns = content["fields"]

df = pd.DataFrame(data, columns=columns)
print(df.shape)

output:

Status Code:  200
(36, 2)

Additional two points just discovered!

1 - apparently I needed to set max_age=0 and modified_since=0 to not filter out too many (now n=300)
2 - The sensors I’m getting in this list don’t make any sense, as apparently I’m setting the bounding box incorrectly somehow? (This also happens in the API Docs tester).

See screenshot below that has both the lat/lon bounding box and the results. For example: “Downtown New Orleans” is certainly not located in Puget Sound.

Well, I can say it’s not just you. I tried this with your coordinates as well as a smaller area (just a few square miles around my home in Kirkland, WA) with no better results.

If you look at the Purple Air map centered over the Puget Sound area, you’ll see that there are lots of available sensors. (Real-Time Air Quality Map | PurpleAir)

But what I noticed in the results from your call is that the latitude and longitude for all of the stations returned are null. With null coordinate values there’s no way to place them on a map.

It seems like this must be an issue with the API. It should be ignoring all stations with null latitude and/or longitude values, but it’s instead returning only stations with null coordinate values.

For my own use, I picked a bunch of stations near me using the map page (Real-Time Air Quality Map | PurpleAir) to get the sensor_index value. All of the stations I picked (like 34585 and 76973) have valid coordinate values - otherwise they wouldn’t be on the map. (As an aside, I created a group containing all of the sensors I wanted, and then I use the Get Member Data API call to get the data for those stations. This method, though, might not work for you if you’re looking for “all” of the stations in the Puget Sound area rather than picking them manually.)

I’ll leave it to you to contact Purple Air and report the issue with the coordinates search.

Thanks for confirming that I’m not crazy!

In regards to the null values for latitude and longitude, @jeffgo is correct. These are, in fact, sensors that do not have a location set and are thus located in what we refer to as “No Man’s Land,” which you can see down near Antarctica on our map.

The reason your request isn’t returning data for the sensors you want is that your latitude and longitude are backwards. Flip those and you should be good to go!

1 Like

:man_facepalming:

Well, that’s a fantastically dumb mistake. Thanks for the catch and not just pointing and laughing.

1 Like

I now have a big red sore spot on my forehead from slapping it. At least I got the part about the null coordinate values right! :face_with_spiral_eyes:

1 Like

Actually, a very easy mistake to make, considering that we put longitude before latitude in our documentation, which is backwards for most people.