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