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.


Installing the Software

You will first need to make sure that R version 3.3.0 or later is installed on your computer.

You can download and install R from the following link: https://mirror.aarnet.edu.au/pub/CRAN/

Then download and install RStudio from the following link: https://www.rstudio.com/products/rstudio/download

You will also need to install the Geospatial Data Abstraction Library (GDAL).

On Windows, we recommend installing GDAL via OSGeo4W: https://trac.osgeo.org/osgeo4w/

For other operating systems, it can be installed from binaries available on the GDAL website: https://gdal.org/download


Installing and Loading R 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)

sf,rgdal,gdalUtils,leaflet

  • Check the console output to see if each library was correctly installed.
  • Now load each library by entering the following commands in the console:

library(sf)
library(rgdal)
library(gdalUtils)
library(leaflet)

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.


Assign an Object for your Data

Assign an empty object to your output data by entering the following into your console:

temp_file <- "output.geojson"


Create Datasource XML file

To download data from the AURIN API, we need to create a datasource XML file to authenticate against the API. To do this, open a text editor (Like BBEdit, or Notepad++) and enter the following text:

<OGRWFSDataSource>

<URL>http://openapi.aurin.org.au/wfs?version=1.0.0</URL>

<HttpAuth>BASIC</HttpAuth>

<UserPwd>username:password</UserPwd>

</OGRWFSDataSource>

Make sure to enter your AURIN API Username and Password where it says username:password

Save as aurin_wfs_connection.xml in your R project directory.


Specify Layer Name and Download

We need to specify the layer name associated with the dataset we wish to download, and issue it to the ogr2ogr command. In this tutorial, we will be looking at the spatial distribution of public toilets across Australia, the layer name is:

aurin:datasource-au_govt_dss-UoM_AURIN_national_public_toilets_2017

You can find the layer names for AURIN’s datasets by browsing the Data Catalogue. The layer name is found in the ‘AURIN Open API ID’ field in a dataset’s metadata.

Now it’s time to use ogr2ogr function within R to download the specified dataset into our object we assigned earlier. There are various parameters we need to put in to get going, and we will include these five:

  • src_datasource_name is your datasource 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 format (“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):

ogr2ogr("aurin_wfs_connection.xml",temp_file,"aurin:datasource-au_govt_dss-UoM_AURIN_national_public_toilets_2017",f="GeoJSON",oo="INVERT_AXIS_ORDER_IF_LAT_LONG=NO")

If your console returns ‘Character(0)’ you have successfully downloaded the data from the AURIN API.

For more information about using ogr2ogr in R, visit the R documentation.


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
dimension: XY
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:

test_geom

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, 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:

  • setView
  • addTiles
  • addProviderTiles
  • addCircleMarkers

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)) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(radius = 5, color = c('blue'), label = ~name)

You should get the following appear in your Viewer tab:

Click on image to enlarge

Try experimenting with the different options!

For more information refer to Leaflet’s API reference page.


Alternatively…

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:

install.packages("mapview")
library(mapview)
mapview(test_geom_wgs84)

You should get an image like the following

Click on image to enlarge