Hello all the PostGIS wizards!

I want to programmatically clip raster with bounding box which is overstepping the border of raster. Ok, i can do it with ST_Clip, but the problem is that ST_Clip only returns the area where is raster data. I want the data from the whole bounding box area and if there is no data it should be same as nodata value (in my case it would be just zero).

Ok here is a really simple example picture i made with MS Paint just for this post: enter image description here

In the st_clip i just use ST_MakePolygon for the use of clipping the raster with geometry:

ST_Clip(rast,ST_MakePolygon(ST_GeomFromText('LINESTRING(4920 70964,4920 71476,5432 71476,5432 70964,4920 70964)')),0)

So... Is there any way i can get also the 'empty area' into the clipped raster for example as a nodata value? Or is there a any way how i can get the wanted result?

I've been searching answer for this with no success so far.

1 Answer 1


This is not a clip. This is an overlay which can be done with ST_MapAlgebra().

Something like:

WITH targetrast AS (
    SELECT ST_AddBand(ST_MakeEmptyRaster(w, h, ulx, uly, pixelsize), '32BSI', -9999, -9999) rast
SELECT ST_MapAlgebra(a.rast, b.rast, '[rast1]', '32BSI', 'SECOND', NULL, '[rast1]') FROM yourrastertable a, targetrast b
  • 1
    Thanks! this is the way to do this. However is faced a problem with this: "rt_raster_iterator: The set of rasters provided (custom extent included, if appropriate) do not have the same alignment". What could be the problem? I already setted SRID to match but still same error message.
    – snuffkin
    Commented Aug 18, 2016 at 9:10
  • 1
    The pixels of the raster you create in the first part of the query must be aligned with the pixels from yourrastertable. So everything is in choosing the right parameters for ST_MakeEmptyRaster(). Commented Aug 18, 2016 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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