Created
June 1, 2023 10:56
-
-
Save simonrolph/a821dfbe25140b2cf128db2e7f4c1a19 to your computer and use it in GitHub Desktop.
An R script to generate summary data about data providers on the NBN Atlas using the faceting functionality in the occurrence API endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
library(jsonlite) | |
library(dplyr) | |
# Get a list of data providers | |
response <- GET("https://registry.nbnatlas.org/ws/dataProvider") | |
content <- content(response, "text") | |
data_providers <- fromJSON(content) # Parse the JSON response into a dataframe | |
#define query | |
dp <- "dp239" | |
facets <- c( | |
# temporal | |
"year", | |
"month", | |
"date_precision", | |
#spatial | |
"coordinate_uncertainty", | |
"country", | |
"county", | |
#environmental | |
"habitat", | |
# metadata | |
"data_resource", | |
"dataset_name", | |
"license", | |
"basis_of_record", | |
"identification_verification_status" | |
) | |
url <- paste0("https://records-ws.nbnatlas.org/occurrences/search?q=data_provider_uid:", | |
dp, | |
"&facets=", | |
facets %>%paste0(collapse=","), | |
"&pageSize=0") | |
# get facets for dp | |
response <- GET(url) | |
# Get the content from the response | |
content <- content(response, "text") | |
content | |
# Parse the JSON response into a dataframe | |
facet_content <- fromJSON(content) | |
facet_data <- facet_content$facetResults$fieldResult %>%lapply(FUN = function(x){x %>% select(label,count) %>% arrange(label)}) | |
names(facet_data) <- facets | |
facet_data | |
#do whatever plotting of the data you like eg | |
plot(facet_data$year,count~year) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment