Skip to content

Instantly share code, notes, and snippets.

@dholstius
Last active Aug 29, 2015
Embed
What would you like to do?
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