Skip to content

Instantly share code, notes, and snippets.

@dholstius
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dholstius/00afc2e5cf6b9628dec8 to your computer and use it in GitHub Desktop.
Save dholstius/00afc2e5cf6b9628dec8 to your computer and use it in GitHub Desktop.
Write a Spatial* object to a GeoJSON file
write_geojson <- function (
spobj,
dsn = getwd(),
layer = deparse(substitute(spobj)),
pretty = TRUE,
digits = 6,
...,
quiet = FALSE
) {
require(rgdal)
require(jsonlite)
tmpfn <- tempfile()
writeOGR(spobj, dsn = tmpfn, layer = "", driver = "GeoJSON")
obj <- jsonlite::fromJSON(tmpfn)
outfile <- file.path(dsn, str_c(layer, ".geojson"))
cat(jsonlite::toJSON(obj, pretty = pretty, digits = digits, ...), file = outfile)
return(invisible(outfile))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment