1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
----------------------------------------------------------------------
--
--
-- Copyright (c) 2009-2010 Pierre Racine <pierre.racine@sbf.ulaval.ca>
--
----------------------------------------------------------------------
CREATE TYPE geomvalxy AS (
geom geometry,
val double precision,
x int,
y int
);
-----------------------------------------------------------------------
-- 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 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;
|