-
-
Save geotheory/33a7f8818fcefaf15a8929f4a1b83fa6 to your computer and use it in GitHub Desktop.
Read and write from R to postgresql/postgis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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