R/point.R

Defines functions print.geopoint point.character point.default point

Documented in point

#' point class
#'
#' @export
#' @param x input
#' @examples
#' x <- '{ "type": "Point", "coordinates": [100.0, 0.0] }'
#' (y <- point(x))
#' geo_type(y)
#' geo_pretty(y)
#' geo_write(y, f <- tempfile(fileext = ".geojson"))
#' jsonlite::fromJSON(f, FALSE)
#' unlink(f)
#'
#' # add to a data.frame
#' library('tibble')
#' tibble(a = 1:5, b = list(y))
#'
#' # as.geojson coercion
#' as.geojson(x)
point <- function(x) {
  UseMethod("point")
}

#' @export
point.default <- function(x) {
  stop("no method for ", class(x)[1L], call. = FALSE)
}

#' @export
point.character <- function(x) {
  json_val(x)
  hint_geojson(x)
  x <- as_x("Point", x)
  verify_names(x, c("type", "coordinates"))
  verify_class(x, "Point")
  hint_geojson(x)
  structure(x,
            class = c("geopoint", "geojson"),
            coords = cchar(jqr::jq(unclass(x), ".coordinates")))
}

#' @export
print.geopoint <- function(x, ...) {
  cat("<Point>", "\n")
  cat("  coordinates: ", attr(x, "coords"), "\n")
}

Try the geojson package in your browser

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

geojson documentation built on Aug. 8, 2023, 5:11 p.m.