]> granicus.if.org Git - postgis/commitdiff
Addition of rt_raster_contains_properly and regression tests
authorBborie Park <bkpark at ucdavis.edu>
Mon, 23 Jul 2012 18:58:05 +0000 (18:58 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 23 Jul 2012 18:58:05 +0000 (18:58 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@10100 b70326c6-7e19-0410-871a-916f4a2858ee

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

index eeb8a2b4c02bcf96ed4bf69403cf6127e9778789..8f718e8c6109846356aeb3bd07f4e94ed3414425 100644 (file)
@@ -11559,6 +11559,9 @@ int rt_raster_geos_spatial_relationship(
                case GSR_CONTAINS:
                        rtn = GEOSContains(geom1, geom2);
                        break;
+               case GSR_CONTAINSPROPERLY:
+                       rtn = GEOSRelatePattern(geom1, geom2, "T**FF*FF*");
+                       break;
                default:
                        rterror("rt_raster_geos_spatial_relationship: Unknown or unsupported GEOS spatial relationship test");
                        flag = -1;
@@ -11678,6 +11681,37 @@ int rt_raster_contains(
        );
 }
 
+/**
+ * Return zero if error occurred in function.
+ * Parameter contains returns non-zero if rast1 properly 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 properly contains rast2
+ *
+ * @return if zero, an error occurred in function
+ */
+int rt_raster_contains_properly(
+       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_CONTAINSPROPERLY,
+               contains
+       );
+}
+
 /*
  * Return zero if error occurred in function.
  * Paramter aligned returns non-zero if two rasters are aligned
index 2d3cf69c352e946c5b9a11102b08171eac629070..873fb2c2c87eefac9338a55ba527ed7ad1b02b1e 100644 (file)
@@ -182,7 +182,8 @@ typedef enum {
 typedef enum {
        GSR_OVERLAPS = 0,
        GSR_TOUCHES,
-       GSR_CONTAINS
+       GSR_CONTAINS,
+       GSR_CONTAINSPROPERLY,
 } rt_geos_spatial_test;
 
 /**
@@ -1500,6 +1501,28 @@ int rt_raster_contains(
        int *contains
 );
 
+/**
+ * Return zero if error occurred in function.
+ * Parameter contains returns non-zero if rast1 properly 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 properly contains rast2
+ *
+ * @return if zero, an error occurred in function
+ */
+int rt_raster_contains_properly(
+       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 f80fcf7f80765fb51a3972832508e94eada7b2c9..54a2d5a57db534d049dc24a10295e3eeb9a40ad4 100644 (file)
@@ -3892,6 +3892,529 @@ static void testContains() {
        deepRelease(rast1);
 }
 
+static void testContainsProperly() {
+       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_properly(
+               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_properly(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       rtn = rt_raster_contains_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               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_properly(
+               rast1, 0,
+               rast2, 0,
+               &contains
+       );
+       CHECK((rtn != 0));
+       CHECK((contains != 1));
+
+       deepRelease(rast2);
+       deepRelease(rast1);
+}
+
 static void testAlignment() {
        rt_raster rast1;
        rt_raster rast2;
@@ -4828,6 +5351,10 @@ main()
                testIntersects();
                printf("OK\n");
 
+               printf("Testing rt_raster_surface... ");
+               testRasterSurface();
+               printf("OK\n");
+
                printf("Testing rt_raster_overlaps... ");
                testOverlaps();
                printf("OK\n");
@@ -4840,6 +5367,10 @@ main()
                testContains();
                printf("OK\n");
 
+               printf("Testing rt_raster_contains_properly... ");
+               testContainsProperly();
+               printf("OK\n");
+
                printf("Testing rt_raster_same_alignment... ");
                testAlignment();
                printf("OK\n");
@@ -4868,10 +5399,6 @@ main()
                testPixelAsPolygon();
                printf("OK\n");
 
-               printf("Testing rt_raster_surface... ");
-               testRasterSurface();
-               printf("OK\n");
-
     deepRelease(raster);
 
     return EXIT_SUCCESS;