PostGIS  2.5.7dev-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 5138 of file rtpg_mapalgebra.c.

5139 {
5140  rt_pgraster *pgraster = NULL;
5141  rt_pgraster *pgrtn = NULL;
5142  rt_raster raster = NULL;
5143  rt_raster newrast = NULL;
5144  rt_band band = NULL;
5145  rt_band newband = NULL;
5146  int x, y, nband, width, height;
5147  double r;
5148  double newnodatavalue = 0.0;
5149  double newinitialvalue = 0.0;
5150  double newval = 0.0;
5151  rt_pixtype newpixeltype;
5152  int ret = -1;
5153  Oid oid;
5154  FmgrInfo cbinfo;
5155 #if POSTGIS_PGSQL_VERSION < 120
5156  FunctionCallInfoData cbdata;
5157 #else
5158  LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS); /* Could be optimized */
5159 #endif
5160  Datum tmpnewval;
5161  char * strFromText = NULL;
5162  int k = 0;
5163 
5164  POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
5165 
5166  /* Check raster */
5167  if (PG_ARGISNULL(0)) {
5168  elog(WARNING, "Raster is NULL. Returning NULL");
5169  PG_RETURN_NULL();
5170  }
5171 
5172 
5173  /* Deserialize raster */
5174  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5175  raster = rt_raster_deserialize(pgraster, FALSE);
5176  if (NULL == raster) {
5177  PG_FREE_IF_COPY(pgraster, 0);
5178  elog(ERROR, "RASTER_mapAlgebraFct: Could not deserialize raster");
5179  PG_RETURN_NULL();
5180  }
5181 
5182  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting arguments...");
5183 
5184  /* Get the rest of the arguments */
5185 
5186  if (PG_ARGISNULL(1))
5187  nband = 1;
5188  else
5189  nband = PG_GETARG_INT32(1);
5190 
5191  if (nband < 1)
5192  nband = 1;
5193 
5194  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Creating new empty raster...");
5195 
5200  width = rt_raster_get_width(raster);
5201  height = rt_raster_get_height(raster);
5202 
5203  newrast = rt_raster_new(width, height);
5204 
5205  if ( NULL == newrast ) {
5206 
5208  PG_FREE_IF_COPY(pgraster, 0);
5209 
5210  elog(ERROR, "RASTER_mapAlgebraFct: Could not create a new raster");
5211  PG_RETURN_NULL();
5212  }
5213 
5214  rt_raster_set_scale(newrast,
5217 
5218  rt_raster_set_offsets(newrast,
5221 
5222  rt_raster_set_skews(newrast,
5225 
5227 
5228 
5233  if (rt_raster_is_empty(newrast))
5234  {
5235  elog(NOTICE, "Raster is empty. Returning an empty raster");
5237  PG_FREE_IF_COPY(pgraster, 0);
5238 
5239  pgrtn = rt_raster_serialize(newrast);
5240  rt_raster_destroy(newrast);
5241  if (NULL == pgrtn) {
5242  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5243  PG_RETURN_NULL();
5244  }
5245 
5246  SET_VARSIZE(pgrtn, pgrtn->size);
5247  PG_RETURN_POINTER(pgrtn);
5248  }
5249 
5250  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Getting raster band %d...", nband);
5251 
5256  if (!rt_raster_has_band(raster, nband - 1)) {
5257  elog(NOTICE, "Raster does not have the required band. Returning a raster "
5258  "without a band");
5260  PG_FREE_IF_COPY(pgraster, 0);
5261 
5262  pgrtn = rt_raster_serialize(newrast);
5263  rt_raster_destroy(newrast);
5264  if (NULL == pgrtn) {
5265  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5266  PG_RETURN_NULL();
5267  }
5268 
5269  SET_VARSIZE(pgrtn, pgrtn->size);
5270  PG_RETURN_POINTER(pgrtn);
5271  }
5272 
5273  /* Get the raster band */
5275  if ( NULL == band ) {
5276  elog(NOTICE, "Could not get the required band. Returning a raster "
5277  "without a band");
5279  PG_FREE_IF_COPY(pgraster, 0);
5280 
5281  pgrtn = rt_raster_serialize(newrast);
5282  rt_raster_destroy(newrast);
5283  if (NULL == pgrtn) {
5284  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5285  PG_RETURN_NULL();
5286  }
5287 
5288  SET_VARSIZE(pgrtn, pgrtn->size);
5289  PG_RETURN_POINTER(pgrtn);
5290  }
5291 
5292  /*
5293  * Get NODATA value
5294  */
5295  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting NODATA value for band...");
5296 
5298  rt_band_get_nodata(band, &newnodatavalue);
5299  }
5300 
5301  else {
5302  newnodatavalue = rt_band_get_min_value(band);
5303  }
5304 
5305  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: NODATA value for band: %f",
5306  newnodatavalue);
5312  newinitialvalue = newnodatavalue;
5313 
5317  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Setting pixeltype...");
5318 
5319  if (PG_ARGISNULL(2)) {
5320  newpixeltype = rt_band_get_pixtype(band);
5321  }
5322 
5323  else {
5324  strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
5325  newpixeltype = rt_pixtype_index_from_name(strFromText);
5326  pfree(strFromText);
5327  if (newpixeltype == PT_END)
5328  newpixeltype = rt_band_get_pixtype(band);
5329  }
5330 
5331  if (newpixeltype == PT_END) {
5332 
5334  PG_FREE_IF_COPY(pgraster, 0);
5335  rt_raster_destroy(newrast);
5336 
5337  elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5338  PG_RETURN_NULL();
5339  }
5340 
5341  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Pixeltype set to %s",
5342  rt_pixtype_name(newpixeltype));
5343 
5344  /* Get the name of the callback user function for raster values */
5345  if (PG_ARGISNULL(3)) {
5346 
5348  PG_FREE_IF_COPY(pgraster, 0);
5349  rt_raster_destroy(newrast);
5350 
5351  elog(ERROR, "RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5352  PG_RETURN_NULL();
5353  }
5354 
5355  oid = PG_GETARG_OID(3);
5356  if (oid == InvalidOid) {
5357 
5359  PG_FREE_IF_COPY(pgraster, 0);
5360  rt_raster_destroy(newrast);
5361 
5362  elog(ERROR, "RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5363  PG_RETURN_NULL();
5364  }
5365 
5366  fmgr_info(oid, &cbinfo);
5367 
5368  /* function cannot return set */
5369  if (cbinfo.fn_retset) {
5370 
5372  PG_FREE_IF_COPY(pgraster, 0);
5373  rt_raster_destroy(newrast);
5374 
5375  elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5376  PG_RETURN_NULL();
5377  }
5378  /* function should have correct # of args */
5379  else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5380 
5382  PG_FREE_IF_COPY(pgraster, 0);
5383  rt_raster_destroy(newrast);
5384 
5385  elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5386  PG_RETURN_NULL();
5387  }
5388 
5389  if (cbinfo.fn_nargs == 2)
5390  k = 1;
5391  else
5392  k = 2;
5393 
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");
5396  }
5397 
5398  /* prep function call data */
5399 #if POSTGIS_PGSQL_VERSION < 120
5400  InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5401 
5402  cbdata.argnull[0] = FALSE;
5403  cbdata.argnull[1] = FALSE;
5404  cbdata.argnull[2] = FALSE;
5405 #else
5406  InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5407 
5408  cbdata->args[0].isnull = FALSE;
5409  cbdata->args[1].isnull = FALSE;
5410  cbdata->args[2].isnull = FALSE;
5411 #endif
5412 
5413  /* check that the function isn't strict if the args are null. */
5414  if (PG_ARGISNULL(4)) {
5415  if (cbinfo.fn_strict) {
5416 
5418  PG_FREE_IF_COPY(pgraster, 0);
5419  rt_raster_destroy(newrast);
5420 
5421  elog(ERROR, "RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5422  PG_RETURN_NULL();
5423  }
5424 
5425 #if POSTGIS_PGSQL_VERSION < 120
5426  cbdata.arg[k] = (Datum)NULL;
5427  cbdata.argnull[k] = TRUE;
5428 #else
5429  cbdata->args[k].value = (Datum)NULL;
5430  cbdata->args[k].isnull = TRUE;
5431 #endif
5432  }
5433  else {
5434 #if POSTGIS_PGSQL_VERSION < 120
5435  cbdata.arg[k] = PG_GETARG_DATUM(4);
5436 #else
5437  cbdata->args[k].value = PG_GETARG_DATUM(4);
5438 #endif
5439  }
5440 
5447 
5448  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Band is a nodata band, returning "
5449  "a raster filled with nodata");
5450 
5451  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5452  newinitialvalue, TRUE, newnodatavalue, 0);
5453 
5455  PG_FREE_IF_COPY(pgraster, 0);
5456 
5457  /* Serialize created raster */
5458  pgrtn = rt_raster_serialize(newrast);
5459  rt_raster_destroy(newrast);
5460  if (NULL == pgrtn) {
5461  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5462  PG_RETURN_NULL();
5463  }
5464 
5465  SET_VARSIZE(pgrtn, pgrtn->size);
5466  PG_RETURN_POINTER(pgrtn);
5467  }
5468 
5469 
5474  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5475  newinitialvalue, TRUE, newnodatavalue, 0);
5476 
5477  /* Get the new raster band */
5478  newband = rt_raster_get_band(newrast, 0);
5479  if ( NULL == newband ) {
5480  elog(NOTICE, "Could not modify band for new raster. Returning new "
5481  "raster with the original band");
5482 
5484  PG_FREE_IF_COPY(pgraster, 0);
5485 
5486  /* Serialize created raster */
5487  pgrtn = rt_raster_serialize(newrast);
5488  rt_raster_destroy(newrast);
5489  if (NULL == pgrtn) {
5490  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5491  PG_RETURN_NULL();
5492  }
5493 
5494  SET_VARSIZE(pgrtn, pgrtn->size);
5495  PG_RETURN_POINTER(pgrtn);
5496  }
5497 
5498  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Main computing loop (%d x %d)",
5499  width, height);
5500 
5501  for (x = 0; x < width; x++) {
5502  for(y = 0; y < height; y++) {
5503  ret = rt_band_get_pixel(band, x, y, &r, NULL);
5504 
5509  if (ret == ES_NONE) {
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.");
5513  continue;
5514  }
5515 #if POSTGIS_PGSQL_VERSION < 120
5516  cbdata.argnull[0] = TRUE;
5517  cbdata.arg[0] = (Datum)NULL;
5518 #else
5519  cbdata->args[0].isnull = TRUE;
5520  cbdata->args[0].value = (Datum)NULL;
5521 #endif
5522  }
5523  else {
5524 #if POSTGIS_PGSQL_VERSION < 120
5525  cbdata.argnull[0] = FALSE;
5526  cbdata.arg[0] = Float8GetDatum(r);
5527 #else
5528  cbdata->args[0].isnull = FALSE;
5529  cbdata->args[0].value = Float8GetDatum(r);
5530 #endif
5531  }
5532 
5533  /* Add pixel positions if callback has proper # of args */
5534  if (cbinfo.fn_nargs == 3) {
5535  Datum d[2];
5536  ArrayType *a;
5537 
5538  d[0] = Int32GetDatum(x+1);
5539  d[1] = Int32GetDatum(y+1);
5540 
5541  a = construct_array(d, 2, INT4OID, sizeof(int32), true, 'i');
5542 
5543 #if POSTGIS_PGSQL_VERSION < 120
5544  cbdata.argnull[1] = FALSE;
5545  cbdata.arg[1] = PointerGetDatum(a);
5546 #else
5547  cbdata->args[1].isnull = FALSE;
5548  cbdata->args[1].value = PointerGetDatum(a);
5549 #endif
5550  }
5551 
5552  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
5553  x, y, r);
5554 
5555 #if POSTGIS_PGSQL_VERSION < 120
5556  tmpnewval = FunctionCallInvoke(&cbdata);
5557 
5558  if (cbdata.isnull) {
5559  newval = newnodatavalue;
5560  }
5561 #else
5562  tmpnewval = FunctionCallInvoke(cbdata);
5563 
5564  if (cbdata->isnull)
5565  {
5566  newval = newnodatavalue;
5567  }
5568 #endif
5569  else {
5570  newval = DatumGetFloat8(tmpnewval);
5571  }
5572 
5573  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: new value = %f",
5574  newval);
5575 
5576  rt_band_set_pixel(newband, x, y, newval, NULL);
5577  }
5578 
5579  }
5580  }
5581 
5582  /* The newrast band has been modified */
5583 
5584  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster modified, serializing it.");
5585  /* Serialize created raster */
5586 
5588  PG_FREE_IF_COPY(pgraster, 0);
5589 
5590  pgrtn = rt_raster_serialize(newrast);
5591  rt_raster_destroy(newrast);
5592  if (NULL == pgrtn)
5593  PG_RETURN_NULL();
5594 
5595  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster serialized");
5596 
5597  POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebraFct: returning raster");
5598 
5599  SET_VARSIZE(pgrtn, pgrtn->size);
5600  PG_RETURN_POINTER(pgrtn);
5601 }
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:1347
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:2234
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:150
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
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:121
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.
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:222
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1334
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
band
Definition: ovdump.py:57
nband
Definition: pixval.py:52
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:2250

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, text_to_cstring(), TRUE, pixval::x, and pixval::y.

Here is the call graph for this function: