Skip to content
Snippets Groups Projects
Commit 97970b53 authored by Ben Anderson's avatar Ben Anderson
Browse files

Merge branch 'master' into 'master'

Update to include Rushmoor local authority area in Hampshire (previously missing)

See merge request !6
parents 84b9ffe5 d3d861c2
No related branches found
No related tags found
1 merge request!6Update to include Rushmoor local authority area in Hampshire (previously missing)
......@@ -4,15 +4,27 @@ library(sf) # install first if needed
library(data.table)
library(here)
# Local Authority boundaries for the Solent region ----
# Load repo params (file locations etc)
source("env.R")
# Local Authority boundaries for the Solent region (2018) ----
# see https://medium.com/@traffordDataLab/pushing-the-boundaries-with-the-open-geography-portal-api-4d70637bddc3
geo_query <- "https://ons-inspire.esriuk.com/arcgis/rest/services/Administrative_Boundaries/Local_Authority_Districts_December_2018_Boundaries_UK_BGC/MapServer/0/query?where=lad18nm%20IN%20(%27Southampton%27,%27Portsmouth%27,%27Winchester%27,%27Eastleigh%27,%27Isle%20of%20Wight%27,%27Fareham%27,%27Gosport%27,%27Test%20Valley%27,%27East%20Hampshire%27,%27Havant%27,%27New%20Forest%27,%27Hart%27,%27Basingstoke%20and%20Deane%27)&outFields=lad18cd,lad18nm,long,lat&outSR=4326&f=geojson"
geo_query <- "https://ons-inspire.esriuk.com/arcgis/rest/services/Administrative_Boundaries/Local_Authority_Districts_December_2018_Boundaries_UK_BGC/MapServer/0/query?where=lad18nm%20IN%20(%27Southampton%27,%27Portsmouth%27,%27Winchester%27,%27Eastleigh%27,%27Isle%20of%20Wight%27,%27Fareham%27,%27Gosport%27,%27Test%20Valley%27,%27Rushmoor%27,%27East%20Hampshire%27,%27Havant%27,%27New%20Forest%27,%27Hart%27,%27Basingstoke%20and%20Deane%27)&outFields=lad18cd,lad18nm,long,lat&outSR=4326&f=geojson"
message("Loading LA geometry from ONS Open Geography API")
la_sf_data <- sf::st_read(geo_query)
head(la_sf_data)
# save it out for re-use
# two possible locations - repo or UoS resource drive
# Repo
outf <- path.expand(paste0(here::here("data", "boundaries", "la_solent.shp")))
# UoS resource drive
outf <- paste0(outPath, "LA_solent_2018.shp")
sf::write_sf(la_sf_data, outf)
# MSOA boundaries for the Solent region ----
......@@ -25,6 +37,9 @@ sf::write_sf(la_sf_data, outf)
# the code is included here for reference
inFile <- path.expand(paste0(here::here("data", "boundaries", "msoa_all",
"Middle_Layer_Super_Output_Areas__December_2011__Boundaries_Full_Clipped__BFC__EW_V3.shp")))
inFile <- paste0(dPath, "census/downloads/msoa/", "Middle_Layer_Super_Output_Areas_(December_2011)_Boundaries_Generalised_Clipped_(BGC)_EW_V3-shp")
msoa_all_sf <- sf::read_sf(inFile)
# load the look up table so we can filter
......@@ -38,19 +53,22 @@ oa_lut <- data.table::fread(lutFile)
# "Havant","New Forest","Hart","Basingstoke and Deane")
# select the OAs we want via LAs
oa_solentLut <- oa_lut[LAD11NM == "Southampton" |
LAD11NM == "Portsmouth" |
LAD11NM == "Winchester" |
LAD11NM == "Eastleigh" |
LAD11NM == "Isle of Wight" |
LAD11NM == "Fareham" |
LAD11NM == "Gosport" |
LAD11NM == "Test Valley" |
LAD11NM == "East Hampshire" |
LAD11NM == "Havant" |
LAD11NM == "New Forest" |
LAD11NM == "Hart" |
LAD11NM == "Basingstoke and Deane"]
oa_solentLut <- oa_lut[
LAD11NM == "Basingstoke and Deane" |
LAD11NM == "East Hampshire" |
LAD11NM == "Eastleigh" |
LAD11NM == "Fareham" |
LAD11NM == "Gosport" |
LAD11NM == "Hart" |
LAD11NM == "Havant" |
LAD11NM == "New Forest" |
LAD11NM == "Rushmoor" | # Added
LAD11NM == "Test Valley" |
LAD11NM == "Isle of Wight" |
LAD11NM == "Portsmouth" |
LAD11NM == "Southampton" |
LAD11NM == "Winchester"
]
oa_solentLut[, .(nOAs = .N), keyby = .(LAD11NM)]
......@@ -67,7 +85,13 @@ msoa_solent <- merge(msoa_all_sf, msoa_solentLut)
table(msoa_solent$LAD11NM)
# save it out for re-use
outf <- path.expand(paste0(here::here("data", "boundaries", "msoa_solent.shp")))
# two possible locations - repo or UoS resource drive
# Repo
#outf <- path.expand(paste0(here::here("data", "boundaries", "msoa_solent.shp")))
# UoS resource drive
outf <- paste0(outPath, "MSOA_solent_2011.shp")
sf::write_sf(msoa_solent, outf)
# LSOA boundaries for the Solent region ----
......@@ -78,6 +102,9 @@ sf::write_sf(msoa_solent, outf)
# load all LSOAs from file
inFile <- path.expand("~//Dropbox/data/UK_census2011/Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Generalised_Clipped_(BGC)_EW_V3-shp/Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Generalised_Clipped_(BGC)_EW_V3.shp") # local version
inFile <- paste0(dPath, "census/downloads/lsoa/", "Lower_Layer_Super_Output_Areas_(December_2011)_Boundaries_Generalised_Clipped_(BGC)_EW_V3.shp")
lsoa_all_sf <- sf::read_sf(inFile)
# las_to_load <- c("Southampton","Portsmouth","Winchester",
......@@ -98,5 +125,13 @@ lsoa_solent <- merge(lsoa_all_sf, lsoa_solentLut)
table(lsoa_solent$LAD11NM)
# save it out for re-use
outf <- paste0(here::here("data", "boundaries", "lsoa_solent.shp"))
# two possible locations - repo or UoS resource drive
# Repo
#outf <- paste0(here::here("data", "boundaries", "lsoa_solent.shp"))
# UoS resource drive
outf <- paste0(outPath, "LSOA_solent_2011.shp")
sf::write_sf(lsoa_solent, outf)
No preview for this file type
No preview for this file type
No preview for this file type
env.R 0 → 100644
# parameters widely re-used across this repo
repoParams <- list() # params holder as a list
listFiles <- FALSE
# Location ----
# attempt to guess the platform & user
repoParams$info <- Sys.info()
repoParams$sysname <- repoParams$info[[1]]
repoParams$nodename <- repoParams$info[[4]]
repoParams$login <- repoParams$info[[6]]
repoParams$user <- repoParams$info[[7]]
# Base Data path ----
dLoc <- "unknown - find this line in env.R and fix it!"
if (repoParams$sysname == "Darwin") {
# we're on a Mac (local) so we can set the data path to the Resource drive (if mounted)
dLoc <- "/Volumes/Resource/CivilEnvResearch/Public/SERG/data/"
}
if (repoParams$sysname == "Windows") {
# we're on Windows (local) so we can set the data path to J: drive (if mounted)
dLoc <- "J:/CivilEnvResearch/Public/SERG/data/"
}
if (repoParams$nodename == "srv02405") {
# we're on the UoS RStudio server so we set the data path to:
dLoc <- path.expand("/mnt/SERG_data/")
}
if (grepl("prd-cls2k4", repoParams$nodename)) {
# we're on the UoS SVE so we can set the output path to J: (as mounted)
dLoc <- "J:/CivilEnvResearch/Public/SERG/data/"
}
# Data (SSEN) ----
dPath <- paste0(dLoc, "geography/")
### Output ----
outPath <- paste0(dLoc,"geography/_processed/")
# Feedback ----
message("You're ", repoParams$user, " using " , repoParams$sysname, " on ", repoParams$nodename)
message("Default dPath has been set to: \n", dPath)
if (listFiles) { # optional
message("and these are the files/folders in that dPath:")
print(try(list.files(dPath))) # in case it breaks
}
message("Check env.R if that's not what you expected...")
......@@ -25,6 +25,8 @@ always_allow_html: yes
---
```{r setup, include=FALSE}
# Set to save downloaded geom to shp file
writeFile <- TRUE
# you might need to install these first
library(sf)
......@@ -35,6 +37,11 @@ library(dplyr)
library(knitr)
```
```{r}
source("env.R")
```
# Introduction
This script provides an example of downloading and importing administrative boundaries from the Office for National Statistics Open Geography portal into RStudio and plotting on a map. It is based upon a useful example by [Trafford Data Lab](https://medium.com/@traffordDataLab/pushing-the-boundaries-with-the-open-geography-portal-api-4d70637bddc3).
......@@ -55,12 +62,13 @@ geo_search <- "LAD20NM IN "
# Construct a vector of local authorities to load
# the following local authorities are the 'Solent' region
las_to_load <- c("Southampton","Portsmouth","Winchester",
"Eastleigh","Isle of Wight","Fareham",
"Gosport","Test Valley","East Hampshire",
"Havant","New Forest","Hart","Basingstoke and Deane")
las_to_load_hampshire <- c("Basingstoke and Deane", "East Hampshire",
"Eastleigh", "Fareham", "Gosport",
"Hart", "Havant", "New Forest",
"Rushmoor", "Test Valley", "Winchester",
"Southampton", "Portsmouth", "Isle of Wight") # These last three are not defined as Hampshire
geo_where <- las_to_load # sometimes we don't want all boundaries
geo_where <- las_to_load_hampshire # sometimes we don't want all boundaries
geo_outfields <- "*" # returns all fields
#geo_outfields <- c("LAD20CD","LAD20NM","LONG","LAT") # use in place of line above to return selected fields only
geo_outSR <- "4326"
......@@ -144,3 +152,14 @@ leaflet(sf_data) %>%
bringToFront = TRUE))
```
# Write out shape file
```{r}
# Set output directory
outDir <- paste0(dPath, "_R_output/")
# Write simple features to file see https://r-spatial.github.io/sf/articles/sf2.html#using-st_write
if(writeFile) {
st_write(sf_data, paste0(outDir,"LA_solent_2020.shp"))
}
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment