]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/obtaining_SLIP_data_using_R.org
Gebruik org timestamps in posts.
[www2.vanrenterghem.biz.git] / source / posts / obtaining_SLIP_data_using_R.org
1 #+date: <2023-10-11 23:41:54 +08:00>
2 #+opengraph2: ogimage="https://www.vanrenterghem.biz/blog/pics/SLIP_WA_schools.png"
3 #+filetags: R spatial analysis visualisation
4 #+title: Obtaining WA Landgate Shared Location Information using R.
6 Six years ago, [[file:using spatial features and openstreetmap][I wrote about Simple Features (sf) in R]]. I mapped the number of pupils per high
7 school in the Perth metro area. At the time, I didn't include how to
8 obtain the shapefile, provided as open data by Landgate on behalf of the
9 Western Australian government through its Shared Location Information
10 Platform ([[https://data.wa.gov.au/slip][SLIP]]).
12 I have now updated the script,
13 [[http://git.vanrenterghem.biz/?p=R/project-wa-schools.git;a=summary][available in my code repository]], with an R implementation of
14 [[https://toolkit.data.wa.gov.au/hc/en-gb/articles/115000962734][the methodology in SLIP's How To Guides]] ([[https://web.archive.org/web/20230608184007/https://toolkit.data.wa.gov.au/hc/en-gb/articles/115000962734][Archive]]).
16 The relevant code looks as follows, simplified greatly through the use
17 of the [[https://httr2.r-lib.org/][httr2]] library - the equivalent of the [[https://docs.python-requests.org/en/latest/][Requests]] library
18 used in the Python example in the SLIP knowledge base:
20 #+BEGIN_SRC R
21 library(httr2) 
22 tempdirSHP <- tempdir() 
23 tempfileSHP <- tempfile()
24 # Create the token request 
25 req <- request("https://sso.slip.wa.gov.au/as/token.oauth2") |>
26   req_headers("Authorization" = "Basic ZGlyZWN0LWRvd25sb2Fk") |>
27   req_body_form(grant_type = "password", # SLIP username and password stored in 
28                                          # pass - the standard unix password manager
29   username = system2("pass", args = "slip.wa.gov.au | grep Username | sed -e 's/Username: //'", stdout = TRUE), 
30   password = system2("pass", args = "slip.wa.gov.au | head -1", stdout = TRUE)) 
31 # Obtain the token response
32 tokenResponse <- req_perform(req) 
33 # Define the SLIP file to download
34 slipUrl <- "https://direct-download.slip.wa.gov.au/datadownload/Education/Current_Active_Schools_Sem_1_2022_Public_DET_020_WA_GDA94_Public_Shapefile.zip"
35 # Create the request for the SLIP file using the received token req <-
36 request(slipUrl) |> 
37   req_headers( 'Authorization' = paste0('Bearer',resp_body_json(tokenResponse)$access_token)) 
38 # Obtain the SLIP file using the created request 
39 responseSlip <- req_perform(req)
40 #+END_SRC
42 An updated plot of the high school enrollment numbers looks as follows
43 (for clarity, I've only included the names of schools in the top 5% as
44 ranked by student numbers):
46 #+CAPTION: Pupil density in Western Australian high schools
47 #+ATTR_HTML: :class img-fluid :alt Pupil density in Western Australian high schools
48 [[file:../assets/SLIP_WA_schools.png]]