Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to save to geojson file #96

Open
mem48 opened this issue Jul 8, 2022 · 2 comments
Open

Add function to save to geojson file #96

mem48 opened this issue Jul 8, 2022 · 2 comments

Comments

@mem48
Copy link

mem48 commented Jul 8, 2022

Unless I've missed it, why there is no easy way to save an sf object to disk as a geojson file?

I created a little test function

write_geojson <- function(x, path){
  x <- geojsonsf::sf_geojson(x)
  filecon <- file(path)
  writeLines(x, filecon)
  close(filecon)
}

And it was 7x faster than sf::st_write. The output wasn't identical as sf writes each feature to a new line in the file. But I was able to st_read it back in and it was identical to the original sf data frame.

I'm sure this function needs some refining, but it would be a really useful feature for geojsonsf

Thanks.

@dcooley
Copy link
Collaborator

dcooley commented Jul 10, 2022

why there is no easy way to save an sf object to disk as geojson file?

sf::st_write() is the easy way to save an sf object to disk as geojosn


If this was to become part of {geojosnsf} it would need to be implemented in C++, basically the opposite of read_geojson.cpp, using FileWriteStream

I don't have any time to implement this, but woud accept a PR if you're willing to write it.

@mem48
Copy link
Author

mem48 commented Jul 20, 2022

My C++ is not great so I probably couldn't contribute in that way.

But in case anybody else comes across this issue; the problem with sf::st_write seems to be converting the sf object to geojson rather than the writing to disk. So sf_geojson provides a worthwhile speed boost on large datasets.

The best I could come up with is:

write_geojson <- function(x, path){
  headder <- paste0('{\n"type":"FeatureCollection",\n"name": "',deparse(substitute(x)),'",')
  if(sf::st_is_longlat(x)){
    headder <- paste0(headder,'"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },\n')
  }
  headder <- paste0(headder,'"features":[')
  footer <- ']\n}'
  commas <- c(rep(',', nrow(x) - 1),'')
  x <- c(headder, 
         paste0(geojsonsf::sf_geojson(x, atomise = TRUE),commas),
         footer)
  data.table::fwrite(list(x), path, quote  = FALSE)
}

This writes geojson that is identical to st_write at least for my test cases and saved hours on some large datasets I was working on. It uses data.table for writing to disk, but that could probably be replaced with a base R solution as the main benefit is from sf_geojson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants