R Variant of WPD smart-meter data analysis

Smart meter visualisation using WPD data portal and R libraries & code

#####[NB - run the code within Rstudio and any missing libraries will be loaded/installed via prompt]

#####  download WPD open data and convert to data table
url <- 'https://connecteddata.westernpower.co.uk/api/3/action/datastore_search?resource_id=5e531ff5-02ff-48d7-8512-6e603fb569b4&limit=300000'  
wpdd <- fromJSON(url, flatten = TRUE)
wpd1 <- wpdd[["result"]][["records"]]
wpd1 <- as.data.table(wpd1)
## calculate percentage smart meters
wpd1$pct_smart <- 100 * (wpd1$SMETS1 + wpd1$SMETS2) / (wpd1$SMETS1 + wpd1$SMETS2 + wpd1$NON_SMART)

First Plots

graph some data (prefer boxplots to pie charts)
boxplot(wpd1[,c(8:10)], outline = F, col="green", main="Number of meters at each WPD site")

load wpd geo-polygons
wpe <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/c0bc2a2d-deda-4dc8-8317-0ec8a24af677/download/east-midlands-primary.gpkg")
## Reading layer `East Midlands primary' from data source 
##   `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/c0bc2a2d-deda-4dc8-8317-0ec8a24af677/download/east-midlands-primary.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 395 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 400746 ymin: 220104 xmax: 557570 ymax: 400502
## Projected CRS: OSGB 1936 / British National Grid
wpw <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/547db41e-fb44-423f-9c71-98266b666c38/download/south-wales-primary.gpkg")
## Reading layer `South Wales primary' from data source 
##   `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/547db41e-fb44-423f-9c71-98266b666c38/download/south-wales-primary.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 189 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 171370 ymin: 165580 xmax: 359518 ymax: 283812
## Projected CRS: OSGB 1936 / British National Grid
wps <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/fe4c3f49-7de9-4ebf-bb10-413c50d59f83/download/south-west-primary.gpkg")
## Reading layer `South West primary' from data source 
##   `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/fe4c3f49-7de9-4ebf-bb10-413c50d59f83/download/south-west-primary.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 302 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 87050 ymin: 7020 xmax: 381194 ymax: 188276
## Projected CRS: OSGB 1936 / British National Grid
wpm <- st_read("https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/83c22640-3a54-415f-b253-e51bee3bd146/download/west-midlands-primary.gpkg")
## Reading layer `West Midlands primary' from data source 
##   `https://connecteddata.westernpower.co.uk/dataset/d8e73301-e26a-4b5f-8eb5-e9a2f1b61338/resource/83c22640-3a54-415f-b253-e51bee3bd146/download/west-midlands-primary.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 216 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 309351.3 ymin: 168633 xmax: 451348 ymax: 369905
## Projected CRS: OSGB 1936 / British National Grid
# merge 4x regions into 1
wpdall <- rbind(wpe,wpw,wps,wpm)
add National Grid open data
from: https://www.nationalgrid.com/uk/electricity-transmission/network-and-infrastructure/network-route-maps
ngc <- st_read("all_datasets/cable.shp")
## Reading layer `Cable' from data source 
##   `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/all_datasets/Cable.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 3489 features and 12 fields
## Geometry type: LINESTRING
## Dimension:     XYZ
## Bounding box:  xmin: 226860.3 ymin: 84633.77 xmax: 647410.7 ymax: 583203.3
## z_range:       zmin: 0 zmax: 0
## Projected CRS: OSGB 1936 / British National Grid
ngo <- st_read("all_datasets/OHL.shp")
## Reading layer `OHL' from data source 
##   `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/all_datasets/OHL.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 757 features and 8 fields
## Geometry type: MULTILINESTRING
## Dimension:     XYZ
## Bounding box:  xmin: 193297 ymin: 54718.68 xmax: 647069 ymax: 641074
## z_range:       zmin: 0 zmax: 0
## Projected CRS: OSGB 1936 / British National Grid
ngs <- st_read("all_datasets/Substations.shp")
## Reading layer `Substations' from data source 
##   `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/all_datasets/Substations.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 490 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XYZ
## Bounding box:  xmin: 193253 ymin: 56234.37 xmax: 647220.3 ymax: 583162.8
## z_range:       zmin: 0 zmax: 0
## Projected CRS: OSGB 1936 / British National Grid
ngt <- st_read("all_datasets/Towers.shp")
## Reading layer `Towers' from data source 
##   `/Users/davidhaw/OneDrive/R analysis/NGRID/WPD_open_data/smart-meters/all_datasets/Towers.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 21965 features and 8 fields
## Geometry type: POINT
## Dimension:     XYZ
## Bounding box:  xmin: 193297 ymin: 54718.68 xmax: 647063 ymax: 641074
## z_range:       zmin: 0 zmax: 0
## Projected CRS: OSGB 1936 / British National Grid

Merge meter data with geo-data

first ‘align’ the names so that the data sets can be merged

wn<- names(wpd1)
wn[7] <- "PRIM_NRID"
names(wpd1)  <- wn
wpdall <- left_join(wpdall, wpd1, by=c("PRIM_NRID"))

calculate percentage smart meters in this new dataset

wpdall$pct_smart <- 100 * (wpdall$SMETS1 + wpdall$SMETS2) / (wpdall$SMETS1 + wpdall$SMETS2 + wpdall$NON_SMART)

remove duplicate columns in merged data table

wpdall$GSP_NRID <- NULL
wpdall$BSP_NRID <- NULL
reduce the data burden - extract the Nat Grid geo-data where it overlaps with WPD’s region
align coord-ref-systems CRS (not needed here)

Limit National Grid data to WPD region by shape

wpdt <- st_intersection(wpdall, ngt)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpdo <- st_intersection(wpdall, ngo)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpdc <- st_intersection(wpdall, ngc)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
wpds <- st_intersection(wpdall, ngs)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
…and generate interactive map via TMAP and Leaflet (takes ~ 1 minute on my laptop)
visible at: https://rpubs.com/davehawkins/849799
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(wpdall) +
  tm_polygons(col = "green",
              alpha=0.2,
              lwd=0.7,
              id="NAME") +
  tm_shape(wpds) +
  tm_dots(col = "grey",
           alpha=1,
           size=0.1,
           id="Substati_1") +
  tm_shape(wpdc)+
  tm_lines(col="black",
           alpha=1,
           lwd=0.75,
           id="CABLE_SET") +
    tm_shape(wpdo)+
  tm_lines(col="black",
           alpha=1,
           lwd=0.75,
           id="Towers_In") +
  tm_shape(wpdt) +
  tm_dots(col = "grey",
          alpha=1,
          size=0.01,
          id="TOWER_ASSE") +
  tm_basemap(server = "Esri.WorldStreetMap")