if (!PG_ARGISNULL(2))
exclude_nodata_value = PG_GETARG_BOOL(2);
+ /* see if band is NODATA */
+ if (rt_band_get_isnodata_flag(rt_raster_get_band(raster, nband - 1))) {
+ POSTGIS_RT_DEBUGF(3, "Band at index %d is NODATA. Returning NULL");
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ MemoryContextSwitchTo(oldcontext);
+ SRF_RETURN_DONE(funcctx);
+ }
+
/* Polygonize raster */
/**
PG_FUNCTION_INFO_V1(RASTER_makeEmpty);
Datum RASTER_makeEmpty(PG_FUNCTION_ARGS)
{
- uint16 width, height;
- double ipx, ipy, scalex, scaley, skewx, skewy;
- int32_t srid;
- rt_pgraster *pgraster;
- rt_raster raster;
+ uint16 width = 0, height = 0;
+ double ipx = 0, ipy = 0, scalex = 0, scaley = 0, skewx = 0, skewy = 0;
+ int32_t srid = SRID_UNKNOWN;
+ rt_pgraster *pgraster = NULL;
+ rt_raster raster;
- if ( PG_NARGS() < 9 )
- {
- elog(ERROR, "RASTER_makeEmpty: ST_MakeEmptyRaster requires 9 args");
- PG_RETURN_NULL();
- }
+ if (PG_NARGS() < 9) {
+ elog(ERROR, "RASTER_makeEmpty: ST_MakeEmptyRaster requires 9 args");
+ PG_RETURN_NULL();
+ }
- if (PG_ARGISNULL(0))
- width = 0;
- else
- width = PG_GETARG_UINT16(0);
+ if (!PG_ARGISNULL(0))
+ width = PG_GETARG_UINT16(0);
- if (PG_ARGISNULL(1))
- height = 0;
- else
- height = PG_GETARG_UINT16(1);
-
- if (PG_ARGISNULL(2))
- ipx = 0;
- else
- ipx = PG_GETARG_FLOAT8(2);
+ if (!PG_ARGISNULL(1))
+ height = PG_GETARG_UINT16(1);
- if (PG_ARGISNULL(3))
- ipy = 0;
- else
- ipy = PG_GETARG_FLOAT8(3);
+ if (!PG_ARGISNULL(2))
+ ipx = PG_GETARG_FLOAT8(2);
- if (PG_ARGISNULL(4))
- scalex = 0;
- else
- scalex = PG_GETARG_FLOAT8(4);
+ if (!PG_ARGISNULL(3))
+ ipy = PG_GETARG_FLOAT8(3);
- if (PG_ARGISNULL(5))
- scaley = 0;
- else
- scaley = PG_GETARG_FLOAT8(5);
+ if (!PG_ARGISNULL(4))
+ scalex = PG_GETARG_FLOAT8(4);
- if (PG_ARGISNULL(6))
- skewx = 0;
- else
- skewx = PG_GETARG_FLOAT8(6);
+ if (!PG_ARGISNULL(5))
+ scaley = PG_GETARG_FLOAT8(5);
- if (PG_ARGISNULL(7))
- skewy = 0;
- else
- skewy = PG_GETARG_FLOAT8(7);
+ if (!PG_ARGISNULL(6))
+ skewx = PG_GETARG_FLOAT8(6);
- if (PG_ARGISNULL(8))
- srid = SRID_UNKNOWN;
- else
- srid = PG_GETARG_INT32(8);
+ if (!PG_ARGISNULL(7))
+ skewy = PG_GETARG_FLOAT8(7);
- POSTGIS_RT_DEBUGF(4, "%dx%d, ip:%g,%g, scale:%g,%g, skew:%g,%g srid:%d",
- width, height, ipx, ipy, scalex, scaley,
- skewx, skewy, srid);
+ if (!PG_ARGISNULL(8))
+ srid = PG_GETARG_INT32(8);
- raster = rt_raster_new(width, height);
- if ( ! raster ) {
- PG_RETURN_NULL(); /* error was supposedly printed already */
- }
+ POSTGIS_RT_DEBUGF(4, "%dx%d, ip:%g,%g, scale:%g,%g, skew:%g,%g srid:%d",
+ width, height, ipx, ipy, scalex, scaley,
+ skewx, skewy, srid);
- rt_raster_set_scale(raster, scalex, scaley);
- rt_raster_set_offsets(raster, ipx, ipy);
- rt_raster_set_skews(raster, skewx, skewy);
- rt_raster_set_srid(raster, srid);
+ raster = rt_raster_new(width, height);
+ if (!raster) {
+ PG_RETURN_NULL(); /* error was supposedly printed already */
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ rt_raster_set_scale(raster, scalex, scaley);
+ rt_raster_set_offsets(raster, ipx, ipy);
+ rt_raster_set_skews(raster, skewx, skewy);
+ rt_raster_set_srid(raster, srid);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgraster = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ if (!pgraster)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgraster, pgraster->size);
+ PG_RETURN_POINTER(pgraster);
}
/**
/* Set the band's nodata value */
rt_band_set_nodata(band, nodata);
- /* Set the hasnodata flag to TRUE */
- rt_band_set_hasnodata_flag(band, TRUE);
-
/* Recheck all pixels if requested */
if (forcechecking)
rt_band_check_is_nodata(band);
if (!band)
elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
else {
+ if (!rt_band_get_hasnodata_flag(band)) {
+ elog(NOTICE, "Band of index %d has no NODATA so cannot be NODATA. Returning original raster", bandindex);
+ }
/* Set the band's nodata value */
- rt_band_set_isnodata_flag(band, 1);
+ else {
+ rt_band_set_isnodata_flag(band, 1);
+ }
}
}
bool *nulls;
double val = 0;
+ int isnodata = 0;
int hasnodata = 0;
double nodataval = 0;
memset(arg1->nodata[z], 0, sizeof(bool) * arg1->rows * arg1->columns);
i = 0;
+
+ /* shortcut if band is NODATA */
+ if (rt_band_get_isnodata_flag(band)) {
+ for (i = (arg1->rows * arg1->columns) - 1; i >= 0; i--)
+ arg1->nodata[z][i] = TRUE;
+ continue;
+ }
+
for (y = 0; y < arg1->rows; y++) {
for (x = 0; x < arg1->columns; x++) {
/* get pixel */
- if (rt_band_get_pixel(band, x, y, &val, NULL) != 0) {
+ if (rt_band_get_pixel(band, x, y, &val, &isnodata) != 0) {
elog(ERROR, "RASTER_dumpValues: Unable to pixel (%d, %d) of band %d", x, y, arg1->nbands[z] + 1);
rtpg_dumpvalues_arg_destroy(arg1);
rt_raster_destroy(raster);
}
POSTGIS_RT_DEBUGF(5, "clamped is?: %d", rt_band_clamped_value_is_nodata(band, val));
- if (
- exclude_nodata_value &&
- hasnodata && (
- FLT_EQ(val, nodataval) ||
- rt_band_clamped_value_is_nodata(band, val) == 1
- )
- ) {
+ if (exclude_nodata_value && isnodata) {
arg1->nodata[z][i] = TRUE;
POSTGIS_RT_DEBUG(5, "nodata = 1");
}
int rtn = 0;
double val = 0;
+ int isnodata = 0;
int i = 0;
int j = 0;
/* if hasnodata = TRUE and keepnodata = TRUE, inspect pixel value */
if (hasnodata && keepnodata) {
- rtn = rt_band_get_pixel(band, pixval[i].x, pixval[i].y, &val, NULL);
+ rtn = rt_band_get_pixel(band, pixval[i].x, pixval[i].y, &val, &isnodata);
if (rtn != 0) {
elog(ERROR, "Cannot get value of pixel. Returning NULL");
pfree(pixval);
}
/* pixel value = NODATA, skip */
- if (
- FLT_EQ(val, nodataval) ||
- rt_band_clamped_value_is_nodata(band, val) == 1
- ) {
+ if (isnodata) {
continue;
}
}
arg->keepnodata = PG_GETARG_BOOL(3);
POSTGIS_RT_DEBUGF(3, "keepnodata = %d", arg->keepnodata);
+ /* keepnodata = TRUE and band is NODATA */
+ if (arg->keepnodata && rt_band_get_isnodata_flag(band)) {
+ POSTGIS_RT_DEBUG(3, "keepnodata = TRUE and band is NODATA. Not doing anything");
+ }
/* all elements are points */
- if (allpoint == arg->ngv) {
+ else if (allpoint == arg->ngv) {
double igt[6] = {0};
double xy[2] = {0};
double value = 0;
+ int isnodata = 0;
LWCOLLECTION *coll = NULL;
LWPOINT *point = NULL;
}
/* get pixel value */
- if (rt_band_get_pixel(band, xy[0], xy[1], &value, NULL) != 0) {
+ if (rt_band_get_pixel(band, xy[0], xy[1], &value, &isnodata) != 0) {
elog(ERROR, "RASTER_setPixelValuesGeomval: Unable to get pixel value");
rtpg_setvaluesgv_arg_destroy(arg);
rt_raster_destroy(raster);
}
/* keepnodata = TRUE AND pixel value is NODATA */
- if (
- arg->keepnodata &&
- hasnodata && (
- FLT_EQ(value, nodataval) ||
- rt_band_clamped_value_is_nodata(band, value) == 1
- )
- ) {
+ if (arg->keepnodata && isnodata)
continue;
- }
/* set pixel */
if (arg->gv[i].pixval.nodata)
int y = 0;
int bounds[4] = {0};
int pixcount = 0;
+ int isnodata = 0;
LWPOLY *poly;
/* value, NODATA flag */
if (!noband) {
- if (rt_band_get_pixel(band, x - 1, y - 1, &(pix[pixcount].value), NULL) != 0) {
+ if (rt_band_get_pixel(band, x - 1, y - 1, &(pix[pixcount].value), &isnodata) != 0) {
elog(ERROR, "RASTER_getPixelPolygons: Could not get pixel value");
for (i = 0; i < pixcount; i++)
SRF_RETURN_DONE(funcctx);
}
- if (
- !exclude_nodata_value ||
- !hasnodata || (
- exclude_nodata_value &&
- (hasnodata != FALSE) && (
- FLT_NEQ(pix[pixcount].value, nodataval) &&
- (rt_band_clamped_value_is_nodata(band, pix[pixcount].value) != 1)
- )
- )
- ) {
+ if (!exclude_nodata_value || !isnodata) {
pix[pixcount].nodata = 0;
}
else {
double argval[3] = {0.};
int hasnodatanodataval = 0;
double nodatanodataval = 0;
+ int isnodata = 0;
Oid ufc_noid = InvalidOid;
FmgrInfo ufl_info;
(_x >= 0 && _x < _dim[i][0]) &&
(_y >= 0 && _y < _dim[i][1])
) {
- err = rt_band_get_pixel(_band[i], _x, _y, &(_pixel[i]), NULL);
+ err = rt_band_get_pixel(_band[i], _x, _y, &(_pixel[i]), &isnodata);
if (err < 0) {
elog(ERROR, "RASTER_mapAlgebra2: Unable to get pixel of %s raster", (i < 1 ? "FIRST" : "SECOND"));
PG_RETURN_NULL();
}
- if (!_hasnodata[i] || FLT_NEQ(_nodataval[i], _pixel[i]))
+ if (!_hasnodata[i] || !isnodata)
_haspixel[i] = 1;
}