Search code examples
rpostgresqlpostgisrpostgresql

Can I import PostGIS raster data type into R by using the RPostgreSQL-package?


I have a PostgreSQL / PostGIS table with 30 rows (only 3 are shown) and 3 columns as follows (raster is a PostGIS data type) - It's the EFSA CAPRI data set btw, if somebody's fimilar with it:

enter image description here

// Can I import the raster data type from PostGIS into R with the help of the RPostgreSQL-package (see the code below) OR do I have to use the rgdal-package inevitably as described by @Jot eN?

require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "")
dbGetquery(con, "SELECT rid, rast, filename FROM schema.capri")

Importing it without transformation and St_AsText(rast) (which works for the geometry data type of PostGIS) don't work.


Solution

  • You have an answer on gis.stackexchange page - https://gis.stackexchange.com/a/118401/20955:

    library('raster')
    library('rgdal')
    
    dsn="PG:dbname='plots' host=localhost user='test' password='test' port=5432 schema='gisdata' table='map' mode=2"
    
    ras <- readGDAL(dsn) # Get your file as SpatialGridDataFrame
    ras2 <- raster(ras,1) # Convert the first Band to Raster
    plot(ras2)
    

    Additional info could be found here https://rpubs.com/dgolicher/6373