diff --git a/howTo/openGeogAPI/app.R b/howTo/openGeogAPI/app.R index 96ad8c821f6952ab63705688a4e1d0b4b2499fe7..44f359ed09540e78d043c6ebca5525fd3ee1171f 100644 --- a/howTo/openGeogAPI/app.R +++ b/howTo/openGeogAPI/app.R @@ -1,5 +1,6 @@ +# Load libraries ---- - +library(readxl) library(ggplot2) library(sf) library(htmltools) @@ -8,6 +9,9 @@ library(plotly) library(dplyr) library(reshape2) +# List local authority areas to load +# These used to filter emissions data +# and construct Open Geog API query (geo_query ... to do) las_to_load <- c("Southampton","Portsmouth","Winchester", "Eastleigh","Isle of Wight","Fareham", "Gosport","Test Valley","East Hampshire", @@ -15,8 +19,6 @@ las_to_load <- c("Southampton","Portsmouth","Winchester", # Load GHG emissions data ---- -library(readxl) - url_to_get <- "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/894787/2005-18-uk-local-regional-co2-emissions.xlsx" tempf <- tempfile(fileext = ".xlsx") @@ -24,6 +26,30 @@ download.file(url_to_get, tempf, method = "curl") dt <- readxl::read_xlsx(tempf, sheet = "Full dataset",skip = 1) +x_min <- min(dt$Year) +x_max <- max(dt$Year) + +# Load LA geography ---- + +# URL as API query - sometimes we don't want all boundaries +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" + +message("Loading LA geometry from ONS Open Geography API") +sf_data <- st_read(geo_query) +#plot(st_geometry(sf_data)) + +# Useful lookup spatial reference for CRS +# https://spatialreference.org/ref/epsg/27700/ +st_coord_sys <- st_crs(sf_data) # check coord system +st_coord_sys # current coord system EPSG: 4326 (is what leaflet wants - good) + +# transform the coord system if required +if(st_coord_sys$epsg != 4326){ + sf_data <- st_transform(sf_data, "+proj=longlat +datum=WGS84") +} + +# Functions ---- + lvl_detail <- "high" filter_detail <- function(lvl_detail = lvl_detail){ @@ -53,28 +79,10 @@ filter_detail <- function(lvl_detail = lvl_detail){ return(ghg_emissions) } -ghg_emissions <- filter_detail(lvl_detail = "high") +ghg_emissions <- filter_detail(lvl_detail = "low") +# ghg_emissions_high <- filter_detail(lvl_detail = "high") rm(dt) -# Load LA geography ---- - -# URL as API query - sometimes we don't want all boundaries -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" - -message("Loading LA geometry from ONS Open Geography API") -sf_data <- st_read(geo_query) -#plot(st_geometry(sf_data)) - -# Useful lookup spatial reference for CRS -# https://spatialreference.org/ref/epsg/27700/ -st_coord_sys <- st_crs(sf_data) # check coord system -st_coord_sys # current coord system EPSG: 4326 (is what leaflet wants - good) - -# transform the coord system if required -if(st_coord_sys$epsg != 4326){ - sf_data <- st_transform(sf_data, "+proj=longlat +datum=WGS84") -} - # Create map (leaflet) ---- # create popup first (requires htmltools) @@ -166,12 +174,15 @@ server <- function(input, output, session) { ggplot(ghg_subset(auth_area = input$authmap_shape_click$id), aes(x = Year, y = value)) + geom_col(aes(fill = variable), position = "stack") + + scale_x_continuous(limits = c(x_min-1,x_max+1), breaks = x_min:x_max) + + #scale_y_continuous(limits = c(y_min,y_max)) + labs(x = "Year", y = expression("tCO"[2]), fill = "Sector", title = plotTitle, caption = plotCaption) + - theme(legend.position = "bottom") + theme(legend.position = "right") + + theme_classic() })