I am trying to clip a raster so that the clipped raster would have the same minimum and maximum values for the color band as the original raster.

For example if i clip the raster with this command:

create table clipped AS SELECT ST_Clip(rast,ST_MakePolygon(ST_GeomFromText ('LINESTRING(424920 7370964, 424920 7371476,425432 7371476,425432  7370964,424920 7370964) ')) ) FROM original;

I get the following result:

Picture from QGIS

So the clipped raster has band minimum value as 0 and maximum value as 20. Instead of that, i want it to be exactly same as the 'original' raster: from 0 to 33. In other words, I want the color of the clipped raster to be same as it is in that area in the 'original' raster.

I am new for PostGIS, but i have tried to find an answer for a while to this question but i haven't found anything. I have tried the functions like ST_Reclass but with no success. What is the proper way to do this with PostGIS?

  • Just copy the same style and you will see the same colors.
    – WKT
    Commented Apr 8, 2016 at 15:42
  • Thank you for your reply, but how do you do that in PostGIS? I want to do it in PostGIS query, not in QGIS. That picture from QGIS-editor is just for visualizing the problem.
    – snuffkin
    Commented Apr 8, 2016 at 19:59
  • ST_Clip does not alter the values. The style (ramp) used in QGIS let you believe that. Try to use ST_Value in a SQL query to check.
    – WKT
    Commented Apr 8, 2016 at 20:03
  • Ok i noticed that i did not specify my problem good enough.Yes, but in my case after the clipping i want to make a picture from the clipped raster (with ST_AsPNG and ST_ColorMap) and analyze the picture band color values (black to white). The problem is that after making the png, the picture is just like in that QGIS-picture example i posted above. I want the band colors to match the 'orginal' band colors, because i want analyze the png.
    – snuffkin
    Commented Apr 8, 2016 at 20:29

1 Answer 1


You will have to ST_ReClass() the values of your raster before passing it to ST_AsPNG() so that it looks the same as the first raster. This will not affect the values stored in the raster in the database but will definitely produce a very different image.

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.