Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a work in progress, any comments and suggestions are welcome!
2. Reading coordinate information from text files (coming up..)
3. Writing shapefiles (coming up..)
3. [Getting data from MS Access database] (ReadingDataIntoR/GetDataFromMSAccess.R)
4. Getting data from PostgreSQL database (coming up..)
4. Getting data from PostgreSQL database (ReadingDataIntoR/GetDataFromPostGIS.R)

### Data processing and spatial analysis in R

Expand Down
62 changes: 62 additions & 0 deletions ReadingDataIntoR/GetDataFromPostGIS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#####################
# Read PostGIS data to R
#####################
#
# R script for reading PostGIS data for further processing
# Ideas and info from:
# - https://github.com/mablab/rpostgis
# - https://cran.r-project.org/web/packages/rpostgis/rpostgis.pdf
# - https://cran.r-project.org/web/packages/RPostgreSQL/RPostgreSQL.pdf
# - https://cran.r-project.org/web/packages/sp/sp.pdf

##----------------
## PACKAGES
##----------------

#Install rpostgis and sp if needed and required dependencies
#install.packages("rpostgis", "sp")

#Load rpostgis
library(rpostgis)

#Load sp (Classes and Methods for Spatial Data)
library(sp)


##---------------
## CONNECT TO POSTGIS DATABASE
##---------------

# Create connection (in the example you have a PostgreSQL server running on localhost and it has the database with the name osmtm)
#Parameters
#drv should be string "PostgreSQL"
#dbname is name of the database
#host is the DNS name of the host that runs the PostgreSQL server
#port is the port where the PostgreSQL server is listening in
#user is the username that has access to the database
#password is the password for the user
conn <- dbConnect("PostgreSQL",dbname='osmtm',host='localhost',port='5432',user='postgres',password='postgres')

##---------------
## READ DATA FROM THE POSTGIS DATABASE
##---------------

#read data with pgGetGeom from the database
#Parameters
#conn is the connection created above
#name is a vector that has the schema name and table name elements
#geom is the name of the geometry column in the table
#other.cols is a vector that contains names of table columns to retrieve besides the geometry column
tasks <- pgGetGeom(conn, name=c("public","task"), geom = "geometry", other.cols=c("project_id","date"))

#view tasks as a string:
str(tasks)

#Quick visualization:
plot(tasks)

##---------------
## DISCONNECT FROM THE POSTGIS DATABASE
##---------------

dbDisconnect(conn)