R/GeoClient-wrapper.r

Defines functions .un_object geo_query geo_is_operator_allowed geo_get_settings geo_get_setting geo_get_paths geo_get_geo_json_for_locality geo_get_field_info geo_get_distinct_values_per_group geo_get_distinct_values geo_find_by_ids geo_find geo_count_distinct_values_per_group geo_count_distinct_values geo_count

Documented in geo_count geo_count_distinct_values geo_count_distinct_values_per_group geo_find geo_find_by_ids geo_get_distinct_values geo_get_distinct_values_per_group geo_get_field_info geo_get_geo_json_for_locality geo_get_paths geo_get_setting geo_get_settings geo_is_operator_allowed geo_query

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

# Geo client wrapper
# for nbaR.Geo objects

#' @name geo_count
#' @title Get the number of geo areas matching a given condition
#' @description This is a wrapper for the method \code{ count }
#' from class \code{ GeoClient}.
#' @details Conditions given as query string
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param queryParams Named list or vector with names being the fields to be queried and values being the values to match
#' @param ... additional parameters passed to count from class nbaR.GeoClient
#' @export
geo_count <- function(
                      queryParams = list(),
                      ...) {
  sc <- GeoClient$new()
  res <- sc$count(
    queryParams = queryParams,
    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_count_distinct_values
#' @title Count the distinct number of values that exist for a given field
#' @description This is a wrapper for the method \code{ count_distinct_values }
#' from class \code{ GeoClient}.
#' @details Field given as string. See also getDistinctValues
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param field name of field in taxon object, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to count_distinct_values from class nbaR.GeoClient
#' @export
geo_count_distinct_values <- function(
                                      field = NULL,
                                      returnType = "data.frame",
                                      ...) {
  sc <- GeoClient$new()
  res <- sc$count_distinct_values(
    field,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_count_distinct_values_per_group
#' @title Count the distinct number of field values that exist per the given field to group by
#' @description This is a wrapper for the method \code{ count_distinct_values_per_group }
#' from class \code{ GeoClient}.
#' @details See also endpoint /getDistinctValues. See also getDistinctValuesPerGroup
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param group name of field in the geo area object to group by, type:
#' @param field name of field in the geo area object, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to count_distinct_values_per_group from class nbaR.GeoClient
#' @export
geo_count_distinct_values_per_group <- function(
                                                group = NULL,
                                                field = NULL,
                                                returnType = "data.frame",
                                                ...) {
  sc <- GeoClient$new()
  res <- sc$count_distinct_values_per_group(
    group,
    field,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_find
#' @title Find a GEO area by id
#' @description This is a wrapper for the method \code{ find }
#' from class \code{ GeoClient}.
#' @details Returns a GEO object containing a GEO json polygon
#' @family nbaR.GeoClient-wrappers
#' @return list or data.frame, as specified by \code{returnType}
#' @param id id of geo area, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to find from class nbaR.GeoClient
#' @export
geo_find <- function(
                     id = NULL,
                     returnType = "data.frame",
                     ...) {

  ## check returnType argument
  if (!returnType %in% c("list", "data.frame")) {
    stop("Invalid returnType argument. Must be 'data.frame' or 'list'")
  }

  sc <- GeoClient$new()
  res <- sc$find(
    id,

    ...
  )
  ## return simpler data structure for object response
  result <- .un_object(res, returnType = returnType)
  return(result)
}
#' @name geo_find_by_ids
#' @title Find geo areas by ids
#' @description This is a wrapper for the method \code{ find_by_ids }
#' from class \code{ GeoClient}.
#' @details Given multiple ids, returns a list of geo area objects
#' @family nbaR.GeoClient-wrappers
#' @return list or data.frame, as specified by \code{returnType}
#' @param ids ids of multiple geo areas, separated by comma, type: character
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to find_by_ids from class nbaR.GeoClient
#' @export
geo_find_by_ids <- function(
                            ids = NULL,
                            returnType = "data.frame",
                            ...) {

  ## check returnType argument
  if (!returnType %in% c("list", "data.frame")) {
    stop("Invalid returnType argument. Must be 'data.frame' or 'list'")
  }

  sc <- GeoClient$new()
  res <- sc$find_by_ids(
    ids,

    ...
  )
  ## return simpler data structure for object response
  result <- .un_object(res, returnType = returnType)
  return(result)
}
#' @name geo_get_distinct_values
#' @title Get all different values that exist for a field
#' @description This is a wrapper for the method \code{ get_distinct_values }
#' from class \code{ GeoClient}.
#' @details A list of all fields for geo area documents can be retrieved with /metadata/getFieldInfo
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param field name of field in geo area object, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to get_distinct_values from class nbaR.GeoClient
#' @export
geo_get_distinct_values <- function(
                                    field = NULL,
                                    returnType = "data.frame",
                                    ...) {
  sc <- GeoClient$new()
  res <- sc$get_distinct_values(
    field,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_distinct_values_per_group
#' @title Get all distinct values (and their document count) for the field given divided per distinct value of the field to group by
#' @description This is a wrapper for the method \code{ get_distinct_values_per_group }
#' from class \code{ GeoClient}.
#' @details See also endpoint /getDistinctValues
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param group name of field in the geo area object to group by, type:
#' @param field name of field in the geo area object, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to get_distinct_values_per_group from class nbaR.GeoClient
#' @export
geo_get_distinct_values_per_group <- function(
                                              group = NULL,
                                              field = NULL,
                                              returnType = "data.frame",
                                              ...) {
  sc <- GeoClient$new()
  res <- sc$get_distinct_values_per_group(
    group,
    field,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_field_info
#' @title Returns extended information for each field of a specimen document
#' @description This is a wrapper for the method \code{ get_field_info }
#' from class \code{ GeoClient}.
#' @details Info consists of whether the fields is indexed, the ElasticSearch datatype and a list of allowed operators
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to get_field_info from class nbaR.GeoClient
#' @export
geo_get_field_info <- function(
                               returnType = "data.frame",
                               ...) {
  sc <- GeoClient$new()
  res <- sc$get_field_info(
    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_geo_json_for_locality
#' @title Retrieve a GeoJson object for a given locality
#' @description This is a wrapper for the method \code{ get_geo_json_for_locality }
#' from class \code{ GeoClient}.
#' @details Returns a GeoJson polygon
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param locality , type:
#' @param ... additional parameters passed to get_geo_json_for_locality from class nbaR.GeoClient
#' @export
geo_get_geo_json_for_locality <- function(
                                          locality = NULL,
                                          ...) {
  sc <- GeoClient$new()
  res <- sc$get_geo_json_for_locality(
    locality,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_paths
#' @title Returns the full path of all fields within a document
#' @description This is a wrapper for the method \code{ get_paths }
#' from class \code{ GeoClient}.
#' @details See also metadata/getFieldInfo for all allowed operators per field
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to get_paths from class nbaR.GeoClient
#' @export
geo_get_paths <- function(
                          returnType = "data.frame",
                          ...) {
  sc <- GeoClient$new()
  res <- sc$get_paths(
    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_setting
#' @title Get the value of an NBA setting
#' @description This is a wrapper for the method \code{ get_setting }
#' from class \code{ GeoClient}.
#' @details All settings can be queried with /metadata/getSettings
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param name name of setting, type:
#' @param ... additional parameters passed to get_setting from class nbaR.GeoClient
#' @export
geo_get_setting <- function(
                            name = NULL,
                            ...) {
  sc <- GeoClient$new()
  res <- sc$get_setting(
    name,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_get_settings
#' @title List all publicly available configuration settings for the NBA
#' @description This is a wrapper for the method \code{ get_settings }
#' from class \code{ GeoClient}.
#' @details The value of a specific setting can be queried with metadata/getSetting/{name}
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to get_settings from class nbaR.GeoClient
#' @export
geo_get_settings <- function(
                             returnType = "data.frame",
                             ...) {
  sc <- GeoClient$new()
  res <- sc$get_settings(
    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_is_operator_allowed
#' @title Checks if a given operator is allowed for a given field
#' @description This is a wrapper for the method \code{ is_operator_allowed }
#' from class \code{ GeoClient}.
#' @details See also metadata/getFieldInfo
#' @family nbaR.GeoClient-wrappers
#' @return scalar
#' @param field Field in geo area document, type:
#' @param operator operator, type:
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to is_operator_allowed from class nbaR.GeoClient
#' @export
geo_is_operator_allowed <- function(
                                    field = NULL,
                                    operator = NULL,
                                    returnType = "data.frame",
                                    ...) {
  sc <- GeoClient$new()
  res <- sc$is_operator_allowed(
    field,
    operator,

    ...
  )
  result <- res$content
  return(result)
}
#' @name geo_query
#' @title Query for geo areas
#' @description This is a wrapper for the method \code{ query }
#' from class \code{ GeoClient}.
#' @details Query on searchable fields to retrieve matching geo areas
#' @family nbaR.GeoClient-wrappers
#' @return list or data.frame, as specified by \code{returnType}
#' @param queryParams Named list or vector with names being the fields to be queried and values being the values to match
#' @param returnType Either \code{list} or \code{data.frame} (default)
#' @param ... additional parameters passed to query from class nbaR.GeoClient
#' @export
geo_query <- function(
                      queryParams = list(),
                      returnType = "data.frame",
                      ...) {

  ## check returnType argument
  if (!returnType %in% c("list", "data.frame")) {
    stop("Invalid returnType argument. Must be 'data.frame' or 'list'")
  }

  sc <- GeoClient$new()
  res <- sc$query(
    queryParams = queryParams,
    ...
  )
  ## return simpler data structure for object response
  result <- .un_object(res, returnType = returnType)
  return(result)
}

#' @noRd
#' @param response Object of class Response
#' @param returnType either 'list' or 'data.frame'
#' Internal function to convert all (nested) objects
#' in a response object to lists or data frames
.un_object <- function(response, returnType = "data.frame") {
  l <- response$content

  ## Handle return objects of class QueryResult
  if (class(l)[1] == "QueryResult") {
    tmp <- list()
    for (r in l$resultSet) {
      tmp <- append(tmp, r$item)
    }
    l <- tmp
  }

  if (returnType == "data.frame") {
    if (!is.list(l)) {
      result <- as.data.frame(l$toList())
    } else {
      strs <- vapply(l, function(x) x$toJSONString(), FUN.VALUE = character(1))
      result <- jsonlite::fromJSON(
        paste("[", paste(strs, collapse = ","), "]")
      )
    }
  } else {
    ## Return type is list, coerce everything to list
    if (!is.list(l)) {
      result <- l$toList()
    } else {
      result <- list()
      for (i in seq_along(l)) {
        if (is.object(l[[i]])) {
          result[[i]] <- l[[i]]$toList()
        } else {
          result[[i]] <- l[[i]]
        }
      }
    }
  }
  return(result)
}
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.