]> git.vanrenterghem.biz Git - R/project-wa-schools.git/commitdiff
Update to 2022 data.
authorFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 2 Oct 2023 12:00:57 +0000 (20:00 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 2 Oct 2023 12:00:57 +0000 (20:00 +0800)
- Use SLIP API via OAuth 2.0 to obtain shapefile.
- Use 2022 instead of 2017 data.

WA_active_schools.R

index 5076c609984c47b5368ac5202044185187e040e8..18816aab1f3537dd90e100aa5d23974f19456503 100644 (file)
@@ -1,16 +1,32 @@
-# install libgdal-dev on debian first
-# install libudunits2-dev on debian first
-#install.packages("sf")
 library(sf)
 library(sf)
-library(tidyverse)
-library(broom)
-#install.packages("OpenStreetMap")
+library(dplyr)
+library(ggplot2)
 library(OpenStreetMap)
 library(viridis)
 library(OpenStreetMap)
 library(viridis)
+library(httr2)
 
 
-# Read the shapefile obtained from WA opendata (SLIP)
-#shape <- readOGR(dsn = "data/", layer = "CurrentActiveSchoolsSemester12017_PublicDET_014")
-shape.df <- st_read(dsn = "data/", layer = "CurrentActiveSchoolsSemester12017_PublicDET_014", stringsAsFactors = FALSE)
+## Obtain the shapefile from WA opendata (SLIP)
+tempdirSHP <- tempdir()
+tempfileSHP <- tempfile()
+req <- request("https://sso.slip.wa.gov.au/as/token.oauth2") |>
+    req_headers("Authorization" = "Basic ZGlyZWN0LWRvd25sb2Fk") |>
+    req_body_form(list("grant_type" = "password",
+                       # SLIP username and password stored in
+                       #pass - the standard unix password manager
+                       "username" = system2("pass", args = "slip.wa.gov.au | grep Username | sed -e 's/Username: //'", stdout = TRUE),
+                       "password" = system2("pass", args = "slip.wa.gov.au | head -1", stdout = TRUE)))
+
+tokenResponse <- req_perform(req)
+
+slipUrl <-  "https://direct-download.slip.wa.gov.au/datadownload/Education/Current_Active_Schools_Sem_1_2022_Public_DET_020_WA_GDA94_Public_Shapefile.zip"
+
+req <- request(slipUrl) |>
+    req_headers( 'Authorization' = paste0('Bearer ',resp_body_json(tokenResponse)$access_token))
+
+responseSlip <- req_perform(req)
+writeBin(resp_body_raw(responseSlip), con = tempfileSHP)
+unzip(tempfileSHP, exdir = tempdirSHP)
+shape.df <- st_read(dsn = tempdirSHP)
 shape.df$latitude <- as.numeric(shape.df$latitude)
 shape.df$longitude <- as.numeric(shape.df$longitude)
 shape.df$totalschoo <- as.numeric(shape.df$totalschoo)
 shape.df$latitude <- as.numeric(shape.df$latitude)
 shape.df$longitude <- as.numeric(shape.df$longitude)
 shape.df$totalschoo <- as.numeric(shape.df$totalschoo)
@@ -18,7 +34,6 @@ shape.df$totalsecon <- as.numeric(shape.df$totalsecon)
 
 # filter out remote islands
 shape.df <- filter(shape.df,longitude > 100)
 
 # filter out remote islands
 shape.df <- filter(shape.df,longitude > 100)
-#shape.df <- filter(shape.df, totalschoo < 500)
 # Obtain the contour box of the shape
 
 lat <- c(min(shape.df$latitude), max(shape.df$latitude))
 # Obtain the contour box of the shape
 
 lat <- c(min(shape.df$latitude), max(shape.df$latitude))
@@ -41,7 +56,7 @@ map.metro <- openmap(upperLeft = upperLeft.metro,
 
 mapLatLon <- openproj(map)
 
 
 mapLatLon <- openproj(map)
 
-autoplot(mapLatLon) +
+OpenStreetMap::autoplot.OpenStreetMap(mapLatLon) +
   geom_point(data = shape.df, 
              aes(x = longitude, 
                  y = latitude,
   geom_point(data = shape.df, 
              aes(x = longitude, 
                  y = latitude,
@@ -54,7 +69,7 @@ autoplot(mapLatLon) +
         axis.ticks = element_blank()) +
   labs(title = "Western Australian schools",
        caption = "Map tiles by Stamen Design, under CC BY 3.0.\nMapdata by OpenStreetMap, under ODbL.
         axis.ticks = element_blank()) +
   labs(title = "Western Australian schools",
        caption = "Map tiles by Stamen Design, under CC BY 3.0.\nMapdata by OpenStreetMap, under ODbL.
-       School data © Government of WA 2017.\n Dep. of Education & Training.",
+       School data © Government of WA 2022.\n Dep. of Education & Training.",
        x="",
        y="")
 
        x="",
        y="")
 
@@ -68,7 +83,7 @@ mapLatLon.metro <- openproj(map.metro)
 # Let's look at secondary school only
 # 95th percentile of school size
 secon95th <- quantile(shape.metro.df[,"totalsecon">0]$totalsecon,probs = .95)
 # Let's look at secondary school only
 # 95th percentile of school size
 secon95th <- quantile(shape.metro.df[,"totalsecon">0]$totalsecon,probs = .95)
-autoplot(mapLatLon.metro) +
+OpenStreetMap::autoplot.OpenStreetMap(mapLatLon.metro) +
   geom_point(data = filter(shape.metro.df, totalsecon > 0), # geom_sf is not yet in ggplot2 on CRAN, so sticking to this for now
              aes(x = longitude, 
                  y = latitude,
   geom_point(data = filter(shape.metro.df, totalsecon > 0), # geom_sf is not yet in ggplot2 on CRAN, so sticking to this for now
              aes(x = longitude, 
                  y = latitude,
@@ -82,13 +97,13 @@ autoplot(mapLatLon.metro) +
             size = 2) +
   scale_color_viridis(option="viridis",
                       name = "Number\nof pupils") +
             size = 2) +
   scale_color_viridis(option="viridis",
                       name = "Number\nof pupils") +
-  guides(size = FALSE) + # Do not show legend for size, as color is the main key
+  guides(size = 'none') + # Do not show legend for size, as color is the main key
   theme(legend.position = "right",
         axis.text = element_blank(),
         axis.ticks = element_blank()) +
   labs(title = "Western Australian secondary schools",
        subtitle = "Perth metro area",
        caption = "Map tiles by Stamen Design, under CC BY 3.0.\nMapdata by OpenStreetMap, under ODbL.
   theme(legend.position = "right",
         axis.text = element_blank(),
         axis.ticks = element_blank()) +
   labs(title = "Western Australian secondary schools",
        subtitle = "Perth metro area",
        caption = "Map tiles by Stamen Design, under CC BY 3.0.\nMapdata by OpenStreetMap, under ODbL.
-       School data © Government of WA 2017.\n Dep. of Education & Training.",
+       School data © Government of WA 2022.\n Dep. of Education & Training.",
        x="",
        y="")
        x="",
        y="")