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"

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?


It sounds to me like maybe rgdal relies on raster_columns metadata for info. http://postgis.net/docs/manual-2.2/RT_AddRasterConstraints.html

Try running this:

SELECT AddRasterConstraints('clippingtable'::name, 'st_union'::name);

I should add that I'm guessing your raster column is called st_union since you didn't explicitly name it with

As rast

You should explicitly name it to keep your sanity.

Even after you do this, it's possible rgdal will still complain since as I recall early versions of GDAL would not work with non-rectangular, non-even blocked. rasters which a clipped version is likely to be

  • Thanks for your reply. I added the column name in my raster object, and the output in my R program was different. See: PG:dbname=testdb host=localhost table=clippingtable has GDAL driver PostGISRaster and has 0 rows and 0 columns Error in validObject(.Object) : invalid class “GridTopology” object: cells.dim has incorrect dimension In addition: Warning messages: 1: In dim(x) : no bands in dataset 2: In dim(x) : no bands in dataset I didn't understand well the objective of the function AddRasterConstraints in my case, should I use it? Thanks
    – mkdev
    Dec 21 '15 at 18:21
  • Well as a recall gdal tries to query the raster_columns view. That view reads the constraints from the table. Calling that function will add constraints like dim sizes to the table.
    – LR1234567
    Dec 22 '15 at 2:33
  • Thank you so much. The constrains was added with the AddRasterConstraints function. Problem solved.
    – mkdev
    Dec 22 '15 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.