/* convert set of rt_pixel to 2D array */
status = rt_pixel_set_to_array(
- npixels, status,mask,
+ npixels, status, mask,
x, y,
distancex, distancey,
&(_param->arg->values[i]),
RASTER_DEBUGF(4, "dimensions = %d x %d", dim[0], dim[1]);
/* make sure that the dimx and dimy match mask */
- if( mask != NULL) {
- if ( dim[0] != mask->dimx || dim[1] != mask->dimy ){
+ if (mask != NULL) {
+ if (dim[0] != mask->dimx || dim[1] != mask->dimy) {
rterror("rt_pixel_set_array: mask dimensions %d x %d do not match given dims %d x %d", mask->dimx, mask->dimy, dim[0], dim[1]);
return ES_ERROR;
}
- if ( mask->values == NULL || mask->nodata == NULL ) {
- rterror("rt_pixel_set_array: was not properly setup");
+ if (mask->values == NULL || mask->nodata == NULL) {
+ rterror("rt_pixel_set_array: Invalid mask");
return ES_ERROR;
}
}
+
/* establish 2D arrays (Y axis) */
values = rtalloc(sizeof(double *) * dim[1]);
nodatas = rtalloc(sizeof(int *) * dim[1]);
RASTER_DEBUGF(4, "absolute x,y: %d x %d", npixel[i].x, npixel[i].y);
RASTER_DEBUGF(4, "relative x,y: %d x %d", _x, _y);
- if ( mask == NULL ) {
- values[_y][_x] = npixel[i].value;
- nodatas[_y][_x] = 0;
- }else{
- if( mask->weighted == 0 ){
- if( FLT_EQ( mask->values[_y][_x],0) || mask->nodata[_y][_x] == 1 ){
- values[_y][_x] = 0;
- nodatas[_y][_x] = 1;
- }else{
- values[_y][_x] = npixel[i].value;
- nodatas[_y][_x] = 0;
- }
- }else{
- if( mask->nodata[_y][_x] == 1 ){
- values[_y][_x] = 0;
- nodatas[_y][_x] = 1;
- }else{
- values[_y][_x] = npixel[i].value * mask->values[_y][_x];
- nodatas[_y][_x] = 0;
- }
- }
+ /* no mask */
+ if (mask == NULL) {
+ values[_y][_x] = npixel[i].value;
+ nodatas[_y][_x] = 0;
+ }
+ /* mask */
+ else {
+ /* unweighted (boolean) mask */
+ if (mask->weighted == 0) {
+ /* pixel is set to zero or nodata */
+ if (FLT_EQ(mask->values[_y][_x],0) || mask->nodata[_y][_x] == 1) {
+ values[_y][_x] = 0;
+ nodatas[_y][_x] = 1;
+ }
+ /* use pixel */
+ else {
+ values[_y][_x] = npixel[i].value;
+ nodatas[_y][_x] = 0;
+ }
+ }
+ /* weighted mask */
+ else {
+ /* nodata */
+ if(mask->nodata[_y][_x] == 1) {
+ values[_y][_x] = 0;
+ nodatas[_y][_x] = 1;
+ }
+ /* apply weight to pixel value */
+ else {
+ values[_y][_x] = npixel[i].value * mask->values[_y][_x];
+ nodatas[_y][_x] = 0;
+ }
+ }
}
RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_y][_x], values[_y][_x]);
}
}
- noerr = 1;
-
/* mask (7) */
if( PG_ARGISNULL(7) ){
- pfree(arg->mask);
- arg->mask = NULL;
- }else{
- maskArray = PG_GETARG_ARRAYTYPE_P(7);
- etype = ARR_ELEMTYPE(maskArray);
- get_typlenbyvalalign(etype,&typlen,&typbyval,&typalign);
-
- switch(etype){
- case FLOAT4OID:
- case FLOAT8OID:
- break;
- default:
- rtpg_nmapalgebra_arg_destroy(arg);
- elog(ERROR,"RASTER_nMapAlgebra: Mask data type must be FLOAT8 or FLOAT4.");
- PG_RETURN_NULL();
+ pfree(arg->mask);
+ arg->mask = NULL;
}
+ else {
+ maskArray = PG_GETARG_ARRAYTYPE_P(7);
+ etype = ARR_ELEMTYPE(maskArray);
+ get_typlenbyvalalign(etype,&typlen,&typbyval,&typalign);
- ndims = ARR_NDIM(maskArray);
+ switch (etype) {
+ case FLOAT4OID:
+ case FLOAT8OID:
+ break;
+ default:
+ rtpg_nmapalgebra_arg_destroy(arg);
+ elog(ERROR,"RASTER_nMapAlgebra: Mask data type must be FLOAT8 or FLOAT4");
+ PG_RETURN_NULL();
+ }
+
+ ndims = ARR_NDIM(maskArray);
+
+ if (ndims != 2) {
+ elog(ERROR, "RASTER_nMapAlgebra: Mask Must be a 2D array");
+ rtpg_nmapalgebra_arg_destroy(arg);
+ PG_RETURN_NULL();
+ }
- if( ndims != 2 ){
- elog(ERROR, "RASTER_nMapAlgebra: Mask Must be a 2D array.");
- rtpg_nmapalgebra_arg_destroy(arg);
- PG_RETURN_NULL();
- }
+ maskDims = ARR_DIMS(maskArray);
+
+ if (maskDims[0] % 2 == 0 || maskDims[1] % 2 == 0) {
+ elog(ERROR,"RASTER_nMapAlgebra: Mask dimensions must be odd");
+ rtpg_nmapalgebra_arg_destroy(arg);
+ PG_RETURN_NULL();
+ }
- maskDims = ARR_DIMS(maskArray);
+ deconstruct_array(
+ maskArray,
+ etype,
+ typlen, typbyval,typalign,
+ &maskElements,&maskNulls,&num
+ );
+ if (num < 1 || num != (maskDims[0] * maskDims[1])) {
+ if (num) {
+ pfree(maskElements);
+ pfree(maskNulls);
+ }
+ elog(ERROR, "RASTER_nMapAlgebra: Could not deconstruct new values array");
+ rtpg_nmapalgebra_arg_destroy(arg);
+ PG_RETURN_NULL();
+ }
- if ( maskDims[0] % 2 == 0 || maskDims[1] % 2 == 0 ){
- elog(ERROR,"RASTER_nMapAlgebra: Mask dimensions must be odd.");
- rtpg_nmapalgebra_arg_destroy(arg);
- PG_RETURN_NULL();
- }
-
- deconstruct_array(
- maskArray,
- etype,
- typlen, typbyval,typalign,
- &maskElements,&maskNulls,&num
- );
-
- if (num < 1 || num != (maskDims[0] * maskDims[1])) {
- if (num) {
- pfree(maskElements);
- pfree(maskNulls);
- }
- elog(ERROR, "RASTER_nMapAlgebra: Could not deconstruct new values array.");
- rtpg_nmapalgebra_arg_destroy(arg);
- PG_RETURN_NULL();
- }
+ /* allocate mem for mask array */
+ arg->mask->values = palloc(sizeof(double*)* maskDims[0]);
+ arg->mask->nodata = palloc(sizeof(int*)*maskDims[0]);
+ for (i = 0; i < maskDims[0]; i++) {
+ arg->mask->values[i] = (double*) palloc(sizeof(double) * maskDims[1]);
+ arg->mask->nodata[i] = (int*) palloc(sizeof(int) * maskDims[1]);
+ }
+ /* place values in to mask */
+ i = 0;
+ for (y = 0; y < maskDims[0]; y++) {
+ for (x = 0; x < maskDims[1]; x++) {
+ if (maskNulls[i]) {
+ arg->mask->values[y][x] = 0;
+ arg->mask->nodata[y][x] = 1;
+ }
+ else {
+ switch (etype) {
+ case FLOAT4OID:
+ arg->mask->values[y][x] = (double) DatumGetFloat4(maskElements[i]);
+ arg->mask->nodata[y][x] = 0;
+ break;
+ case FLOAT8OID:
+ arg->mask->values[y][x] = (double) DatumGetFloat8(maskElements[i]);
+ arg->mask->nodata[y][x] = 0;
+ }
+ }
+ i++;
+ }
+ }
- /* allocate mem for mask array */
- arg->mask->values = palloc(sizeof(double*)* maskDims[0]);
- arg->mask->nodata = palloc(sizeof(int*)*maskDims[0]);
- for(i = 0; i < maskDims[0]; i++){
- arg->mask->values[i] = (double*) palloc(sizeof(double) * maskDims[1]);
- arg->mask->nodata[i] = (int*) palloc(sizeof(int) * maskDims[1]);
- }
- /* place values in to mask */
- i = 0;
- for( y = 0; y < maskDims[0]; y++ ){
- for( x = 0; x < maskDims[1]; x++){
- if(maskNulls[i]){
- arg->mask->values[y][x] = 0;
- arg->mask->nodata[y][x] = 1;
- }else{
- switch(etype){
- case FLOAT4OID:
- arg->mask->values[y][x] = (double) DatumGetFloat4(maskElements[i]);
- arg->mask->nodata[y][x] = 0;
- break;
- case FLOAT8OID:
- arg->mask->values[y][x] = (double) DatumGetFloat8(maskElements[i]);
- arg->mask->nodata[y][x] = 0;
- }
- }
- i++;
- }
- }
- /*set mask dimensions*/
- arg->mask->dimx = maskDims[0];
- arg->mask->dimy = maskDims[1];
- if ( maskDims[0] == 1 && maskDims[1] == 1){
- arg->distance[0] = 0;
- arg->distance[1] = 0;
- }else{
- arg->distance[0] = maskDims[0] % 2;
- arg->distance[1] = maskDims[1] % 2;
- }
+ /*set mask dimensions*/
+ arg->mask->dimx = maskDims[0];
+ arg->mask->dimy = maskDims[1];
+ if (maskDims[0] == 1 && maskDims[1] == 1) {
+ arg->distance[0] = 0;
+ arg->distance[1] = 0;
+ }
+ else {
+ arg->distance[0] = maskDims[0] % 2;
+ arg->distance[1] = maskDims[1] % 2;
+ }
}/*end if else argisnull*/
/* (8) weighted boolean */
- if (PG_ARGISNULL(8) || !PG_GETARG_BOOL(8) ){
- if ( arg->mask != NULL )
- arg->mask->weighted = 0;
+ if (PG_ARGISNULL(8) || !PG_GETARG_BOOL(8)) {
+ if (arg->mask != NULL)
+ arg->mask->weighted = 0;
}else{
- if(arg->mask !=NULL )
- arg->mask->weighted = 1;
+ if(arg->mask !=NULL)
+ arg->mask->weighted = 1;
}
+ noerr = 1;
+
/* all rasters are empty, return empty raster */
if (allempty == arg->numraster) {
elog(NOTICE, "All input rasters are empty. Returning empty raster");
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
NOTICE: All input rasters do not have bands at indicated indexes. Returning empty raster
4|(1,"{{255,255},{255,255}}")
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array.
+ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array.
+ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
NOTICE: All input rasters do not have bands at indicated indexes. Returning empty raster
4|(1,"{{255,255},{255,255}}")
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
NOTICE: All input rasters do not have bands at indicated indexes. Returning empty raster
4|(1,"{{255,255},{255,255}}")
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array.
+ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array.
+ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
NOTICE: All input rasters do not have bands at indicated indexes. Returning empty raster
4|(1,"{{255,255},{255,255}}")
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd.
+ERROR: RASTER_nMapAlgebra: Mask dimensions must be odd
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
-ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array.
+ERROR: RASTER_nMapAlgebra: Mask Must be a 2D array
NOTICE: First argument (nband) of rastbandarg at index 0 is NULL. Assuming NULL raster
NOTICE: All input rasters are NULL. Returning NULL
NOTICE: All input rasters do not have bands at indicated indexes. Returning empty raster