Is there a direct way to load a CSV as an sf? I've looked through the sf documentation and nothing jumps out at me, currently using:

my_csv <- read.csv('my_data.csv', header = TRUE)

my_sp <- sp::SpatialPointsDataFrame(coords = data.frame(my_csv$y, 
my_csv$x), data = data.frame(my_csv$data), proj4string = CRS(BNG))

my_sf <- sf::st_as_sf(my_sp)

When you read a CSV with no options the driver doesn't know where the coordinate columns are so returns a data frame:

> st_read("./pts.csv")
Reading layer `pts' from data source `/home/rowlings/Downloads/SO/csv/pts.csv' using driver `CSV'
  id     x     y
1  1  10.1   2.1
2  2   2.4  12.1
3  3   3.2   4.5
Warning message:
no simple feature geometries present: returning a data.frame or tbl_df 

So you need to pass a couple of options that end up telling the OGR driver where the coordinates are:

> st_read("./pts.csv", options=c("X_POSSIBLE_NAMES=x","Y_POSSIBLE_NAMES=y"))
Reading layer `pts' from data source `/home/rowlings/Downloads/SO/csv/pts.csv' using driver `CSV'
Simple feature collection with 3 features and 3 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 2.4 ymin: 2.1 xmax: 10.1 ymax: 12.1
epsg (SRID):    NA
proj4string:    NA

and there's your sf spatial object.

OGR CSV driver documentation is here: https://gdal.org/drv_csv.html

  • Who downvoted and why? This is still a perfectly correct and, I think, optimal solution to the problem. I don't mind downvotes with constructive comments. – Spacedman Mar 11 '20 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.