knitr::opts_chunk$set(echo = TRUE, fig.path = "man/figures/README-") library(dplyr) library(sf)
Install from github using devtools:
library(devtools) install_github('Chrisjb/rgeoportal')
get_boundary(boundary_type = c("administrative", "census", "electoral", "eurostat", "health", "other", "postcodes"), boundary_name = NA, bbox = NA, redius = NA, point = NA, names_like = NA, names_equal = NA, custom_polygon = NA)
We can get the boundary names available within each boundary type by specifying boundary_type
with no boundary_name
option:
library(rgeoportal) census_boundaries <- get_boundary(boundary_type = 'census') head(census_boundaries)
administrative <- get_boundary(boundary_type = 'administrative') head(administrative)
Some of the boundary names come with a range of suffixes:
Note that most boundaries have a maximim record count of 200. Large requests will typically be limited to 200 observations.
We can select one of the boundary names returned from the above request and set it as the boundary_name
:
merged_auth <- get_boundary(boundary_type = 'census', boundary_name='Census_Merged_Local_Authority_Districts_December_2011_Boundaries')
head(merged_auth)
library(ggplot2) ggplot() + geom_sf(data=merged_auth)
In general we don't want to request more data than we need. Many boundaries also limit the amount of polygons any given request will return to 200. There are several ways to request only the subset of the total boundary data we need:
One way to request a subset of boundary data is to add a bbox
to the request. We can set a bounding box in the form 'xmin,ymin,xmax,ymax' or as an sf
bbox
as generated by sf::st_bbox()
.
One way to get a bounding box is to use st_bbox
on another set of polygons:
my_auths <- merged_auth %>% filter(cmlad11nm %in% c('Wandsworth')) my_bbox <- st_bbox(my_auths)
msoa_in_bbox <- get_boundary(boundary_type = 'census', boundary_name='Middle_Super_Output_Areas_December_2011_Boundaries', bbox = my_bbox) head(msoa_in_bbox)
ggplot() + geom_sf(data=msoa_in_bbox) + geom_sf(data = st_as_sfc(my_bbox), colour = 'black', fill=NA)
We can also specify a bbox
from a user specified string:
msoa_in_bbox_2 <- get_boundary(boundary_type = 'census', boundary_name='Middle_Super_Output_Areas_December_2011_Boundaries', bbox = '-2.65,51.41,-2.52,51.50') head(msoa_in_bbox)
ggplot() + geom_sf(data=msoa_in_bbox_2) + coord_sf(xlim = c(-2.65,-2.52), ylim =c(51.41, 51.50))
Another way to filter the request is to get boundaries within a set radius of a given point. If radius is set, point
must be set too.
point
should be a longitude latitude point in the form 'lng,lat'
radius
is specified in meters
ward_in_radius <- get_boundary(boundary_type = 'admin', boundary_name='Wards_May_2019_Boundaries_UK_BFC', point = '-2.58791,51.45451', radius = 2000)
If we don't want to list the exact names of the wards, we can do partial matching by specifying names_like
. This is especially useful for boundaries where the area name forms a predictable pattern such as msoa names which all start with their local authority name.
Specifying names_like
will return polygons that (partially) match the name of the polygon
msoa_dartford <- get_boundary(boundary_type = 'census', boundary_name='Middle_Super_Output_Areas_December_2011_Boundaries', names_like = c('Dartford')) head(msoa_dartford)
If we know the exact name(s) of the polygon we want to return, we should specify names_equal
:
dg <- get_boundary(boundary_type = 'admin', boundary_name='Local_Authority_Districts_December_2019_Boundaries_UK_BFC', names_equal = c('Dartford', 'Gravesham')) head(dg)
There is also the option to use a custom polygon and return boundaries that intersect the given polygon.
We may need to simplify the polygon before sending the request:
custom_poly <- ward_in_radius %>% filter(wd19nm == 'Clifton') custom_poly_simp <- rmapshaper::ms_simplify(custom_poly)
lsoa_in_poly <- get_boundary(boundary_type = 'census', boundary_name='Lower_Super_Output_Areas_December_2011_Boundaries', custom_polygon = custom_poly_simp) ggplot() + geom_sf(data = st_intersection(lsoa_in_poly, custom_poly)) + geom_sf(data =custom_poly, color = 'red', fill=NA )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.