Mapping Air Quality

· wordCount · 1 minute read

Leaflet For R uses the JavaScript mapping library Leaflet to let turn data straight into nifty maps. We can use this to turn query results from the UK Air Quality API into maps of ozone levels from monitoring sites. All the data comes ultimately from the DEFRA Air Information Resource.

I describe the process in detail with R code here but here is a brief outline.

The API provides several endpoints, two of which we’ll use here:

Nearest Site 🔗

URL: https://uk-air-quality.p.rapidapi.com/nearestsite

This returns the nearest DEFRA air quality monitoring site given a latitude and longitude.

Latest Pollutants 🔗

URL: https://uk-air-quality.p.rapidapi.com/latestpollutants

This returns the latest measurements for all pollutants that the site monitors.

By first finding the nearest monitoring site to a location and then using that site to find the latest pollutants we can get some data like so:

site data

With a bit of R we can combine this with the results for other monitoring sites to produce a new data frame of ozone levels per site:

ozone

We can then plug this data frame straight into Leaflet:

leaflet map


Originally published at https://cosmo-opticon.com on April 20, 2020.