From b53ce2c834516dd64051c226c9414d5a5b3954cd Mon Sep 17 00:00:00 2001 From: Regina Obe Date: Sat, 10 Dec 2011 03:12:06 +0000 Subject: [PATCH] ST_AddBand version that takes an array of rasters git-svn-id: http://svn.osgeo.org/postgis/trunk@8343 b70326c6-7e19-0410-871a-916f4a2858ee --- doc/reference_raster.xml | 22 +++++++++++++++++++++- raster/rt_pg/rtpostgis.sql.in.c | 22 ++++++++++++++++++++++ raster/test/regress/rt_addband.sql | 2 ++ raster/test/regress/rt_addband_expected | 1 + 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/doc/reference_raster.xml b/doc/reference_raster.xml index 505e9b966..c5d08fcba 100644 --- a/doc/reference_raster.xml +++ b/doc/reference_raster.xml @@ -1091,6 +1091,13 @@ WHERE short_name = 'GTiff') As g; integer fromband=1 integer torastindex=at_end + + + raster ST_AddBand + raster torast + raster[] fromrasts + integer fromband=1 + @@ -1105,10 +1112,12 @@ WHERE short_name = 'GTiff') As g; is specified, then the initial value is set to the highest value allowed by the pixel type. The last version add the fromband from fromrast raster to torast in position torastindex. + For the version that takes an array of bands if torast is NULL, then the fromband band of each raster in the array + is accumulated into a new raster - Examples + Examples: Single Add Band versions -- Add another band of type 8 bit unsigned integer with pixels initialized to 200 UPDATE dummy_rast @@ -1142,6 +1151,17 @@ FROM (SELECT ST_MetaData(rast) As rmd + + + Examples: Multi-Band versions + + -- Aggregate a bunch of rasters 1st bands into a single band rast +-- not the rasters must be aligned. +SELECT ST_AddBand(NULL, array_agg(rast), 1 ) As rast + FROM some_rast_table; + + + See Also diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 0d08998b4..07624ebf3 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -220,6 +220,28 @@ CREATE OR REPLACE FUNCTION st_addband(torast raster, fromrast raster, fromband i RETURNS RASTER AS 'MODULE_PATHNAME', 'RASTER_copyband' LANGUAGE 'C' IMMUTABLE; + +-- Variant that adds multiple raster bands in one array call -- +-- If null is passed in for the torast, then array of rasts is accumulated. +CREATE OR REPLACE FUNCTION ST_AddBand(torast raster, fromrasts raster[], fromband integer DEFAULT 1) + RETURNS raster + AS $$ + DECLARE var_result raster := torast; + var_num integer := array_upper(fromrasts,1); + var_i integer := 1; + BEGIN + IF torast IS NULL AND var_num > 0 THEN + var_result := ST_Band(fromrasts[1],fromband); + var_i := 2; + END IF; + WHILE var_i <= var_num LOOP + var_result := ST_AddBand(var_result, fromrasts[var_i], 1); + var_i := var_i + 1; + END LOOP; + + RETURN var_result; + END; +$$ LANGUAGE 'plpgsql'; ----------------------------------------------------------------------- -- Constructor ST_Band diff --git a/raster/test/regress/rt_addband.sql b/raster/test/regress/rt_addband.sql index c5a0210a6..0f961b651 100644 --- a/raster/test/regress/rt_addband.sql +++ b/raster/test/regress/rt_addband.sql @@ -111,3 +111,5 @@ SELECT St_Value(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1 SELECT St_Value(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0,-1), 1, '64BF', 1234.5678, NULL), ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1), 1, 1), 3, 3); SELECT St_Value(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0,-1), 1, '64BF', 1234.5678, NULL), ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1), 1), 3, 3); SELECT St_Value(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0,-1), 1, '64BF', 1234.5678, NULL), ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1)), 3, 3); +-- array version test +SELECT (ST_DumpAsPolygons(newrast,3)).val As b3val FROM (SELECT ST_AddBand(NULL, array_agg(rast)) AS newrast FROM (SELECT ST_AsRaster(ST_Buffer(ST_Point(10,10), 34),200,200, '8BUI',i*30) As rast FROM generate_series(1,3) As i ) As foo ) As foofoo; diff --git a/raster/test/regress/rt_addband_expected b/raster/test/regress/rt_addband_expected index a8891ff30..2f616f7ad 100644 --- a/raster/test/regress/rt_addband_expected +++ b/raster/test/regress/rt_addband_expected @@ -89,3 +89,4 @@ NOTICE: RASTER_copyband: Could not add band to raster. Returning original raste NOTICE: rt_raster_copy_band: Second raster has no band NOTICE: RASTER_copyband: Could not add band to raster. Returning original raster. 1234.5678 +90 -- 2.40.0