ST_DumpAsPolygons — Returns a set of geomval (geom,val) rows, from a given raster band. If no band number is specified, band num defaults to 1.
setof geomval ST_DumpAsPolygons(raster  rast, integer  band_num=1, boolean  exclude_nodata_value=TRUE);
This is a set-returning function (SRF). It returns a set of geomval rows, formed by a geometry (geom) and a pixel band value (val). Each polygon is the union of all pixels for that band that have the same pixel value denoted by val.
ST_DumpAsPolygon is useful for polygonizing rasters. It is the reverse of a GROUP BY in that it creates new rows. For example it can be used to expand a single raster into multiple POLYGONS/MULTIPOLYGONS.
Changed 3.3.0, validation and fixing is disabled to improve performance. May result invalid geometries.
Availability: Requires GDAL 1.7 or higher.
| ![[Note]](../images/note.png)  | |
| If there is a no data value set for a band, pixels with that value will not be returned except in the case of exclude_nodata_value=false. | 
| ![[Note]](../images/note.png)  | |
| If you only care about count of pixels with a given value in a raster, it is faster to use ST_ValueCount. | 
| ![[Note]](../images/note.png)  | |
| This is different than ST_PixelAsPolygons where one geometry is returned for each pixel regardless of pixel value. | 
 -- this syntax requires PostgreSQL 9.3+
SELECT val, ST_AsText(geom) As geomwkt
FROM (
SELECT dp.*
FROM dummy_rast, LATERAL ST_DumpAsPolygons(rast) AS dp
WHERE rid = 2
) As foo
WHERE val BETWEEN 249 and 251
ORDER BY val;
 val |                                                       geomwkt
-----+--------------------------------------------------------------------------
 249 | POLYGON((3427927.95 5793243.95,3427927.95 5793243.85,3427928 5793243.85,
        3427928 5793243.95,3427927.95 5793243.95))
 250 | POLYGON((3427927.75 5793243.9,3427927.75 5793243.85,3427927.8 5793243.85,
        3427927.8 5793243.9,3427927.75 5793243.9))
 250 | POLYGON((3427927.8 5793243.8,3427927.8 5793243.75,3427927.85 5793243.75,
        3427927.85 5793243.8, 3427927.8 5793243.8))
 251 | POLYGON((3427927.75 5793243.85,3427927.75 5793243.8,3427927.8 5793243.8,
        3427927.8 5793243.85,3427927.75 5793243.85))
                    
      geomval, ST_AsRasterAgg, ST_Value, ST_Polygon, ST_ValueCount