Create a new empty raster with having the same georeference as the provided raster
If this new raster is empty (width = 0 OR height = 0) then there is nothing to compute and we return it right now
Check if the raster has the required band. Otherwise, return a raster without band
We set the initial value of the future band to nodata value. If nodata value is null, then the raster will be initialized to rt_band_get_min_value but all the values should be recomputed anyway
Optimization: If the raster is only filled with nodata values return right now a raster filled with the nodatavalueexpr TODO: Call rt_band_check_isnodata instead?
Create the raster receiving all the computed values. Initialize it to the new initial value
We compute a value only for the withdata value pixel since the nodata value has already been set by the first optimization
5146 int x,
y,
nband, width, height;
5148 double newnodatavalue = 0.0;
5149 double newinitialvalue = 0.0;
5150 double newval = 0.0;
5155 #if POSTGIS_PGSQL_VERSION < 120
5156 FunctionCallInfoData cbdata;
5158 LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS);
5161 char * strFromText = NULL;
5167 if (PG_ARGISNULL(0)) {
5168 elog(WARNING,
"Raster is NULL. Returning NULL");
5174 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5177 PG_FREE_IF_COPY(pgraster, 0);
5178 elog(ERROR,
"RASTER_mapAlgebraFct: Could not deserialize raster");
5186 if (PG_ARGISNULL(1))
5189 nband = PG_GETARG_INT32(1);
5205 if ( NULL == newrast ) {
5208 PG_FREE_IF_COPY(pgraster, 0);
5210 elog(ERROR,
"RASTER_mapAlgebraFct: Could not create a new raster");
5235 elog(NOTICE,
"Raster is empty. Returning an empty raster");
5237 PG_FREE_IF_COPY(pgraster, 0);
5241 if (NULL == pgrtn) {
5242 elog(ERROR,
"RASTER_mapAlgebraFct: Could not serialize raster");
5246 SET_VARSIZE(pgrtn, pgrtn->
size);
5247 PG_RETURN_POINTER(pgrtn);
5257 elog(NOTICE,
"Raster does not have the required band. Returning a raster "
5260 PG_FREE_IF_COPY(pgraster, 0);
5264 if (NULL == pgrtn) {
5265 elog(ERROR,
"RASTER_mapAlgebraFct: Could not serialize raster");
5269 SET_VARSIZE(pgrtn, pgrtn->
size);
5270 PG_RETURN_POINTER(pgrtn);
5275 if ( NULL ==
band ) {
5276 elog(NOTICE,
"Could not get the required band. Returning a raster "
5279 PG_FREE_IF_COPY(pgraster, 0);
5283 if (NULL == pgrtn) {
5284 elog(ERROR,
"RASTER_mapAlgebraFct: Could not serialize raster");
5288 SET_VARSIZE(pgrtn, pgrtn->
size);
5289 PG_RETURN_POINTER(pgrtn);
5295 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Getting NODATA value for band...");
5312 newinitialvalue = newnodatavalue;
5319 if (PG_ARGISNULL(2)) {
5327 if (newpixeltype ==
PT_END)
5331 if (newpixeltype ==
PT_END) {
5334 PG_FREE_IF_COPY(pgraster, 0);
5337 elog(ERROR,
"RASTER_mapAlgebraFct: Invalid pixeltype");
5345 if (PG_ARGISNULL(3)) {
5348 PG_FREE_IF_COPY(pgraster, 0);
5351 elog(ERROR,
"RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5355 oid = PG_GETARG_OID(3);
5356 if (oid == InvalidOid) {
5359 PG_FREE_IF_COPY(pgraster, 0);
5362 elog(ERROR,
"RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5366 fmgr_info(oid, &cbinfo);
5369 if (cbinfo.fn_retset) {
5372 PG_FREE_IF_COPY(pgraster, 0);
5375 elog(ERROR,
"RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5379 else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5382 PG_FREE_IF_COPY(pgraster, 0);
5385 elog(ERROR,
"RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5389 if (cbinfo.fn_nargs == 2)
5394 if (func_volatile(oid) ==
'v') {
5395 elog(NOTICE,
"Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
5399 #if POSTGIS_PGSQL_VERSION < 120
5400 InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5402 cbdata.argnull[0] =
FALSE;
5403 cbdata.argnull[1] =
FALSE;
5404 cbdata.argnull[2] =
FALSE;
5406 InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5408 cbdata->args[0].isnull =
FALSE;
5409 cbdata->args[1].isnull =
FALSE;
5410 cbdata->args[2].isnull =
FALSE;
5414 if (PG_ARGISNULL(4)) {
5415 if (cbinfo.fn_strict) {
5418 PG_FREE_IF_COPY(pgraster, 0);
5421 elog(ERROR,
"RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5425 #if POSTGIS_PGSQL_VERSION < 120
5426 cbdata.arg[k] = (Datum)NULL;
5427 cbdata.argnull[k] =
TRUE;
5429 cbdata->args[k].value = (Datum)NULL;
5430 cbdata->args[k].isnull =
TRUE;
5434 #if POSTGIS_PGSQL_VERSION < 120
5435 cbdata.arg[k] = PG_GETARG_DATUM(4);
5437 cbdata->args[k].value = PG_GETARG_DATUM(4);
5448 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Band is a nodata band, returning "
5449 "a raster filled with nodata");
5452 newinitialvalue,
TRUE, newnodatavalue, 0);
5455 PG_FREE_IF_COPY(pgraster, 0);
5460 if (NULL == pgrtn) {
5461 elog(ERROR,
"RASTER_mapAlgebraFct: Could not serialize raster");
5465 SET_VARSIZE(pgrtn, pgrtn->
size);
5466 PG_RETURN_POINTER(pgrtn);
5475 newinitialvalue,
TRUE, newnodatavalue, 0);
5479 if ( NULL == newband ) {
5480 elog(NOTICE,
"Could not modify band for new raster. Returning new "
5481 "raster with the original band");
5484 PG_FREE_IF_COPY(pgraster, 0);
5489 if (NULL == pgrtn) {
5490 elog(ERROR,
"RASTER_mapAlgebraFct: Could not serialize raster");
5494 SET_VARSIZE(pgrtn, pgrtn->
size);
5495 PG_RETURN_POINTER(pgrtn);
5501 for (
x = 0;
x < width;
x++) {
5502 for(
y = 0;
y < height;
y++) {
5510 if (
FLT_EQ(
r, newnodatavalue)) {
5511 if (cbinfo.fn_strict) {
5512 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Strict callbacks cannot accept NULL arguments, skipping NODATA cell.");
5515 #if POSTGIS_PGSQL_VERSION < 120
5516 cbdata.argnull[0] =
TRUE;
5517 cbdata.arg[0] = (Datum)NULL;
5519 cbdata->args[0].isnull =
TRUE;
5520 cbdata->args[0].value = (Datum)NULL;
5524 #if POSTGIS_PGSQL_VERSION < 120
5525 cbdata.argnull[0] =
FALSE;
5526 cbdata.arg[0] = Float8GetDatum(
r);
5528 cbdata->args[0].isnull =
FALSE;
5529 cbdata->args[0].value = Float8GetDatum(
r);
5534 if (cbinfo.fn_nargs == 3) {
5538 d[0] = Int32GetDatum(
x+1);
5539 d[1] = Int32GetDatum(
y+1);
5541 a = construct_array(d, 2, INT4OID,
sizeof(
int32),
true,
'i');
5543 #if POSTGIS_PGSQL_VERSION < 120
5544 cbdata.argnull[1] =
FALSE;
5545 cbdata.arg[1] = PointerGetDatum(a);
5547 cbdata->args[1].isnull =
FALSE;
5548 cbdata->args[1].value = PointerGetDatum(a);
5555 #if POSTGIS_PGSQL_VERSION < 120
5556 tmpnewval = FunctionCallInvoke(&cbdata);
5558 if (cbdata.isnull) {
5559 newval = newnodatavalue;
5562 tmpnewval = FunctionCallInvoke(cbdata);
5566 newval = newnodatavalue;
5570 newval = DatumGetFloat8(tmpnewval);
5584 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: raster modified, serializing it.");
5588 PG_FREE_IF_COPY(pgraster, 0);
5599 SET_VARSIZE(pgrtn, pgrtn->
size);
5600 PG_RETURN_POINTER(pgrtn);
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
int rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
Generate a new inline band and add it to a raster.
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
uint16_t rt_raster_get_height(rt_raster raster)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
const char * rt_pixtype_name(rt_pixtype pixtype)
uint16_t rt_raster_get_width(rt_raster raster)
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
char * text_to_cstring(const text *textptr)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)