]> granicus.if.org Git - postgis/commitdiff
Ticket 1342. Integrate ST_PixelAsPolygons into rtpostgis.sql
authorPierre Racine <Pierre.Racine@sbf.ulaval.ca>
Thu, 1 Dec 2011 16:14:16 +0000 (16:14 +0000)
committerPierre Racine <Pierre.Racine@sbf.ulaval.ca>
Thu, 1 Dec 2011 16:14:16 +0000 (16:14 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8287 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rtpostgis.sql.in.c

index cf022490c83524cbc4b57fac292bc7ac8d10019f..fa5d8b1d568e134607d8eefcdf257313fdde8464 100644 (file)
@@ -2448,6 +2448,55 @@ CREATE OR REPLACE FUNCTION st_pixelaspolygon(rast raster, x integer, y integer)
     $$ SELECT st_pixelaspolygon($1, 1, $2, $3) $$
     LANGUAGE SQL IMMUTABLE STRICT;
 
+-----------------------------------------------------------------------
+-- ST_PixelAsPolygons
+-- Return all the pixels of a raster as a geomval record
+-- Should be called like this:
+-- SELECT (gv).geom, (gv).val FROM (SELECT ST_PixelAsPolygons(rast) gv FROM mytable) foo
+-----------------------------------------------------------------------
+CREATE TYPE geomvalxy AS (
+    geom geometry,
+    val double precision,
+    x int,
+    y int
+);
+
+CREATE OR REPLACE FUNCTION ST_PixelAsPolygons(rast raster, band integer)
+    RETURNS SETOF geomvalxy AS
+    $$
+    DECLARE
+        rast alias for $1;
+        w integer;
+        h integer;
+        x integer;
+        y integer;
+        result geomvalxy;
+    BEGIN
+        IF rast IS NOT NULL THEN
+            IF ST_HasNoBand(rast, band) THEN
+                RAISE NOTICE 'Raster do not have band %. Returning null', band;
+            ELSE
+                SELECT ST_Width(rast), ST_Height(rast)
+                INTO w, h;
+                FOR x IN 1..w LOOP
+                    FOR y IN 1..h LOOP
+                        SELECT ST_PixelAsPolygon(rast, band, x, y), ST_Value(rast, band, x, y), x, y INTO result;
+                        RETURN NEXT result;
+                    END LOOP;
+                END LOOP;
+            END IF;
+        END IF;
+        RETURN;
+    END;
+    $$
+    LANGUAGE 'plpgsql';
+
+CREATE FUNCTION ST_PixelAsPolygons(raster) RETURNS SETOF geomvalxy AS
+       $$
+               SELECT ST_PixelAsPolygons($1, 1);
+       $$
+       LANGUAGE SQL;
+
 -----------------------------------------------------------------------
 -- Raster Utility Functions
 -----------------------------------------------------------------------