From f529a4ef48b429152bc5e86af70f62d945b8e482 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jorge=20Ar=C3=A9valo?= Date: Wed, 16 Feb 2011 19:12:25 +0000 Subject: [PATCH] Added a new variant for ST_AddBand, taking 2 rasters as input. Needs doc. git-svn-id: http://svn.osgeo.org/postgis/trunk@6833 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_pg/rt_pg.c | 78 +++++++++++++++++++++++++ raster/rt_pg/rtpostgis.sql.in.c | 8 ++- raster/test/regress/rt_addband.sql | 1 + raster/test/regress/rt_addband_expected | 1 + 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 05efbbf8e..17f6a1289 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -112,6 +112,7 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS); Datum RASTER_getPixelValue(PG_FUNCTION_ARGS); Datum RASTER_setPixelValue(PG_FUNCTION_ARGS); Datum RASTER_addband(PG_FUNCTION_ARGS); +Datum RASTER_copyband(PG_FUNCTION_ARGS); Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS); Datum RASTER_isEmpty(PG_FUNCTION_ARGS); Datum RASTER_hasNoBand(PG_FUNCTION_ARGS); @@ -1827,6 +1828,83 @@ Datum RASTER_addband(PG_FUNCTION_ARGS) PG_RETURN_POINTER(pgraster); } + +/** + * Copy a band from one raster to another one at the given position. + */ +PG_FUNCTION_INFO_V1(RASTER_copyband); +Datum RASTER_copyband(PG_FUNCTION_ARGS) +{ + rt_pgraster *pgraster = NULL; + rt_raster raster1 = NULL; + rt_raster raster2 = NULL; + int nband1 = 0; + int nband2 = 0; + int oldnumbands = 0; + int numbands = 0; + int index = 0; + int max = 0; + rt_context ctx = NULL; + + /* Get band numbers */ + nband1 = PG_GETARG_UINT16(2); + nband2 = PG_GETARG_UINT16(3); + + if (nband1 < 1 || nband2 < 1) { + elog(ERROR, "Invalid band index (must be 1-based)"); + PG_RETURN_NULL(); + } + + /* Check if raster1 has the given band */ + + /* Deserialize raster1 */ + pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)); + ctx = get_rt_context(fcinfo); + + raster1 = rt_raster_deserialize(ctx, pgraster); + if ( ! raster1 ) { + elog(ERROR, "Could not deserialize raster"); + PG_RETURN_NULL(); + } + + /* Make sure index (1 based) is in range */ + max = rt_raster_get_num_bands(ctx, raster1); + if (nband1 > max) { + elog(WARNING, "Band index number exceed possible values, truncated to " + "number of band (%u) + 1", max); + nband1 = max; + } + + /* Deserialize raster2 */ + pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1)); + ctx = get_rt_context(fcinfo); + + raster2 = rt_raster_deserialize(ctx, pgraster); + if ( ! raster2 ) { + elog(ERROR, "Could not deserialize raster"); + PG_RETURN_NULL(); + } + + /* Copy band from raster1 to raster2 */ + oldnumbands = rt_raster_get_num_bands(ctx, raster2); + + index = rt_raster_copy_band(ctx, raster1, raster2, nband1, nband2); + + numbands = rt_raster_get_num_bands(ctx, raster2); + if (numbands == oldnumbands || index == -1) { + elog(ERROR, "Could not add band to raster. Returning NULL"); + PG_RETURN_NULL(); + } + + /* Serialize and return raster2 */ + pgraster = rt_raster_serialize(ctx, raster2); + if (!pgraster) PG_RETURN_NULL(); + + SET_VARSIZE(pgraster, pgraster->size); + PG_RETURN_POINTER(pgraster); +} + + /** * Check if raster is empty or not */ diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 383c6859c..9a695601b 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -210,6 +210,11 @@ CREATE OR REPLACE FUNCTION st_addband(rast raster, index int, pixeltype text, in AS 'select st_addband($1, $2, $3, $4, NULL)' LANGUAGE 'SQL' IMMUTABLE; +CREATE OR REPLACE FUNCTION st_addband(raster1 raster, raster2 raster, nband1 int, nband2 int) + RETURNS RASTER + AS 'MODULE_PATHNAME', 'RASTER_copyband' + LANGUAGE 'C' IMMUTABLE STRICT; + ----------------------------------------------------------------------- -- MapAlgebra @@ -1953,7 +1958,7 @@ CREATE OR REPLACE FUNCTION DropRasterTable(schema_name varchar, END; $$ LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict); - + ----------------------------------------------------------------------- -- DropRasterTable (with default catalog and schema name) -- Drop a table and all its references in raster_columns @@ -1976,4 +1981,3 @@ CREATE OR REPLACE FUNCTION DropRasterTable(table_name varchar) ------------------------------------------------------------------- -- COMMIT; - diff --git a/raster/test/regress/rt_addband.sql b/raster/test/regress/rt_addband.sql index 7317c8e0d..b2061f6dc 100644 --- a/raster/test/regress/rt_addband.sql +++ b/raster/test/regress/rt_addband.sql @@ -108,3 +108,4 @@ SELECT St_Value(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1 SELECT St_Value(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1), 1, '64BF', 1234.567, NULL), 3, 3); SELECT St_Value(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1), 1, '64BF', 210000.4645643647457, NULL), 3, 3); SELECT St_Value(ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 10, 10, 2, 2, 0, 0, -1), 1, '64BF', 1234.4645643647457, NULL), 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, 1), 3, 3); diff --git a/raster/test/regress/rt_addband_expected b/raster/test/regress/rt_addband_expected index cce7c4ec9..75032d70b 100644 --- a/raster/test/regress/rt_addband_expected +++ b/raster/test/regress/rt_addband_expected @@ -129,3 +129,4 @@ WARNING: Initial pixel value for 32BF band got truncated from 210000.464564 to 1234.567 210000.464564365 1234.46456436475 +1234.5678 -- 2.50.1