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