From 1d9b503ead579e7e291b979824a4db193cdf2f8c Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Mon, 23 Jul 2012 22:57:17 +0000 Subject: [PATCH] Added ST_CoveredBy and regression tests. Ticket is #1917. git-svn-id: http://svn.osgeo.org/postgis/trunk@10108 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_pg/rt_pg.c | 126 +++++++++++++- raster/rt_pg/rtpostgis.sql.in.c | 22 +++ raster/test/regress/Makefile.in | 1 + raster/test/regress/rt_coveredby.sql | 198 ++++++++++++++++++++++ raster/test/regress/rt_coveredby_expected | 65 +++++++ 5 files changed, 410 insertions(+), 2 deletions(-) create mode 100644 raster/test/regress/rt_coveredby.sql create mode 100644 raster/test/regress/rt_coveredby_expected diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 2d699972d..eaa40f436 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -285,6 +285,9 @@ Datum RASTER_containsProperly(PG_FUNCTION_ARGS); /* determine if the first raster covers the second raster */ Datum RASTER_covers(PG_FUNCTION_ARGS); +/* determine if the first raster is covered by the second raster */ +Datum RASTER_coveredby(PG_FUNCTION_ARGS); + /* determine if two rasters are aligned */ Datum RASTER_sameAlignment(PG_FUNCTION_ARGS); @@ -10459,7 +10462,7 @@ Datum RASTER_containsProperly(PG_FUNCTION_ARGS) } if (!rtn) { - elog(ERROR, "RASTER_containsProperly: Unable to test that the first raster contains the second raster"); + elog(ERROR, "RASTER_containsProperly: Unable to test that the first raster contains properly the second raster"); PG_RETURN_NULL(); } @@ -10578,7 +10581,126 @@ Datum RASTER_covers(PG_FUNCTION_ARGS) } if (!rtn) { - elog(ERROR, "RASTER_covers: Unable to test that the first raster contains the second raster"); + elog(ERROR, "RASTER_covers: Unable to test that the first raster covers the second raster"); + PG_RETURN_NULL(); + } + + PG_RETURN_BOOL(result); +} + +/** + * See if the first raster is covered by the second raster + */ +PG_FUNCTION_INFO_V1(RASTER_coveredby); +Datum RASTER_coveredby(PG_FUNCTION_ARGS) +{ + const int set_count = 2; + rt_pgraster *pgrast[2]; + int pgrastpos[2] = {-1, -1}; + rt_raster rast[2] = {NULL}; + uint32_t bandindex[2] = {0}; + uint32_t hasbandindex[2] = {0}; + + uint32_t i; + uint32_t j; + uint32_t k; + uint32_t numBands; + int rtn; + int result; + + for (i = 0, j = 0; i < set_count; i++) { + /* pgrast is null, return null */ + if (PG_ARGISNULL(j)) { + for (k = 0; k < i; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + pgrast[i] = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(j)); + pgrastpos[i] = j; + j++; + + /* raster */ + rast[i] = rt_raster_deserialize(pgrast[i], FALSE); + if (!rast[i]) { + elog(ERROR, "RASTER_coveredby: Could not deserialize the %s raster", i < 1 ? "first" : "second"); + for (k = 0; k <= i; k++) { + if (k < i) + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + + /* numbands */ + numBands = rt_raster_get_num_bands(rast[i]); + if (numBands < 1) { + elog(NOTICE, "The %s raster provided has no bands", i < 1 ? "first" : "second"); + if (i > 0) i++; + for (k = 0; k < i; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + + /* band index */ + if (!PG_ARGISNULL(j)) { + bandindex[i] = PG_GETARG_INT32(j); + if (bandindex[i] < 1 || bandindex[i] > numBands) { + elog(NOTICE, "Invalid band index (must use 1-based) for the %s raster. Returning NULL", i < 1 ? "first" : "second"); + if (i > 0) i++; + for (k = 0; k < i; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + hasbandindex[i] = 1; + } + else + hasbandindex[i] = 0; + POSTGIS_RT_DEBUGF(4, "hasbandindex[%d] = %d", i, hasbandindex[i]); + POSTGIS_RT_DEBUGF(4, "bandindex[%d] = %d", i, bandindex[i]); + j++; + } + + /* hasbandindex must be balanced */ + if ( + (hasbandindex[0] && !hasbandindex[1]) || + (!hasbandindex[0] && hasbandindex[1]) + ) { + elog(NOTICE, "Missing band index. Band indices must be provided for both rasters if any one is provided"); + for (k = 0; k < set_count; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + + /* SRID must match */ + if (rt_raster_get_srid(rast[0]) != rt_raster_get_srid(rast[1])) { + elog(ERROR, "The two rasters provided have different SRIDs"); + for (k = 0; k < set_count; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + PG_RETURN_NULL(); + } + + rtn = rt_raster_coveredby( + rast[0], (hasbandindex[0] ? bandindex[0] - 1 : -1), + rast[1], (hasbandindex[1] ? bandindex[1] - 1 : -1), + &result + ); + for (k = 0; k < set_count; k++) { + rt_raster_destroy(rast[k]); + PG_FREE_IF_COPY(pgrast[k], pgrastpos[k]); + } + + if (!rtn) { + elog(ERROR, "RASTER_coveredby: Unable to test that the first raster is covered by the second raster"); PG_RETURN_NULL(); } diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 30a3d43cc..8dc9162a6 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -3400,6 +3400,28 @@ CREATE OR REPLACE FUNCTION st_covers(rast1 raster, rast2 raster) LANGUAGE 'sql' IMMUTABLE COST 1000; +----------------------------------------------------------------------- +-- ST_CoveredBy(raster, raster) +----------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION _st_coveredby(rast1 raster, nband1 integer, rast2 raster, nband2 integer) + RETURNS boolean + AS 'MODULE_PATHNAME', 'RASTER_coveredby' + LANGUAGE 'c' IMMUTABLE STRICT + COST 1000; + +CREATE OR REPLACE FUNCTION st_coveredby(rast1 raster, nband1 integer, rast2 raster, nband2 integer) + RETURNS boolean + AS $$ SELECT $1 && $3 AND CASE WHEN $2 IS NULL OR $4 IS NULL THEN st_coveredby(st_convexhull($1), st_convexhull($3)) ELSE _st_coveredby($1, $2, $3, $4) END $$ + LANGUAGE 'sql' IMMUTABLE + COST 1000; + +CREATE OR REPLACE FUNCTION st_coveredby(rast1 raster, rast2 raster) + RETURNS boolean + AS $$ SELECT st_coveredby($1, NULL::integer, $2, NULL::integer) $$ + LANGUAGE 'sql' IMMUTABLE + COST 1000; + ----------------------------------------------------------------------- -- ST_Within(raster, raster) ----------------------------------------------------------------------- diff --git a/raster/test/regress/Makefile.in b/raster/test/regress/Makefile.in index 6c13d9b26..ec3e355e4 100644 --- a/raster/test/regress/Makefile.in +++ b/raster/test/regress/Makefile.in @@ -130,6 +130,7 @@ TEST_SREL = \ rt_contains \ rt_containsproperly \ rt_covers \ + rt_coveredby \ rt_within \ rt_samealignment diff --git a/raster/test/regress/rt_coveredby.sql b/raster/test/regress/rt_coveredby.sql new file mode 100644 index 000000000..76eb0977e --- /dev/null +++ b/raster/test/regress/rt_coveredby.sql @@ -0,0 +1,198 @@ +SET client_min_messages TO warning; + +DROP TABLE IF EXISTS raster_coveredby_rast; +CREATE TABLE raster_coveredby_rast ( + rid integer, + rast raster +); +CREATE OR REPLACE FUNCTION make_test_raster( + rid integer, + width integer DEFAULT 2, height integer DEFAULT 2, + ul_x double precision DEFAULT 0, ul_y double precision DEFAULT 0, + skew_x double precision DEFAULT 0, skew_y double precision DEFAULT 0 +) + RETURNS void + AS $$ + DECLARE + x int; + y int; + rast raster; + BEGIN + rast := ST_MakeEmptyRaster(width, height, ul_x, ul_y, 1, 1, skew_x, skew_y, 0); + rast := ST_AddBand(rast, 1, '8BUI', 1, 0); + + + INSERT INTO raster_coveredby_rast VALUES (rid, rast); + + RETURN; + END; + $$ LANGUAGE 'plpgsql'; +SELECT make_test_raster(0, 2, 2, -1, -1); +SELECT make_test_raster(1, 2, 2); +SELECT make_test_raster(2, 3, 3); +DROP FUNCTION make_test_raster(integer, integer, integer, double precision, double precision, double precision, double precision); + +INSERT INTO raster_coveredby_rast VALUES (10, ( + SELECT + ST_SetValue(rast, 1, 1, 1, 0) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (11, ( + SELECT + ST_SetValue(rast, 1, 2, 1, 0) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (12, ( + SELECT + ST_SetValue( + ST_SetValue( + ST_SetValue(rast, 1, 1, 1, 0), + 1, 2, 1, 0 + ), + 1, 1, 2, 0 + ) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (13, ( + SELECT + ST_SetValue( + ST_SetValue( + ST_SetValue( + ST_SetValue(rast, 1, 1, 1, 0), + 1, 2, 1, 0 + ), + 1, 1, 2, 0 + ), + 1, 2, 2, 0 + ) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (14, ( + SELECT + ST_SetUpperLeft(rast, 2, 0) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (15, ( + SELECT + ST_SetScale( + ST_SetUpperLeft(rast, 0.1, 0.1), + 0.4, 0.4 + ) + FROM raster_coveredby_rast + WHERE rid = 1 +)); +INSERT INTO raster_coveredby_rast VALUES (16, ( + SELECT + ST_SetScale( + ST_SetUpperLeft(rast, -0.1, 0.1), + 0.4, 0.4 + ) + FROM raster_coveredby_rast + WHERE rid = 1 +)); + +INSERT INTO raster_coveredby_rast VALUES (20, ( + SELECT + ST_SetUpperLeft(rast, -2, -2) + FROM raster_coveredby_rast + WHERE rid = 2 +)); +INSERT INTO raster_coveredby_rast VALUES (21, ( + SELECT + ST_SetValue( + ST_SetValue( + ST_SetValue(rast, 1, 1, 1, 0), + 1, 2, 2, 0 + ), + 1, 3, 3, 0 + ) + FROM raster_coveredby_rast + WHERE rid = 20 +)); +INSERT INTO raster_coveredby_rast VALUES (22, ( + SELECT + ST_SetValue( + ST_SetValue( + rast, 1, 3, 2, 0 + ), + 1, 2, 3, 0 + ) + FROM raster_coveredby_rast + WHERE rid = 21 +)); +INSERT INTO raster_coveredby_rast VALUES (23, ( + SELECT + ST_SetValue( + ST_SetValue( + rast, 1, 3, 1, 0 + ), + 1, 1, 3, 0 + ) + FROM raster_coveredby_rast + WHERE rid = 22 +)); + +INSERT INTO raster_coveredby_rast VALUES (30, ( + SELECT + ST_SetSkew(rast, -0.5, 0.5) + FROM raster_coveredby_rast + WHERE rid = 2 +)); +INSERT INTO raster_coveredby_rast VALUES (31, ( + SELECT + ST_SetSkew(rast, -1, 1) + FROM raster_coveredby_rast + WHERE rid = 2 +)); +INSERT INTO raster_coveredby_rast VALUES (32, ( + SELECT + ST_SetSkew(rast, 1, -1) + FROM raster_coveredby_rast + WHERE rid = 2 +)); + +SELECT + '1.1', + r1.rid, + r2.rid, + ST_CoveredBy(r1.rast, NULL, r2.rast, NULL) +FROM raster_coveredby_rast r1 +CROSS JOIN raster_coveredby_rast r2 +WHERE r1.rid = 0; + +SELECT + '1.2', + r1.rid, + r2.rid, + ST_CoveredBy(r1.rast, 1, r2.rast, 1) +FROM raster_coveredby_rast r1 +JOIN raster_coveredby_rast r2 + ON r1.rid != r2.rid +WHERE r1.rid = 0; + +SELECT + '1.3', + r1.rid, + r2.rid, + ST_CoveredBy(r1.rast, NULL, r2.rast, NULL) +FROM raster_coveredby_rast r1 +JOIN raster_coveredby_rast r2 + ON r1.rid != r2.rid +WHERE r2.rid = 0; + +SELECT + '1.4', + r1.rid, + r2.rid, + ST_CoveredBy(r1.rast, 1, r2.rast, 1) +FROM raster_coveredby_rast r1 +JOIN raster_coveredby_rast r2 + ON r1.rid != r2.rid +WHERE r2.rid = 0; + +DROP TABLE IF EXISTS raster_coveredby_rast; diff --git a/raster/test/regress/rt_coveredby_expected b/raster/test/regress/rt_coveredby_expected new file mode 100644 index 000000000..155a81371 --- /dev/null +++ b/raster/test/regress/rt_coveredby_expected @@ -0,0 +1,65 @@ +1.1|0|0|t +1.1|0|1|f +1.1|0|2|f +1.1|0|10|f +1.1|0|11|f +1.1|0|12|f +1.1|0|13|f +1.1|0|14|f +1.1|0|15|f +1.1|0|16|f +1.1|0|20|t +1.1|0|21|t +1.1|0|22|t +1.1|0|23|t +1.1|0|30|f +1.1|0|31|f +1.1|0|32|f +1.2|0|1|f +1.2|0|2|f +1.2|0|10|f +1.2|0|11|f +1.2|0|12|f +1.2|0|13|f +1.2|0|14|f +1.2|0|15|f +1.2|0|16|f +1.2|0|20|t +1.2|0|21|f +1.2|0|22|f +1.2|0|23|f +1.2|0|30|f +1.2|0|31|f +1.2|0|32|f +1.3|1|0|f +1.3|2|0|f +1.3|10|0|f +1.3|11|0|f +1.3|12|0|f +1.3|13|0|f +1.3|14|0|f +1.3|15|0|t +1.3|16|0|t +1.3|20|0|f +1.3|21|0|f +1.3|22|0|f +1.3|23|0|f +1.3|30|0|f +1.3|31|0|f +1.3|32|0|f +1.4|1|0|f +1.4|2|0|f +1.4|10|0|f +1.4|11|0|f +1.4|12|0|f +1.4|13|0|f +1.4|14|0|f +1.4|15|0|t +1.4|16|0|t +1.4|20|0|f +1.4|21|0|f +1.4|22|0|f +1.4|23|0|f +1.4|30|0|f +1.4|31|0|f +1.4|32|0|f -- 2.40.0