R/as_feature.R

Defines functions as_feature

Documented in as_feature

#' Convert a FeatureCollection to a Feature
#'
#' @export
#' @param x A [data-FeatureCollection].
#' @details If there are more than one feature within the featurecollection,
#' each feature is split out into a separate feature, returned in a list.
#' Each feature is assigned a class matching it's GeoJSON data type
#' (e.g., point, polygon, linestring).
#' @seealso [as.feature] , which is similarly named, but has a different
#' purpose
#' @examples
#' as_feature(lawn_random())
#' # as_feature(lawn_random("polygons"))
as_feature <- function(x) {
  if (inherits(x, "featurecollection")) {
    ct$eval(sprintf("var x = %s;", convert(x)))
    tmp <- ct$get("x.features", simplifyVector = FALSE)
    tmp <- lapply(tmp, function(z) {
      structure(z, class = tolower(z$geometry$type))
    })
    if (length(tmp) == 1) {
      tmp[[1]]
    } else {
      tmp
    }
  } else {
    x
  }
}

Try the lawn package in your browser

Any scripts or data that you put into this service are public.

lawn documentation built on Jan. 6, 2021, 5:07 p.m.