PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_mapAlgebraFct()

Datum RASTER_mapAlgebraFct ( PG_FUNCTION_ARGS  )

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

Set the new pixeltype

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

Definition at line 5137 of file rtpg_mapalgebra.c.

5138{
5139 rt_pgraster *pgraster = NULL;
5140 rt_pgraster *pgrtn = NULL;
5141 rt_raster raster = NULL;
5142 rt_raster newrast = NULL;
5143 rt_band band = NULL;
5144 rt_band newband = NULL;
5145 int x, y, nband, width, height;
5146 double r;
5147 double newnodatavalue = 0.0;
5148 double newinitialvalue = 0.0;
5149 double newval = 0.0;
5150 rt_pixtype newpixeltype;
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); /* Could be optimized */
5158#endif
5159 Datum tmpnewval;
5160 char * strFromText = NULL;
5161 int k = 0;
5162
5163 POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
5164
5165 /* Check raster */
5166 if (PG_ARGISNULL(0)) {
5167 elog(WARNING, "Raster is NULL. Returning NULL");
5168 PG_RETURN_NULL();
5169 }
5170
5171
5172 /* Deserialize raster */
5173 pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5174 raster = rt_raster_deserialize(pgraster, FALSE);
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
5181 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting arguments...");
5182
5183 /* Get the rest of the arguments */
5184
5185 if (PG_ARGISNULL(1))
5186 nband = 1;
5187 else
5188 nband = PG_GETARG_INT32(1);
5189
5190 if (nband < 1)
5191 nband = 1;
5192
5193 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Creating new empty raster...");
5194
5199 width = rt_raster_get_width(raster);
5200 height = rt_raster_get_height(raster);
5201
5202 newrast = rt_raster_new(width, height);
5203
5204 if ( NULL == newrast ) {
5205
5206 rt_raster_destroy(raster);
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
5213 rt_raster_set_scale(newrast,
5214 rt_raster_get_x_scale(raster),
5215 rt_raster_get_y_scale(raster));
5216
5217 rt_raster_set_offsets(newrast,
5218 rt_raster_get_x_offset(raster),
5219 rt_raster_get_y_offset(raster));
5220
5221 rt_raster_set_skews(newrast,
5222 rt_raster_get_x_skew(raster),
5223 rt_raster_get_y_skew(raster));
5224
5225 rt_raster_set_srid(newrast, rt_raster_get_srid(raster));
5226
5227
5232 if (rt_raster_is_empty(newrast))
5233 {
5234 elog(NOTICE, "Raster is empty. Returning an empty raster");
5235 rt_raster_destroy(raster);
5236 PG_FREE_IF_COPY(pgraster, 0);
5237
5238 pgrtn = rt_raster_serialize(newrast);
5239 rt_raster_destroy(newrast);
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
5249 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Getting raster band %d...", nband);
5250
5255 if (!rt_raster_has_band(raster, nband - 1)) {
5256 elog(NOTICE, "Raster does not have the required band. Returning a raster "
5257 "without a band");
5258 rt_raster_destroy(raster);
5259 PG_FREE_IF_COPY(pgraster, 0);
5260
5261 pgrtn = rt_raster_serialize(newrast);
5262 rt_raster_destroy(newrast);
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 /* Get the raster band */
5273 band = rt_raster_get_band(raster, nband - 1);
5274 if ( NULL == band ) {
5275 elog(NOTICE, "Could not get the required band. Returning a raster "
5276 "without a band");
5277 rt_raster_destroy(raster);
5278 PG_FREE_IF_COPY(pgraster, 0);
5279
5280 pgrtn = rt_raster_serialize(newrast);
5281 rt_raster_destroy(newrast);
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 * Get NODATA value
5293 */
5294 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting NODATA value for band...");
5295
5296 if (rt_band_get_hasnodata_flag(band)) {
5297 rt_band_get_nodata(band, &newnodatavalue);
5298 }
5299
5300 else {
5301 newnodatavalue = rt_band_get_min_value(band);
5302 }
5303
5304 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: NODATA value for band: %f",
5305 newnodatavalue);
5311 newinitialvalue = newnodatavalue;
5312
5316 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Setting pixeltype...");
5317
5318 if (PG_ARGISNULL(2)) {
5319 newpixeltype = rt_band_get_pixtype(band);
5320 }
5321
5322 else {
5323 strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
5324 newpixeltype = rt_pixtype_index_from_name(strFromText);
5325 pfree(strFromText);
5326 if (newpixeltype == PT_END)
5327 newpixeltype = rt_band_get_pixtype(band);
5328 }
5329
5330 if (newpixeltype == PT_END) {
5331
5332 rt_raster_destroy(raster);
5333 PG_FREE_IF_COPY(pgraster, 0);
5334 rt_raster_destroy(newrast);
5335
5336 elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5337 PG_RETURN_NULL();
5338 }
5339
5340 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Pixeltype set to %s",
5341 rt_pixtype_name(newpixeltype));
5342
5343 /* Get the name of the callback user function for raster values */
5344 if (PG_ARGISNULL(3)) {
5345
5346 rt_raster_destroy(raster);
5347 PG_FREE_IF_COPY(pgraster, 0);
5348 rt_raster_destroy(newrast);
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
5357 rt_raster_destroy(raster);
5358 PG_FREE_IF_COPY(pgraster, 0);
5359 rt_raster_destroy(newrast);
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 /* function cannot return set */
5368 if (cbinfo.fn_retset) {
5369
5370 rt_raster_destroy(raster);
5371 PG_FREE_IF_COPY(pgraster, 0);
5372 rt_raster_destroy(newrast);
5373
5374 elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5375 PG_RETURN_NULL();
5376 }
5377 /* function should have correct # of args */
5378 else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5379
5380 rt_raster_destroy(raster);
5381 PG_FREE_IF_COPY(pgraster, 0);
5382 rt_raster_destroy(newrast);
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 /* prep function call data */
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 /* check that the function isn't strict if the args are null. */
5413 if (PG_ARGISNULL(4)) {
5414 if (cbinfo.fn_strict) {
5415
5416 rt_raster_destroy(raster);
5417 PG_FREE_IF_COPY(pgraster, 0);
5418 rt_raster_destroy(newrast);
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
5445 if (rt_band_get_isnodata_flag(band)) {
5446
5447 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Band is a nodata band, returning "
5448 "a raster filled with nodata");
5449
5450 ret = rt_raster_generate_new_band(newrast, newpixeltype,
5451 newinitialvalue, TRUE, newnodatavalue, 0);
5452
5453 rt_raster_destroy(raster);
5454 PG_FREE_IF_COPY(pgraster, 0);
5455
5456 /* Serialize created raster */
5457 pgrtn = rt_raster_serialize(newrast);
5458 rt_raster_destroy(newrast);
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
5473 ret = rt_raster_generate_new_band(newrast, newpixeltype,
5474 newinitialvalue, TRUE, newnodatavalue, 0);
5475
5476 /* Get the new raster band */
5477 newband = rt_raster_get_band(newrast, 0);
5478 if ( NULL == newband ) {
5479 elog(NOTICE, "Could not modify band for new raster. Returning new "
5480 "raster with the original band");
5481
5482 rt_raster_destroy(raster);
5483 PG_FREE_IF_COPY(pgraster, 0);
5484
5485 /* Serialize created raster */
5486 pgrtn = rt_raster_serialize(newrast);
5487 rt_raster_destroy(newrast);
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
5497 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Main computing loop (%d x %d)",
5498 width, height);
5499
5500 for (x = 0; x < width; x++) {
5501 for(y = 0; y < height; y++) {
5502 ret = rt_band_get_pixel(band, x, y, &r, NULL);
5503
5508 if (ret == ES_NONE) {
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 /* Add pixel positions if callback has proper # of args */
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
5551 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
5552 x, y, r);
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
5572 POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: new value = %f",
5573 newval);
5574
5575 rt_band_set_pixel(newband, x, y, newval, NULL);
5576 }
5577
5578 }
5579 }
5580
5581 /* The newrast band has been modified */
5582
5583 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster modified, serializing it.");
5584 /* Serialize created raster */
5585
5586 rt_raster_destroy(raster);
5587 PG_FREE_IF_COPY(pgraster, 0);
5588
5589 pgrtn = rt_raster_serialize(newrast);
5590 rt_raster_destroy(newrast);
5591 if (NULL == pgrtn)
5592 PG_RETURN_NULL();
5593
5594 POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster serialized");
5595
5596 POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebraFct: returning raster");
5597
5598 SET_VARSIZE(pgrtn, pgrtn->size);
5599 PG_RETURN_POINTER(pgrtn);
5600}
char * r
Definition cu_in_wkt.c:24
#define TRUE
Definition dbfopen.c:169
#define FALSE
Definition dbfopen.c:168
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition rt_raster.c:356
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition rt_raster.c:181
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition rt_raster.c:213
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.
Definition rt_raster.c:485
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition rt_raster.c:137
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition rt_band.c:674
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
Definition rt_pixel.c:80
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1221
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:82
rt_pixtype
Definition librtcore.h:185
@ PT_END
Definition librtcore.h:197
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
Definition rt_raster.c:168
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition rt_band.c:714
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition rt_raster.c:48
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition rt_raster.c:1342
#define FLT_EQ(x, y)
Definition librtcore.h:2235
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition rt_raster.c:150
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition rt_pixel.c:110
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
Definition rt_band.c:1745
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition rt_band.c:974
@ ES_NONE
Definition librtcore.h:180
uint16_t rt_raster_get_height(rt_raster raster)
Definition rt_raster.c:129
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition rt_raster.c:363
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition rt_band.c:1730
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition rt_band.c:631
uint16_t rt_raster_get_width(rt_raster raster)
Definition rt_raster.c:121
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.
Definition rt_raster.c:159
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition rt_raster.c:190
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition rt_raster.c:199
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.
Definition rt_raster.c:222
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition rt_raster.c:1329
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:381
nband
Definition pixval.py:53
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:121
char * text_to_cstring(const text *textptr)
#define POSTGIS_RT_DEBUG(level, msg)
Definition rtpostgis.h:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:65
unsigned int int32
Definition shpopen.c:273
Struct definitions.
Definition librtcore.h:2251

References ES_NONE, FALSE, FLT_EQ, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, PT_END, r, rt_band_get_hasnodata_flag(), rt_band_get_isnodata_flag(), rt_band_get_min_value(), rt_band_get_nodata(), rt_band_get_pixel(), rt_band_get_pixtype(), rt_band_set_pixel(), rt_pixtype_index_from_name(), rt_pixtype_name(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_generate_new_band(), rt_raster_get_band(), rt_raster_get_height(), rt_raster_get_srid(), rt_raster_get_width(), rt_raster_get_x_offset(), rt_raster_get_x_scale(), rt_raster_get_x_skew(), rt_raster_get_y_offset(), rt_raster_get_y_scale(), rt_raster_get_y_skew(), rt_raster_has_band(), rt_raster_is_empty(), rt_raster_new(), rt_raster_serialize(), rt_raster_set_offsets(), rt_raster_set_scale(), rt_raster_set_skews(), rt_raster_set_srid(), rt_raster_serialized_t::size, text_to_cstring(), and TRUE.

Here is the call graph for this function: