Edit this page

< back to recipes

The Office for National Statistics’ Open Geography Portal provide a range of geospatial datasets under the Open Government Licence 3.0. Digital vector boundaries for different administrative and statistical geographies are available to freely download. Here we’ll download a single local authority boundary file.

Ingredients

R packages

Population pyramid

Instructions

Method 1: download all boundaries and filter in R


  1. Load the necessary R packages.
library(tidyverse) ; library(sf)
  1. Visit the ONS Open Geography Portal and select your local authority boundary: Boundaries > Administrative Boundaries > Local Authority Districts. For example, choose Local Authority Districts (December 2018) Boundaries UK BGC from the list.

  2. Select the ‘APIs’ button on the right hand side and copy the URL path under GeoJSON.

  3. Paste the URL path into the st_read() function and retrieve the data.

sf <- st_read("https://opendata.arcgis.com/datasets/2c5b8eb836c7475ba3b305106ac9dfc3_0.geojson")
  1. Filter by your chosen local authority
sf <- filter(sf, lad18nm == "Trafford")
  1. Check the coordinate reference system
st_crs(bdy)
  1. Plot the results
plot(st_geometry(sf))
  1. Save the boundary as a GeoJSON
st_write(sf, "local_authority_boundary.geojson")


Method 2: filter using the API


  1. Load the necessary R packages.
library(tidyverse) ; library(sf)
  1. Visit the ONS Open Geography Portal and select your local authority boundary: Boundaries > Administrative Boundaries > Local Authority Districts. For example, choose Local Authority Districts (December 2018) Boundaries UK BGC from the list.

  2. Select the ‘API Explorer’ button at the top of the page.

  3. Navigate to the ‘Query’ box and click the + next to the ‘Where’ parameter. Select ‘lad18nm | Text’ from the ‘Attributes’ pick list and enter your local authority name in the box. Press Enter on your keyboard.

  4. Highlight and copy the URL that appears in the ‘Query URL’ box.

  5. Paste the URL path into the st_read() function and retrieve the data. Note that we’ve added on ‘geojson’.

sf <- st_read("https://ons-inspire.esriuk.com/arcgis/rest/services/Administrative_Boundaries/Local_Authority_Districts_December_2018_Boundaries_UK_BGC/MapServer/0/query?where=lad18nm%20=%20%27Trafford%27&outFields=lad18cd,lad18nm,long,lat&outSR=4326&f=geojson")

Notes

Further information can be found in our Medium article: “Pushing the boundaries with the Open Geography Portal API”.