Setup tscr

Read how to setup tscr to the TSC API.

Download Occurrences from TSC

We get Taxon occurrences with their point representation from TSC. Currently, all Fauna and Flora occurrences have only points, not polygons. Flora Populations can be captured as polygons representing the entire population, or a surveyed part of it. While some taxon occurrences might have polygon extents, all have a point representation.

Community occurrences are mostly captured as polygons. Each community polygon has a corresponding point representation (the centroid), but we limit this example to their polygons.

The examples are mutually applicable to taxon polygons and community points.

Areas as table

TSC maintains several types of reference areas, such as DBCA Regions and Districts.

In this example, we download a subset of 10 Areas, parse the data into a table, and display selected columns of the downloaded Areas.

areas <- tsc_GET("area", chunk_size = 100, max_records = 10) %>% tsc_parse()
areas %>% 
  head(10) %>% 
  dplyr::select("area_type", "name") %>% 
  dplyr::arrange("area_type") %>% 
  janitor::clean_names(case="sentence") %>% 
  knitr::kable()
Area type Name
Region Kimberley
District East Kimberley
District West Kimberley
Locality Cape Dommett
Site Cape Dommett
MPA Lalang-garram / Camden Sound
Site Smokey Bay
Locality Smokey Bay Area
Site Langgi
MPA Rowley Shoals

Areas on a map

In this example, we download Areas of all types, convert it to a spatial format (GeoJSON), and display it on a map.

See the Areas API for other possible filters.

We set mapview::mapviewOptions with some sensible defaults. See the mapview homepage for far more map configuration options.

areas_gj <- 
  tsc_GET("area", 
          # query = list(area_type="Region"),
          chunk_size = 100) %>% 
  magrittr::extract2("data") %>%
  geojsonio::as.json() %>%
  geojsonsf::geojson_sf()
data("areas_gj", package="tscr")
mapview::mapviewOptions(basemaps = c("Esri.WorldImagery", 
                                     "Esri.WorldShadedRelief", 
                                     "OpenTopoMap",
                                     "OpenStreetMap"),
                        layers.control.pos = "topright")

mapview::mapview(areas_gj, zcol="area_type")

Download Occurrences

In this example, we download the point representation of Taxon occurrence records.

We limit the result size to 10 records for demonstration purposes, but this method can also download the entire dataset if the parameter max_records is omitted.

See the TSC API for the full list of available endpoints.

We show here Taxon Point Occurrences and Community Polygon Occurrences.

Each data field can serve as a filter via the query attribute.

# Taxon occurrences with their Point representation
tae_res <- tsc_GET("occ-taxon-points", max_records = 10)

# Community occurrences with their Polygon representation
cae_res <- tsc_GET("occ-community-areas", max_records = 10)
data("tae_res", package="tscr")
data("cae_res", package="tscr")

Occurrences as table

We show some fields as an interactive table. The field as_html contains rendered content which can be used as a map popup.

Taxon points

tae <- tae_res %>% tsc_parse()
# head(tae) %>% knitr::kable()
tae %>% 
  dplyr::select(label, encountered_on, geolocation_capture_method, as_html) %>% 
  janitor::clean_names(case="sentence") %>% 
  reactable::reactable(filterable=TRUE, sortable = TRUE, searchable = TRUE,
                      defaultColDef = reactable::colDef(html=TRUE))

Community areas

cae <- cae_res %>% tsc_parse()
cae %>% 
  dplyr::select(label, encountered_on, as_html) %>% 
  janitor::clean_names(case="sentence") %>% 
  reactable::reactable(filterable=TRUE, sortable = TRUE, searchable = TRUE,
                      defaultColDef = reactable::colDef(html=TRUE))

Occurrences on a map

Taxon occurrences as GeoJSON SimpleFeatures for plotting. We use the field as_html as the popup and the geolocation capture method for grouping.

Taxon map

tae_gj <- tae_res %>%
  magrittr::extract2("data") %>%
  geojsonio::as.json() %>%
  geojsonsf::geojson_sf()
#> Registered S3 method overwritten by 'geojson':
#>   method        from     
#>   print.geojson geojsonsf

mapview::mapview(tae_gj, popup="as_html", zcol="geolocation_capture_method")

Community map

cae_gj <- cae_res %>%
  magrittr::extract2("data") %>%
  geojsonio::as.json() %>%
  geojsonsf::geojson_sf()

mapview::mapview(cae_gj, popup="as_html", zcol="name")

Download all Occurrences from TSC

We get Taxon occurrences with their point representations from TSC through the API endpoint occ-taxon-points.

Currently, all Fauna and Flora occurrences are georeferenced only with point coordinates, not polygons.

We then get TSC Areas and split them into DBCA Regions and Districts. Caveat: Regions and Districts to not comprehensively cover WA.

Lastly, we get a list of taxonomic names from TSC to resolve taxon IDs from taxon occurrences to names.

Taxon Occurrences (taxon points) are presented as occ with DBCA Region and District names (where applicable), plus taxonomic names. Community Occurrences (community areas) are named com_occ.

tsc_tae <- tsc_GET("occ-taxon-points") # 137k records
tsc_cae <- tsc_GET("occ-community-areas")
tsc_taxa <- tsc_GET("taxon") %>%
    wastdr::wastd_parse() %>%
    dplyr::mutate(name_id = name_id %>% as.character(), taxon = pk)
tsc_areas <- tsc_GET("area") %>%
      magrittr::extract2("features") %>%
      geojsonio::as.json() %>%
      geojsonsf::geojson_sf()

save(tsc_tae, tsc_cae, tsc_taxa, tsc_areas, 
     file=here::here("data/tsc.RData"), compress="xz")
regions <- tsc_areas %>%
  dplyr::filter(area_type == "Region") %>%
  dplyr::transmute(region_id = pk, region_name = name)

districts <- tsc_areas %>%
  dplyr::filter(area_type == "District") %>%
  dplyr::transmute(district_id = pk, district_name = name)

occ <- tsc_tae %>%
  magrittr::extract2("features") %>%
  geojsonio::as.json() %>%
  geojsonsf::geojson_sf() %>% 
  sf::st_join(regions) %>% 
  sf::st_join(districts) %>% 
  dplyr::left_join(tsc_taxa, by="taxon")

com_occ <- tsc_cae %>%
  magrittr::extract2("features") %>%
  geojsonio::as.json() %>%
  geojsonsf::geojson_sf() %>% 
  sf::st_join(regions) %>% 
  sf::st_join(districts)

Analyse occurrences

Taxon occurrences by DBCA Region

This example demonstrates how to tally taxon occurences by DBCA Regions, which were joined spatially earlier.

occ_by_region <- occ %>% 
    dplyr::group_by(region_name, taxonomic_name) %>% 
    dplyr::tally()  %>% 
    wastdr::sf_as_tbl() %>% 
    tidyr::pivot_wider(
        id_cols=taxonomic_name,
        names_from = region_name,
        values_from = n,
        values_fill = NA
    )
occ_by_region %>% reactable::reactable(sortable = T, filterable = T)

Species occurrences by DBCA District

The same summary, but by District.

occ_by_district <- occ %>% 
    dplyr::group_by(district_name, taxonomic_name) %>% 
    dplyr::tally()  %>% 
    wastdr::sf_as_tbl() %>% 
    tidyr::pivot_wider(
        id_cols=taxonomic_name,
        names_from = district_name,
        values_from = n,
        values_fill = NA
    )
occ_by_district %>% reactable::reactable(sortable = T, filterable = T)

Save to various formats

Export to GeoJSON

Objects of class “sf” can be saved to GeoJSON. GeoJSON files can be opened in GIS like Quantum GIS or ArcGIS for further processing and mapping.

areas_gj %>% sf::st_write("areas.geojson")
tae_gj %>% sf::st_write("taxon_occurrences_point.geojson")
cae_gj %>% sf::st_write("community_occurrences_polygon.geojson")

Export to spreadsheets

Any rectangular (tabular) data can be exported to CSV.

areas %>% readr::write_csv("areas.csv")
tae %>% readr::write_csv("taxon_occurrences_point.csv")
cae %>% readr::write_csv("community_occurrences_polygon.csv")