Extracting sensors with API using lat-lon box outputs the same sensors no matter the coordinates

I have been trying to extract sensors with the PurpleAir API using a bounding box, which is allowed. However, when I try using different bounding boxes, the same sensors are returned.

Specifically, I am using the PurpleAir R package written by

cole-brokamp

The following code attempts to extract sensors from Clifton, Ohio and Ithaca, NY and returns the same output (I also tried the Bay Area and got the same output):

if (!require("pacman")) install.packages("pacman")
pacman::p_load(PurpleAir, sf) 

get_sensors_data(st_bbox(
  c("xmin" = -76.52, "ymin" = 42.41,
    "xmax" = -76.47, "ymax" = 42.46),
  crs = 4326) , fields = c("name"), location_type = "outside") 

get_sensors_data(st_bbox(c("xmin" = -84.82030, "ymin" = 39.02153,
                           "xmax" = -84.25633, "ymax" = 39.31206),
                         crs = 4326), 
                 fields = c("name"), location_type = "outside")

I contacted the creator of the package, and he told me that the problem appears to be coming from PurpleAir and not the package. He said he tried querying the same bounding boxes from the API directly and also gets the same output with the same boxes.

1 Like

@asmarcheva, thank you for the detail in your post and including a code snippet.

It looks like your code is sending requests using xmin and xmax as latitude values and ymin and ymax as longitude values. Swapping xmin with ymin and swapping xmax with ymax will likely use the correct bounding box without any other code changes.

I confirmed this by examining the logs for your API account. You can view these logs for your requests by logging in to https://develop.purpleair.com and clicking “Logs” at the top. The table below shows how the parameters were being mixed up for Clifton, Ohio:

Parameter Bounding Box for Clifton Supplied in Your Requests
nwlat 39.31206 -84.8203
nwlng -84.8203 39.02153
selat 39.02153 -84.25633
selng -84.25633 39.31206

Why Both Bounding Boxes Received the Same Set of Sensors

Since latitude and longitude were mixed up, both of the bounding boxes you created were over Antarctica. Sensors registered without a location are placed near Antarctica (more specifically around a latitude of -70), which we refer to as “No Mans Land”.

Requesting a bounding box from the API that goes below a latitude of -70 will result in sensors from No Man’s Land being included in the response. Since these sensors have no location set, requesting the fields latitude and longitude from them will return values of null.

Both mixed-up bounding boxes sent in your requests were below -70, which is why they both returned these sensors.

2 Likes

Ok, it seems like the package I am using may be inputting the coordinates incorrectly to the API. I will check with the pacakge creator then. Thanks for solving the mystery!

1 Like