From: Regina Obe Date: Fri, 18 Nov 2011 02:31:02 +0000 (+0000) Subject: ST_MapAlgebraFctNgb (and put in immuatable for other st_mapalgebras) X-Git-Tag: 2.0.0alpha1~690 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=51715284de0833daed1674f5d7693ae12cbba2ed;p=postgis ST_MapAlgebraFctNgb (and put in immuatable for other st_mapalgebras) git-svn-id: http://svn.osgeo.org/postgis/trunk@8170 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/doc/html/images/st_mapalgebrafctngb01.png b/doc/html/images/st_mapalgebrafctngb01.png new file mode 100644 index 000000000..5a70af9ac Binary files /dev/null and b/doc/html/images/st_mapalgebrafctngb01.png differ diff --git a/doc/html/images/st_mapalgebrafctngb02.png b/doc/html/images/st_mapalgebrafctngb02.png new file mode 100644 index 000000000..90163b3fc Binary files /dev/null and b/doc/html/images/st_mapalgebrafctngb02.png differ diff --git a/doc/reference_raster.xml b/doc/reference_raster.xml index b86a59f2d..6b7673c33 100644 --- a/doc/reference_raster.xml +++ b/doc/reference_raster.xml @@ -5712,7 +5712,7 @@ WITH mygeoms AS $$ BEGIN RETURN 0.0; END; $$ - LANGUAGE 'plpgsql'; + LANGUAGE 'plpgsql' IMMUTABLE; The userfunction is required to accept two arguments: a float value, and a variadic text array. The first argument is the value of an individual raster cell (regardless of the raster datatype), and the second argument indicates that all remaining parameters to shall be passed through to the userfunction. Passing a regprodedure argument to a SQL function requires the full function signature to be passed, then cast to a regprocedure type. To pass the above example PL/pgSQL function as an argument, the SQL for the argument is:'simple_function(float,text[])'::regprocedureNote that the argument contains the name of the function, the types of the function arguments, quotes around the name and argument types, and a cast to a regprocedure. @@ -5738,7 +5738,7 @@ BEGIN RETURN pixel::integer % 2; END; $$ -LANGUAGE 'plpgsql'; +LANGUAGE 'plpgsql' IMMUTABLE; UPDATE dummy_rast SET map_rast = ST_MapAlgebraFct(rast,NULL,'mod_fct(float,text[])'::regprocedure) WHERE rid = 2; @@ -6050,6 +6050,148 @@ SELECT ST_MapAlgebraFct(m1.rast, 1, m1.rast,3 , + + + ST_MapAlgebraFctNgb + + 1-band version: Map Algebra Nearest Neighbor using user-defined PostgreSQL function. Return a raster which values are the result of a PLPGSQL user function involving a neighborhood of values from the input raster band. + + + + + + raster ST_MapAlgebraFctNgb + raster rast + integer band + integer ngbwidth + integer ngbheight + regprocedure userfunction + text nodatamode + text[] VARIADIC args + + + + + + + Description + + (one raster version) Return a raster which values + are the result of a PLPGSQL user function involving a + neighborhood of values from the input raster band. + + + + rast + Raster on which the user function is evaluated. + + + band + Band number of the raster to be evaluated. Default to 1. + + + pixeltype + The resulting pixel type of the output raster. Must be one listed in or left out or set to NULL. If not passed in or set to NULL, will default to the pixeltype of the rast. Results are truncated if they are larger than what is allowed for the pixeltype + + + ngbwidth + The width of the neighborhood, in cells. + + + ngbheight + The height of the neighborhood, in cells. + + + userfunction + PLPGSQL user function to apply to neighborhod pixels. + + + args + Arguments to pass into the user function. + + + Availability: 2.0.0 + + + + + + + Examples + +-- +-- A simple 'callback' user function that sums up all the values in a neighborhood. +-- +CREATE OR REPLACE FUNCTION rast_avg(matrix float[][], nodatamode text, variadic args text[]) + RETURNS float AS + $$ + DECLARE + _matrix float[][]; + x1 integer; + x2 integer; + y1 integer; + y2 integer; + sum float; + BEGIN + _matrix := matrix; + sum := 0; + FOR x in array_lower(matrix, 1)..array_upper(matrix, 1) LOOP + FOR y in array_lower(matrix, 2)..array_upper(matrix, 2) LOOP + IF _matrix[x][y] IS NULL THEN + IF nodatamode = 'ignore' THEN + _matrix[x][y] := 0; + ELSE + _matrix[x][y] := nodatamode::float; + END IF; + END IF; + sum := sum + _matrix[x][y]; + END LOOP; + END LOOP; + RETURN (sum*1.0/(array_upper(matrix,1)*array_upper(matrix,2) ))::integer ; + END; + $$ + LANGUAGE 'plpgsql' IMMUTABLE COST 1000; + +-- now we apply to our rescaled katrina averaging pixels within 2 pixels of each other -- +SELECT ST_MapAlgebraFctNgb(rast, 1, '8BUI', 2, 2, 'rast_avg(float[][], text, text[])'::regprocedure, 'NULL', NULL) As neared + FROM katrinas_rescaled + limit 1; + + + + + + + + + + + + First band of our raster + + + + + + + + new raster after averaging pixels withing 2x2 pixels of each other + + + + + + + + + + + See Also + + + + + ST_Polygon