]> granicus.if.org Git - postgis/commitdiff
-Fix for ticket 792: Clamp double to known values when pixeltype is other than double...
authorPierre Racine <Pierre.Racine@sbf.ulaval.ca>
Fri, 18 Mar 2011 14:06:55 +0000 (14:06 +0000)
committerPierre Racine <Pierre.Racine@sbf.ulaval.ca>
Fri, 18 Mar 2011 14:06:55 +0000 (14:06 +0000)
-Added function name to many error messages
-Standardized the way we write "nodata" from "NODATA" to "nodata"
-Moved some documentation to the header file
-My editor remove a lot of trailing space and convert tab to 4 spaces. Please make your editor do the same.
-Removed declarations of many unused variable

git-svn-id: http://svn.osgeo.org/postgis/trunk@6920 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/rt_pg/rt_pg.c
raster/test/regress/rt_addband_expected
raster/test/regress/rt_io_expected
raster/test/regress/rt_pixelvalue_expected

index 051c88395169acc8d816ddd389e87962c6dabcac..1437694ad4925dd74da03a3815182e4849efa9a7 100644 (file)
@@ -75,6 +75,56 @@ flip_endian_64(uint8_t *d) {
     swap_char(d + 4, d + 3);
 }
 
+uint8_t
+rt_util_clamp_to_1BB(double value) {
+    return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_1BBMAX);
+}
+
+uint8_t
+rt_util_clamp_to_2BUI(double value) {
+    return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_2BUIMAX);
+}
+
+uint8_t
+rt_util_clamp_to_4BUI(double value) {
+    return (uint8_t)fmin(fmax((value), 0), POSTGIS_RT_4BUIMAX);
+}
+
+int8_t
+rt_util_clamp_to_8BSI(double value) {
+    return (int8_t)fmin(fmax((value), SCHAR_MIN), SCHAR_MAX);
+}
+
+uint8_t
+rt_util_clamp_to_8BUI(double value) {
+    return (uint8_t)fmin(fmax((value), 0), UCHAR_MAX);
+}
+
+int16_t
+rt_util_clamp_to_16BSI(double value) {
+    return (int16_t)fmin(fmax((value), SHRT_MIN), SHRT_MAX);
+}
+
+uint16_t
+rt_util_clamp_to_16BUI(double value) {
+    return (uint16_t)fmin(fmax((value), 0), USHRT_MAX);
+}
+
+int32_t
+rt_util_clamp_to_32BSI(double value) {
+    return (int32_t)fmin(fmax((value), INT_MIN), INT_MAX);
+}
+
+uint32_t
+rt_util_clamp_to_32BUI(double value) {
+    return (uint32_t)fmin(fmax((value), 0), UINT_MAX);
+}
+
+float
+rt_util_clamp_to_32F(double value) {
+    return (float)fmin(fmax((value), FLT_MIN), FLT_MAX);
+}
+
 /*- rt_context -------------------------------------------------------*/
 
 static void
@@ -189,6 +239,80 @@ rt_context_destroy(rt_context ctx) {
     ctx->dealloc(ctx);
 }
 
+int
+rt_util_display_dbl_trunc_warning(rt_context ctx,
+                                  double initialvalue,
+                                  int32_t checkvalint,
+                                  uint32_t checkvaluint,
+                                  float checkvalfloat,
+                                  double checkvaldouble,
+                                  rt_pixtype pixtype) {
+    int result = 0;
+    switch (pixtype)
+    {
+        case PT_1BB:
+        case PT_2BUI:
+        case PT_4BUI:
+        case PT_8BSI:
+        case PT_8BUI:
+        case PT_16BSI:
+        case PT_16BUI:
+        case PT_32BSI:
+        {
+            if (fabs(checkvalint - initialvalue) >= 1) {
+                ctx->warn("Initial pixel value for %s band got clamped from %f to %d",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvalint);
+                result = -1;
+            }
+            else if (fabs(checkvalint - initialvalue) > FLT_EPSILON) {
+                ctx->warn("Initial pixel value for %s band got truncated from %f to %d",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvalint);
+                result = -1;
+            }
+            break;
+        }
+        case PT_32BUI:
+        {
+            if (fabs(checkvaluint - initialvalue) >= 1) {
+                ctx->warn("Initial pixel value for %s band got clamped from %f to %u",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvaluint);
+                result = -1;
+            }
+            else if (fabs(checkvaluint - initialvalue) > FLT_EPSILON) {
+                ctx->warn("Initial pixel value for %s band got truncated from %f to %u",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvaluint);
+                result = -1;
+            }
+            break;
+        }
+        case PT_32BF:
+        {
+            /* For float, because the initial value is a double,
+            there is very often a difference between the desired value and the obtained one */
+            if (fabs(checkvalfloat - initialvalue) > FLT_EPSILON)
+                ctx->warn("Initial pixel value for %s band got converted from %f to %f",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvalfloat);
+            break;
+        }
+        case PT_64BF:
+        {
+            if (fabs(checkvaldouble - initialvalue) > FLT_EPSILON)
+                ctx->warn("Initial pixel value for %s band got converted from %f to %f",
+                    rt_pixtype_name(ctx, pixtype),
+                    initialvalue, checkvaldouble);
+            break;
+        }
+        case PT_END:
+            break;
+    }
+    return result;
+}
+
 /*--- Debug and Testing Utilities --------------------------------------------*/
 
 #if POSTGIS_DEBUG_LEVEL > 3
@@ -205,7 +329,7 @@ d_binary_to_hex(rt_context ctx, const uint8_t * const raw, uint32_t size, uint32
     *hexsize = size * 2; /* hex is 2 times bytes */
     hex = (char*) ctx->alloc((*hexsize) + 1);
     if (!hex) {
-        ctx->err("Out of memory hexifying raw binary\n");
+        ctx->err("d_binary_to_hex: Out of memory hexifying raw binary\n");
         return NULL;
     }
     hex[*hexsize] = '\0'; /* Null-terminate */
@@ -283,7 +407,7 @@ rt_pixtype_size(rt_context ctx, rt_pixtype pixtype) {
             pixbytes = 8;
             break;
         default:
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_pixtype_size: Unknown pixeltype %d", pixtype);
             pixbytes = -1;
             break;
     }
@@ -357,7 +481,7 @@ rt_pixtype_name(rt_context ctx, rt_pixtype pixtype) {
         case PT_64BF:
             return "64BF";
         default:
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_pixtype_name: Unknown pixeltype %d", pixtype);
             return "Unknown";
     }
 }
@@ -398,7 +522,7 @@ rt_band_new_inline(rt_context ctx, uint16_t width, uint16_t height,
 
     band = ctx->alloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("Out of memory allocating rt_band");
+        ctx->err("rt_band_new_inline: Out of memory allocating rt_band");
         return 0;
     }
 
@@ -430,7 +554,7 @@ rt_band_new_offline(rt_context ctx, uint16_t width, uint16_t height,
 
     band = ctx->alloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("Out of memory allocating rt_band");
+        ctx->err("rt_band_new_offline: Out of memory allocating rt_band");
         return 0;
     }
 
@@ -635,88 +759,91 @@ rt_band_get_isnodata_flag(rt_context ctx, rt_band band) {
 int
 rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
     rt_pixtype pixtype = PT_END;
-    double oldnodataval = band->nodataval;
+    //double oldnodataval = band->nodataval;
+
+    int32_t checkvalint = 0;
+    uint32_t checkvaluint = 0;
+    float checkvalfloat = 0;
+    double checkvaldouble = 0;
 
     assert(NULL != ctx);
     assert(NULL != band);
 
     pixtype = band->pixtype;
 
-    RASTER_DEBUGF(3, "rt_band_set_nodata: setting NODATA %g with band type %s", val, rt_pixtype_name(ctx, pixtype));
+    RASTER_DEBUGF(3, "rt_band_set_nodata: setting nodata value %g with band type %s", val, rt_pixtype_name(ctx, pixtype));
 
     /* return -1 on out of range */
     switch (pixtype) {
         case PT_1BB:
         {
-            uint8_t v = val;
-            v &= 0x01;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_1BB(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_2BUI:
         {
-            uint8_t v = val;
-            v &= 0x03;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_2BUI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_4BUI:
         {
-            uint8_t v = val;
-            v &= 0x0F;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_4BUI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_8BSI:
         {
-            int8_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_8BSI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_8BUI:
         {
-            uint8_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_8BUI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_16BSI:
         {
-            int16_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_16BSI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_16BUI:
         {
-            uint16_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_16BUI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_32BSI:
         {
-            int32_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_32BSI(val);
+            checkvalint = band->nodataval;
             break;
         }
         case PT_32BUI:
         {
-            uint32_t v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_32BUI(val);
+            checkvaluint = band->nodataval;
             break;
         }
         case PT_32BF:
         {
-            float v = val;
-            band->nodataval = v;
+            band->nodataval = rt_util_clamp_to_32F(val);
+            checkvalfloat = band->nodataval;
             break;
         }
         case PT_64BF:
         {
             band->nodataval = val;
+            checkvaldouble = band->nodataval;
             break;
         }
         default:
             {
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_band_set_nodata: Unknown pixeltype %d", pixtype);
             band->hasnodata = 0;
             return -1;
         }
@@ -726,21 +853,17 @@ rt_band_set_nodata(rt_context ctx, rt_band band, double val) {
     RASTER_DEBUGF(3, "rt_band_set_nodata: band->nodataval = %d", band->nodataval);
 
 
-    // the NODATA value was just set, so this band has NODATA
+    // the nodata value was just set, so this band has NODATA
     rt_band_set_hasnodata_flag(ctx, band, 1);
 
-    if (fabs(band->nodataval - val) > FLT_EPSILON) {
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
-        ctx->warn("rt_band_set_nodata: NODATA value for %s band got truncated"
-                " from %g to %g",
-                rt_pixtype_name(ctx, pixtype),
-                val, band->nodataval);
-#endif
+    if (rt_util_display_dbl_trunc_warning(ctx, val, checkvalint, checkvaluint, checkvalfloat,
+                                      checkvaldouble, pixtype))
         return -1;
-    }
+#endif
 
     /* If the nodata value is different from the previous one, we need to check
-     * again if the band is a NODATA band
+     * again if the band is a nodata band
      * TODO: NO, THAT'S TOO SLOW!!!
      */
 
@@ -758,6 +881,12 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
     rt_pixtype pixtype = PT_END;
     unsigned char* data = NULL;
     uint32_t offset = 0;
+
+    int32_t checkvalint = 0;
+    uint32_t checkvaluint = 0;
+    float checkvalfloat = 0;
+    double checkvaldouble = 0;
+
     double checkval = 0;
 
     assert(NULL != ctx);
@@ -766,7 +895,7 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
     pixtype = band->pixtype;
 
     if (x >= band->width || y >= band->height) {
-        ctx->err("Coordinates out of range");
+        ctx->err("rt_band_set_pixel: Coordinates out of range");
         return -1;
     }
 
@@ -781,93 +910,89 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
     switch (pixtype) {
         case PT_1BB:
         {
-            data[offset] = (int) val & 0x01;
-            checkval = data[offset];
+            data[offset] = rt_util_clamp_to_1BB(val);
+            checkvalint = data[offset];
             break;
         }
         case PT_2BUI:
         {
-            data[offset] = (int) val & 0x03;
-            checkval = data[offset];
+            data[offset] = rt_util_clamp_to_2BUI(val);
+            checkvalint = data[offset];
             break;
         }
         case PT_4BUI:
         {
-            data[offset] = (int) val & 0x0F;
-            checkval = data[offset];
+            data[offset] = rt_util_clamp_to_4BUI(val);
+            checkvalint = data[offset];
             break;
         }
         case PT_8BSI:
         {
-            data[offset] = (int8_t) val;
-            checkval = (int8_t) data[offset];
+            data[offset] = rt_util_clamp_to_8BSI(val);
+            checkvalint = (int8_t) data[offset];
             break;
         }
         case PT_8BUI:
         {
-            data[offset] = val;
-            checkval = data[offset];
+            data[offset] = rt_util_clamp_to_8BUI(val);
+            checkvalint = data[offset];
             break;
         }
         case PT_16BSI:
         {
             int16_t *ptr = (int16_t*) data; /* we assume correct alignment */
-            ptr[offset] = val;
-            checkval = (int16_t) ptr[offset];
+            ptr[offset] = rt_util_clamp_to_16BSI(val);
+            checkvalint = (int16_t) ptr[offset];
             break;
         }
         case PT_16BUI:
         {
             uint16_t *ptr = (uint16_t*) data; /* we assume correct alignment */
-            ptr[offset] = val;
-            checkval = ptr[offset];
+            ptr[offset] = rt_util_clamp_to_16BUI(val);
+            checkvalint = ptr[offset];
             break;
         }
         case PT_32BSI:
         {
             int32_t *ptr = (int32_t*) data; /* we assume correct alignment */
-            ptr[offset] = val;
-            checkval = (int32_t) ptr[offset];
+            ptr[offset] = rt_util_clamp_to_32BSI(val);
+            checkvalint = (int32_t) ptr[offset];
             break;
         }
         case PT_32BUI:
         {
             uint32_t *ptr = (uint32_t*) data; /* we assume correct alignment */
-            ptr[offset] = val;
-            checkval = ptr[offset];
+            ptr[offset] = rt_util_clamp_to_32BUI(val);
+            checkvaluint = ptr[offset];
             break;
         }
         case PT_32BF:
         {
             float *ptr = (float*) data; /* we assume correct alignment */
-            ptr[offset] = val;
-            checkval = ptr[offset];
+            ptr[offset] = rt_util_clamp_to_32F(val);
+            checkvalfloat = ptr[offset];
             break;
         }
         case PT_64BF:
         {
             double *ptr = (double*) data; /* we assume correct alignment */
             ptr[offset] = val;
-            checkval = ptr[offset];
+            checkvaldouble = ptr[offset];
             break;
         }
         default:
         {
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_band_set_pixel: Unknown pixeltype %d", pixtype);
             return -1;
         }
     }
 
     /* Overflow checking */
-    if (fabs(checkval - val) > FLT_EPSILON) {
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
-        ctx->warn("Pixel value for %s band got truncated"
-                " from %g to %g",
-                rt_pixtype_name(ctx, band->pixtype),
-                val, checkval);
+    if (rt_util_display_dbl_trunc_warning(ctx, val, checkvalint, checkvaluint, checkvalfloat,
+                                      checkvaldouble, pixtype))
+       return -1;
 #endif /* POSTGIS_RASTER_WARN_ON_TRUNCATION */
-        return -1;
-    }
 
     /* If the stored value is different from no data, reset the isnodata flag */
     if (fabs(checkval - band->nodataval) > FLT_EPSILON) {
@@ -875,7 +1000,7 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
     }
 
     /*
-     * If the pixel was a NODATA value, now the band may be NODATA band)
+     * If the pixel was a nodata value, now the band may be NODATA band)
      * TODO: NO, THAT'S TOO SLOW!!!
      */
 
@@ -884,7 +1009,7 @@ rt_band_set_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y,
         rt_band_check_is_nodata(ctx, band);
     }
     */
-    
+
 
     return 0;
 }
@@ -1003,7 +1128,7 @@ rt_band_get_pixel(rt_context ctx, rt_band band, uint16_t x, uint16_t y, double *
         }
         default:
         {
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_band_get_pixel: Unknown pixeltype %d", pixtype);
             return -1;
         }
     }
@@ -1015,19 +1140,14 @@ rt_band_get_nodata(rt_context ctx, rt_band band) {
     assert(NULL != band);
 
     if (!band->hasnodata)
-        RASTER_DEBUGF(3, "Getting NODATA value for a band without NODATA values. Using %g", band->nodataval);
-        
+        RASTER_DEBUGF(3, "Getting nodata value for a band without NODATA values. Using %g", band->nodataval);
+
 
     return band->nodataval;
 }
 
-/**
- * Returns the minimal possible value for the band according to the pixel type.
- * @param ctx: context, for thread safety
- * @param band: the band to get info from
- * @return the minimal possible value for the band.
- */
-double rt_band_get_min_value(rt_context ctx, rt_band band) {
+double
+rt_band_get_min_value(rt_context ctx, rt_band band) {
     rt_pixtype pixtype = PT_END;
 
     assert(NULL != ctx);
@@ -1040,22 +1160,18 @@ double rt_band_get_min_value(rt_context ctx, rt_band band) {
         {
             return (double)CHAR_MIN;
         }
-        
         case PT_8BSI:
         {
             return (double)SCHAR_MIN;
         }
-        
         case PT_16BSI: case PT_16BUI:
         {
             return (double)SHRT_MIN;
         }
-        
         case PT_32BSI: case PT_32BUI:
         {
             return (double)INT_MIN;
         }
-              
         case PT_32BF:
         {
             return (double)FLT_MIN;
@@ -1064,23 +1180,17 @@ double rt_band_get_min_value(rt_context ctx, rt_band band) {
         {
             return (double)DBL_MIN;
         }
-
         default:
         {
-            ctx->err("Unknown pixeltype %d", pixtype);
+            ctx->err("rt_band_get_min_value: Unknown pixeltype %d", pixtype);
             return (double)CHAR_MIN;
         }
     }
 }
 
 
-/**
- * Returns TRUE if the band is only nodata values
- * @param ctx: context, for thread safety
- * @param band: the band to get info from
- * @return TRUE if the band is only nodata values, FALSE otherwise
- */
-int rt_band_check_is_nodata(rt_context ctx, rt_band band)
+int
+rt_band_check_is_nodata(rt_context ctx, rt_band band)
 {
     int i, j;
     double pxValue = band->nodataval;
@@ -1091,15 +1201,15 @@ int rt_band_check_is_nodata(rt_context ctx, rt_band band)
 
     /* Check if band has nodata value */
     if (!band->hasnodata)
-    {        
-        RASTER_DEBUG(3, "Unknown NODATA value for band");
+    {
+        RASTER_DEBUG(3, "Unknown nodata value for band");
         band->isnodata = FALSE;
         return FALSE;
     }
-    
+
     /* TODO: How to know it in case of offline bands? */
     if (band->offline) {
-        RASTER_DEBUG(3, "Unknown NODATA value for OFFDB band");
+        RASTER_DEBUG(3, "Unknown nodata value for OFFDB band");
         band->isnodata = FALSE;
     }
 
@@ -1111,7 +1221,7 @@ int rt_band_check_is_nodata(rt_context ctx, rt_band band)
             rt_band_get_pixel(ctx, band, i, j, &pxValue);
             dEpsilon = fabs(pxValue - band->nodataval);
             if (dEpsilon > FLT_EPSILON) {
-                band->isnodata = FALSE;                
+                band->isnodata = FALSE;
                 return FALSE;
             }
         }
@@ -1189,7 +1299,7 @@ rt_raster_new(rt_context ctx, uint16_t width, uint16_t height) {
 
     ret = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
     if (!ret) {
-        ctx->err("Out of virtual memory creating an rt_raster");
+        ctx->err("rt_raster_new: Out of virtual memory creating an rt_raster");
         return 0;
     }
 
@@ -1363,7 +1473,7 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
     RASTER_DEBUGF(3, "Adding band %p to raster %p", band, raster);
 
     if (band->width != raster->width) {
-        ctx->err("Can't add a %dx%d band to a %dx%d raster",
+        ctx->err("rt_raster_add_band: Can't add a %dx%d band to a %dx%d raster",
                 band->width, band->height, raster->width, raster->height);
         return -1;
     }
@@ -1383,14 +1493,14 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
             );
 
     if (!raster->bands) {
-        ctx->err("Out of virtual memory "
+        ctx->err("rt_raster_add_band: Out of virtual memory "
                 "reallocating band pointers");
         raster->bands = oldbands;
         return -1;
     }
 
     RASTER_DEBUGF(4, "realloc returned %p", raster->bands);
-    
+
     for (i = 0; i <= raster->numBands; ++i) {
         if (i == index) {
             oldband = raster->bands[i];
@@ -1411,9 +1521,9 @@ rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int index) {
 
 
 int32_t
-rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype, 
+rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype,
         double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
-{    
+{
     rt_band band = NULL;
     int width = 0;
     int height = 0;
@@ -1427,8 +1537,8 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
     double checkvaldouble = 0;
     float checkvalfloat = 0;
     int i;
-    
+
+
     assert(NULL != ctx);
     assert(NULL != raster);
 
@@ -1447,7 +1557,7 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
 
     mem = (int *)ctx->alloc(datasize);
     if (!mem) {
-        ctx->err("Could not allocate memory for band");
+        ctx->err("rt_raster_generate_new_band: Could not allocate memory for band");
         return -1;
     }
 
@@ -1459,80 +1569,90 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
             case PT_1BB:
             {
                 uint8_t *ptr = mem;
+                uint8_t clamped_initval = rt_util_clamp_to_1BB(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint8_t) initialvalue&0x01;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_2BUI:
             {
                 uint8_t *ptr = mem;
+                uint8_t clamped_initval = rt_util_clamp_to_2BUI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint8_t) initialvalue&0x03;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_4BUI:
             {
                 uint8_t *ptr = mem;
+                uint8_t clamped_initval = rt_util_clamp_to_4BUI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint8_t) initialvalue&0x0F;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_8BSI:
             {
                 int8_t *ptr = mem;
+                int8_t clamped_initval = rt_util_clamp_to_8BSI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (int8_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_8BUI:
             {
                 uint8_t *ptr = mem;
+                uint8_t clamped_initval = rt_util_clamp_to_8BUI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint8_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_16BSI:
             {
                 int16_t *ptr = mem;
+                int16_t clamped_initval = rt_util_clamp_to_16BSI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (int16_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_16BUI:
             {
                 uint16_t *ptr = mem;
+                uint16_t clamped_initval = rt_util_clamp_to_16BUI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint16_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_32BSI:
             {
                 int32_t *ptr = mem;
+                int32_t clamped_initval = rt_util_clamp_to_32BSI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (int32_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalint = ptr[0];
                 break;
             }
             case PT_32BUI:
             {
                 uint32_t *ptr = mem;
+                uint32_t clamped_initval = rt_util_clamp_to_32BUI(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (uint32_t) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvaluint = ptr[0];
                 break;
             }
             case PT_32BF:
             {
                 float *ptr = mem;
+                float clamped_initval = rt_util_clamp_to_32F(initialvalue);
                 for (i = 0; i < numval; i++)
-                    ptr[i] = (float) initialvalue;
+                    ptr[i] = clamped_initval;
                 checkvalfloat = ptr[0];
                 break;
             }
@@ -1546,7 +1666,7 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
             }
             default:
             {
-                ctx->err("Unknown pixeltype %d", pixtype);
+                ctx->err("rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
                 ctx->dealloc(mem);
                 return -1;
             }
@@ -1555,65 +1675,23 @@ rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype
 
 #ifdef POSTGIS_RASTER_WARN_ON_TRUNCATION
     /* Overflow checking */
-    switch (pixtype)
-    {
-        case PT_1BB:
-        case PT_2BUI:
-        case PT_4BUI:
-        case PT_8BSI:
-        case PT_8BUI:
-        case PT_16BSI:
-        case PT_16BUI:
-        case PT_32BSI:
-        {
-            if (fabs(checkvalint - initialvalue) > FLT_EPSILON)
-                ctx->warn("Initial pixel value for %s band got truncated from %f to %d",
-                    rt_pixtype_name(ctx, pixtype),
-                    initialvalue, checkvalint);
-            break;
-        }
-        case PT_32BUI:
-        {
-            if (fabs(checkvaluint - initialvalue) > FLT_EPSILON)
-                ctx->warn("Initial pixel value for %s band got truncated from %f to %u",
-                    rt_pixtype_name(ctx, pixtype),
-                    initialvalue, checkvaluint);
-            break;
-        }
-        case PT_32BF:
-        {
-            /* For float, because the initial value is a double, 
-            there is very often a difference between the desired value and the obtained one */
-            if (fabs(checkvalfloat - initialvalue) > FLT_EPSILON)
-                ctx->warn("Initial pixel value for %s band got truncated from %f to %g",
-                    rt_pixtype_name(ctx, pixtype),
-                    initialvalue, checkvalfloat);
-            break;
-        }
-        case PT_64BF:
-        {
-            if (fabs(checkvaldouble - initialvalue) > FLT_EPSILON)
-                ctx->warn("Initial pixel value for %s band got truncated from %f to %g",
-                    rt_pixtype_name(ctx, pixtype),
-                    initialvalue, checkvaldouble);
-            break;
-        }
-    }
+    rt_util_display_dbl_trunc_warning(ctx, initialvalue, checkvalint, checkvaluint, checkvalfloat,
+                                      checkvaldouble, pixtype);
 #endif /* POSTGIS_RASTER_WARN_ON_TRUNCATION */
 
     band = rt_band_new_inline(ctx, width, height, pixtype, hasnodata, nodatavalue, mem);
     if (! band) {
-        ctx->err("Could not add band to raster. Aborting");
+        ctx->err("rt_raster_generate_new_band: Could not add band to raster. Aborting");
         ctx->dealloc(mem);
         return -1;
     }
     index = rt_raster_add_band(ctx, raster, band, index);
     numbands = rt_raster_get_num_bands(ctx, raster);
     if (numbands == oldnumbands || index == -1) {
-        ctx->err("Could not add band to raster. Aborting");
+        ctx->err("rt_raster_generate_new_band: Could not add band to raster. Aborting");
         rt_band_destroy(ctx, band);
     }
-    
+
     return index;
 }
 
@@ -1638,7 +1716,7 @@ rt_raster_cell_to_geopoint(rt_context ctx, rt_raster raster,
 
 }
 
-/* WKT string representing each polygon in WKT format acompagned by its 
+/* WKT string representing each polygon in WKT format acompagned by its
 correspoding value */
 struct rt_geomval_t {
     int srid;
@@ -1689,7 +1767,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      *******************************/
     band = rt_raster_get_band(ctx, raster, nband - 1);
     if (NULL == band) {
-        ctx->err("Error getting band %d from raster", nband);
+        ctx->err("rt_raster_dump_as_wktpolygons: Error getting band %d from raster", nband);
         return 0;
     }
 
@@ -1709,7 +1787,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     memdatasource = OGR_Dr_CreateDataSource(ogr_drv, "", NULL);
 
     if (NULL == memdatasource) {
-        ctx->err("Couldn't create a OGR Datasource to store pols\n");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a OGR Datasource to store pols\n");
         return 0;
     }
 
@@ -1717,7 +1795,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      * Can MEM driver create new layers?
      **/
     if (!OGR_DS_TestCapability(memdatasource, ODsCCreateLayer)) {
-        ctx->err("MEM driver can't create new layers, aborting\n");
+        ctx->err("rt_raster_dump_as_wktpolygons: MEM driver can't create new layers, aborting\n");
         /* xxx jorgearevalo: what should we do now? */
         OGRReleaseDataSource(memdatasource);
         return 0;
@@ -1740,7 +1818,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             rt_band_get_height(ctx, band), 0, GDT_Byte, NULL);
 
     if (NULL == memdataset) {
-        ctx->err("Couldn't create a GDALDataset to polygonize it\n");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create a GDALDataset to polygonize it\n");
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
         OGRReleaseDataSource(memdatasource);
@@ -1833,7 +1911,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     ctx->dealloc(pszDataPointer);
 
     if (GDALAddBand(memdataset, nPixelType, apszOptions) == CE_Failure) {
-        ctx->err("Couldn't transform WKT Raster band in GDALRasterBand format to polygonize it");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't transform raster band in GDALRasterBand format to polygonize it");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1844,7 +1922,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
 
     /* Checking */
     if (GDALGetRasterCount(memdataset) != 1) {
-        ctx->err("Error creating GDAL MEM raster bands");
+        ctx->err("rt_raster_dump_as_wktpolygons: Error creating GDAL MEM raster bands");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1868,7 +1946,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             wkbPolygon, NULL);
 
     if (NULL == hLayer) {
-        ctx->err("Couldn't create layer to store polygons");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't create layer to store polygons");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1898,7 +1976,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
     /* Get GDAL raster band */
     gdal_band = GDALGetRasterBand(memdataset, 1);
     if (NULL == gdal_band) {
-        ctx->err("Couldn't get GDAL band to polygonize");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't get GDAL band to polygonize");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -1923,7 +2001,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
      **/
     GDALPolygonize(gdal_band, NULL, hLayer, iPixVal, NULL, NULL, NULL);
 
-    /********************************************************************* 
+    /*********************************************************************
      * Transform OGR layers in WKT polygons
      * XXX jorgearevalo: GDALPolygonize does not set the coordinate system
      * on the output layer. Application code should do this when the layer
@@ -1967,8 +2045,8 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
 
 
             /**
-             * The field was stored as int, but we can use this function 
-             * because uses "atof" to transform the string representation of 
+             * The field was stored as int, but we can use this function
+             * because uses "atof" to transform the string representation of
              * the number into a double. We shouldn't have problems
              **/
             dValueToCompare = OGR_F_GetFieldAsDouble(hFeature, iPixVal);
@@ -1994,7 +2072,7 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             sizeof (struct rt_geomval_t));
 
     if (NULL == pols) {
-        ctx->err("Couldn't allocate memory for geomval structure");
+        ctx->err("rt_raster_dump_as_wktpolygons: Couldn't allocate memory for geomval structure");
         GDALClose(memdataset);
         GDALDeregisterDriver(gdal_drv);
         GDALDestroyDriver(gdal_drv);
@@ -2035,12 +2113,12 @@ rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
             nCont++;
 
             /**
-             * We can't use deallocator from rt_context, because it comes from 
+             * We can't use deallocator from rt_context, because it comes from
              * postgresql backend, that uses pfree. This function needs a
              * postgresql memory context to work with, and the memory created
              * for pszSrcText is created outside this context.
              **/
-            //ctx->dealloc(pszSrcText);            
+            //ctx->dealloc(pszSrcText);
             free(pszSrcText);
             pszSrcText = NULL;
         }
@@ -2085,14 +2163,14 @@ rt_raster_get_convex_hull(rt_context ctx, rt_raster raster) {
 
     rings = (POINTARRAY **) ctx->alloc(sizeof (POINTARRAY*));
     if (!rings) {
-        ctx->err("Out of memory [%s:%d]", __FILE__, __LINE__);
+        ctx->err("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
         return 0;
     }
     rings[0] = ptarray_construct(0, 0, 5);
     /* TODO: handle error on ptarray construction */
     /* XXX jorgearevalo: the error conditions aren't managed in ptarray_construct */
     if (!rings[0]) {
-        ctx->err("Out of memory [%s:%d]", __FILE__, __LINE__);
+        ctx->err("rt_raster_get_convex_hull: Out of memory [%s:%d]", __FILE__, __LINE__);
         return 0;
     }
     pts = rings[0];
@@ -2437,19 +2515,19 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     band = ctx->alloc(sizeof (struct rt_band_t));
     if (!band) {
-        ctx->err("Out of memory allocating rt_band during WKB parsing");
+        ctx->err("rt_band_from_wkb: Out of memory allocating rt_band during WKB parsing");
         return 0;
     }
 
     if (end - *ptr < 1) {
-        ctx->err("Premature end of WKB on band reading (%s:%d)",
+        ctx->err("rt_band_from_wkb: Premature end of WKB on band reading (%s:%d)",
                 __FILE__, __LINE__);
         return 0;
     }
     type = read_uint8(ptr);
 
     if ((type & BANDTYPE_PIXTYPE_MASK) >= PT_END) {
-        ctx->err("Invalid pixtype %d", type & BANDTYPE_PIXTYPE_MASK);
+        ctx->err("rt_band_from_wkb: Invalid pixtype %d", type & BANDTYPE_PIXTYPE_MASK);
         ctx->dealloc(band);
         return 0;
     }
@@ -2471,7 +2549,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     pixbytes = rt_pixtype_size(ctx, band->pixtype);
     if (((*ptr) + pixbytes) >= end) {
-        ctx->err("Premature end of WKB on band novalue reading");
+        ctx->err("rt_band_from_wkb: Premature end of WKB on band novalue reading");
         ctx->dealloc(band);
         return 0;
     }
@@ -2535,7 +2613,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
         }
         default:
         {
-            ctx->err("Unknown pixeltype %d", band->pixtype);
+            ctx->err("rt_band_from_wkb: Unknown pixeltype %d", band->pixtype);
             ctx->dealloc(band);
             return 0;
         }
@@ -2546,7 +2624,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     if (band->offline) {
         if (((*ptr) + 1) >= end) {
-            ctx->err("Premature end of WKB on offline "
+            ctx->err("rt_band_from_wkb: Premature end of WKB on offline "
                     "band data bandNum reading (%s:%d)",
                     __FILE__, __LINE__);
             ctx->dealloc(band);
@@ -2559,7 +2637,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
             sz = 0;
             while ((*ptr)[sz] && &((*ptr)[sz]) < end) ++sz;
             if (&((*ptr)[sz]) >= end) {
-                ctx->err("Premature end of WKB on band offline path reading");
+                ctx->err("rt_band_from_wkb: Premature end of WKB on band offline path reading");
                 ctx->dealloc(band);
                 return 0;
             }
@@ -2575,7 +2653,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
             *ptr += sz + 1;
 
-            /* TODO: How could we know if the offline band is a NODATA band? */
+            /* TODO: How could we know if the offline band is a nodata band? */
             band->isnodata = FALSE;
         }
         return band;
@@ -2584,7 +2662,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
     /* This is an on-disk band */
     sz = width * height * pixbytes;
     if (((*ptr) + sz) > end) {
-        ctx->err("Premature end of WKB on band data reading (%s:%d)",
+        ctx->err("rt_band_from_wkb: Premature end of WKB on band data reading (%s:%d)",
                 __FILE__, __LINE__);
         ctx->dealloc(band);
         return 0;
@@ -2592,7 +2670,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
 
     band->data.mem = ctx->alloc(sz);
     if (!band->data.mem) {
-        ctx->err("Out of memory during band creation in WKB parser");
+        ctx->err("rt_band_from_wkb: Out of memory during band creation in WKB parser");
         ctx->dealloc(band);
         return 0;
     }
@@ -2612,7 +2690,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
                 else if (pixbytes == 4) flipper = flip_endian_32;
                 else if (pixbytes == 8) flipper = flip_endian_64;
                 else {
-                    ctx->err("Unexpected pix bytes %d", pixbytes);
+                    ctx->err("rt_band_from_wkb: Unexpected pix bytes %d", pixbytes);
                     ctx->dealloc(band);
                     ctx->dealloc(band->data.mem);
                     return 0;
@@ -2641,7 +2719,7 @@ rt_band_from_wkb(rt_context ctx, uint16_t width, uint16_t height,
             for (v = 0; v < sz; ++v) {
                 val = ((uint8_t*) band->data.mem)[v];
                 if (val > maxVal) {
-                    ctx->err("Invalid value %d for pixel of type %s",
+                    ctx->err("rt_band_from_wkb: Invalid value %d for pixel of type %s",
                             val, rt_pixtype_name(ctx, band->pixtype));
                     ctx->dealloc(band->data.mem);
                     ctx->dealloc(band);
@@ -2701,7 +2779,7 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
     /* Read other components of raster header */
     rast = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
     if (!rast) {
-        ctx->err("Out of memory allocating raster for wkb input");
+        ctx->err("rt_raster_from_wkb: Out of memory allocating raster for wkb input");
         return 0;
     }
     rast->numBands = read_uint16(&ptr, endian);
@@ -2752,7 +2830,7 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
     /* Now read the bands */
     rast->bands = (rt_band*) ctx->alloc(sizeof (rt_band) * rast->numBands);
     if (!rast->bands) {
-        ctx->err("Out of memory allocating bands for WKB raster decoding");
+        ctx->err("rt_raster_from_wkb: Out of memory allocating bands for WKB raster decoding");
         ctx->dealloc(rast);
         return 0;
     }
@@ -2767,7 +2845,7 @@ rt_raster_from_wkb(rt_context ctx, const uint8_t* wkb, uint32_t wkbsize) {
         rt_band band = rt_band_from_wkb(ctx, rast->width, rast->height,
                 &ptr, wkbend, endian);
         if (!band) {
-            ctx->err("Error reading WKB form of band %d", i);
+            ctx->err("rt_raster_from_wkb: Error reading WKB form of band %d", i);
             ctx->dealloc(rast);
             /* TODO: dealloc any previously allocated band too ! */
             return 0;
@@ -2801,14 +2879,14 @@ rt_raster_from_hexwkb(rt_context ctx, const char* hexwkb,
     RASTER_DEBUGF(3, "rt_raster_from_hexwkb: input wkb: %s", hexwkb);
 
     if (hexwkbsize % 2) {
-        ctx->err("Raster HEXWKB input must have an even number of characters");
+        ctx->err("rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters");
         return 0;
     }
     wkbsize = hexwkbsize / 2;
 
     wkb = ctx->alloc(wkbsize);
     if (!wkb) {
-        ctx->err("Out of memory allocating memory for decoding HEXWKB");
+        ctx->err("rt_raster_from_hexwkb: Out of memory allocating memory for decoding HEXWKB");
         return 0;
     }
 
@@ -2842,7 +2920,7 @@ rt_raster_wkb_size(rt_context ctx, rt_raster raster) {
         RASTER_DEBUGF(3, "rt_raster_wkb_size: adding size of band %d", i);
 
         if (pixbytes < 1) {
-            ctx->err("Corrupted band: unkonwn pixtype");
+            ctx->err("rt_raster_wkb_size: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -2890,7 +2968,7 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
 
     wkb = (uint8_t*) ctx->alloc(*wkbsize);
     if (!wkb) {
-        ctx->err("Out of memory allocating WKB for raster");
+        ctx->err("rt_raster_to_wkb: Out of memory allocating WKB for raster");
         return 0;
     }
 
@@ -2927,7 +3005,7 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
                 d_binptr_to_pos(ptr, wkbend, *wkbsize));
 
         if (pixbytes < 1) {
-            ctx->err("Corrupted band: unkonwn pixtype");
+            ctx->err("rt_raster_to_wkb: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3000,7 +3078,7 @@ rt_raster_to_wkb(rt_context ctx, rt_raster raster, uint32_t *wkbsize) {
                 break;
             }
             default:
-                ctx->err("Fatal error caused by unknown pixel type. Aborting.");
+                ctx->err("rt_raster_to_wkb: Fatal error caused by unknown pixel type. Aborting.");
                 abort(); /* shoudn't happen */
                 return 0;
         }
@@ -3063,7 +3141,7 @@ rt_raster_to_hexwkb(rt_context ctx, rt_raster raster, uint32_t *hexwkbsize) {
     hexwkb = (char*) ctx->alloc((*hexwkbsize) + 1);
     if (!hexwkb) {
         ctx->dealloc(wkb);
-        ctx->err("Out of memory hexifying raster WKB");
+        ctx->err("rt_raster_to_hexwkb: Out of memory hexifying raster WKB");
         return 0;
     }
     hexwkb[*hexwkbsize] = '\0'; /* Null-terminate */
@@ -3098,7 +3176,7 @@ rt_raster_serialized_size(rt_context ctx, rt_raster raster) {
         int pixbytes = rt_pixtype_size(ctx, pixtype);
 
         if (pixbytes < 1) {
-            ctx->err("Corrupted band: unknown pixtype");
+            ctx->err("rt_raster_serialized_size: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3143,7 +3221,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
 
     ret = (uint8_t*) ctx->alloc(size);
     if (!ret) {
-        ctx->err("Out of memory allocating %d bytes for serializing a raster",
+        ctx->err("rt_raster_serialize: Out of memory allocating %d bytes for serializing a raster",
                 size);
         return 0;
     }
@@ -3172,7 +3250,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
 
     RASTER_DEBUG(3, "Start hex dump of raster being serialized using 0x2D to mark non-written bytes");
 
-#if POSTGIS_DEBUG_LEVEL > 2 
+#if POSTGIS_DEBUG_LEVEL > 2
     uint8_t* dbg_ptr = ptr;
     d_print_binary_hex(ctx, "HEADER", dbg_ptr, size);
 #endif
@@ -3187,7 +3265,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         rt_pixtype pixtype = band->pixtype;
         int pixbytes = rt_pixtype_size(ctx, pixtype);
         if (pixbytes < 1) {
-            ctx->err("Corrupted band: unkonwn pixtype");
+            ctx->err("rt_raster_serialize: Corrupted band: unknown pixtype");
             return 0;
         }
 
@@ -3272,7 +3350,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
                 break;
             }
             default:
-                ctx->err("Fatal error caused by unknown pixel type. Aborting.");
+                ctx->err("rt_raster_serialize: Fatal error caused by unknown pixel type. Aborting.");
                 abort(); /* shoudn't happen */
                 return 0;
         }
@@ -3281,7 +3359,7 @@ rt_raster_serialize(rt_context ctx, rt_raster raster) {
         assert(!((uintptr_t) ptr % pixbytes));
 
 #if POSTGIS_DEBUG_LEVEL > 2
-        d_print_binary_hex(ctx, "NODATA", dbg_ptr, size);
+        d_print_binary_hex(ctx, "nodata", dbg_ptr, size);
 #endif
 
         if (band->offline) {
@@ -3343,7 +3421,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
     /* Allocate memory for deserialized raster header */
     rast = (rt_raster) ctx->alloc(sizeof (struct rt_raster_t));
     if (!rast) {
-        ctx->err("Out of memory allocating raster for deserialization");
+        ctx->err("rt_raster_deserialize: Out of memory allocating raster for deserialization");
         return 0;
     }
 
@@ -3373,7 +3451,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
 
         band = ctx->alloc(sizeof (struct rt_band_t));
         if (!band) {
-            ctx->err("Out of memory allocating rt_band during deserialization");
+            ctx->err("rt_raster_deserialize: Out of memory allocating rt_band during deserialization");
             return 0;
         }
 
@@ -3386,7 +3464,7 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
         RASTER_DEBUGF(3, "rt_raster_deserialize: band %d with pixel type %s", i, rt_pixtype_name(ctx, band->pixtype));
 
         band->offline = BANDTYPE_IS_OFFDB(type) ? 1 : 0;
-        band->hasnodata = BANDTYPE_HAS_NODATA(type) ? 1 : 0;        
+        band->hasnodata = BANDTYPE_HAS_NODATA(type) ? 1 : 0;
         band->isnodata = BANDTYPE_IS_NODATA(type) ? 1 : 0;
         band->width = rast->width;
         band->height = rast->height;
@@ -3455,15 +3533,15 @@ rt_raster_deserialize(rt_context ctx, void* serialized) {
             }
             default:
             {
-                ctx->err("Unknown pixeltype %d", band->pixtype);
+                ctx->err("rt_raster_deserialize: Unknown pixeltype %d", band->pixtype);
                 ctx->dealloc(band);
                 ctx->dealloc(rast);
                 return 0;
             }
         }
 
-        RASTER_DEBUGF(3, "rt_raster_deserialize: has NODATA flag %d", band->hasnodata);
-        RASTER_DEBUGF(3, "rt_raster_deserialize: NODATA value %g", band->nodataval);
+        RASTER_DEBUGF(3, "rt_raster_deserialize: has nodata flag %d", band->hasnodata);
+        RASTER_DEBUGF(3, "rt_raster_deserialize: nodata value %g", band->nodataval);
 
         /* Consistency checking (ptr is pixbytes-aligned) */
         assert(!(((uintptr_t) ptr) % pixbytes));
@@ -3546,7 +3624,7 @@ int32_t rt_raster_copy_band(rt_context ctx, rt_raster raster1,
     /* Check raster dimensions */
     if (raster1->height != raster2->height || raster1->width != raster2->width)
     {
-        ctx->err("Attempting to add a band with different width or height");
+        ctx->err("rt_raster_copy_band: Attempting to add a band with different width or height");
         return -1;
     }
 
@@ -3565,6 +3643,6 @@ int32_t rt_raster_copy_band(rt_context ctx, rt_raster raster1,
     newband = rt_raster_get_band(ctx, raster1, nband1);
 
     /* Add band to the second raster */
-    return rt_raster_add_band(ctx, raster2, newband, nband2);    
+    return rt_raster_add_band(ctx, raster2, newband, nband2);
 }
 
index 0828da5dae993ee1df2f7ed60aade9af247dded7..ecaa2b8caaafe359b7974aa5ff0220f4ee59ef52 100644 (file)
 #define RT_API_H_INCLUDED
 
 /* define the systems */
-#if defined(__linux__) /* (predefined) */
+#if defined(__linux__)  /* (predefined) */
 #if !defined(LINUX)
-#define        LINUX
+#define LINUX
 #endif
 #if !defined(UNIX)
-#define        UNIX            /* make sure this is defined */
+#define UNIX        /* make sure this is defined */
 #endif
 #endif
 
 
-#if defined(__FreeBSD__) || defined(__OpenBSD__)       /* seems to work like Linux... */
+#if defined(__FreeBSD__) || defined(__OpenBSD__)    /* seems to work like Linux... */
 #if !defined(LINUX)
-#define        LINUX
+#define LINUX
 #endif
 #if !defined(UNIX)
-#define        UNIX            /* make sure this is defined */
+#define UNIX        /* make sure this is defined */
 #endif
 #endif
 
 #if defined(__MSDOS__)
 #if !defined(MSDOS)
-#define        MSDOS           /* make sure this is defined */
+#define MSDOS       /* make sure this is defined */
 #endif
 #endif
 
 #if defined(__WIN32__) || defined(__NT__) || defined(_WIN32)
 #if !defined(WIN32)
-#define        WIN32
+#define WIN32
 #endif
 #if defined(__BORLANDC__) && defined(MSDOS) /* Borland always defines MSDOS */
-#undef MSDOS
+#undef  MSDOS
 #endif
 #endif
 
 #if defined(__APPLE__)
 #if !defined(UNIX)
-#define        UNIX
+#define UNIX
 #endif
 #endif
 
 /* does. */
 #if defined(UNIX)
 #if !defined(HAVE_STRICMP)
-#define        stricmp         strcasecmp
+#define stricmp     strcasecmp
 #endif
 #if !defined(HAVE_STRNICMP)
-#define        strnicmp        strncasecmp
+#define strnicmp    strncasecmp
 #endif
 #endif
 
@@ -112,7 +112,7 @@ typedef void  (*rt_message_handler)(const char* string, ...);
 
 /* Debugging macros */
 #if POSTGIS_DEBUG_LEVEL > 0
+
 /* Display a simple message at NOTICE level */
 #define RASTER_DEBUG(level, msg) \
     do { \
@@ -126,20 +126,20 @@ typedef void  (*rt_message_handler)(const char* string, ...);
         if (POSTGIS_DEBUG_LEVEL >= level) \
         ctx->warn("[%s:%s:%d] " msg, __FILE__, __func__, __LINE__, __VA_ARGS__); \
     } while (0);
+
 #else
 
 /* Empty prototype that can be optimised away by the compiler for non-debug builds */
 #define RASTER_DEBUG(level, msg) \
     ((void) 0)
+
 /* Empty prototype that can be optimised away by the compiler for non-debug builds */
 #define RASTER_DEBUGF(level, msg, ...) \
     ((void) 0)
 
 #endif
 
-
+/*- memory context -------------------------------------------------------*/
 
 /* Initialize a context object
  * @param allocator memory allocator to use, 0 to use malloc
@@ -205,7 +205,7 @@ rt_pixtype rt_pixtype_index_from_name(rt_context ctx, const char* pixname);
  * @param width     : number of pixel columns
  * @param height    : number of pixel rows
  * @param pixtype   : pixel type for the band
- * @param hasnodata : indicates if the band has nodata value 
+ * @param hasnodata : indicates if the band has nodata value
  * @param nodataval : the nodata value, will be appropriately
  *                    truncated to fit the pixtype size.
  * @param data      : pointer to actual band data, required to
@@ -390,6 +390,15 @@ double rt_band_get_min_value(rt_context ctx, rt_band band);
  */
 int rt_band_is_nodata(rt_context ctx, rt_band band);
 
+/**
+ * Returns TRUE if the band is only nodata values
+ * @param ctx: context, for thread safety
+ * @param band: the band to get info from
+ * @return TRUE if the band is only nodata values, FALSE otherwise
+ */
+int rt_band_check_is_nodata(rt_context ctx, rt_band band);
+
+
 
 /*- rt_raster --------------------------------------------------------*/
 
@@ -509,7 +518,7 @@ int32_t rt_raster_add_band(rt_context ctx, rt_raster raster, rt_band band, int i
  *
  * @return identifier (position) for the just-added raster, or -1 on error
  */
-int32_t rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype, 
+int32_t rt_raster_generate_new_band(rt_context ctx, rt_raster raster, rt_pixtype pixtype,
         double initialvalue, uint32_t hasnodata, double nodatavalue, int index);
 
 /**
@@ -658,13 +667,13 @@ LWPOLY* rt_raster_get_convex_hull(rt_context ctx, rt_raster raster);
 
 
 /**
- * Returns a set of "geomval" value, one for each group of pixel 
+ * Returns a set of "geomval" value, one for each group of pixel
  * sharing the same value for the provided band.
  *
- * A "geomval " value is a complex type composed of a the wkt 
- * representation of a geometry (one for each group of pixel sharing 
+ * A "geomval " value is a complex type composed of a the wkt
+ * representation of a geometry (one for each group of pixel sharing
  * the same value) and the value associated with this geometry.
- * 
+ *
  * @param ctx: context for thread safety.
  * @param raster: the raster to get info from.
  * @param nband: the band to polygonize. From 1 to rt_raster_get_num_bands
@@ -678,7 +687,7 @@ rt_geomval
 rt_raster_dump_as_wktpolygons(rt_context ctx, rt_raster raster, int nband,
         int * pnElements);
 
-    
+
 /**
  * Return this raster in serialized form.
  *
@@ -728,5 +737,52 @@ int rt_raster_has_no_band(rt_context ctx, rt_raster raster, int nband);
 int32_t rt_raster_copy_band(rt_context ctx, rt_raster raster1,
         rt_raster raster2, int nband1, int nband2);
 
-#endif /* RT_API_H_INCLUDED */
+/*- utilities -------------------------------------------------------*/
 
+/* Set of functions to clamp double to int of different size
+ */
+
+#define POSTGIS_RT_1BBMAX 1
+#define POSTGIS_RT_2BUIMAX 3
+#define POSTGIS_RT_4BUIMAX 15
+
+uint8_t
+rt_util_clamp_to_1BB(double value);
+
+uint8_t
+rt_util_clamp_to_2BUI(double value);
+
+uint8_t
+rt_util_clamp_to_4BUI(double value);
+
+int8_t
+rt_util_clamp_to_8BSI(double value);
+
+uint8_t
+rt_util_clamp_to_8BUI(double value);
+
+int16_t
+rt_util_clamp_to_16BSI(double value);
+
+uint16_t
+rt_util_clamp_to_16BUI(double value);
+
+int32_t
+rt_util_clamp_to_32BSI(double value);
+
+uint32_t
+rt_util_clamp_to_32BUI(double value);
+
+float
+rt_util_clamp_to_32F(double value);
+
+int
+rt_util_display_dbl_trunc_warning(rt_context ctx,
+                                  double initialvalue,
+                                  int32_t checkvalint,
+                                  uint32_t checkvaluint,
+                                  float checkvalfloat,
+                                  double checkvaldouble,
+                                  rt_pixtype pixtype);
+
+#endif /* RT_API_H_INCLUDED */
index 17f6a12893c5a8f7b672c4c56e7c4320011766b7..10b7ce429fc1b3d2c90b4754214453e98a9dab08 100644 (file)
@@ -120,21 +120,21 @@ Datum RASTER_hasNoBand(PG_FUNCTION_ARGS);
 
 /* Replace function taken from http://ubuntuforums.org/showthread.php?s=aa6f015109fd7e4c7e30d2fd8b717497&t=141670&page=3 */
 /* ---------------------------------------------------------------------------
-  Name       : replace - Search & replace a substring by another one. 
+  Name       : replace - Search & replace a substring by another one.
   Creation   : Thierry Husson, Sept 2010
   Parameters :
       str    : Big string where we search
       oldstr : Substring we are looking for
       newstr : Substring we want to replace with
-      count  : Optional pointer to int (input / output value). NULL to ignore.  
+      count  : Optional pointer to int (input / output value). NULL to ignore.
                Input:  Maximum replacements to be done. NULL or < 1 to do all.
                Output: Number of replacements done or -1 if not enough memory.
   Returns    : Pointer to the new string or NULL if error.
-  Notes      : 
+  Notes      :
      - Case sensitive - Otherwise, replace functions "strstr" by "strcasestr"
      - Always allocates memory for the result.
 --------------------------------------------------------------------------- */
-static char* 
+static char*
 replace(const char *str, const char *oldstr, const char *newstr, int *count)
 {
    const char *tmp = str;
@@ -143,12 +143,12 @@ replace(const char *str, const char *oldstr, const char *newstr, int *count)
    int   length, reslen;
    int   oldlen = strlen(oldstr);
    int   newlen = strlen(newstr);
-   int   limit = (count != NULL && *count > 0) ? *count : -1; 
+   int   limit = (count != NULL && *count > 0) ? *count : -1;
 
    tmp = str;
    while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
       found++, tmp += oldlen;
-   
+
    length = strlen(str) + found * (newlen - oldlen);
    if ( (result = (char *)palloc(length+1)) == NULL) {
       fprintf(stderr, "Not enough memory\n");
@@ -156,11 +156,11 @@ replace(const char *str, const char *oldstr, const char *newstr, int *count)
    } else {
       tmp = str;
       limit = found; /* Countdown */
-      reslen = 0; /* length of current result */ 
+      reslen = 0; /* length of current result */
       /* Replace each old string found with new string  */
       while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
          length = (tmp - str); /* Number of chars to keep intouched */
-         strncpy(result + reslen, str, length); /* Original part keeped */ 
+         strncpy(result + reslen, str, length); /* Original part keeped */
          strcpy(result + (reslen += length), newstr); /* Insert new string */
          reslen += newlen;
          tmp += oldlen;
@@ -199,12 +199,12 @@ static void *
 rt_pgrealloc(void *mem, size_t size)
 {
     void* ret;
-    
+
     POSTGIS_RT_DEBUGF(5, "rt_pgrealloc(%p, %ld) called", mem, size);
 
     if ( mem )
     {
-    
+
         POSTGIS_RT_DEBUGF(5, "rt_pgrealloc calling repalloc(%p, %ld) called", mem, size);
         ret = repalloc(mem, size);
     }
@@ -347,7 +347,7 @@ Datum RASTER_out(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_out: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -356,7 +356,7 @@ Datum RASTER_out(PG_FUNCTION_ARGS)
     hexwkb = rt_raster_to_hexwkb(ctx, raster, &hexwkbsize);
     if ( ! hexwkb )
     {
-        elog(ERROR, "Could not HEX-WKBize raster");
+        elog(ERROR, "RASTER_out: Could not HEX-WKBize raster");
         PG_RETURN_NULL();
     }
 
@@ -381,7 +381,7 @@ Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_to_bytea: Could not deserialize raster");
         PG_RETURN_NULL();
     }
     /*elog(NOTICE, "RASTER_to_binary: rt_raster_deserialize returned %p", raster);*/
@@ -389,7 +389,7 @@ Datum RASTER_to_bytea(PG_FUNCTION_ARGS)
     /* Uses context allocator */
     wkb = rt_raster_to_wkb(ctx, raster, &wkb_size);
     if ( ! wkb ) {
-        elog(ERROR, "Could not allocate and generate WKB data");
+        elog(ERROR, "RASTER_to_bytea: Could not allocate and generate WKB data");
         PG_RETURN_NULL();
     }
 
@@ -420,15 +420,14 @@ Datum RASTER_to_binary(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_to_binary: Could not deserialize raster");
         PG_RETURN_NULL();
     }
-    /*elog(NOTICE, "RASTER_to_binary: rt_raster_deserialize returned %p", raster);*/
 
     /* Uses context allocator */
     wkb = rt_raster_to_wkb(ctx, raster, &wkb_size);
     if ( ! wkb ) {
-        elog(ERROR, "Could not allocate and generate WKB data");
+        elog(ERROR, "RASTER_to_binary: Could not allocate and generate WKB data");
         PG_RETURN_NULL();
     }
 
@@ -458,7 +457,7 @@ Datum RASTER_convex_hull(PG_FUNCTION_ARGS)
     { /* TODO: can be optimized to only detoast the header! */
         raster = rt_raster_deserialize(ctx, pgraster);
         if ( ! raster ) {
-            elog(ERROR, "Could not deserialize raster");
+            elog(ERROR, "RASTER_convex_hull: Could not deserialize raster");
             PG_RETURN_NULL();
         }
 
@@ -466,18 +465,18 @@ Datum RASTER_convex_hull(PG_FUNCTION_ARGS)
 
         convexhull = rt_raster_get_convex_hull(ctx, raster);
         if ( ! convexhull ) {
-            elog(ERROR, "Could not get raster's convex hull");
+            elog(ERROR, "RASTER_convex_hull: Could not get raster's convex hull");
             PG_RETURN_NULL();
         }
     }
 
     {
 #ifdef GSERIALIZED_ON
-               size_t gser_size;
-               GSERIALIZED *gser;
-               gser = gserialized_from_lwgeom(lwpoly_as_lwgeom(convexhull), 0, &gser_size);
-               SET_VARSIZE(gser, gser_size);
-               pglwgeom = (uchar*)gser;
+        size_t gser_size;
+        GSERIALIZED *gser;
+        gser = gserialized_from_lwgeom(lwpoly_as_lwgeom(convexhull), 0, &gser_size);
+        SET_VARSIZE(gser, gser_size);
+        pglwgeom = (uchar*)gser;
 #else
         size_t sz = lwpoly_serialize_size(convexhull);
         pglwgeom = palloc(VARHDRSZ+sz);
@@ -508,7 +507,7 @@ struct rt_geomval_t {
 
 PG_FUNCTION_INFO_V1(RASTER_dumpAsWKTPolygons);
 Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
-{    
+{
     rt_pgraster *pgraster;
     rt_raster raster;
     rt_context ctx;
@@ -546,10 +545,10 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         /* Get input arguments */
         pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
         raster = rt_raster_deserialize(ctx, pgraster);
-        if ( ! raster ) 
+        if ( ! raster )
         {
             ereport(ERROR,
-                    (errcode(ERRCODE_OUT_OF_MEMORY), 
+                    (errcode(ERRCODE_OUT_OF_MEMORY),
                     errmsg("Could not deserialize raster")));
             PG_RETURN_NULL();
         }
@@ -558,11 +557,11 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
             nband = PG_GETARG_UINT32(1);
         else
             nband = 1; /* By default, first band */
-                       
+
         POSTGIS_RT_DEBUGF(3, "band %d", nband);
 
         /* Polygonize raster */
-        
+
         /**
          * Dump raster
          */
@@ -570,11 +569,11 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         if (NULL == geomval)
         {
             ereport(ERROR,
-                    (errcode(ERRCODE_NO_DATA_FOUND), 
+                    (errcode(ERRCODE_NO_DATA_FOUND),
                     errmsg("Could not polygonize raster")));
             PG_RETURN_NULL();
         }
-               
+
         POSTGIS_RT_DEBUGF(3, "raster dump, %d elements returned", nElements);
 
         /**
@@ -587,7 +586,7 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
 
         /* total number of tuples to be returned */
         funcctx->max_calls = nElements;
-               
+
         /* Build a tuple descriptor for our result type */
         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
             ereport(ERROR,
@@ -607,7 +606,7 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
 
     /* stuff done on every call of the function */
     funcctx = SRF_PERCALL_SETUP();
-       
+
     call_cntr = funcctx->call_cntr;
     max_calls = funcctx->max_calls;
     attinmeta = funcctx->attinmeta;
@@ -619,24 +618,24 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         char       **values;
         HeapTuple    tuple;
         Datum        result;
-                               
+
         POSTGIS_RT_DEBUGF(3, "call number %d", call_cntr);
-                       
+
         /*
          * Prepare a values array for building the returned tuple.
          * This should be an array of C strings which will
          * be processed later by the type input functions.
          */
-        values = (char **) palloc(3 * sizeof(char *));            
+        values = (char **) palloc(3 * sizeof(char *));
 
         values[0] = (char *) palloc(
                 (strlen(geomval2[call_cntr].geom) + 1) * sizeof(char));
         values[1] = (char *) palloc(18 * sizeof(char));
         values[2] = (char *) palloc(16 * sizeof(char));
 
-        snprintf(values[0], 
-            (strlen(geomval2[call_cntr].geom) + 1) * sizeof(char), "%s", 
-            geomval2[call_cntr].geom);        
+        snprintf(values[0],
+            (strlen(geomval2[call_cntr].geom) + 1) * sizeof(char), "%s",
+            geomval2[call_cntr].geom);
         snprintf(values[1], 18 * sizeof(char), "%f", geomval2[call_cntr].val);
         snprintf(values[2], 16 * sizeof(char), "%d", geomval2[call_cntr].srid);
 
@@ -644,12 +643,12 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         POSTGIS_RT_DEBUGF(4, "Result %d, val %s", call_cntr, values[1]);
         POSTGIS_RT_DEBUGF(4, "Result %d, val %s", call_cntr, values[2]);
 
-               /** 
-                * Free resources. 
-                * TODO: Do we have to use the same context we used
-                * for creating them?
-                */
-               pfree(geomval2[call_cntr].geom);
+        /**
+         * Free resources.
+         * TODO: Do we have to use the same context we used
+         * for creating them?
+         */
+        pfree(geomval2[call_cntr].geom);
 
 
         /* build a tuple */
@@ -663,13 +662,13 @@ Datum RASTER_dumpAsWKTPolygons(PG_FUNCTION_ARGS)
         pfree(values[1]);
         pfree(values[2]);
         pfree(values);
-        
+
 
         SRF_RETURN_NEXT(funcctx, result);
     }
     else    /* do when there is no more left */
-    {        
-               pfree(geomval2);
+    {
+        pfree(geomval2);
         SRF_RETURN_DONE(funcctx);
     }
 
@@ -694,7 +693,7 @@ Datum RASTER_makeEmpty(PG_FUNCTION_ARGS)
 
     if ( PG_NARGS() < 9 )
     {
-        elog(ERROR, "MakeEmptyRaster requires 9 args");
+        elog(ERROR, "RASTER_makeEmpty: ST_MakeEmptyRaster requires 9 args");
         PG_RETURN_NULL();
     }
 
@@ -780,7 +779,7 @@ Datum RASTER_getSRID(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getSRID: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -802,7 +801,7 @@ Datum RASTER_setSRID(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setSRID: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -829,7 +828,7 @@ Datum RASTER_getWidth(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getWidth: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -851,7 +850,7 @@ Datum RASTER_getHeight(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getHeight: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -873,7 +872,7 @@ Datum RASTER_getNumBands(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getNumBands: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -895,7 +894,7 @@ Datum RASTER_getXScale(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getXScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -917,7 +916,7 @@ Datum RASTER_getYScale(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getYScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -938,7 +937,7 @@ Datum RASTER_setScale(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setScale: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -965,7 +964,7 @@ Datum RASTER_setScaleXY(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setScaleXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -992,7 +991,7 @@ Datum RASTER_getXSkew(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getXSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1014,7 +1013,7 @@ Datum RASTER_getYSkew(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getYSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1035,7 +1034,7 @@ Datum RASTER_setSkew(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setSkew: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1062,7 +1061,7 @@ Datum RASTER_setSkewXY(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setSkewXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1089,7 +1088,7 @@ Datum RASTER_getXUpperLeft(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getXUpperLeft: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1111,7 +1110,7 @@ Datum RASTER_getYUpperLeft(PG_FUNCTION_ARGS)
     /* TODO: can be optimized to only detoast the header! */
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getYUpperLeft: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1133,7 +1132,7 @@ Datum RASTER_setUpperLeftXY(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setUpperLeftXY: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1163,7 +1162,7 @@ Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_getBandPixelType: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1174,14 +1173,14 @@ Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getBandPixelType: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its pixel type */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when getting pixel type. Returning NULL", index);
         PG_RETURN_NULL();
     }
 
@@ -1211,7 +1210,7 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_getBandPixelTypeName: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1222,14 +1221,14 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getBandPixelTypeName: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band and its pixel type */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when getting pixel type name. Returning NULL", index);
         PG_RETURN_NULL();
     }
 
@@ -1237,7 +1236,7 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
 
     result = palloc(VARHDRSZ + name_size);
     if ( ! result ) {
-        elog(ERROR, "Could not allocate memory for output text object");
+        elog(ERROR, "RASTER_getBandPixelTypeName: Could not allocate memory for output text object");
         PG_RETURN_NULL();
     }
     memset(VARDATA(result), 0, name_size);
@@ -1280,7 +1279,7 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
             strcpy(ptr, "64BF");
             break;
         default:      /* PT_END=13 */
-            elog(ERROR, "Invalid value of pixel type");
+            elog(ERROR, "RASTER_getBandPixelTypeName: Invalid value of pixel type");
             pfree(result);
             PG_RETURN_NULL();
     }
@@ -1292,7 +1291,7 @@ Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS)
 }
 
 /**
- * Return NODATA value of the specified band of raster.
+ * Return nodata value of the specified band of raster.
  * The value is always returned as FLOAT32 even if the pixel type is INTEGER.
  */
 PG_FUNCTION_INFO_V1(RASTER_getBandNoDataValue);
@@ -1308,7 +1307,7 @@ Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_getBandNoDataValue: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1319,19 +1318,19 @@ Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getBandNoDataValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    /* Fetch requested band and its NODATA value */
+    /* Fetch requested band and its nodata value */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when getting band nodata value. Returning NULL", index);
         PG_RETURN_NULL();
     }
 
     if ( ! rt_band_get_hasnodata_flag(ctx, band) ) {
-        //elog(WARNING, "Raster band %d does not have a NODATA value", index);
+        //elog(WARNING, "RASTER_getBandNoDataValue: Raster band %d does not have a nodata value", index);
         PG_RETURN_NULL();
     }
 
@@ -1339,9 +1338,9 @@ Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS)
     PG_RETURN_FLOAT4(nodata);
 }
 
-  
+
 /**
- * Set the NODATA value of the specified band of raster.
+ * Set the nodata value of the specified band of raster.
  */
 PG_FUNCTION_INFO_V1(RASTER_setBandNoDataValue);
 Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
@@ -1363,7 +1362,7 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_setBandNoDataValue: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1378,18 +1377,18 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when setting band nodata value. Returning NULL", index);
         PG_RETURN_NULL();
     }
-    
-    
+
+
     forceChecking = PG_GETARG_BOOL(3);
 
 
@@ -1397,17 +1396,17 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
         /* Set the hasnodata flag to FALSE */
         rt_band_set_hasnodata_flag(ctx, band, FALSE);
 
-        POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a NODATA value",
+        POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value",
                 index);
     }
 
     else {
 
-        /* Get the NODATA value */
+        /* Get the nodata value */
         nodata = PG_GETARG_FLOAT8(2);
 
 
-        /* Set the band's NODATA value */
+        /* Set the band's nodata value */
         rt_band_set_nodata(ctx, band, nodata);
 
         /* Set the hasnodata flag to TRUE */
@@ -1415,7 +1414,7 @@ Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
 
         /* Recheck all pixels if requested */
         if (forceChecking)
-           rt_band_check_is_nodata(ctx, band); 
+           rt_band_check_is_nodata(ctx, band);
 
     }
 
@@ -1433,13 +1432,12 @@ Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
     rt_raster raster = NULL;
     rt_band band = NULL;
     rt_context ctx = NULL;
-    double nodata;
     int32_t index;
 
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_setBandIsNoData: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1449,18 +1447,18 @@ Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when setting band as nodata. Returning NULL", index);
         PG_RETURN_NULL();
     }
-    
-    /* Set the band's NODATA value */
+
+    /* Set the band's nodata value */
     rt_band_set_isnodata_flag(ctx, band, 1);
 
     /* Serialize raster again */
@@ -1469,7 +1467,7 @@ Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
 
     SET_VARSIZE(pgraster, pgraster->size);
     PG_RETURN_POINTER(pgraster);
-   
+
 }
 
 
@@ -1486,7 +1484,7 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_bandIsNoData: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1497,21 +1495,21 @@ Datum RASTER_bandIsNoData(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_bandIsNoData: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    /* Fetch requested band and its NODATA value */
+    /* Fetch requested band and its nodata value */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when determining if band is nodata. Returning NULL", index);
         PG_RETURN_NULL();
     }
 
     forceChecking = PG_GETARG_BOOL(2);
-    
-    if (forceChecking)    
-           PG_RETURN_BOOL(rt_band_check_is_nodata(ctx, band));
+
+    if (forceChecking)
+        PG_RETURN_BOOL(rt_band_check_is_nodata(ctx, band));
     else
         PG_RETURN_BOOL(rt_band_get_isnodata_flag(ctx, band));
 }
@@ -1530,12 +1528,12 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
     rt_context ctx = NULL;
     int32_t index;
     const char *bandpath;
-       text *result;
+    text *result;
 
     /* Index is 1-based */
     index = PG_GETARG_INT32(1);
     if ( index < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_getBandPath: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (index - 1));
@@ -1546,14 +1544,14 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getBandPath: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
-    /* Fetch requested band and its NODATA value */
+    /* Fetch requested band and its nodata value */
     band = rt_raster_get_band(ctx, raster, index - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", index);
+        elog(NOTICE, "Could not find raster band of index %d when getting band path. Returning NULL", index);
         PG_RETURN_NULL();
     }
 
@@ -1565,7 +1563,7 @@ Datum RASTER_getBandPath(PG_FUNCTION_ARGS)
 
     result = (text *) palloc(VARHDRSZ + strlen(bandpath) + 1);
     if ( ! result ) {
-        elog(ERROR, "Could not allocate memory for output text object");
+        elog(ERROR, "RASTER_getBandPath: Could not allocate memory for output text object");
         PG_RETURN_NULL();
     }
     SET_VARSIZE(result, VARHDRSZ + strlen(bandpath) + 1);
@@ -1599,20 +1597,20 @@ Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
     /* Index is 1-based */
     nband = PG_GETARG_INT32(1);
     if ( nband < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_getPixelValue: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (nband - 1));
 
     /* Validate pixel coordinates are in range */
     if (PG_ARGISNULL(2)) {
-        elog(NOTICE, "x coordinate can not be NULL. Returning NULL");
+        elog(NOTICE, "X coordinate can not be NULL when getting pixel value. Returning NULL");
         PG_RETURN_NULL();
     }
     x = PG_GETARG_INT32(2);
 
     if (PG_ARGISNULL(3)) {
-        elog(NOTICE, "y coordinate can not be NULL. Returning NULL");
+        elog(NOTICE, "Y coordinate can not be NULL when getting pixel value. Returning NULL");
         PG_RETURN_NULL();
     }
     y = PG_GETARG_INT32(3);
@@ -1628,20 +1626,20 @@ Datum RASTER_getPixelValue(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if (!raster) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_getPixelValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch Nth band using 0-based internal index */
     band = rt_raster_get_band(ctx, raster, nband - 1);
     if (! band) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", nband);
+        elog(NOTICE, "Could not find raster band of index %d when getting pixel value. Returning NULL", nband);
         PG_RETURN_NULL();
     }
     /* Fetch pixel using 0-based coordiantes */
     result = rt_band_get_pixel(ctx, band, x - 1, y - 1, &pixvalue);
     if (result == -1 || (hasnodata && rt_band_get_hasnodata_flag(ctx, band) && pixvalue == rt_band_get_nodata(ctx, band))) {
-        //elog(WARNING, "Raster band %d does not have a NODATA value", index);
+        //elog(WARNING, "RASTER_getPixelValue: Raster band %d does not have a nodata value", index);
         PG_RETURN_NULL();
     }
 
@@ -1666,7 +1664,7 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
     /* nband is 1-based */
     nband = PG_GETARG_INT32(1);
     if ( nband < 1 ) {
-        elog(ERROR, "Invalid band index (must use 1-based)");
+        elog(ERROR, "RASTER_setPixelValue: Invalid band index (must use 1-based)");
         PG_RETURN_NULL();
     }
     assert(0 <= (nband - 1));
@@ -1674,7 +1672,7 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
     /* Validate pixel coordinates are in range */
     x = PG_GETARG_INT32(2);
     y = PG_GETARG_INT32(3);
-    
+
     POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
 
     /* Deserialize raster */
@@ -1683,14 +1681,14 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
     /* Fetch requested band */
     band = rt_raster_get_band(ctx, raster, nband - 1);
     if ( ! band ) {
-        elog(NOTICE, "Could not find raster band of index %d. Returning NULL", nband);
+        elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Returning NULL", nband);
         PG_RETURN_NULL();
     }
 
@@ -1711,7 +1709,7 @@ Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
 
     pgraster = rt_raster_serialize(ctx, raster);
     if ( ! pgraster ) PG_RETURN_NULL();
+
     SET_VARSIZE(pgraster, pgraster->size);
     PG_RETURN_POINTER(pgraster);
 }
@@ -1724,9 +1722,8 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
 {
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
-    rt_band band = NULL;
     rt_context ctx = NULL;
-    
+
     int index = 0;
     double initialvalue = 0;
     double nodatavalue = 0;
@@ -1736,26 +1733,15 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
     char *new_pixeltypename = NULL;
 
     int pixtype = PT_END;
-    int width = 0;
-    int height = 0;
     int32_t oldnumbands = 0;
     int32_t numbands = 0;
-    
-    int datasize = 0;
-    int numval = 0;
-    void *mem;
-    int32_t checkvalint = 0;
-    uint32_t checkvaluint = 0;
-    double checkvaldouble = 0;
-    float checkvalfloat = 0;
-    int i;
-    
+
     /* Get the initial pixel value */
     if (PG_ARGISNULL(3))
         initialvalue = 0;
     else
         initialvalue = PG_GETARG_FLOAT8(3);
-    
+
     /* Get the nodata value */
     if (PG_ARGISNULL(4))
         nodatavalue = 0;
@@ -1764,17 +1750,17 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
         nodatavalue = PG_GETARG_FLOAT8(4);
         hasnodata = TRUE;
     }
-    
+
     /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
     ctx = get_rt_context(fcinfo);
 
     /* Get the pixel type in text form */
     if (PG_ARGISNULL(2)) {
-        elog(ERROR, "Pixel type can not be null");
+        elog(ERROR, "RASTER_addband: Pixel type can not be null");
         PG_RETURN_NULL();
     }
-      
+
     pixeltypename = PG_GETARG_TEXT_P(2);
     new_pixeltypename = (char *) palloc(VARSIZE(pixeltypename) + 1 - VARHDRSZ);
     SET_VARSIZE(new_pixeltypename, VARSIZE(pixeltypename));
@@ -1784,17 +1770,17 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
     /* Get the pixel type index */
     pixtype = rt_pixtype_index_from_name(ctx, new_pixeltypename);
     if ( pixtype == PT_END ) {
-        elog(ERROR, "Invalid pixel type: %s", new_pixeltypename);
+        elog(ERROR, "RASTER_addband: Invalid pixel type: %s", new_pixeltypename);
         PG_RETURN_NULL();
     }
     assert(-1 < pixtype && pixtype < PT_END);
 
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_addband: Could not deserialize raster");
         PG_RETURN_NULL();
     }
-    
+
     /* Make sure index (1 based) is in a valid range */
     oldnumbands = rt_raster_get_num_bands(ctx, raster);
     if (PG_ARGISNULL(1))
@@ -1803,22 +1789,22 @@ Datum RASTER_addband(PG_FUNCTION_ARGS)
     {
         index = PG_GETARG_UINT16(1);
         if (index < 1) {
-            elog(ERROR, "Invalid band index (must be 1-based)");
+            elog(ERROR, "RASTER_addband: Invalid band index (must be 1-based)");
             PG_RETURN_NULL();
         }
-        if (index > oldnumbands + 1) {   
-            elog(WARNING, "Band index number exceed possible values, truncated to number of band (%u) + 1", oldnumbands);
+        if (index > oldnumbands + 1) {
+            elog(WARNING, "RASTER_addband: Band index number exceed possible values, truncated to number of band (%u) + 1", oldnumbands);
             index = oldnumbands + 1;
         }
     }
 
-    
+
     index = rt_raster_generate_new_band(ctx, raster, pixtype, initialvalue,
             hasnodata, nodatavalue, index - 1);
 
     numbands = rt_raster_get_num_bands(ctx, raster);
     if (numbands == oldnumbands || index == -1) {
-        elog(ERROR, "Could not add band to raster. Returning NULL");
+        elog(ERROR, "RASTER_addband: Could not add band to raster. Returning NULL");
         PG_RETURN_NULL();
     }
     pgraster = rt_raster_serialize(ctx, raster);
@@ -1845,32 +1831,32 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
     int index = 0;
     int max = 0;
     rt_context ctx = NULL;
-    
+
     /* Get band numbers */
     nband1 = PG_GETARG_UINT16(2);
-    nband2 = PG_GETARG_UINT16(3);    
+    nband2 = PG_GETARG_UINT16(3);
 
     if (nband1 < 1 || nband2 < 1) {
-        elog(ERROR, "Invalid band index (must be 1-based)");
+        elog(ERROR, "RASTER_copyband: Invalid band index (must be 1-based)");
         PG_RETURN_NULL();
     }
 
     /* Check if raster1 has the given band */
-   
+
     /* Deserialize raster1 */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
     ctx = get_rt_context(fcinfo);
 
     raster1 = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster1 ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_copyband: Could not deserialize raster");
         PG_RETURN_NULL();
     }
-     
+
     /* Make sure index (1 based) is in range */
     max = rt_raster_get_num_bands(ctx, raster1);
     if (nband1 > max) {
-        elog(WARNING, "Band index number exceed possible values, truncated to "
+        elog(WARNING, "RASTER_copyband: Band index number exceed possible values, truncated to "
                 "number of band (%u) + 1", max);
         nband1 = max;
     }
@@ -1881,7 +1867,7 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
 
     raster2 = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster2 ) {
-        elog(ERROR, "Could not deserialize raster");
+        elog(ERROR, "RASTER_copyband: Could not deserialize raster");
         PG_RETURN_NULL();
     }
 
@@ -1892,10 +1878,10 @@ Datum RASTER_copyband(PG_FUNCTION_ARGS)
 
     numbands = rt_raster_get_num_bands(ctx, raster2);
     if (numbands == oldnumbands || index == -1) {
-        elog(ERROR, "Could not add band to raster. Returning NULL");
+        elog(ERROR, "RASTER_copyband: Could not add band to raster. Returning NULL");
         PG_RETURN_NULL();
     }
+
     /* Serialize and return raster2 */
     pgraster = rt_raster_serialize(ctx, raster2);
     if (!pgraster) PG_RETURN_NULL();
@@ -1914,20 +1900,20 @@ Datum RASTER_isEmpty(PG_FUNCTION_ARGS)
     rt_pgraster *pgraster = NULL;
     rt_raster raster = NULL;
     rt_context ctx = NULL;
-       
-       /* Deserialize raster */
+
+    /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
     ctx = get_rt_context(fcinfo);
-       raster = rt_raster_deserialize(ctx, pgraster);
-       if ( ! raster ) 
-       {
-               ereport(ERROR,
-                       (errcode(ERRCODE_OUT_OF_MEMORY), 
-                               errmsg("Could not deserialize raster")));
-               PG_RETURN_NULL();
-       }
-       
-       PG_RETURN_BOOL(rt_raster_is_empty(ctx, raster));
+    raster = rt_raster_deserialize(ctx, pgraster);
+    if ( ! raster )
+    {
+        ereport(ERROR,
+            (errcode(ERRCODE_OUT_OF_MEMORY),
+                errmsg("RASTER_isEmpty: Could not deserialize raster")));
+        PG_RETURN_NULL();
+    }
+
+    PG_RETURN_BOOL(rt_raster_is_empty(ctx, raster));
 }
 
 /**
@@ -1940,23 +1926,23 @@ Datum RASTER_hasNoBand(PG_FUNCTION_ARGS)
     rt_raster raster = NULL;
     int nBand = 0;
     rt_context ctx = NULL;
-       
-       /* Deserialize raster */
+
+    /* Deserialize raster */
     pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
     ctx = get_rt_context(fcinfo);
-       raster = rt_raster_deserialize(ctx, pgraster);
-       if ( ! raster ) 
-       {
-               ereport(ERROR,
-                       (errcode(ERRCODE_OUT_OF_MEMORY), 
-                               errmsg("Could not deserialize raster")));
-               PG_RETURN_NULL();
-       }
-       
-       /* Get band number */
-       nBand = PG_GETARG_INT32(1);
-       
-       PG_RETURN_BOOL(rt_raster_has_no_band(ctx, raster, nBand));
+    raster = rt_raster_deserialize(ctx, pgraster);
+    if ( ! raster )
+    {
+        ereport(ERROR,
+            (errcode(ERRCODE_OUT_OF_MEMORY),
+                errmsg("RASTER_hasNoBand: Could not deserialize raster")));
+        PG_RETURN_NULL();
+    }
+
+    /* Get band number */
+    nBand = PG_GETARG_INT32(1);
+
+    PG_RETURN_BOOL(rt_raster_has_no_band(ctx, raster, nBand));
 }
 
 PG_FUNCTION_INFO_V1(RASTER_mapAlgebra);
@@ -1970,31 +1956,30 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     rt_band newband = NULL;
     int x, y, nband, width, height;
     double r;
-    double newnodatavalue, newinitialvalue, newval;
+    double newnodatavalue = 0.0;
+    double newinitialvalue = 0.0;
+    double newval = 0.0;
     char *newexpr = NULL;
     char *initexpr = NULL;
     char *initndvexpr = NULL;
     char *expression = NULL;
     char *nodatavalueexpr = NULL;
-    rt_pixtype newpixeltype, pixeltype;
+    rt_pixtype newpixeltype;
     int skipcomputation = 0;
-    bool newhasnodatavalue = FALSE;
     char strnewnodatavalue[50];
     char strnewval[50];
     int count = 0;
-    char *command = NULL;
     int len = 0;
     int ret = -1;
-    int proc;
     TupleDesc tupdesc;
     SPITupleTable * tuptable = NULL;
     HeapTuple tuple;
 
-    POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebra: STARTING...");
+    POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebra: Starting...");
 
     /* Check raster */
     if (PG_ARGISNULL(0)) {
-        elog(WARNING, "MapAlgebra: Raster is NULL. Returning NULL");
+        elog(WARNING, "RASTER_mapAlgebra: Raster is NULL. Returning NULL");
         PG_RETURN_NULL();
     }
 
@@ -2005,13 +1990,13 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     raster = rt_raster_deserialize(ctx, pgraster);
     if ( ! raster )
     {
-               ereport(ERROR,
-                       (errcode(ERRCODE_OUT_OF_MEMORY), 
-                               errmsg("MapAlgebra: Could not deserialize raster")));
-               PG_RETURN_NULL();    
+        ereport(ERROR,
+            (errcode(ERRCODE_OUT_OF_MEMORY),
+                errmsg("RASTER_mapAlgebra: Could not deserialize raster")));
+        PG_RETURN_NULL();
     }
 
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: GETTING ARGUMENTS...");
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Getting arguments...");
 
     /* Get the rest of the arguments */
 
@@ -2022,10 +2007,10 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
     if (nband < 1)
         nband = 1;
-    
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: CREATING NEW RASTER...");
 
-    /** 
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Creating new raster...");
+
+    /**
      * Create a new empty raster with having the same georeference as the
      * provided raster
      **/
@@ -2033,15 +2018,15 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     height = rt_raster_get_height(ctx, raster);
 
     newrast = rt_raster_new(ctx,
-            rt_raster_get_width(ctx, raster), 
+            rt_raster_get_width(ctx, raster),
             rt_raster_get_height(ctx, raster));
 
     if ( ! newrast ) {
-        elog(ERROR, "MapAlgebra: Could not create a new raster");
+        elog(ERROR, "RASTER_mapAlgebra: Could not create a new raster");
         PG_RETURN_NULL();
     }
 
-    rt_raster_set_scale(ctx, newrast, 
+    rt_raster_set_scale(ctx, newrast,
             rt_raster_get_x_scale(ctx, raster),
             rt_raster_get_y_scale(ctx, raster));
 
@@ -2053,7 +2038,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
             rt_raster_get_x_skew(ctx, raster),
             rt_raster_get_y_skew(ctx, raster));
 
-    rt_raster_set_srid(ctx, newrast, rt_raster_get_srid(ctx, raster));            
+    rt_raster_set_srid(ctx, newrast, rt_raster_get_srid(ctx, raster));
 
 
 
@@ -2061,8 +2046,8 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      * If this new raster is empty (width = 0 OR height = 0) then there is
      * nothing to compute and we return it right now
      **/
-    if (rt_raster_is_empty(ctx, newrast)) {        
-        elog(WARNING, "MapAlgebra: Raster is empty. Returning an empty raster");
+    if (rt_raster_is_empty(ctx, newrast)) {
+        elog(WARNING, "RASTER_mapAlgebra: Raster is empty. Returning an empty raster");
         pgraster = rt_raster_serialize(ctx, newrast);
         if (!pgraster) PG_RETURN_NULL();
 
@@ -2070,15 +2055,15 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         PG_RETURN_POINTER(pgraster);
     }
 
-    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: GETTING RASTER BAND %d...", nband);
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Getting raster band %d...", nband);
+
 
-   
     /**
      * Check if the raster has the required band. Otherwise, return a raster
      * without band
      **/
     if (rt_raster_has_no_band(ctx, raster, nband)) {
-        elog(WARNING, "Mapalgebra: Raster do not have the required band. "
+        elog(WARNING, "RASTER_mapAlgebra: Raster do not have the required band. "
                 "Returning a raster without a band");
         pgraster = rt_raster_serialize(ctx, newrast);
         if (!pgraster) PG_RETURN_NULL();
@@ -2090,7 +2075,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
     /* Get the raster band */
     band = rt_raster_get_band(ctx, raster, nband - 1);
     if ( ! band ) {
-        elog(ERROR, "MapAlgebra: Could not get band %d for raster", nband);
+        elog(ERROR, "RASTER_mapAlgebra: Could not get band %d for raster", nband);
         PG_RETURN_NULL();
     }
 
@@ -2103,7 +2088,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         newnodatavalue = rt_band_get_min_value(ctx, band);
     }
 
-    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: NODATA VALUE FOR BAND: %f...",
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: nodata value for band: %f...",
             newnodatavalue);
     /**
      * We set the initial value of the future band to nodata value. If nodata
@@ -2112,27 +2097,27 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      **/
     newinitialvalue = newnodatavalue;
 
-    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: SETTING PIXELTYPE...");
+    POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Setting pixeltype...");
 
     /**
      * Set the new pixeltype
-     **/    
+     **/
     if (PG_ARGISNULL(4)) {
         newpixeltype = rt_band_get_pixtype(ctx, band);
     }
 
     else {
         newpixeltype = rt_pixtype_index_from_name(ctx,
-                text_to_cstring(PG_GETARG_TEXT_P(4)));    
+                text_to_cstring(PG_GETARG_TEXT_P(4)));
         if (newpixeltype == PT_END)
             newpixeltype = rt_band_get_pixtype(ctx, band);
     }
-    
+
     if (newpixeltype == PT_END) {
-        elog(ERROR, "MapAlgebra: Invalid pixeltype. Aborting");
+        elog(ERROR, "RASTER_mapAlgebra: Invalid pixeltype. Aborting");
         PG_RETURN_NULL();
-    }    
-    
+    }
+
     /* Connect with SPI manager */
     SPI_connect();
 
@@ -2142,22 +2127,22 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         expression = text_to_cstring(PG_GETARG_TEXT_P(2));
         len = strlen("SELECT ") + strlen(expression);
         initexpr = (char *)palloc(len + 1);
-    
+
         strncpy(initexpr, "SELECT ", strlen("SELECT "));
         strncpy(initexpr + strlen("SELECT "), strtoupper(expression),
             strlen(expression));
-        initexpr[len] = '\0';        
+        initexpr[len] = '\0';
 
-        POSTGIS_RT_DEBUGF(3, "MapAlgebra: Expression is %s", initexpr);
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression is %s", initexpr);
 
-    }    
+    }
 
     /**
      * Optimization: If a nodatavalueexpr is provided, recompute the initial
      * value. Then, we can initialize the raster with this value and skip the
      * computation of nodata values one by one in the main computing loop
-     **/        
-    if (!PG_ARGISNULL(3)) {        
+     **/
+    if (!PG_ARGISNULL(3)) {
         nodatavalueexpr = text_to_cstring(PG_GETARG_TEXT_P(3));
         len = strlen("SELECT ") + strlen(nodatavalueexpr);
         initndvexpr = (char *)palloc(len + 1);
@@ -2169,16 +2154,16 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         newexpr = replace(initndvexpr, "RAST", strnewnodatavalue, &count);
 
-        POSTGIS_RT_DEBUGF(3, "MapAlgebra: initndvexpr = %s", initndvexpr);
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: initndvexpr = %s", initndvexpr);
 
-        /** 
+        /**
          * Execute the expression for nodata value and store the result as new
          * initial value
          **/
         ret = SPI_execute(newexpr, FALSE, 0);
 
         if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
-            elog(ERROR, "MapAlgebra: invalid construction for NODATA "
+            elog(ERROR, "RASTER_mapAlgebra: Invalid construction for nodata "
                     "expression. Aborting");
             SPI_finish();
             PG_RETURN_NULL();
@@ -2190,9 +2175,9 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         tuple = tuptable->vals[0];
         newinitialvalue = atof(SPI_getvalue(tuple, tupdesc, 1));
 
-        POSTGIS_RT_DEBUGF(3, "MapAlgebra: new initial value = %f",
-                newinitialvalue);       
-        
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: new initial value = %f",
+                newinitialvalue);
+
     }
 
 
@@ -2203,7 +2188,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
      **/
     if (rt_band_get_isnodata_flag(ctx, band)) {
 
-        POSTGIS_RT_DEBUG(3, "MapAlgebra: Band is a nodata band, returning "
+        POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: Band is a nodata band, returning "
                 "a raster filled with nodata");
 
         ret = rt_raster_generate_new_band(ctx, newrast, newpixeltype,
@@ -2214,31 +2199,31 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         if (!pgraster) PG_RETURN_NULL();
 
         SET_VARSIZE(pgraster, pgraster->size);
-        
+
         /* Disconnect function from SPI manager */
         SPI_finish();
-        
-        PG_RETURN_POINTER(pgraster);               
+
+        PG_RETURN_POINTER(pgraster);
     }
 
 
     /**
      * Optimization: If expression resume to 'RAST' and nodatavalueexpr is NULL
      * or also equal to 'RAST', we can just return the band from the original
-     * raster 
+     * raster
      **/
-    if (initexpr != NULL && !strcmp(initexpr, "SELECT RAST") && 
+    if (initexpr != NULL && !strcmp(initexpr, "SELECT RAST") &&
             (nodatavalueexpr == NULL || !strcmp(initndvexpr, "SELECT RAST"))) {
 
-        POSTGIS_RT_DEBUGF(3, "MapAlgebra: Expression resumes to RAST. Returning "
-               "raster with band %d from original raster", nband); 
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression resumes to RAST. Returning "
+               "raster with band %d from original raster", nband);
 
-        POSTGIS_RT_DEBUGF(4, "MapAlgebra: New raster has %d bands",
+        POSTGIS_RT_DEBUGF(4, "RASTER_mapAlgebra: New raster has %d bands",
                 rt_raster_get_num_bands(ctx, newrast));
 
         rt_raster_copy_band(ctx, raster, newrast, nband - 1, 0);
-        
-        POSTGIS_RT_DEBUGF(4, "MapAlgebra: New raster now has %d bands",
+
+        POSTGIS_RT_DEBUGF(4, "RASTER_mapAlgebra: New raster now has %d bands",
                 rt_raster_get_num_bands(ctx, newrast));
 
         /* Serialize created raster */
@@ -2246,11 +2231,11 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         if (!pgraster) PG_RETURN_NULL();
 
         SET_VARSIZE(pgraster, pgraster->size);
-        
+
         /* Disconnect function from SPI manager */
         SPI_finish();
-        
-        PG_RETURN_POINTER(pgraster);      
+
+        PG_RETURN_POINTER(pgraster);
     }
 
     /**
@@ -2262,7 +2247,7 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         ret = SPI_execute(initexpr, FALSE, 0);
 
         if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
-            elog(ERROR, "MapAlgebra: invalid construction for expression. Aborting");
+            elog(ERROR, "RASTER_mapAlgebra: Invalid construction for expression. Aborting");
             SPI_finish();
             PG_RETURN_NULL();
         }
@@ -2273,18 +2258,18 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
         tuple = tuptable->vals[0];
         newval = atof(SPI_getvalue(tuple, tupdesc, 1));
 
-        POSTGIS_RT_DEBUGF(3, "MapAlgebra: new raster value = %f",
-                newval);       
+        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: New raster value = %f",
+                newval);
 
         skipcomputation = 1;
 
         /**
-         * Compute the new value, set it and we will return after creating the 
+         * Compute the new value, set it and we will return after creating the
          * new raster
          **/
         if (nodatavalueexpr == NULL) {
             newinitialvalue = newval;
-            skipcomputation = 2;            
+            skipcomputation = 2;
         }
 
         /* Return the new raster as it will be before computing pixel by pixel */
@@ -2313,14 +2298,14 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         /* Disconnect function from SPI manager */
         SPI_finish();
-        
-        PG_RETURN_POINTER(pgraster);      
+
+        PG_RETURN_POINTER(pgraster);
     }
 
     /* Get the new raster band */
     newband = rt_raster_get_band(ctx, newrast, 0);
     if ( ! newband ) {
-        elog(WARNING, "MapAlgebra: Could not modify band for new raster. "
+        elog(WARNING, "RASTER_mapAlgebra: Could not modify band for new raster. "
                 "Returning new raster with the original band");
 
         /* Serialize created raster */
@@ -2331,12 +2316,12 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 
         /* Disconnect function from SPI manager */
         SPI_finish();
-        
-        PG_RETURN_POINTER(pgraster);      
+
+        PG_RETURN_POINTER(pgraster);
     }
 
-    
-    POSTGIS_RT_DEBUGF(3, "MapAlgebra: Main computing loop (%d x %d)",
+
+    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Main computing loop (%d x %d)",
             width, height);
 
     for (x = 0; x < width; x++) {
@@ -2350,24 +2335,24 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
             if (ret != -1 && fabs(r - newnodatavalue) > FLT_EPSILON) {
                 if (skipcomputation == 0) {
                     sprintf(strnewval, "%f", r);
-                    
-                    if (initexpr != NULL) {                        
+
+                    if (initexpr != NULL) {
                         newexpr = replace(initexpr, "RAST", strnewval, &count);
 
-                        POSTGIS_RT_DEBUGF(3, "MapAlgebra: (%dx%d), r = %s, newexpr = %s",
+                        POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: (%dx%d), r = %s, newexpr = %s",
                                 x, y, strnewval, newexpr);
-                    
+
                         ret = SPI_execute(newexpr, FALSE, 0);
 
                         if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
-                            elog(ERROR, "MapAlgebra: invalid construction for expression. Aborting");
+                            elog(ERROR, "RASTER_mapAlgebra: Invalid construction for expression. Aborting");
                             SPI_finish();
                             PG_RETURN_NULL();
                         }
 
                         tupdesc = SPI_tuptable->tupdesc;
                         tuptable = SPI_tuptable;
-    
+
                         tuple = tuptable->vals[0];
                         newval = atof(SPI_getvalue(tuple, tupdesc, 1));
                     }
@@ -2375,24 +2360,24 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
                     else
                         newval = newinitialvalue;
 
-                    POSTGIS_RT_DEBUGF(3, "MapAlgebra: new value = %f", newval);
+                    POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: new value = %f", newval);
                 }
 
-                
+
                 rt_band_set_pixel(ctx, newband, x, y, newval);
             }
 
         }
     }
-    
+
     /* The newrast band has been modified */
 
     /* Serialize created raster */
     pgraster = rt_raster_serialize(ctx, newrast);
     if (!pgraster) PG_RETURN_NULL();
 
-    SET_VARSIZE(pgraster, pgraster->size);    
-    
+    SET_VARSIZE(pgraster, pgraster->size);
+
     /* Disconnect function from SPI manager */
     SPI_finish();
 
@@ -2406,32 +2391,32 @@ Datum RASTER_mapAlgebra(PG_FUNCTION_ARGS)
 static void *
 rt_pg_alloc(size_t size)
 {
-       void * result;
+    void * result;
 
-       result = palloc(size);
+    result = palloc(size);
 
-       if ( ! result )
-       {
-               ereport(ERROR, (errmsg_internal("Out of virtual memory")));
-               return NULL;
-       }
-       return result;
+    if ( ! result )
+    {
+        ereport(ERROR, (errmsg_internal("Out of virtual memory")));
+        return NULL;
+    }
+    return result;
 }
 
 static void *
 rt_pg_realloc(void *mem, size_t size)
 {
-       void * result;
+    void * result;
 
-       result = repalloc(mem, size);
+    result = repalloc(mem, size);
 
-       return result;
+    return result;
 }
 
 static void
 rt_pg_free(void *ptr)
 {
-       pfree(ptr);
+    pfree(ptr);
 }
 
 static void
@@ -2439,30 +2424,30 @@ rt_pg_error(const char *fmt, va_list ap)
 {
 #define ERRMSG_MAXLEN 256
 
-       char errmsg[ERRMSG_MAXLEN+1];
+    char errmsg[ERRMSG_MAXLEN+1];
 
-       vsnprintf (errmsg, ERRMSG_MAXLEN, fmt, ap);
+    vsnprintf (errmsg, ERRMSG_MAXLEN, fmt, ap);
 
-       errmsg[ERRMSG_MAXLEN]='\0';
-       ereport(ERROR, (errmsg_internal("%s", errmsg)));
+    errmsg[ERRMSG_MAXLEN]='\0';
+    ereport(ERROR, (errmsg_internal("%s", errmsg)));
 }
 
 static void
 rt_pg_notice(const char *fmt, va_list ap)
 {
-       char *msg;
-
-       /*
-        * This is a GNU extension.
-        * Dunno how to handle errors here.
-        */
-       if (!lw_vasprintf (&msg, fmt, ap))
-       {
-               va_end (ap);
-               return;
-       }
-       ereport(NOTICE, (errmsg_internal("%s", msg)));
-       free(msg);
+    char *msg;
+
+    /*
+     * This is a GNU extension.
+     * Dunno how to handle errors here.
+     */
+    if (!lw_vasprintf (&msg, fmt, ap))
+    {
+        va_end (ap);
+        return;
+    }
+    ereport(NOTICE, (errmsg_internal("%s", msg)));
+    free(msg);
 }
 
 
index 75032d70bbbfc8b07465615a97438315ba0f677c..0afa784b649454fef54d6662d0da8a5c2027f7a3 100644 (file)
-WARNING:  Initial pixel value for 1BB band got truncated from -1.000000 to 1
-1
+WARNING:  Initial pixel value for 1BB band got clamped from -1.000000 to 0
 0
-1
-WARNING:  Initial pixel value for 1BB band got truncated from 2.000000 to 0
 0
-WARNING:  Initial pixel value for 1BB band got truncated from 21.460000 to 1
 1
-WARNING:  Initial pixel value for 2BUI band got truncated from -1.000000 to 3
-3
+WARNING:  Initial pixel value for 1BB band got clamped from 2.000000 to 1
+1
+WARNING:  Initial pixel value for 1BB band got clamped from 21.460000 to 1
+1
+WARNING:  Initial pixel value for 2BUI band got clamped from -1.000000 to 0
+0
 0
 3
-WARNING:  Initial pixel value for 2BUI band got truncated from 4.000000 to 0
+WARNING:  Initial pixel value for 2BUI band got clamped from 4.000000 to 3
+3
+WARNING:  Initial pixel value for 2BUI band got clamped from 21.460000 to 3
+3
+WARNING:  Initial pixel value for 4BUI band got clamped from -1.000000 to 0
 0
-WARNING:  Initial pixel value for 2BUI band got truncated from 21.460000 to 1
-1
-WARNING:  Initial pixel value for 4BUI band got truncated from -1.000000 to 15
-15
 0
 15
-WARNING:  Initial pixel value for 4BUI band got truncated from 16.000000 to 0
-0
-WARNING:  Initial pixel value for 4BUI band got truncated from 21.460000 to 5
-5
-WARNING:  Initial pixel value for 8BSI band got truncated from -129.000000 to 127
-127
+WARNING:  Initial pixel value for 4BUI band got clamped from 16.000000 to 15
+15
+WARNING:  Initial pixel value for 4BUI band got clamped from 21.460000 to 15
+15
+WARNING:  Initial pixel value for 8BSI band got clamped from -129.000000 to -128
+-128
 -128
 0
 127
-WARNING:  Initial pixel value for 8BSI band got truncated from 128.000000 to -128
--128
+WARNING:  Initial pixel value for 8BSI band got clamped from 128.000000 to 127
+127
 WARNING:  Initial pixel value for 8BSI band got truncated from 21.460000 to 21
 21
-WARNING:  Initial pixel value for 8BSI band got truncated from 210.460000 to -46
--46
-WARNING:  Initial pixel value for 8BUI band got truncated from -1.000000 to 255
-255
+WARNING:  Initial pixel value for 8BSI band got clamped from 210.460000 to 127
+127
+WARNING:  Initial pixel value for 8BUI band got clamped from -1.000000 to 0
 0
-255
-WARNING:  Initial pixel value for 8BUI band got truncated from 256.000000 to 0
 0
+255
+WARNING:  Initial pixel value for 8BUI band got clamped from 256.000000 to 255
+255
 WARNING:  Initial pixel value for 8BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Initial pixel value for 8BUI band got truncated from 410.460000 to 154
-154
+WARNING:  Initial pixel value for 8BUI band got clamped from 410.460000 to 255
+255
 WARNING:  Initial pixel value for 8BUI band got truncated from 256.000000 to 255
 255
-WARNING:  Initial pixel value for 16BSI band got truncated from -32769.000000 to -32768
+WARNING:  Initial pixel value for 16BSI band got clamped from -32769.000000 to -32768
 -32768
 -32768
 0
 32767
-WARNING:  Initial pixel value for 16BSI band got truncated from 32768.000000 to -32768
--32768
+WARNING:  Initial pixel value for 16BSI band got clamped from 32768.000000 to 32767
+32767
 WARNING:  Initial pixel value for 16BSI band got truncated from 21.460000 to 21
 21
-WARNING:  Initial pixel value for 16BSI band got truncated from 210000.460000 to -32768
--32768
-WARNING:  Initial pixel value for 16BUI band got truncated from -1.000000 to 65535
-65535
+WARNING:  Initial pixel value for 16BSI band got clamped from 210000.460000 to 32767
+32767
+WARNING:  Initial pixel value for 16BUI band got clamped from -1.000000 to 0
+0
 0
 65535
-WARNING:  Initial pixel value for 16BUI band got truncated from 65537.000000 to 1
-1
+WARNING:  Initial pixel value for 16BUI band got clamped from 65537.000000 to 65535
+65535
 WARNING:  Initial pixel value for 16BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Initial pixel value for 16BUI band got truncated from 210000.464564 to 13392
-13392
-WARNING:  Initial pixel value for 32BSI band got truncated from -2147483649.000000 to -2147483648
+WARNING:  Initial pixel value for 16BUI band got clamped from 210000.464564 to 65535
+65535
+WARNING:  Initial pixel value for 32BSI band got clamped from -2147483649.000000 to -2147483648
 -2147483648
 -2147483648
 0
 2147483647
-WARNING:  Initial pixel value for 32BSI band got truncated from 2147483648.000000 to -2147483648
--2147483648
+WARNING:  Initial pixel value for 32BSI band got clamped from 2147483648.000000 to 2147483647
+2147483647
 WARNING:  Initial pixel value for 32BSI band got truncated from 21.460000 to 21
 21
 WARNING:  Initial pixel value for 32BSI band got truncated from 210000.464564 to 210000
 210000
-WARNING:  Initial pixel value for 32BUI band got truncated from -1.000000 to 4294967295
-4294967295
+WARNING:  Initial pixel value for 32BUI band got clamped from -1.000000 to 0
 0
-4294967295
-WARNING:  Initial pixel value for 32BUI band got truncated from 4294967296.000000 to 0
 0
-WARNING:  Initial pixel value for 32BUI band got truncated from 214294967296.000000 to 3841569792
-3841569792
+4294967295
+WARNING:  Initial pixel value for 32BUI band got clamped from 4294967296.000000 to 4294967295
+4294967295
+WARNING:  Initial pixel value for 32BUI band got clamped from 214294967296.000000 to 4294967295
+4294967295
 WARNING:  Initial pixel value for 32BUI band got truncated from 21.460000 to 21
 21
-WARNING:  Initial pixel value for 32BUI band got truncated from 4294967296.000000 to 0
-0
+WARNING:  Initial pixel value for 32BUI band got clamped from 4294967296.000000 to 4294967295
+4294967295
 0
-WARNING:  Initial pixel value for 32BF band got truncated from 4294967000.000000 to 4.29497e+9
+WARNING:  Initial pixel value for 32BF band got converted from 4294967000.000000 to 4294967040.000000
 4294967040
-WARNING:  Initial pixel value for 32BF band got truncated from 4294967000.000000 to 4.29497e+9
+WARNING:  Initial pixel value for 32BF band got converted from 4294967000.000000 to 4294967040.000000
 4.29497e+9
-WARNING:  Initial pixel value for 32BF band got truncated from 4294967295.000000 to 4.29497e+9
+WARNING:  Initial pixel value for 32BF band got converted from 4294967295.000000 to 4294967296.000000
 4294967296
-WARNING:  Initial pixel value for 32BF band got truncated from 4294967295.000000 to 4.29497e+9
+WARNING:  Initial pixel value for 32BF band got converted from 4294967295.000000 to 4294967296.000000
 4.29497e+9
 4294967296
 4.29497e+9
-WARNING:  Initial pixel value for 32BF band got truncated from 21.460000 to 21.46
+WARNING:  Initial pixel value for 32BF band got converted from 21.460000 to 21.459999
 21.4599990844727
-WARNING:  Initial pixel value for 32BF band got truncated from 21.460000 to 21.46
+WARNING:  Initial pixel value for 32BF band got converted from 21.460000 to 21.459999
 21.46
-WARNING:  Initial pixel value for 32BF band got truncated from 21003.100000 to 21003.1
+WARNING:  Initial pixel value for 32BF band got converted from 21003.100000 to 21003.099609
 21003.099609375
-WARNING:  Initial pixel value for 32BF band got truncated from 21003.100000 to 21003.1
+WARNING:  Initial pixel value for 32BF band got converted from 21003.100000 to 21003.099609
 21003.1
-WARNING:  Initial pixel value for 32BF band got truncated from 123.456000 to 123.456
+WARNING:  Initial pixel value for 32BF band got converted from 123.456000 to 123.456001
 123.456001281738
-WARNING:  Initial pixel value for 32BF band got truncated from 123.456000 to 123.456
+WARNING:  Initial pixel value for 32BF band got converted from 123.456000 to 123.456001
 123.456
-WARNING:  Initial pixel value for 32BF band got truncated from 1234.567000 to 1234.57
+WARNING:  Initial pixel value for 32BF band got converted from 1234.567000 to 1234.567017
 1234.56701660156
-WARNING:  Initial pixel value for 32BF band got truncated from 1234.567000 to 1234.57
+WARNING:  Initial pixel value for 32BF band got converted from 1234.567000 to 1234.567017
 1234.57
-WARNING:  Initial pixel value for 32BF band got truncated from 210000.464564 to 210000
+WARNING:  Initial pixel value for 32BF band got converted from 210000.464564 to 210000.468750
 210000.46875
-WARNING:  Initial pixel value for 32BF band got truncated from 210000.464564 to 210000
+WARNING:  Initial pixel value for 32BF band got converted from 210000.464564 to 210000.468750
 210000
 -1
 0
index f38d4ece16daa0780a41ec0c113cacf80d510719..9e73aa3c2c5a06ba4c5a066bed3518bf5cf6b543 100644 (file)
@@ -11,6 +11,6 @@
 1x1 single band (32BF) no transform|t|t
 1x1 single band (64BF) no transform|t|t
 1x1 single band (64BF external) no transform|t|t
-ERROR:  Invalid value 2 for pixel of type 1BB
-ERROR:  Invalid value 4 for pixel of type 2BUI
-ERROR:  Invalid value 16 for pixel of type 4BUI
+ERROR:  rt_band_from_wkb: Invalid value 2 for pixel of type 1BB
+ERROR:  rt_band_from_wkb: Invalid value 4 for pixel of type 2BUI
+ERROR:  rt_band_from_wkb: Invalid value 16 for pixel of type 4BUI
index b6064111b28aa37aa205ee4eb7c17fd3aaf17180..0c7865c387d655b87733cfd62f65f2be3c70d9b8 100644 (file)
@@ -1,11 +1,11 @@
-NOTICE:  y coordinate can not be NULL. Returning NULL
-NOTICE:  y coordinate can not be NULL. Returning NULL
-NOTICE:  y coordinate can not be NULL. Returning NULL
-NOTICE:  y coordinate can not be NULL. Returning NULL
-NOTICE:  x coordinate can not be NULL. Returning NULL
-NOTICE:  x coordinate can not be NULL. Returning NULL
-NOTICE:  x coordinate can not be NULL. Returning NULL
-NOTICE:  x coordinate can not be NULL. Returning NULL
+NOTICE:  Y coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  Y coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  Y coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  Y coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  X coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  X coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  X coordinate can not be NULL when getting pixel value. Returning NULL
+NOTICE:  X coordinate can not be NULL when getting pixel value. Returning NULL
 NOTICE:  Raster do not have a nodata value defined. Pixel value not set. Returning raster
 NOTICE:  Raster do not have a nodata value defined. Pixel value not set. Returning raster
 NOTICE:  Raster do not have a nodata value defined. Pixel value not set. Returning raster