R/Feature.r

# 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

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # Feature$new()
#'
#' @format
#' R6 class
#'
#' @title Feature Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for Feature objects.
#'
#'
#' @field crs  Crs
#'
#' @field bbox  list(numeric)
#'
#' @field properties  list
#'
#' @field geometry  list
#'
#' @field id  character
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor Feature object.
#'
#' }
#' \item{\code{$fromList(FeatureList)}}{
#'
#'   Create Feature object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of Feature.
#'
#' }
#' \item{\code{fromJSONString(FeatureJson)}}{
#'
#'   Create Feature object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of Feature.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
Feature <- R6::R6Class(
  "Feature",
  public = list(
    `crs` = NULL,
    `bbox` = NULL,
    `properties` = NULL,
    `geometry` = NULL,
    `id` = NULL,
    initialize = function(
                              `crs`,
                              `bbox`,
                              `properties`,
                              `geometry`,
                              `id`) {
      if (!missing(`crs`)) {
        stopifnot(R6::is.R6(`crs`))
        self[["crs"]] <- `crs`
      }
      if (!missing(`bbox`)) {
        stopifnot(
          is.list(`bbox`),
          length(`bbox`) != 0
        )
        lapply(
          `bbox`,
          function(x) stopifnot(is.character(x))
        )
        ## omit names as they should not be part of JSON representation
        self[["bbox"]] <- unname(`bbox`)
      }
      if (!missing(`properties`)) {
        self[["properties"]] <- `properties`
      }
      if (!missing(`geometry`)) {
        self[["geometry"]] <- `geometry`
      }
      if (!missing(`id`)) {
        stopifnot(
          is.character(`id`),
          length(`id`) == 1
        )
        self[["id"]] <- `id`
      }
    },
    toList = function() {
      FeatureList <- list()
      if (!is.null(self[["crs"]])) {
        FeatureList[["crs"]] <-
          self[["crs"]]$toList()
      }
      if (!is.null(self[["bbox"]])) {
        FeatureList[["bbox"]] <-
          self[["bbox"]]
      }
      if (!is.null(self[["properties"]])) {
        FeatureList[["properties"]] <-
          self[["properties"]]
      }
      if (!is.null(self[["geometry"]])) {
        FeatureList[["geometry"]] <-
          self[["geometry"]]
      }
      if (!is.null(self[["id"]])) {
        FeatureList[["id"]] <-
          self[["id"]]
      }
      ## omit empty nested lists in returned list
      FeatureList[vapply(
        FeatureList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(FeatureList,
                            typeMapping = NULL) {
      self[["crs"]] <- Crs$new()$fromList(
        FeatureList[["crs"]],
        typeMapping = typeMapping
      )
      self[["bbox"]] <-
        FeatureList[["bbox"]]
      self[["properties"]] <-
        FeatureList[["properties"]]
      self[["geometry"]] <-
        FeatureList[["geometry"]]
      self[["id"]] <-
        FeatureList[["id"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(FeatureJson,
                                  typeMapping = NULL) {
      FeatureList <- jsonlite::fromJSON(
        FeatureJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(FeatureList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<Feature>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$crs) == "environment") {
        cat("\tcrs:\tobject of class", paste0("<", class(self$crs)[1], ">"), "\n")
      }
      else if (typeof(self$crs) == "list") {
        cat("\tcrs:\tlist of length", length(self$crs), "\n")
      }
      else {
        cat("\tcrs:\t", self$crs, "\n")
      }
      if (typeof(self$bbox) == "environment") {
        cat("\tbbox:\tobject of class", paste0("<", class(self$bbox)[1], ">"), "\n")
      }
      else if (typeof(self$bbox) == "list") {
        cat("\tbbox:\tlist of length", length(self$bbox), "\n")
      }
      else {
        cat("\tbbox:\t", self$bbox, "\n")
      }
      if (typeof(self$properties) == "environment") {
        cat("\tproperties:\tobject of class", paste0("<", class(self$properties)[1], ">"), "\n")
      }
      else if (typeof(self$properties) == "list") {
        cat("\tproperties:\tlist of length", length(self$properties), "\n")
      }
      else {
        cat("\tproperties:\t", self$properties, "\n")
      }
      if (typeof(self$geometry) == "environment") {
        cat("\tgeometry:\tobject of class", paste0("<", class(self$geometry)[1], ">"), "\n")
      }
      else if (typeof(self$geometry) == "list") {
        cat("\tgeometry:\tlist of length", length(self$geometry), "\n")
      }
      else {
        cat("\tgeometry:\t", self$geometry, "\n")
      }
      if (typeof(self$id) == "environment") {
        cat("\tid:\tobject of class", paste0("<", class(self$id)[1], ">"), "\n")
      }
      else if (typeof(self$id) == "list") {
        cat("\tid:\tlist of length", length(self$id), "\n")
      }
      else {
        cat("\tid:\t", self$id, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.