R/encode_locations.R

Defines functions encode_locations

encode_locations = function(x, single = FALSE) {

  if(class(x)[1] == "character") {
    encoded =
      x |>
      paste0(collapse = "|")
  }

  if(class(x)[1] == "numeric") {
    if(single) stopifnot(length(x) == 2)
    encoded =
      x[2:1] |>
      paste0(collapse = ",")
  }

  if(inherits(x, c("sfc", "sf"))) {
    x = sf::st_coordinates(x)
  }

  if(class(x)[1] == "matrix") {
    if(single) stopifnot(nrow(x) == 1)
    encoded =
      x[, c(2,1), drop = FALSE] |>
      apply(1, paste0, collapse = ",") |>
      paste0(collapse = "|")
  }

  return(encoded)

}

Try the mapsapi package in your browser

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

mapsapi documentation built on July 26, 2023, 5:20 p.m.