From: Pierre Racine Date: Thu, 1 Dec 2011 16:14:16 +0000 (+0000) Subject: Ticket 1342. Integrate ST_PixelAsPolygons into rtpostgis.sql X-Git-Tag: 2.0.0alpha1~581 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=39ee83c51d855e9039de66fa8b2daf10d83dc0a3;p=postgis Ticket 1342. Integrate ST_PixelAsPolygons into rtpostgis.sql git-svn-id: http://svn.osgeo.org/postgis/trunk@8287 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index cf022490c..fa5d8b1d5 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -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 -----------------------------------------------------------------------