From: Bborie Park Date: Sat, 8 Sep 2012 01:21:21 +0000 (+0000) Subject: Added new variant on existing ST_SetValues where instead of a noset 2D X-Git-Tag: 2.1.0beta2~663 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c1c79590fe6a5a6f6b9f52419201e7a7e0a17373;p=postgis Added new variant on existing ST_SetValues where instead of a noset 2D array of booleans, the parameter nosetvalue can be used instead. git-svn-id: http://svn.osgeo.org/postgis/trunk@10260 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 60ec9870f..47a055bba 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -2559,6 +2559,9 @@ Datum RASTER_setPixelValuesArray(PG_FUNCTION_ARGS) int hasnodata = FALSE; double nodataval = 0; bool keepnodata = FALSE; + bool hasnosetval = FALSE; + bool nosetvalisnull = FALSE; + double nosetval = 0; int rtn = 0; double val = 0; @@ -2827,6 +2830,14 @@ Datum RASTER_setPixelValuesArray(PG_FUNCTION_ARGS) pfree(elements); pfree(nulls); } + /* hasnosetvalue and nosetvalue */ + else if (!PG_ARGISNULL(6) & PG_GETARG_BOOL(6)) { + hasnosetval = TRUE; + if (PG_ARGISNULL(7)) + nosetvalisnull = TRUE; + else + nosetval = PG_GETARG_FLOAT8(7); + } #if POSTGIS_DEBUG_LEVEL > 0 for (i = 0; i < numpixval; i++) { @@ -2842,8 +2853,8 @@ Datum RASTER_setPixelValuesArray(PG_FUNCTION_ARGS) #endif /* keepnodata flag */ - if (!PG_ARGISNULL(6)) - keepnodata = PG_GETARG_BOOL(6); + if (!PG_ARGISNULL(8)) + keepnodata = PG_GETARG_BOOL(8); /* get band */ band = rt_raster_get_band(raster, nband - 1); @@ -2868,6 +2879,15 @@ Datum RASTER_setPixelValuesArray(PG_FUNCTION_ARGS) /* noset = true, skip */ if (pixval[i].noset) continue; + /* check against nosetval */ + else if (hasnosetval) { + /* pixel = NULL AND nosetval = NULL */ + if (pixval[i].nodata && nosetvalisnull) + continue; + /* pixel value = nosetval */ + else if (!pixval[i].nodata && !nosetvalisnull && FLT_EQ(pixval[i].value, nosetval)) + continue; + } /* if pixel is outside bounds, skip */ if ( diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index a64236584..c66d7a3a5 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -2903,17 +2903,41 @@ CREATE OR REPLACE FUNCTION st_setvalue(rast raster, pt geometry, newvalue float8 ----------------------------------------------------------------------- -- ST_SetValues (set one or more pixels to a one or more values) ----------------------------------------------------------------------- -CREATE OR REPLACE FUNCTION st_setvalues( +CREATE OR REPLACE FUNCTION _st_setvalues( rast raster, nband integer, x integer, y integer, newvalueset double precision[][], noset boolean[][] DEFAULT NULL, + hasnosetvalue boolean DEFAULT FALSE, + nosetvalue double precision DEFAULT NULL, keepnodata boolean DEFAULT FALSE ) RETURNS raster AS 'MODULE_PATHNAME', 'RASTER_setPixelValuesArray' LANGUAGE 'c' IMMUTABLE; +CREATE OR REPLACE FUNCTION st_setvalues( + rast raster, nband integer, + x integer, y integer, + newvalueset double precision[][], + noset boolean[][] DEFAULT NULL, + keepnodata boolean DEFAULT FALSE +) + RETURNS raster + AS $$ SELECT _st_setvalues($1, $2, $3, $4, $5, $6, FALSE, NULL, $7) $$ + LANGUAGE 'sql' IMMUTABLE; + +CREATE OR REPLACE FUNCTION st_setvalues( + rast raster, nband integer, + x integer, y integer, + newvalueset double precision[][], + nosetvalue double precision, + keepnodata boolean DEFAULT FALSE +) + RETURNS raster + AS $$ SELECT _st_setvalues($1, $2, $3, $4, $5, NULL, TRUE, $6, $7) $$ + LANGUAGE 'sql' IMMUTABLE; + -- cannot be STRICT as newvalue can be NULL CREATE OR REPLACE FUNCTION st_setvalues( rast raster, nband integer, @@ -2929,7 +2953,7 @@ CREATE OR REPLACE FUNCTION st_setvalues( RAISE EXCEPTION 'Values for width and height must be greater than zero'; RETURN NULL; END IF; - RETURN st_setvalues($1, $2, $3, $4, array_fill($7, ARRAY[$6, $5]::int[]), NULL, $8); + RETURN _st_setvalues($1, $2, $3, $4, array_fill($7, ARRAY[$6, $5]::int[]), NULL, FALSE, NULL, $8); END; $$ LANGUAGE 'plpgsql' IMMUTABLE; @@ -2949,7 +2973,7 @@ CREATE OR REPLACE FUNCTION st_setvalues( RAISE EXCEPTION 'Values for width and height must be greater than zero'; RETURN NULL; END IF; - RETURN st_setvalues($1, 1, $2, $3, array_fill($6, ARRAY[$5, $4]::int[]), NULL, $7); + RETURN _st_setvalues($1, 1, $2, $3, array_fill($6, ARRAY[$5, $4]::int[]), NULL, FALSE, NULL, $7); END; $$ LANGUAGE 'plpgsql' IMMUTABLE; diff --git a/raster/test/regress/rt_setvalues_array.sql b/raster/test/regress/rt_setvalues_array.sql index f81d1fba3..b96e40d48 100644 --- a/raster/test/regress/rt_setvalues_array.sql +++ b/raster/test/regress/rt_setvalues_array.sql @@ -182,6 +182,36 @@ INSERT INTO raster_setvalues_out VALUES ( FROM raster_setvalues_rast )); +INSERT INTO raster_setvalues_out VALUES ( + 31, ( + SELECT ST_SetValues( + rast, 1, 1, 1, + ARRAY[-1, 31, -1]::double precision[], + -1::double precision + ) + FROM raster_setvalues_rast +)); + +INSERT INTO raster_setvalues_out VALUES ( + 32, ( + SELECT ST_SetValues( + rast, 1, 1, 1, + ARRAY[[-1, 32, -1], [32, -1, 32]]::double precision[], + -1::double precision + ) + FROM raster_setvalues_rast +)); + +INSERT INTO raster_setvalues_out VALUES ( + 33, ( + SELECT ST_SetValues( + rast, 1, 1, 1, + ARRAY[[NULL, 33, NULL], [33, NULL, 33]]::double precision[], + NULL::double precision + ) + FROM raster_setvalues_rast +)); + SELECT rid, (poly).x, diff --git a/raster/test/regress/rt_setvalues_array_expected b/raster/test/regress/rt_setvalues_array_expected index 51aa70055..c2952126b 100644 --- a/raster/test/regress/rt_setvalues_array_expected +++ b/raster/test/regress/rt_setvalues_array_expected @@ -238,3 +238,48 @@ 23|5|1| 23|5|2| 23|5|3| +31|1|1|1 +31|1|2|1 +31|1|3|1 +31|2|1|31 +31|2|2|1 +31|2|3|1 +31|3|1|1 +31|3|2|1 +31|3|3|1 +31|4|1|1 +31|4|2|1 +31|4|3|1 +31|5|1|1 +31|5|2|1 +31|5|3|1 +32|1|1|1 +32|1|2|32 +32|1|3|1 +32|2|1|32 +32|2|2|1 +32|2|3|1 +32|3|1|1 +32|3|2|32 +32|3|3|1 +32|4|1|1 +32|4|2|1 +32|4|3|1 +32|5|1|1 +32|5|2|1 +32|5|3|1 +33|1|1|1 +33|1|2|33 +33|1|3|1 +33|2|1|33 +33|2|2|1 +33|2|3|1 +33|3|1|1 +33|3|2|33 +33|3|3|1 +33|4|1|1 +33|4|2|1 +33|4|3|1 +33|5|1|1 +33|5|2|1 +33|5|3|1