Search code examples
rpostgisrastershapefileclipping

Cut raster objects in PostGIS


I'm trying to cutting a raster object (from a PostGIS table) using a shapefile with reference, extracting raster data from a polygon area. My objective to work with the clipping raster into R.

I'm cutting in this way:

CREATE TABLE clippingtable as (SELECT (ST_Union(ST_Clip(raster.rast, ST_Transform(polygon.geom, ST_SRID(raster.rast) ) ) ) )
  FROM originalraster as raster, originalshape as polygon
  WHERE ST_Intersects(raster.rast, polygon.geom))

The gdalinfo output is:

$gdalinfo "PG:host=localhost port=5432 dbname='testdb' schema='public' table=clippingtable"
ERROR 1: Error retrieving raster metadata
gdalinfo failed - unable to open 'PG:host=localhost port=5432 dbname='testdb' schema='public' table=clippingtable'.

In R I'm using these functions:

dsn="PG:dbname=testdb host=localhost port=5432 table=clippingtable"
rgdal::GDALinfo(dsn)
readGDAL(dsn)

The output is: "Error in .local(.Object, ...) : Error retrieving raster metadata"

In original table (before cutting) I can work normally with my raster.

Any idea what I'm doing wrong?

Thank you.


Solution

  • Its finished. I needed to create constraints to load spacial information in the table column.

    I used the PostGIS function "AddRasterConstraints" to solve the problem

    Function documentation: http://postgis.net/docs/manual-2.2/RT_AddRasterConstraints.html