From: Bborie Park Date: Wed, 28 Nov 2012 15:27:47 +0000 (+0000) Subject: Refactored return and parameters of rt_raster_surface() X-Git-Tag: 2.1.0beta2~356 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f43ac4e12193850c3ffd7c2fcdab7aff47a01829;p=postgis Refactored return and parameters of rt_raster_surface() git-svn-id: http://svn.osgeo.org/postgis/trunk@10744 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/raster/rt_core/rt_api.c b/raster/rt_core/rt_api.c index 68b392b71..6064f6026 100644 --- a/raster/rt_core/rt_api.c +++ b/raster/rt_core/rt_api.c @@ -11892,13 +11892,11 @@ rt_errorstate rt_raster_geos_spatial_relationship( initGEOS(lwnotice, lwgeom_geos_error); /* get LWMPOLY of each band */ - surface1 = rt_raster_surface(rast1, nband1, &rtn); - if (!rtn) { + if (rt_raster_surface(rast1, nband1, &surface1) != ES_NONE) { rterror("rt_raster_geos_spatial_relationship: Unable to get surface of the specified band from the first raster"); return ES_ERROR; } - surface2 = rt_raster_surface(rast2, nband2, &rtn); - if (!rtn) { + if (rt_raster_surface(rast2, nband2, &surface2) != ES_NONE) { rterror("rt_raster_geos_spatial_relationship: Unable to get surface of the specified band from the second raster"); lwmpoly_free(surface1); return ES_ERROR; @@ -12183,10 +12181,10 @@ rt_errorstate rt_raster_within_distance( double distance, int *dwithin ) { + LWMPOLY *surface = NULL; LWGEOM *surface1 = NULL; LWGEOM *surface2 = NULL; double mindist = 0; - int rtn = 0; RASTER_DEBUG(3, "Starting"); @@ -12218,17 +12216,18 @@ rt_errorstate rt_raster_within_distance( } /* get LWMPOLY of each band */ - surface1 = lwmpoly_as_lwgeom(rt_raster_surface(rast1, nband1, &rtn)); - if (!rtn) { + if (rt_raster_surface(rast1, nband1, &surface) != ES_NONE) { rterror("rt_raster_distance_within: Unable to get surface of the specified band from the first raster"); return ES_ERROR; } - surface2 = lwmpoly_as_lwgeom(rt_raster_surface(rast2, nband2, &rtn)); - if (!rtn) { + surface1 = lwmpoly_as_lwgeom(surface); + + if (rt_raster_surface(rast2, nband2, &surface) != ES_NONE) { rterror("rt_raster_distance_within: Unable to get surface of the specified band from the second raster"); lwgeom_free(surface1); return ES_ERROR; } + surface2 = lwmpoly_as_lwgeom(surface); /* either surface is NULL, test is false */ if (surface1 == NULL || surface2 == NULL) { @@ -12276,10 +12275,10 @@ rt_errorstate rt_raster_fully_within_distance( double distance, int *dfwithin ) { + LWMPOLY *surface = NULL; LWGEOM *surface1 = NULL; LWGEOM *surface2 = NULL; double maxdist = 0; - int rtn = 0; RASTER_DEBUG(3, "Starting"); @@ -12300,28 +12299,29 @@ rt_errorstate rt_raster_fully_within_distance( /* same srid */ if (rt_raster_get_srid(rast1) != rt_raster_get_srid(rast2)) { - rterror("rt_raster_distance_within: The two rasters provided have different SRIDs"); + rterror("rt_raster_fully_within_distance: The two rasters provided have different SRIDs"); return ES_ERROR; } /* distance cannot be less than zero */ if (distance < 0) { - rterror("rt_raster_distance_within: Distance cannot be less than zero"); + rterror("rt_raster_fully_within_distance: Distance cannot be less than zero"); return ES_ERROR; } /* get LWMPOLY of each band */ - surface1 = lwmpoly_as_lwgeom(rt_raster_surface(rast1, nband1, &rtn)); - if (!rtn) { - rterror("rt_raster_distance_within: Unable to get surface of the specified band from the first raster"); + if (rt_raster_surface(rast1, nband1, &surface) != ES_NONE) { + rterror("rt_raster_fully_within_distance: Unable to get surface of the specified band from the first raster"); return ES_ERROR; } - surface2 = lwmpoly_as_lwgeom(rt_raster_surface(rast2, nband2, &rtn)); - if (!rtn) { - rterror("rt_raster_distance_within: Unable to get surface of the specified band from the second raster"); + surface1 = lwmpoly_as_lwgeom(surface); + + if (rt_raster_surface(rast2, nband2, &surface) != ES_NONE) { + rterror("rt_raster_fully_within_distance: Unable to get surface of the specified band from the second raster"); lwgeom_free(surface1); return ES_ERROR; } + surface2 = lwmpoly_as_lwgeom(surface); /* either surface is NULL, test is false */ if (surface1 == NULL || surface2 == NULL) { @@ -12818,11 +12818,12 @@ rt_raster_pixel_as_polygon(rt_raster rast, int x, int y) * @param raster : the raster to convert to a multipolygon * @param nband : the 0-based band of raster rast to use * if value is less than zero, bands are ignored. - * @param noerr : if 0, error occurred + * @param *surface : raster as a surface (multipolygon). + * if all pixels are NODATA, NULL is set * - * @return the raster surface or NULL + * @return ES_NONE on success, ES_ERROR on error */ -LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { +rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface) { rt_band band = NULL; LWGEOM *mpoly = NULL; LWGEOM *tmp = NULL; @@ -12835,12 +12836,15 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { int geomscount = 0; int i = 0; - /* initialize to 0, error occurred */ - *noerr = 0; + assert(surface != NULL); - /* raster is empty, return NULL */ - if (rt_raster_is_empty(raster)) - return NULL; + /* init *surface to NULL */ + *surface = NULL; + + /* raster is empty, surface = NULL */ + if (rt_raster_is_empty(raster)) { + return ES_NONE; + } /* if nband < 0, return the convex hull as a multipolygon */ if (nband < 0) { @@ -12856,20 +12860,20 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { lwgeom_free(tmp); lwgeom_free(mpoly); - *noerr = 1; - return lwgeom_as_lwmpoly(clone); + *surface = lwgeom_as_lwmpoly(clone); + return ES_NONE; } /* check that nband is valid */ else if (nband >= rt_raster_get_num_bands(raster)) { rterror("rt_raster_surface: The band index %d is invalid", nband); - return NULL; + return ES_ERROR; } /* get band */ band = rt_raster_get_band(raster, nband); if (band == NULL) { rterror("rt_raster_surface: Error getting band %d from raster", nband); - return NULL; + return ES_ERROR; } /* band does not have a NODATA flag, return convex hull */ @@ -12886,14 +12890,13 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { lwgeom_free(tmp); lwgeom_free(mpoly); - *noerr = 1; - return lwgeom_as_lwmpoly(clone); + *surface = lwgeom_as_lwmpoly(clone); + return ES_NONE; } /* band is NODATA, return NULL */ else if (rt_band_get_isnodata_flag(band)) { RASTER_DEBUG(3, "Band is NODATA. Returning NULL"); - *noerr = 1; - return NULL; + return ES_NONE; } /* initialize GEOS */ @@ -12905,8 +12908,7 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { if (gvcount < 1) { RASTER_DEBUG(3, "All pixels of band are NODATA. Returning NULL"); if (gv != NULL) rtdealloc(gv); - *noerr = 1; - return NULL; + return ES_NONE; } /* more than 1 polygon */ else if (gvcount > 1) { @@ -12917,7 +12919,7 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { rterror("rt_raster_surface: Unable to allocate memory for pixel polygons as GEOSGeometry"); for (i = 0; i < gvcount; i++) lwpoly_free(gv[i].geom); rtdealloc(gv); - return NULL; + return ES_ERROR; } for (i = 0; i < gvcount; i++) { #if POSTGIS_DEBUG_LEVEL > 3 @@ -12950,7 +12952,7 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { for (i = 0; i < geomscount; i++) GEOSGeom_destroy(geoms[i]); rtdealloc(geoms); - return NULL; + return ES_ERROR; } /* run the union */ @@ -12968,7 +12970,7 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { #else rterror("rt_raster_surface: Unable to union the pixel polygons using GEOSUnionCascaded()"); #endif - return NULL; + return ES_ERROR; } /* convert union result from GEOSGeometry to LWGEOM */ @@ -13064,12 +13066,11 @@ LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr) { } #endif - *noerr = 1; - return lwgeom_as_lwmpoly(mpoly); + *surface = lwgeom_as_lwmpoly(mpoly); + return ES_NONE; } - *noerr = 1; - return NULL; + return ES_NONE; } /****************************************************************************** diff --git a/raster/rt_core/rt_api.h b/raster/rt_core/rt_api.h index 838c3c9d2..9dda9c057 100644 --- a/raster/rt_core/rt_api.h +++ b/raster/rt_core/rt_api.h @@ -1382,11 +1382,12 @@ LWPOLY* rt_raster_pixel_as_polygon(rt_raster raster, int x, int y); * @param raster : the raster to convert to a multipolygon * @param nband : the 0-based band of raster rast to use * if value is less than zero, bands are ignored. - * @param noerr : if 0, error occurred + * @param **surface : raster as a surface (multipolygon). + * if all pixels are NODATA, NULL is set * - * @return the raster surface or NULL + * @return ES_NONE on success, ES_ERROR on error */ -LWMPOLY* rt_raster_surface(rt_raster raster, int nband, int *noerr); +rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface); /** * Returns a set of "geomval" value, one for each group of pixel diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index 1dcce26c8..745b03eb7 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -4208,11 +4208,11 @@ Datum RASTER_getPolygon(PG_FUNCTION_ARGS) } /* get band surface */ - surface = rt_raster_surface(raster, nband - 1, &err); + err = rt_raster_surface(raster, nband - 1, &surface); rt_raster_destroy(raster); PG_FREE_IF_COPY(pgraster, 0); - if (!err) { + if (err != ES_NONE) { elog(ERROR, "RASTER_getPolygon: Could not get raster band's surface"); PG_RETURN_NULL(); } diff --git a/raster/test/core/testapi.c b/raster/test/core/testapi.c index 3b89ad204..44cfeb2be 100644 --- a/raster/test/core/testapi.c +++ b/raster/test/core/testapi.c @@ -7357,8 +7357,8 @@ static void testRasterSurface() { } } - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((0 0,0 -5,5 -5,5 0,0 0)))")); @@ -7369,8 +7369,8 @@ static void testRasterSurface() { /* 0,0 NODATA */ rt_band_set_pixel(band, 0, 0, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((1 0,1 -1,0 -1,0 -5,4 -5,5 -5,5 0,1 0)))")); @@ -7381,8 +7381,8 @@ static void testRasterSurface() { /* plus 1,1 NODATA */ rt_band_set_pixel(band, 1, 1, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((1 0,1 -1,0 -1,0 -5,4 -5,5 -5,5 0,1 0),(1 -1,1 -2,2 -2,2 -1,1 -1)))")); @@ -7393,8 +7393,8 @@ static void testRasterSurface() { /* plus 2,2 NODATA */ rt_band_set_pixel(band, 2, 2, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); #if POSTGIS_GEOS_VERSION >= 33 @@ -7409,8 +7409,8 @@ static void testRasterSurface() { /* plus 3,3 NODATA */ rt_band_set_pixel(band, 3, 3, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); #if POSTGIS_GEOS_VERSION >= 33 @@ -7425,8 +7425,8 @@ static void testRasterSurface() { /* plus 4,4 NODATA */ rt_band_set_pixel(band, 4, 4, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((4 -4,4 -5,0 -5,0 -1,1 -1,1 -2,2 -2,2 -3,3 -3,3 -4,4 -4)),((1 -1,1 0,5 0,5 -4,4 -4,4 -3,3 -3,3 -2,2 -2,2 -1,1 -1)))")); @@ -7440,8 +7440,8 @@ static void testRasterSurface() { rt_band_set_pixel(band, 1, 3, 0, NULL); rt_band_set_pixel(band, 0, 4, 0, NULL); - mpoly = rt_raster_surface(rast, 0, &err); - CHECK(err); + err = rt_raster_surface(rast, 0, &mpoly); + CHECK(err == ES_NONE); CHECK((mpoly != NULL)); wkt = lwgeom_to_text(lwmpoly_as_lwgeom(mpoly)); CHECK(!strcmp(wkt, "MULTIPOLYGON(((1 -4,2 -4,2 -3,3 -3,3 -4,4 -4,4 -5,3 -5,1 -5,1 -4)),((1 -4,0 -4,0 -1,1 -1,1 -2,2 -2,2 -3,1 -3,1 -4)),((3 -2,4 -2,4 -1,5 -1,5 -4,4 -4,4 -3,3 -3,3 -2)),((3 -2,2 -2,2 -1,1 -1,1 0,4 0,4 -1,3 -1,3 -2)))"));