Skip to content

Instantly share code, notes, and snippets.

@sdtaylor
Last active December 17, 2020 22:59
Show Gist options
  • Save sdtaylor/c4fb3c60a34e61c0a476fad9124cfc8d to your computer and use it in GitHub Desktop.
Save sdtaylor/c4fb3c60a34e61c0a476fad9124cfc8d to your computer and use it in GitHub Desktop.
csv to sf object
library(tidyverse)
library(sf)
# where lon, lat are columns for longitude, latitude, respectively.
# Any other columns are retained in the sf data.frame.
read_csv('site_list.csv') %>%
st_as_sf(coords = c('lon','lat'), crs=4326)
# If you want to take an SF point object back to a csv
# note the coordinates will become columns X,Y
# the st_transform line is only needed if the crs
# is something besides lat/lon (like utm).
sf_point_object %>%
cbind(., st_coordinates(.)) %>%
st_transform(crs=4326) %>%
st_set_geometry(NULL) %>%
write_csv('my_file.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment