]> granicus.if.org Git - postgis/commitdiff
Added a new variant for ST_AddBand, taking 2 rasters as input. Needs doc.
authorJorge Arévalo <jorge.arevalo at deimos-space.com>
Wed, 16 Feb 2011 19:12:25 +0000 (19:12 +0000)
committerJorge Arévalo <jorge.arevalo at deimos-space.com>
Wed, 16 Feb 2011 19:12:25 +0000 (19:12 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@6833 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_pg/rt_pg.c
raster/rt_pg/rtpostgis.sql.in.c
raster/test/regress/rt_addband.sql
raster/test/regress/rt_addband_expected

index 05efbbf8e71af25834f8a8e67e08a793fccf381a..17f6a12893c5a8f7b672c4c56e7c4320011766b7 100644 (file)
@@ -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
  */
index 383c6859cd70df0b06fa5ebc89af8ac316362fa8..9a695601b90486a2ae46e0b29366e2be92ee5955 100644 (file)
@@ -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;
-
index 7317c8e0d53192d47a669b7fa9847ab3246a62e6..b2061f6dc575f357b1493f942e99bb8a3b79b24c 100644 (file)
@@ -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);
index cce7c4ec91001b0eb59480cf7151a6474e7d156e..75032d70bbbfc8b07465615a97438315ba0f677c 100644 (file)
@@ -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