AURIN API AND R
Accessing the AURIN API via R
R is a free and open statistical software program and language. It is a very powerful tool for data analysts from all disciplines, from economics to ecology to geography. The GIS capabilities of R have developed significantly over the last decade.
In this tutorial, we will download data from the AURIN API using RStudio and create a basic interactive map using the Leaflet library. Feel free to use standalone R, however we recommend using RStudio to ensure that all library dependencies are downloaded.
Downloading and Installing the Software
You will first need to make sure that R is downloaded and installed on your computer.
NOTE: YOU WILL NEED TO INSTALL VERSION 3.3.0 OR LATER FOR THIS TO WORK
You can do this at the following link:
Then download an install RStudio from the following link:
Installing and Loading Libraries
- Open RStudio and create a new Project. This can be created from the File menu
- Next, set your working directory to the same directory that you saved this project file to. To do this go to Session → Set Working Directory → To Project Directory
- Next, navigate to Tools → Install Packages, add the following to the Packages text box, then click Install: (you just need to copy and paste these into the dialogue box that opens)
- Check the console output to see if each library was correctly installed.
- Now load each library by entering the following commands in the console:
In this tutorial you will be using the sf library to create a Spatial Data Frame, which is a data frame that will store your data (geographic data, in this case). For more information about data frames visit this tutorial . For more information about sf visit this information page.
Define a location to save your data
- Enter the following into your console:
temp_file <- "output.geojson"
Create XML file
Make sure to enter your correct User ID and Password where it says
Save as aurin_wfs_connection.xml in your R project directory.
Specify layer name and download
In this tutorial, we will be looking at the spatial distribution of public toilets across Australia
The layer name is:
If you do not know the layer name that you would like to use from the AURIN API, you can find it using QGIS. Please follow the steps in this tutorial to connect to the AURIN API using QGIS.
Once connected to the API in QGIS, you will see a list of available datasets. Click on the dataset you wish to display. In this example you can use “toilet” as the Filter. Navigate to the following layer:
DSS National Public Toilets 2017 – Data Provider: Australian Government – Department of Social Services.
IMPORTANT: The layer name is shown in the second column (next to Title)
To download data from the AURIN API using the Linux command line, we will specify the layer within the XML file created earlier.
Add the following line to the current URL value:
In this instance,
your_chosen_layer_name is aurin:datasource-au_govt_dss-UoM_AURIN_national_public_toilets_2017
So your xml file should look like the following:
Make sure to save the file with all of this text in it.
Feel free to choose any other layer from the AURIN API. Just remember to add your chosen layer name to the XML file.
Now it’s time to use ogr2ogr function within R. There are various parameters we need to put in to get going, and we will include these five:
- src_datasource_name is your xml file (“aurin_wfs_connection.xml”)
- dst_datasource_name is your destination file name (temp_file)
- layer is your layer name (“aurin:datasource-au_govt_dss-UoM_AURIN_national_public_toilets_2017”)
- f is your output file name (“GeoJSON”)
- oo are your open options (“INVERT_AXIS_ORDER_IF_LAT_LONG=NO”)
For later compatibility with Leaflet, we will choose not to invert the axis order.
All of this gets added together into a single command which looks like this (copy and paste into console):
If your console returns ‘Character(0)’ you have successfully downloaded the data from AURIN.
For more information about using ogr2ogr in R click here.
Read layer into R
Now that your layer has been downloaded into temp_file, we will use sf’s st_read to Read Simple Features into test_geom. To do this, copy this code into your console and press enter:
test_geom <- st_read(temp_file)
Your console should then produce the following text:
Reading layer `OGRGeoJSON' from data source `/home/michael/Documents/rstudio/vis/example/output.geojson' using driver `GeoJSON'
Simple feature collection with 18789 features and 46 fields
geometry type: POINT
bbox: xmin: 113.4102 ymin: -43.582 xmax: 153.6263 ymax: -10.5702
epsg (SRID): 4283
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
This means that your layer has been successfully read into your session.
For more information about st_read click on this link.
Define Coordinate System
To check your layer’s coordinate system, you can simply see the output above or enter its name in the console and press enter:
In this dataset we see an SRID of 4283 and proj4string of +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs. You can confirm the correct proj4string at http://epsg.io/4283. For additional information about Proj4Leaflet see this link.
To avoid confusion, as Leaflet does not natively support GDA94 (refer to this page), we will transform our data from 4283 (GDA94) to 4326 (WGS84) using st_transform. Enter the following text into your console and press enter:
test_geom_wgs84 <- st_transform(test_geom,"+init=epsg:4326")
Load layer into Leaflet with options
There are so many methods to present and visualise data using Leaflet.
In this example we will focus on four main options and use the alternate pipe command %>% to separate each:
Enter the following text into your console and press enter
leaflet(data=test_geom_wgs84, options = leafletOptions(worldCopyJump = F)) %>%
setView(146.75, -41.5, 7) %>%
addTiles(options = tileOptions(minZoom = 0, maxZoom = 18, continuousWorld = T)) %>%
addCircleMarkers(radius = 5, color = c('blue'), label = ~name)
You should get the following appear in your Viewer tab:
Try experimenting with the different options!
For more information refer to Leaflet’s API reference page
Try using the Mapview library.
To load the layer into Mapview (you may also include options), enter the following code into your console and press enter:
You should get an image like the following