Skip to content

Instantly share code, notes, and snippets.

@geotheory
Forked from zross/readwrite_sf.R
Created April 4, 2017 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotheory/33a7f8818fcefaf15a8929f4a1b83fa6 to your computer and use it in GitHub Desktop.
Save geotheory/33a7f8818fcefaf15a8929f4a1b83fa6 to your computer and use it in GitHub Desktop.
Read and write from R to postgresql/postgis
library(sf)
library(RPostgreSQL)
dat <- data.frame(ID = 1:2,
long = c(-74.003234, -73.983317),
lat = c(40.731863, 40.722684))
dat_sf <- st_as_sf(dat, coords = c("long", "lat"),
crs = 4326,
agr = "identity")
con <- dbConnect(PostgreSQL(), dbname = "mydb", host = "localhost")
st_write_db(con, dat_sf, "mydata")
dbListTables(con) # [1] "spatial_ref_sys" "mydata"
new_dat <- st_read_db(con, "mydata")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment