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
5138{
5145 int x,
y,
nband, width, height;
5147 double newnodatavalue = 0.0;
5148 double newinitialvalue = 0.0;
5149 double newval = 0.0;
5151 int ret = -1;
5152 Oid oid;
5153 FmgrInfo cbinfo;
5154#if POSTGIS_PGSQL_VERSION < 120
5155 FunctionCallInfoData cbdata;
5156#else
5157 LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS);
5158#endif
5159 Datum tmpnewval;
5160 char * strFromText = NULL;
5161 int k = 0;
5162
5164
5165
5166 if (PG_ARGISNULL(0)) {
5167 elog(WARNING, "Raster is NULL. Returning NULL");
5168 PG_RETURN_NULL();
5169 }
5170
5171
5172
5173 pgraster = (
rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5175 if (NULL == raster) {
5176 PG_FREE_IF_COPY(pgraster, 0);
5177 elog(ERROR, "RASTER_mapAlgebraFct: Could not deserialize raster");
5178 PG_RETURN_NULL();
5179 }
5180
5182
5183
5184
5185 if (PG_ARGISNULL(1))
5187 else
5188 nband = PG_GETARG_INT32(1);
5189
5190 if (nband < 1)
5192
5194
5201
5203
5204 if ( NULL == newrast ) {
5205
5207 PG_FREE_IF_COPY(pgraster, 0);
5208
5209 elog(ERROR, "RASTER_mapAlgebraFct: Could not create a new raster");
5210 PG_RETURN_NULL();
5211 }
5212
5216
5220
5224
5226
5227
5233 {
5234 elog(NOTICE, "Raster is empty. Returning an empty raster");
5236 PG_FREE_IF_COPY(pgraster, 0);
5237
5240 if (NULL == pgrtn) {
5241 elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5242 PG_RETURN_NULL();
5243 }
5244
5245 SET_VARSIZE(pgrtn, pgrtn->
size);
5246 PG_RETURN_POINTER(pgrtn);
5247 }
5248
5250
5256 elog(NOTICE, "Raster does not have the required band. Returning a raster "
5257 "without a band");
5259 PG_FREE_IF_COPY(pgraster, 0);
5260
5263 if (NULL == pgrtn) {
5264 elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5265 PG_RETURN_NULL();
5266 }
5267
5268 SET_VARSIZE(pgrtn, pgrtn->
size);
5269 PG_RETURN_POINTER(pgrtn);
5270 }
5271
5272
5274 if ( NULL == band ) {
5275 elog(NOTICE, "Could not get the required band. Returning a raster "
5276 "without a band");
5278 PG_FREE_IF_COPY(pgraster, 0);
5279
5282 if (NULL == pgrtn) {
5283 elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5284 PG_RETURN_NULL();
5285 }
5286
5287 SET_VARSIZE(pgrtn, pgrtn->
size);
5288 PG_RETURN_POINTER(pgrtn);
5289 }
5290
5291
5292
5293
5294 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Getting NODATA value for band...");
5295
5298 }
5299
5300 else {
5302 }
5303
5305 newnodatavalue);
5311 newinitialvalue = newnodatavalue;
5312
5317
5318 if (PG_ARGISNULL(2)) {
5320 }
5321
5322 else {
5325 pfree(strFromText);
5326 if (newpixeltype ==
PT_END)
5328 }
5329
5330 if (newpixeltype ==
PT_END) {
5331
5333 PG_FREE_IF_COPY(pgraster, 0);
5335
5336 elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5337 PG_RETURN_NULL();
5338 }
5339
5342
5343
5344 if (PG_ARGISNULL(3)) {
5345
5347 PG_FREE_IF_COPY(pgraster, 0);
5349
5350 elog(ERROR, "RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5351 PG_RETURN_NULL();
5352 }
5353
5354 oid = PG_GETARG_OID(3);
5355 if (oid == InvalidOid) {
5356
5358 PG_FREE_IF_COPY(pgraster, 0);
5360
5361 elog(ERROR, "RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5362 PG_RETURN_NULL();
5363 }
5364
5365 fmgr_info(oid, &cbinfo);
5366
5367
5368 if (cbinfo.fn_retset) {
5369
5371 PG_FREE_IF_COPY(pgraster, 0);
5373
5374 elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5375 PG_RETURN_NULL();
5376 }
5377
5378 else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5379
5381 PG_FREE_IF_COPY(pgraster, 0);
5383
5384 elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5385 PG_RETURN_NULL();
5386 }
5387
5388 if (cbinfo.fn_nargs == 2)
5389 k = 1;
5390 else
5391 k = 2;
5392
5393 if (func_volatile(oid) == 'v') {
5394 elog(NOTICE, "Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
5395 }
5396
5397
5398#if POSTGIS_PGSQL_VERSION < 120
5399 InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5400
5401 cbdata.argnull[0] =
FALSE;
5402 cbdata.argnull[1] =
FALSE;
5403 cbdata.argnull[2] =
FALSE;
5404#else
5405 InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5406
5407 cbdata->args[0].isnull =
FALSE;
5408 cbdata->args[1].isnull =
FALSE;
5409 cbdata->args[2].isnull =
FALSE;
5410#endif
5411
5412
5413 if (PG_ARGISNULL(4)) {
5414 if (cbinfo.fn_strict) {
5415
5417 PG_FREE_IF_COPY(pgraster, 0);
5419
5420 elog(ERROR, "RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5421 PG_RETURN_NULL();
5422 }
5423
5424#if POSTGIS_PGSQL_VERSION < 120
5425 cbdata.arg[k] = (Datum)NULL;
5426 cbdata.argnull[k] =
TRUE;
5427#else
5428 cbdata->args[k].value = (Datum)NULL;
5429 cbdata->args[k].isnull =
TRUE;
5430#endif
5431 }
5432 else {
5433#if POSTGIS_PGSQL_VERSION < 120
5434 cbdata.arg[k] = PG_GETARG_DATUM(4);
5435#else
5436 cbdata->args[k].value = PG_GETARG_DATUM(4);
5437#endif
5438 }
5439
5446
5447 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Band is a nodata band, returning "
5448 "a raster filled with nodata");
5449
5451 newinitialvalue,
TRUE, newnodatavalue, 0);
5452
5454 PG_FREE_IF_COPY(pgraster, 0);
5455
5456
5459 if (NULL == pgrtn) {
5460 elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5461 PG_RETURN_NULL();
5462 }
5463
5464 SET_VARSIZE(pgrtn, pgrtn->
size);
5465 PG_RETURN_POINTER(pgrtn);
5466 }
5467
5468
5474 newinitialvalue,
TRUE, newnodatavalue, 0);
5475
5476
5478 if ( NULL == newband ) {
5479 elog(NOTICE, "Could not modify band for new raster. Returning new "
5480 "raster with the original band");
5481
5483 PG_FREE_IF_COPY(pgraster, 0);
5484
5485
5488 if (NULL == pgrtn) {
5489 elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5490 PG_RETURN_NULL();
5491 }
5492
5493 SET_VARSIZE(pgrtn, pgrtn->
size);
5494 PG_RETURN_POINTER(pgrtn);
5495 }
5496
5498 width, height);
5499
5500 for (x = 0;
x < width;
x++) {
5501 for(y = 0;
y < height;
y++) {
5503
5509 if (
FLT_EQ(
r, newnodatavalue)) {
5510 if (cbinfo.fn_strict) {
5511 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: Strict callbacks cannot accept NULL arguments, skipping NODATA cell.");
5512 continue;
5513 }
5514#if POSTGIS_PGSQL_VERSION < 120
5515 cbdata.argnull[0] =
TRUE;
5516 cbdata.arg[0] = (Datum)NULL;
5517#else
5518 cbdata->args[0].isnull =
TRUE;
5519 cbdata->args[0].value = (Datum)NULL;
5520#endif
5521 }
5522 else {
5523#if POSTGIS_PGSQL_VERSION < 120
5524 cbdata.argnull[0] =
FALSE;
5525 cbdata.arg[0] = Float8GetDatum(
r);
5526#else
5527 cbdata->args[0].isnull =
FALSE;
5528 cbdata->args[0].value = Float8GetDatum(
r);
5529#endif
5530 }
5531
5532
5533 if (cbinfo.fn_nargs == 3) {
5534 Datum d[2];
5535 ArrayType *a;
5536
5537 d[0] = Int32GetDatum(x+1);
5538 d[1] = Int32GetDatum(y+1);
5539
5540 a = construct_array(d, 2, INT4OID,
sizeof(
int32),
true,
'i');
5541
5542#if POSTGIS_PGSQL_VERSION < 120
5543 cbdata.argnull[1] =
FALSE;
5544 cbdata.arg[1] = PointerGetDatum(a);
5545#else
5546 cbdata->args[1].isnull =
FALSE;
5547 cbdata->args[1].value = PointerGetDatum(a);
5548#endif
5549 }
5550
5553
5554#if POSTGIS_PGSQL_VERSION < 120
5555 tmpnewval = FunctionCallInvoke(&cbdata);
5556
5557 if (cbdata.isnull) {
5558 newval = newnodatavalue;
5559 }
5560#else
5561 tmpnewval = FunctionCallInvoke(cbdata);
5562
5563 if (cbdata->isnull)
5564 {
5565 newval = newnodatavalue;
5566 }
5567#endif
5568 else {
5569 newval = DatumGetFloat8(tmpnewval);
5570 }
5571
5573 newval);
5574
5576 }
5577
5578 }
5579 }
5580
5581
5582
5583 POSTGIS_RT_DEBUG(3,
"RASTER_mapAlgebraFct: raster modified, serializing it.");
5584
5585
5587 PG_FREE_IF_COPY(pgraster, 0);
5588
5591 if (NULL == pgrtn)
5592 PG_RETURN_NULL();
5593
5595
5597
5598 SET_VARSIZE(pgrtn, pgrtn->
size);
5599 PG_RETURN_POINTER(pgrtn);
5600}
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.
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
const char * rt_pixtype_name(rt_pixtype pixtype)
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.
uint16_t rt_raster_get_width(rt_raster raster)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
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,...)