Cloned from Simple_Energex_Map_4566.Rmd

Last run : 13 May 2023, based on map data extracted 13 May 2023

just text subst “4566” for “4573” for now

should be refactored so can use any postcode……

Just show postcode 4573

To do

display & export list of

11kV OH / UG feeders & characteristics

LV OH / UG feeders

create new field to consolidate LV variants to make viewing simpler

distribution substations

# get postcodes

# read the RDS postcode file instead.....
myPostcode <- readRDS(file=paste0(maps_folder,"Queensland_Postcode_map.rds"))
Just4573 <- myPostcode %>% filter(PC_PID == 'QLD457331')

# set up so we just get data for Noosa LGA 
myLGA <- readRDS(file=paste0(maps_folder,"Queensland_LGA_map.rds"))

#myLGA <- readRDS(file=paste0(maps_folder,"Queensland_LGA_map.rds"))
# just get Gympie=3620, Noosa=5740 & Sunshine Coast=6720
JustNoosaLGA <- myLGA %>% filter(LGA_CODE == '5740')
# NoosaLGA <- myLGA %>% filter(LGA_CODE %in% c('3620','5740','6720'))

# just get 4573
tmap_mode(mode = "view")
## tmap mode set to interactive viewing
tm <- tm_shape(Just4573) + tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "darkmagenta", interactive = FALSE) +
tm_shape(JustNoosaLGA) + tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "blue", interactive = FALSE)

tm
#myfilename <- paste0(ABS_folder,"SAL_2021_AUST_GDA2020.shp")
#print(myfilename)

# Comment out code for ABS_4573 - as this only required for Noosaville grant application to match "eligible locations"
# my_ABS <- st_read(paste0(ABS_folder,"SAL_2021_AUST_GDA2020.shp"))
# # my_ABS
# # shows SAL_CODE21, SAL_NAME_21, STE_CODE21, STE_NAME21   - use STE to filter by state
# # Noosaville = 32162, Caloundra = 30492, The Gap = 32790
# my_ABS_4573 <- my_ABS %>% filter(SAL_CODE21 == '32162')
# 
# tm <- tm_shape(my_ABS_4573) + tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "darkmagenta", interactive = FALSE)
# tm

my_filtered_list <- st_intersects(myOH_11kV, Just4573,sparse = FALSE)
myOH_11kV_4573 <- myOH_11kV[my_filtered_list, ]

my_filtered_list <- st_intersects(myUG_11kV, Just4573,sparse = FALSE)
myUG_11kV_4573 <- myUG_11kV[my_filtered_list, ]

my_filtered_list <- st_intersects(myOH_LV, Just4573,sparse = FALSE)
myOH_LV_4573 <- myOH_LV[my_filtered_list, ]

my_filtered_list <- st_intersects(myUG_LV, Just4573,sparse = FALSE)
myUG_LV_4573 <- myUG_LV[my_filtered_list, ]

my_filtered_list <- st_intersects(mySubstations, Just4573,sparse = FALSE)
Substations_4573 <- mySubstations[my_filtered_list, ]

Noosa_Substations_greater_300KVA <- Substations_4573 %>% filter(as.numeric(MAX_KVA) > 299)
Noosa_Substations_less_300KVA <- Substations_4573 %>% filter(as.numeric(MAX_KVA) < 300)

Noosa_Substations_greater_300KVA_no_geom <- Noosa_Substations_greater_300KVA %>% st_drop_geometry() %>% select(c(1,3,4,5,6)) 
datatable(Noosa_Substations_greater_300KVA_no_geom)
Noosa_Substations_less_300KVA_no_geom <- Noosa_Substations_less_300KVA %>% st_drop_geometry() %>% select(c(1,3,4,5,6)) 
datatable(Noosa_Substations_less_300KVA_no_geom)
tmap_options(max.categories = 220) # as per warning...

tm <- tm_shape(Just4573) + tm_borders("darkmagenta", lwd=5) +
#  tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "darkmagenta", interactive = FALSE) +
  
tm_shape(JustNoosaLGA) + tm_borders("blue4", lwd=5) +
#  tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "blue4" ,interactive = FALSE) +

# tm_shape(my_ABS_4573) + tm_polygons(col = "MAP_COLORS", alpha = 0.2, border.col = "blue4", interactive = FALSE) +

tm_shape(myOH_11kV_4573)+tm_lines(col="red",lwd=6,id='FEEDER') +
# tm_text("FEEDER", size = 1.5, col = "red") +  
  
tm_shape(myUG_11kV_4573)+tm_lines(col="green",lwd=6,id='FEEDER') +
# tm_text("FEEDER", size = 1.5, col = "green") +
  
tm_shape(myZoneSub)+tm_dots(col='grey' ,size=0.75,id='SITE_REF') +
  
tm_shape(myOH_LV_4573)+tm_lines(col = "FEEDER",lwd=2,id='FEEDER', legend.col.show = FALSE) +
# tm_text("FEEDER", size = 1.5, col = "black") +
  
tm_shape(myUG_LV_4573)+tm_lines(col="FEEDER",lwd=2,id='FEEDER', 
legend.col.show = FALSE) +
# don't show feeder names as gets too cluttered.....
# tm_text("FEEDER", size = 1.5, col = "black") +

tm_shape(Noosa_Substations_less_300KVA)+tm_dots( col='greenyellow', size=0.06, id='USER_REF_I') +
tm_shape(Noosa_Substations_greater_300KVA)+tm_dots( col='green3', size=0.12, id='USER_REF_I')  

# https://www.datanovia.com/en/blog/awesome-list-of-657-r-color-names/

tm
# see https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html
# Exporting maps section
# save as stand-alone HTML file ("view" mode)
tmap_save(tm, filename = paste0(maps_folder,"Simple_Energex_Map_Postcode_4573.html"))
## Interactive map saved to /Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/Simple_Energex_Map_Postcode_4573.html
feeders_11kV_OG_4573_no_geom <- myOH_11kV_4573 %>% st_drop_geometry() %>% select(c(2,3,4,6,7)) 
datatable(feeders_11kV_OG_4573_no_geom)
write.csv(feeders_11kV_OG_4573_no_geom,paste0(maps_folder,"feeders_11kV_OG_4573_no_geom.csv"), row.names = FALSE)

myUG_LV_4573_no_geom <- myUG_LV_4573 %>% st_drop_geometry() %>% select(c(2,3,4,6,7)) 
datatable(myUG_LV_4573_no_geom)
write.csv(myUG_LV_4573_no_geom,paste0(maps_folder,"myUG_LV_4573_no_geom.csv"), row.names = FALSE)

# Save the 4573 shape files
# can use online tool to convert to kml for uploading to Google MyMap
shp_filename = paste(maps_folder,"myUG_11kV_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(myUG_11kV_4573,shp_filename )
## Writing layer `myUG_11kV_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/myUG_11kV_4573.shp' using driver `ESRI Shapefile'
## Writing 6 features with 8 fields and geometry type Multi Line String.
shp_filename = paste(maps_folder,"myUG_LV_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(myUG_LV_4573,shp_filename )
## Writing layer `myUG_LV_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/myUG_LV_4573.shp' using driver `ESRI Shapefile'
## Writing 206 features with 8 fields and geometry type Multi Line String.
shp_filename = paste(maps_folder,"myOH_11kV_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(myOH_11kV_4573,shp_filename )
## Writing layer `myOH_11kV_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/myOH_11kV_4573.shp' using driver `ESRI Shapefile'
## Writing 4 features with 8 fields and geometry type Multi Line String.
shp_filename = paste(maps_folder,"myOH_LV_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(myOH_LV_4573,shp_filename )
## Writing layer `myOH_LV_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/myOH_LV_4573.shp' using driver `ESRI Shapefile'
## Writing 46 features with 8 fields and geometry type Multi Line String.
# shp_filename = paste(maps_folder,"Substations_4573.shp",sep = "")
# if(file.exists(shp_filename)) {
#     file.remove(shp_filename)
# }
# st_write(Substations_4573,shp_filename )

shp_filename = paste(maps_folder,"Substations_greater_300KVA_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(Noosa_Substations_greater_300KVA,shp_filename )
## Writing layer `Substations_greater_300KVA_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/Substations_greater_300KVA_4573.shp' using driver `ESRI Shapefile'
## Writing 46 features with 11 fields and geometry type Point.
shp_filename = paste(maps_folder,"Substations_less_300KVA_4573.shp",sep = "")
if(file.exists(shp_filename)) {
    file.remove(shp_filename)
}
## [1] TRUE
st_write(Noosa_Substations_less_300KVA,shp_filename )
## Writing layer `Substations_less_300KVA_4573' to data source 
##   `/Users/geremida/Dropbox/ZENE/R/EQld_NetworkMaps/Energex_maps/Substations_less_300KVA_4573.shp' using driver `ESRI Shapefile'
## Writing 37 features with 11 fields and geometry type Point.
# st_write(my_ABS_4573,"my_ABS_4573.shp")