]> granicus.if.org Git - postgis/commitdiff
Added rt_raster_contains() and regression tests
authorBborie Park <bkpark at ucdavis.edu>
Mon, 23 Jul 2012 17:49:11 +0000 (17:49 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 23 Jul 2012 17:49:11 +0000 (17:49 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@10088 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/test/core/testapi.c

index 488f782aa8dfb92fb7169e84529e71f56ccc4236..eeb8a2b4c02bcf96ed4bf69403cf6127e9778789 100644 (file)
@@ -11556,6 +11556,9 @@ int rt_raster_geos_spatial_relationship(
                case GSR_TOUCHES:
                        rtn = GEOSTouches(geom1, geom2);
                        break;
+               case GSR_CONTAINS:
+                       rtn = GEOSContains(geom1, geom2);
+                       break;
                default:
                        rterror("rt_raster_geos_spatial_relationship: Unknown or unsupported GEOS spatial relationship test");
                        flag = -1;
@@ -11644,6 +11647,37 @@ int rt_raster_touches(
        );
 }
 
+/**
+ * Return zero if error occurred in function.
+ * Parameter contains returns non-zero if rast1 contains rast2
+ *
+ * @param rast1 : the first raster whose band will be tested
+ * @param nband1 : the 0-based band of raster rast1 to use
+ *   if value is less than zero, bands are ignored.
+ *   if nband1 gte zero, nband2 must be gte zero
+ * @param rast2 : the second raster whose band will be tested
+ * @param nband2 : the 0-based band of raster rast2 to use
+ *   if value is less than zero, bands are ignored
+ *   if nband2 gte zero, nband1 must be gte zero
+ * @param touches : non-zero value if rast1 contains rast2
+ *
+ * @return if zero, an error occurred in function
+ */
+int rt_raster_contains(
+       rt_raster rast1, int nband1,
+       rt_raster rast2, int nband2,
+       int *contains
+) {
+       RASTER_DEBUG(3, "Starting");
+
+       return rt_raster_geos_spatial_relationship(
+               rast1, nband1,
+               rast2, nband2,
+               GSR_CONTAINS,
+               contains
+       );
+}
+
 /*
  * Return zero if error occurred in function.
  * Paramter aligned returns non-zero if two rasters are aligned
index 697a590d9b5de69592b7b5370a906cf4c4fbbe7d..2d3cf69c352e946c5b9a11102b08171eac629070 100644 (file)
@@ -181,7 +181,8 @@ typedef enum {
 
 typedef enum {
        GSR_OVERLAPS = 0,
-       GSR_TOUCHES
+       GSR_TOUCHES,
+       GSR_CONTAINS
 } rt_geos_spatial_test;
 
 /**
@@ -1477,6 +1478,28 @@ int rt_raster_overlaps(
        int *overlaps
 );
 
+/**
+ * Return zero if error occurred in function.
+ * Parameter contains returns non-zero if rast1 contains rast2
+ *
+ * @param rast1 : the first raster whose band will be tested
+ * @param nband1 : the 0-based band of raster rast1 to use
+ *   if value is less than zero, bands are ignored.
+ *   if nband1 gte zero, nband2 must be gte zero
+ * @param rast2 : the second raster whose band will be tested
+ * @param nband2 : the 0-based band of raster rast2 to use
+ *   if value is less than zero, bands are ignored
+ *   if nband2 gte zero, nband1 must be gte zero
+ * @param touches : non-zero value if rast1 contains rast2
+ *
+ * @return if zero, an error occurred in function
+ */
+int rt_raster_contains(
+       rt_raster rast1, int nband1,
+       rt_raster rast2, int nband2,
+       int *contains
+);
+
 /**
  * Return zero if error occurred in function.
  * Parameter touches returns non-zero if two rasters touch
index 7b14611b913f986cb8d4055647df18d5424a9731..f80fcf7f80765fb51a3972832508e94eada7b2c9 100644 (file)
@@ -3369,6 +3369,529 @@ static void testTouches() {
        deepRelease(rast1);
 }
 
+static void testContains() {
+       rt_raster rast1;
+       rt_raster rast2;
+       rt_band band1;
+       rt_band band2;
+       double nodata;
+       int rtn;
+       int contains;
+
+       /*
+               rast1
+
+               (-1, -1)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (1, 1)
+       */
+       rast1 = rt_raster_new(2, 2);
+       assert(rast1);
+       rt_raster_set_offsets(rast1, -1, -1);
+
+       band1 = addBand(rast1, PT_8BUI, 1, 0);
+       CHECK(band1);
+       rt_band_set_nodata(band1, 0);
+       rtn = rt_band_set_pixel(band1, 0, 0, 1);
+       rtn = rt_band_set_pixel(band1, 0, 1, 1);
+       rtn = rt_band_set_pixel(band1, 1, 0, 1);
+       rtn = rt_band_set_pixel(band1, 1, 1, 1);
+
+       nodata = rt_band_get_nodata(band1);
+       CHECK_EQUALS(nodata, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast1, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains == 1));
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (2, 2)
+       */
+       rast2 = rt_raster_new(2, 2);
+       assert(rast2);
+
+       band2 = addBand(rast2, PT_8BUI, 1, 0);
+       CHECK(band2);
+       rt_band_set_nodata(band2, 0);
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+
+       nodata = rt_band_get_nodata(band2);
+       CHECK_EQUALS(nodata, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       rtn = rt_raster_contains(
+               rast1, -1,
+               rast2, -1,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+
+                                               |0|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (2, 2)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+
+                                               |1|0|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (2, 2)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+
+                                               |0|0|
+                                               +-+-+
+                                               |0|1|
+                                               +-+-+
+                                                               (2, 2)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+       rtn = rt_band_set_pixel(band2, 1, 0, 0);
+       rtn = rt_band_set_pixel(band2, 0, 1, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+
+                                               |0|0|
+                                               +-+-+
+                                               |0|0|
+                                               +-+-+
+                                                               (2, 2)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+       rtn = rt_band_set_pixel(band2, 1, 0, 0);
+       rtn = rt_band_set_pixel(band2, 0, 1, 0);
+       rtn = rt_band_set_pixel(band2, 1, 1, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (2, 0)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (4, 2)
+       */
+       rt_raster_set_offsets(rast2, 2, 0);
+
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0, 1)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (2, 3)
+       */
+       rt_raster_set_offsets(rast2, 0, 1);
+
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (-1, 1)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (1, 3)
+       */
+       rt_raster_set_offsets(rast2, -1, 1);
+
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (0.1, 0.1)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (0.9, 0.9)
+       */
+       rt_raster_set_offsets(rast2, 0.1, 0.1);
+       rt_raster_set_scale(rast2, 0.4, 0.4);
+
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains == 1));
+
+       /*
+               rast2
+
+               (-0.1, 0.1)
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                               |1|1|
+                                               +-+-+
+                                                               (0.9, 0.9)
+       */
+       rt_raster_set_offsets(rast2, -0.1, 0.1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains == 1));
+
+       deepRelease(rast2);
+
+       /*
+               rast2
+
+               (0, 0)
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                                                       (3, 3)
+       */
+       rast2 = rt_raster_new(3, 3);
+       assert(rast2);
+
+       band2 = addBand(rast2, PT_8BUI, 1, 0);
+       CHECK(band2);
+       rt_band_set_nodata(band2, 0);
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 0, 2, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 2, 1);
+       rtn = rt_band_set_pixel(band2, 2, 0, 1);
+       rtn = rt_band_set_pixel(band2, 2, 1, 1);
+       rtn = rt_band_set_pixel(band2, 2, 2, 1);
+
+       nodata = rt_band_get_nodata(band2);
+       CHECK_EQUALS(nodata, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (-2, -2)
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                               |1|1|1|
+                                               +-+-+-+
+                                                                       (1, 1)
+       */
+       rt_raster_set_offsets(rast2, -2, -2);
+
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 0, 2, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 1);
+       rtn = rt_band_set_pixel(band2, 1, 2, 1);
+       rtn = rt_band_set_pixel(band2, 2, 0, 1);
+       rtn = rt_band_set_pixel(band2, 2, 1, 1);
+       rtn = rt_band_set_pixel(band2, 2, 2, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (-2, -2)
+                                               +-+-+-+
+                                               |0|1|1|
+                                               +-+-+-+
+                                               |1|0|1|
+                                               +-+-+-+
+                                               |1|1|0|
+                                               +-+-+-+
+                                                                       (1, 1)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 0, 2, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 0);
+       rtn = rt_band_set_pixel(band2, 1, 2, 1);
+       rtn = rt_band_set_pixel(band2, 2, 0, 1);
+       rtn = rt_band_set_pixel(band2, 2, 1, 1);
+       rtn = rt_band_set_pixel(band2, 2, 2, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (-2, -2)
+                                               +-+-+-+
+                                               |0|1|1|
+                                               +-+-+-+
+                                               |1|0|0|
+                                               +-+-+-+
+                                               |1|0|0|
+                                               +-+-+-+
+                                                                       (1, 1)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 0, 2, 1);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 0);
+       rtn = rt_band_set_pixel(band2, 1, 2, 0);
+       rtn = rt_band_set_pixel(band2, 2, 0, 1);
+       rtn = rt_band_set_pixel(band2, 2, 1, 0);
+       rtn = rt_band_set_pixel(band2, 2, 2, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /*
+               rast2
+
+               (-2, -2)
+                                               +-+-+-+
+                                               |0|1|0|
+                                               +-+-+-+
+                                               |1|0|0|
+                                               +-+-+-+
+                                               |0|0|0|
+                                               +-+-+-+
+                                                                       (1, 1)
+       */
+       rtn = rt_band_set_pixel(band2, 0, 0, 0);
+       rtn = rt_band_set_pixel(band2, 0, 1, 1);
+       rtn = rt_band_set_pixel(band2, 0, 2, 0);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 0);
+       rtn = rt_band_set_pixel(band2, 1, 2, 0);
+       rtn = rt_band_set_pixel(band2, 2, 0, 0);
+       rtn = rt_band_set_pixel(band2, 2, 1, 0);
+       rtn = rt_band_set_pixel(band2, 2, 2, 0);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       deepRelease(rast2);
+
+       /* skew tests */
+       /* rast2 (skewed by -0.5, 0.5) */
+       rast2 = rt_raster_new(3, 3);
+       assert(rast2);
+       rt_raster_set_skews(rast2, -0.5, 0.5);
+
+       band2 = addBand(rast2, PT_8BUI, 1, 0);
+       CHECK(band2);
+       rt_band_set_nodata(band2, 0);
+       rtn = rt_band_set_pixel(band2, 0, 0, 1);
+       rtn = rt_band_set_pixel(band2, 0, 1, 2);
+       rtn = rt_band_set_pixel(band2, 0, 2, 3);
+       rtn = rt_band_set_pixel(band2, 1, 0, 1);
+       rtn = rt_band_set_pixel(band2, 1, 1, 2);
+       rtn = rt_band_set_pixel(band2, 1, 2, 3);
+       rtn = rt_band_set_pixel(band2, 2, 0, 1);
+       rtn = rt_band_set_pixel(band2, 2, 1, 2);
+       rtn = rt_band_set_pixel(band2, 2, 2, 3);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /* rast2 (skewed by -1, 1) */
+       rt_raster_set_skews(rast2, -1, 1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       /* rast2 (skewed by 1, -1) */
+       rt_raster_set_skews(rast2, 1, -1);
+
+       rtn = rt_raster_contains(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       deepRelease(rast2);
+       deepRelease(rast1);
+}
+
 static void testAlignment() {
        rt_raster rast1;
        rt_raster rast2;
@@ -4313,6 +4836,10 @@ main()
                testTouches();
                printf("OK\n");
 
+               printf("Testing rt_raster_contains... ");
+               testContains();
+               printf("OK\n");
+
                printf("Testing rt_raster_same_alignment... ");
                testAlignment();
                printf("OK\n");