PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ 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 5240 of file rtpg_mapalgebra.c.

5241 {
5242  rt_pgraster *pgraster = NULL;
5243  rt_pgraster *pgrtn = NULL;
5244  rt_raster raster = NULL;
5245  rt_raster newrast = NULL;
5246  rt_band band = NULL;
5247  rt_band newband = NULL;
5248  int x, y, nband, width, height;
5249  double r;
5250  double newnodatavalue = 0.0;
5251  double newinitialvalue = 0.0;
5252  double newval = 0.0;
5253  rt_pixtype newpixeltype;
5254  int ret = -1;
5255  Oid oid;
5256  FmgrInfo cbinfo;
5257  LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS); /* Could be optimized */
5258 
5259  Datum tmpnewval;
5260  char * strFromText = NULL;
5261  int k = 0;
5262 
5263  POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
5264 
5265  /* Check raster */
5266  if (PG_ARGISNULL(0)) {
5267  elog(WARNING, "Raster is NULL. Returning NULL");
5268  PG_RETURN_NULL();
5269  }
5270 
5271 
5272  /* Deserialize raster */
5273  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5274  raster = rt_raster_deserialize(pgraster, FALSE);
5275  if (NULL == raster) {
5276  PG_FREE_IF_COPY(pgraster, 0);
5277  elog(ERROR, "RASTER_mapAlgebraFct: Could not deserialize raster");
5278  PG_RETURN_NULL();
5279  }
5280 
5281  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting arguments...");
5282 
5283  /* Get the rest of the arguments */
5284 
5285  if (PG_ARGISNULL(1))
5286  nband = 1;
5287  else
5288  nband = PG_GETARG_INT32(1);
5289 
5290  if (nband < 1)
5291  nband = 1;
5292 
5293  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Creating new empty raster...");
5294 
5299  width = rt_raster_get_width(raster);
5300  height = rt_raster_get_height(raster);
5301 
5302  newrast = rt_raster_new(width, height);
5303 
5304  if ( NULL == newrast ) {
5305 
5307  PG_FREE_IF_COPY(pgraster, 0);
5308 
5309  elog(ERROR, "RASTER_mapAlgebraFct: Could not create a new raster");
5310  PG_RETURN_NULL();
5311  }
5312 
5313  rt_raster_set_scale(newrast,
5316 
5317  rt_raster_set_offsets(newrast,
5320 
5321  rt_raster_set_skews(newrast,
5324 
5326 
5327 
5332  if (rt_raster_is_empty(newrast))
5333  {
5334  elog(NOTICE, "Raster is empty. Returning an empty raster");
5336  PG_FREE_IF_COPY(pgraster, 0);
5337 
5338  pgrtn = rt_raster_serialize(newrast);
5339  rt_raster_destroy(newrast);
5340  if (NULL == pgrtn) {
5341  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5342  PG_RETURN_NULL();
5343  }
5344 
5345  SET_VARSIZE(pgrtn, pgrtn->size);
5346  PG_RETURN_POINTER(pgrtn);
5347  }
5348 
5349  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Getting raster band %d...", nband);
5350 
5355  if (!rt_raster_has_band(raster, nband - 1)) {
5356  elog(NOTICE, "Raster does not have the required band. Returning a raster "
5357  "without a band");
5359  PG_FREE_IF_COPY(pgraster, 0);
5360 
5361  pgrtn = rt_raster_serialize(newrast);
5362  rt_raster_destroy(newrast);
5363  if (NULL == pgrtn) {
5364  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5365  PG_RETURN_NULL();
5366  }
5367 
5368  SET_VARSIZE(pgrtn, pgrtn->size);
5369  PG_RETURN_POINTER(pgrtn);
5370  }
5371 
5372  /* Get the raster band */
5374  if ( NULL == band ) {
5375  elog(NOTICE, "Could not get the required band. Returning a raster "
5376  "without a band");
5378  PG_FREE_IF_COPY(pgraster, 0);
5379 
5380  pgrtn = rt_raster_serialize(newrast);
5381  rt_raster_destroy(newrast);
5382  if (NULL == pgrtn) {
5383  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5384  PG_RETURN_NULL();
5385  }
5386 
5387  SET_VARSIZE(pgrtn, pgrtn->size);
5388  PG_RETURN_POINTER(pgrtn);
5389  }
5390 
5391  /*
5392  * Get NODATA value
5393  */
5394  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting NODATA value for band...");
5395 
5397  rt_band_get_nodata(band, &newnodatavalue);
5398  }
5399 
5400  else {
5401  newnodatavalue = rt_band_get_min_value(band);
5402  }
5403 
5404  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: NODATA value for band: %f",
5405  newnodatavalue);
5411  newinitialvalue = newnodatavalue;
5412 
5416  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Setting pixeltype...");
5417 
5418  if (PG_ARGISNULL(2)) {
5419  newpixeltype = rt_band_get_pixtype(band);
5420  }
5421 
5422  else {
5423  strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
5424  newpixeltype = rt_pixtype_index_from_name(strFromText);
5425  pfree(strFromText);
5426  if (newpixeltype == PT_END)
5427  newpixeltype = rt_band_get_pixtype(band);
5428  }
5429 
5430  if (newpixeltype == PT_END) {
5431 
5433  PG_FREE_IF_COPY(pgraster, 0);
5434  rt_raster_destroy(newrast);
5435 
5436  elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5437  PG_RETURN_NULL();
5438  }
5439 
5440  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Pixeltype set to %s",
5441  rt_pixtype_name(newpixeltype));
5442 
5443  /* Get the name of the callback user function for raster values */
5444  if (PG_ARGISNULL(3)) {
5445 
5447  PG_FREE_IF_COPY(pgraster, 0);
5448  rt_raster_destroy(newrast);
5449 
5450  elog(ERROR, "RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5451  PG_RETURN_NULL();
5452  }
5453 
5454  oid = PG_GETARG_OID(3);
5455  if (oid == InvalidOid) {
5456 
5458  PG_FREE_IF_COPY(pgraster, 0);
5459  rt_raster_destroy(newrast);
5460 
5461  elog(ERROR, "RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5462  PG_RETURN_NULL();
5463  }
5464 
5465  fmgr_info(oid, &cbinfo);
5466 
5467  /* function cannot return set */
5468  if (cbinfo.fn_retset) {
5469 
5471  PG_FREE_IF_COPY(pgraster, 0);
5472  rt_raster_destroy(newrast);
5473 
5474  elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5475  PG_RETURN_NULL();
5476  }
5477  /* function should have correct # of args */
5478  else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5479 
5481  PG_FREE_IF_COPY(pgraster, 0);
5482  rt_raster_destroy(newrast);
5483 
5484  elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5485  PG_RETURN_NULL();
5486  }
5487 
5488  if (cbinfo.fn_nargs == 2)
5489  k = 1;
5490  else
5491  k = 2;
5492 
5493  if (func_volatile(oid) == 'v') {
5494  elog(NOTICE, "Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
5495  }
5496 
5497  /* prep function call data */
5498  InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5499 
5500  cbdata->args[0].isnull = FALSE;
5501  cbdata->args[1].isnull = FALSE;
5502  cbdata->args[2].isnull = FALSE;
5503 
5504  /* check that the function isn't strict if the args are null. */
5505  if (PG_ARGISNULL(4)) {
5506  if (cbinfo.fn_strict) {
5507 
5509  PG_FREE_IF_COPY(pgraster, 0);
5510  rt_raster_destroy(newrast);
5511 
5512  elog(ERROR, "RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5513  PG_RETURN_NULL();
5514  }
5515 
5516  cbdata->args[k].value = (Datum)NULL;
5517  cbdata->args[k].isnull = TRUE;
5518  }
5519  else {
5520  cbdata->args[k].value = PG_GETARG_DATUM(4);
5521  }
5522 
5529 
5530  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Band is a nodata band, returning "
5531  "a raster filled with nodata");
5532 
5533  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5534  newinitialvalue, TRUE, newnodatavalue, 0);
5535 
5537  PG_FREE_IF_COPY(pgraster, 0);
5538 
5539  /* Serialize created raster */
5540  pgrtn = rt_raster_serialize(newrast);
5541  rt_raster_destroy(newrast);
5542  if (NULL == pgrtn) {
5543  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5544  PG_RETURN_NULL();
5545  }
5546 
5547  SET_VARSIZE(pgrtn, pgrtn->size);
5548  PG_RETURN_POINTER(pgrtn);
5549  }
5550 
5551 
5556  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5557  newinitialvalue, TRUE, newnodatavalue, 0);
5558 
5559  /* Get the new raster band */
5560  newband = rt_raster_get_band(newrast, 0);
5561  if ( NULL == newband ) {
5562  elog(NOTICE, "Could not modify band for new raster. Returning new "
5563  "raster with the original band");
5564 
5566  PG_FREE_IF_COPY(pgraster, 0);
5567 
5568  /* Serialize created raster */
5569  pgrtn = rt_raster_serialize(newrast);
5570  rt_raster_destroy(newrast);
5571  if (NULL == pgrtn) {
5572  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5573  PG_RETURN_NULL();
5574  }
5575 
5576  SET_VARSIZE(pgrtn, pgrtn->size);
5577  PG_RETURN_POINTER(pgrtn);
5578  }
5579 
5580  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Main computing loop (%d x %d)",
5581  width, height);
5582 
5583  for (x = 0; x < width; x++) {
5584  for(y = 0; y < height; y++) {
5585  ret = rt_band_get_pixel(band, x, y, &r, NULL);
5586 
5591  if (ret == ES_NONE) {
5592  if (FLT_EQ(r, newnodatavalue)) {
5593  if (cbinfo.fn_strict) {
5594  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Strict callbacks cannot accept NULL arguments, skipping NODATA cell.");
5595  continue;
5596  }
5597  cbdata->args[0].isnull = TRUE;
5598  cbdata->args[0].value = (Datum)NULL;
5599  }
5600  else {
5601  cbdata->args[0].isnull = FALSE;
5602  cbdata->args[0].value = Float8GetDatum(r);
5603  }
5604 
5605  /* Add pixel positions if callback has proper # of args */
5606  if (cbinfo.fn_nargs == 3) {
5607  Datum d[2];
5608  ArrayType *a;
5609 
5610  d[0] = Int32GetDatum(x+1);
5611  d[1] = Int32GetDatum(y+1);
5612 
5613  a = construct_array(d, 2, INT4OID, sizeof(int32), true, 'i');
5614 
5615  cbdata->args[1].isnull = FALSE;
5616  cbdata->args[1].value = PointerGetDatum(a);
5617  }
5618 
5619  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
5620  x, y, r);
5621 
5622  tmpnewval = FunctionCallInvoke(cbdata);
5623 
5624  if (cbdata->isnull)
5625  {
5626  newval = newnodatavalue;
5627  }
5628  else {
5629  newval = DatumGetFloat8(tmpnewval);
5630  }
5631 
5632  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: new value = %f",
5633  newval);
5634 
5635  rt_band_set_pixel(newband, x, y, newval, NULL);
5636  }
5637 
5638  }
5639  }
5640 
5641  /* The newrast band has been modified */
5642 
5643  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster modified, serializing it.");
5644  /* Serialize created raster */
5645 
5647  PG_FREE_IF_COPY(pgraster, 0);
5648 
5649  pgrtn = rt_raster_serialize(newrast);
5650  rt_raster_destroy(newrast);
5651  if (NULL == pgrtn)
5652  PG_RETURN_NULL();
5653 
5654  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster serialized");
5655 
5656  POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebraFct: returning raster");
5657 
5658  SET_VARSIZE(pgrtn, pgrtn->size);
5659  PG_RETURN_POINTER(pgrtn);
5660 }
char * r
Definition: cu_in_wkt.c:24
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:360
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:185
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:217
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:489
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:141
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:825
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:1527
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
rt_pixtype
Definition: librtcore.h:187
@ PT_END
Definition: librtcore.h:199
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
Definition: rt_raster.c:172
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_band.c:865
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:52
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:1253
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
#define FLT_EQ(x, y)
Definition: librtcore.h:2424
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:154
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:2053
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:1125
@ ES_NONE
Definition: librtcore.h:182
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:133
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_raster.c:367
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:2038
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:782
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:125
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_raster.c:163
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:194
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:203
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_raster.c:226
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1240
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
band
Definition: ovdump.py:58
nband
Definition: pixval.py:54
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:65
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:69
unsigned int int32
Definition: shpopen.c:54
Struct definitions.
Definition: librtcore.h:2440

References ovdump::band, ES_NONE, FALSE, FLT_EQ, pixval::nband, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, PT_END, r, rtrowdump::raster, 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, TRUE, pixval::x, and pixval::y.

Here is the call graph for this function: