Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write CSV with coordinates using R package sf

Tags:

r

csv

spatial

sf

I'm wondering if there is an easy way to write a CSV of a point sf object (sf R package) that includes the coordinates.

You can use st_write(input, "output.csv") and it will write a CSV without coordinates. My hack for writing a file with coordinates is:

coords <- st_coordinates(input)
input_dat <- input %>% st_set_geometry(., NULL)
input_dat <- cbind(input_dat, coords)

But it seems there must be a simpler way.

As requested, here is the setup for the code above:

input <- data.frame(ID = 1:10, longitude = rnorm(10), latitude = rnorm(10))
input <- st_as_sf(input, coords = c("longitude", "latitude"))
like image 330
ZRoss Avatar asked Aug 04 '17 20:08

ZRoss


1 Answers

I was sent to the solution by Jakub Nowosad. He pointed me to this github issue which was solved by Etienne B. Racine.

Apparently GDAL has a flag that allows you to include the coordinates. So:

st_write(input, "output.csv", layer_options = "GEOMETRY=AS_XY")
like image 110
ZRoss Avatar answered Sep 19 '22 12:09

ZRoss