R/mr_features_get.R

Defines functions mr_features_get

Documented in mr_features_get

#' Get features
#'
#' @export
#' @param type (character) a region type, e.g., "MarineRegions:eez". required
#' @param featureID (character) a feature ID. required
#' @param maxFeatures (integer) Number of features. Default: 100
#' @param format (character) output format, see Details for allowed options.
#' Default: json
#' @param path (character) required when `format="SHAPE-ZIP"`,
#' otherwise, ignored
#' @param version (character) either 1.0.0 or 2.0.0 (default). In v1.0.0, the
#' coordinates are in format y,x (long,lat), while in 2.0.0 the coordinates
#' are in format x,y (lat,long)
#' @param ... Curl options passed on to [httr::GET()]
#'
#' @return depends on the `format` option used, usually a text string
#'
#' @details Allowed options for the `format` parameter:
#' - `text/xml; subtype=gml/3.2`
#' - `GML2`
#' - `KML`
#' - `SHAPE-ZIP`
#' - `application/gml+xml; version=3.2`
#' - `application/json`
#' - `application/vnd.google-earth.kml xml`
#' - `application/vnd.google-earth.kml+xml`
#' - `csv`
#' - `gml3`
#' - `gml32`
#' - `json`
#' - `text/xml; subtype=gml/2.1.2`
#' - `text/xml; subtype=gml/3.1.1`
#'
#' @examples \dontrun{
#' # json by default
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3")
#' # csv
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3",
#'   format = "csv")
#' # KML
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3",
#'   format = "KML")
#'
#' # if you want SHAPE-ZIP, give a file path
#' # FIXME - shape files not working right now
#' # file <- tempfile(fileext = ".zip")
#' # mr_features_get(type = "MarineRegions:eez", featureID = "eez.3",
#' #   format = "SHAPE-ZIP", path = file)
#' # file.exists(file)
#' # unlink(file)
#'
#' # glm32
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3",
#'   format = "gml32")
#'
#' # version parameter
#' ## notice the reversed coordinates
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3")
#' mr_features_get(type = "MarineRegions:eez", featureID = "eez.3",
#'   version = "1.0.0")
#' }
mr_features_get <- function(type, featureID, maxFeatures = 100,
                            format = "json", path = NULL, version = "2.0.0",
                            ...) {

  if (!format %in% names(mime_map)) {
    stop("format ", format, " not in acceptable set, see help file",
         call. = FALSE)
  }
  if (format %in% c('SHAPE-ZIP') && is.null(path)) {
    stop("if you specify 'SHAPE-ZIP' format, you must give a file path",
         call. = FALSE)
  }
  if (!version %in% c('1.0.0', '2.0.0')) {
    stop("version must be one of '1.0.0' or '2.0.0'",
         call. = FALSE)
  }
  args <- list(typeNames = type, maxFeatures = maxFeatures,
               featureID = featureID, service = 'wfs',
               request = 'GetFeature', version = version,
               outputFormat = format)
  getter2(url = "http://geo.vliz.be/geoserver/MarineRegions/wfs",
         args, format = mime_map[[format]], path = path, ...)
}

mime_map <- list(
  `text/xml; subtype=gml/3.2` = 'text/xml; subtype=gml/3.2',
  `GML2` = 'text/xml; subtype=gml/2.1.2',
  `KML` = 'application/vnd.google-earth.kml+xml',
  `SHAPE-ZIP` = 'application/zip',
  `application/gml+xml; version=3.2` = 'text/xml; subtype=gml/3.2',
  `application/json` = 'application/json',
  `application/vnd.google-earth.kml xml` = 'application/vnd.google-earth.kml+xml',
  `application/vnd.google-earth.kml+xml` = 'application/vnd.google-earth.kml+xml',
  `csv` = 'text/csv;charset=UTF-8',
  `gml3` = 'text/xml;subtype=gml/3.1.1',
  `gml32` = 'application/gml+xml;version=3.2',
  `json` = "application/json;charset=UTF-8",
  `text/xml; subtype=gml/2.1.2` = 'text/xml; subtype=gml/2.1.2',
  `text/xml; subtype=gml/3.1.1` = 'application/xml'
)
ropenscilabs/mregions documentation built on Feb. 28, 2024, 9:21 p.m.