Skip to content

Instantly share code, notes, and snippets.

@mhweber
Created December 2, 2016 19:27
Show Gist options
  • Select an option

  • Save mhweber/09cd7e3248bbff8cea99b013ce0fe1c7 to your computer and use it in GitHub Desktop.

Select an option

Save mhweber/09cd7e3248bbff8cea99b013ce0fe1c7 to your computer and use it in GitHub Desktop.
Figured out reading my PostGIS data directly in R using Rpostgis package
# example of reading regular data table from Postgres using RPostgresSQL library
library(RPostgreSQL)
con <- dbConnect(PostgreSQL(), host="localhost", port="5432", user="username", password="password", dbname="db_name")
result <- dbGetQuery(con, statement = "SELECT * FROM table1, table2 WHERE table1.key = table2.key")
head(result)
# example of reading spatial data from PostGIS using rpostgis library
library(rpostgis)
conn <- dbConnect(PostgreSQL(), host="localhost", port="5432", user="postgres", password="postgres", dbname="sp_db")
dbSchema(conn, name = 'sp_db')
dbTableInfo(conn, "sp_table")
my_spdf<-pgGetGeom(conn, name="sp_table", geom = "wkb_geometry")
head(my_spdf@data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment