PostGIS  3.3.9dev-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 5122 of file rtpg_mapalgebra.c.

5123 {
5124  rt_pgraster *pgraster = NULL;
5125  rt_pgraster *pgrtn = NULL;
5126  rt_raster raster = NULL;
5127  rt_raster newrast = NULL;
5128  rt_band band = NULL;
5129  rt_band newband = NULL;
5130  int x, y, nband, width, height;
5131  double r;
5132  double newnodatavalue = 0.0;
5133  double newinitialvalue = 0.0;
5134  double newval = 0.0;
5135  rt_pixtype newpixeltype;
5136  int ret = -1;
5137  Oid oid;
5138  FmgrInfo cbinfo;
5139 #if POSTGIS_PGSQL_VERSION < 120
5140  FunctionCallInfoData cbdata;
5141 #else
5142  LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS); /* Could be optimized */
5143 #endif
5144  Datum tmpnewval;
5145  char * strFromText = NULL;
5146  int k = 0;
5147 
5148  POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
5149 
5150  /* Check raster */
5151  if (PG_ARGISNULL(0)) {
5152  elog(WARNING, "Raster is NULL. Returning NULL");
5153  PG_RETURN_NULL();
5154  }
5155 
5156 
5157  /* Deserialize raster */
5158  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5159  raster = rt_raster_deserialize(pgraster, FALSE);
5160  if (NULL == raster) {
5161  PG_FREE_IF_COPY(pgraster, 0);
5162  elog(ERROR, "RASTER_mapAlgebraFct: Could not deserialize raster");
5163  PG_RETURN_NULL();
5164  }
5165 
5166  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting arguments...");
5167 
5168  /* Get the rest of the arguments */
5169 
5170  if (PG_ARGISNULL(1))
5171  nband = 1;
5172  else
5173  nband = PG_GETARG_INT32(1);
5174 
5175  if (nband < 1)
5176  nband = 1;
5177 
5178  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Creating new empty raster...");
5179 
5184  width = rt_raster_get_width(raster);
5185  height = rt_raster_get_height(raster);
5186 
5187  newrast = rt_raster_new(width, height);
5188 
5189  if ( NULL == newrast ) {
5190 
5192  PG_FREE_IF_COPY(pgraster, 0);
5193 
5194  elog(ERROR, "RASTER_mapAlgebraFct: Could not create a new raster");
5195  PG_RETURN_NULL();
5196  }
5197 
5198  rt_raster_set_scale(newrast,
5201 
5202  rt_raster_set_offsets(newrast,
5205 
5206  rt_raster_set_skews(newrast,
5209 
5211 
5212 
5217  if (rt_raster_is_empty(newrast))
5218  {
5219  elog(NOTICE, "Raster is empty. Returning an empty raster");
5221  PG_FREE_IF_COPY(pgraster, 0);
5222 
5223  pgrtn = rt_raster_serialize(newrast);
5224  rt_raster_destroy(newrast);
5225  if (NULL == pgrtn) {
5226  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5227  PG_RETURN_NULL();
5228  }
5229 
5230  SET_VARSIZE(pgrtn, pgrtn->size);
5231  PG_RETURN_POINTER(pgrtn);
5232  }
5233 
5234  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Getting raster band %d...", nband);
5235 
5240  if (!rt_raster_has_band(raster, nband - 1)) {
5241  elog(NOTICE, "Raster does not have the required band. Returning a raster "
5242  "without a band");
5244  PG_FREE_IF_COPY(pgraster, 0);
5245 
5246  pgrtn = rt_raster_serialize(newrast);
5247  rt_raster_destroy(newrast);
5248  if (NULL == pgrtn) {
5249  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5250  PG_RETURN_NULL();
5251  }
5252 
5253  SET_VARSIZE(pgrtn, pgrtn->size);
5254  PG_RETURN_POINTER(pgrtn);
5255  }
5256 
5257  /* Get the raster band */
5259  if ( NULL == band ) {
5260  elog(NOTICE, "Could not get the required band. Returning a raster "
5261  "without a band");
5263  PG_FREE_IF_COPY(pgraster, 0);
5264 
5265  pgrtn = rt_raster_serialize(newrast);
5266  rt_raster_destroy(newrast);
5267  if (NULL == pgrtn) {
5268  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5269  PG_RETURN_NULL();
5270  }
5271 
5272  SET_VARSIZE(pgrtn, pgrtn->size);
5273  PG_RETURN_POINTER(pgrtn);
5274  }
5275 
5276  /*
5277  * Get NODATA value
5278  */
5279  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting NODATA value for band...");
5280 
5282  rt_band_get_nodata(band, &newnodatavalue);
5283  }
5284 
5285  else {
5286  newnodatavalue = rt_band_get_min_value(band);
5287  }
5288 
5289  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: NODATA value for band: %f",
5290  newnodatavalue);
5296  newinitialvalue = newnodatavalue;
5297 
5301  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Setting pixeltype...");
5302 
5303  if (PG_ARGISNULL(2)) {
5304  newpixeltype = rt_band_get_pixtype(band);
5305  }
5306 
5307  else {
5308  strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
5309  newpixeltype = rt_pixtype_index_from_name(strFromText);
5310  pfree(strFromText);
5311  if (newpixeltype == PT_END)
5312  newpixeltype = rt_band_get_pixtype(band);
5313  }
5314 
5315  if (newpixeltype == PT_END) {
5316 
5318  PG_FREE_IF_COPY(pgraster, 0);
5319  rt_raster_destroy(newrast);
5320 
5321  elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5322  PG_RETURN_NULL();
5323  }
5324 
5325  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Pixeltype set to %s",
5326  rt_pixtype_name(newpixeltype));
5327 
5328  /* Get the name of the callback user function for raster values */
5329  if (PG_ARGISNULL(3)) {
5330 
5332  PG_FREE_IF_COPY(pgraster, 0);
5333  rt_raster_destroy(newrast);
5334 
5335  elog(ERROR, "RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5336  PG_RETURN_NULL();
5337  }
5338 
5339  oid = PG_GETARG_OID(3);
5340  if (oid == InvalidOid) {
5341 
5343  PG_FREE_IF_COPY(pgraster, 0);
5344  rt_raster_destroy(newrast);
5345 
5346  elog(ERROR, "RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5347  PG_RETURN_NULL();
5348  }
5349 
5350  fmgr_info(oid, &cbinfo);
5351 
5352  /* function cannot return set */
5353  if (cbinfo.fn_retset) {
5354 
5356  PG_FREE_IF_COPY(pgraster, 0);
5357  rt_raster_destroy(newrast);
5358 
5359  elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5360  PG_RETURN_NULL();
5361  }
5362  /* function should have correct # of args */
5363  else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5364 
5366  PG_FREE_IF_COPY(pgraster, 0);
5367  rt_raster_destroy(newrast);
5368 
5369  elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5370  PG_RETURN_NULL();
5371  }
5372 
5373  if (cbinfo.fn_nargs == 2)
5374  k = 1;
5375  else
5376  k = 2;
5377 
5378  if (func_volatile(oid) == 'v') {
5379  elog(NOTICE, "Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
5380  }
5381 
5382  /* prep function call data */
5383 #if POSTGIS_PGSQL_VERSION < 120
5384  InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5385 
5386  cbdata.argnull[0] = FALSE;
5387  cbdata.argnull[1] = FALSE;
5388  cbdata.argnull[2] = FALSE;
5389 #else
5390  InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5391 
5392  cbdata->args[0].isnull = FALSE;
5393  cbdata->args[1].isnull = FALSE;
5394  cbdata->args[2].isnull = FALSE;
5395 #endif
5396 
5397  /* check that the function isn't strict if the args are null. */
5398  if (PG_ARGISNULL(4)) {
5399  if (cbinfo.fn_strict) {
5400 
5402  PG_FREE_IF_COPY(pgraster, 0);
5403  rt_raster_destroy(newrast);
5404 
5405  elog(ERROR, "RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5406  PG_RETURN_NULL();
5407  }
5408 
5409 #if POSTGIS_PGSQL_VERSION < 120
5410  cbdata.arg[k] = (Datum)NULL;
5411  cbdata.argnull[k] = TRUE;
5412 #else
5413  cbdata->args[k].value = (Datum)NULL;
5414  cbdata->args[k].isnull = TRUE;
5415 #endif
5416  }
5417  else {
5418 #if POSTGIS_PGSQL_VERSION < 120
5419  cbdata.arg[k] = PG_GETARG_DATUM(4);
5420 #else
5421  cbdata->args[k].value = PG_GETARG_DATUM(4);
5422 #endif
5423  }
5424 
5431 
5432  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Band is a nodata band, returning "
5433  "a raster filled with nodata");
5434 
5435  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5436  newinitialvalue, TRUE, newnodatavalue, 0);
5437 
5439  PG_FREE_IF_COPY(pgraster, 0);
5440 
5441  /* Serialize created raster */
5442  pgrtn = rt_raster_serialize(newrast);
5443  rt_raster_destroy(newrast);
5444  if (NULL == pgrtn) {
5445  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5446  PG_RETURN_NULL();
5447  }
5448 
5449  SET_VARSIZE(pgrtn, pgrtn->size);
5450  PG_RETURN_POINTER(pgrtn);
5451  }
5452 
5453 
5458  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5459  newinitialvalue, TRUE, newnodatavalue, 0);
5460 
5461  /* Get the new raster band */
5462  newband = rt_raster_get_band(newrast, 0);
5463  if ( NULL == newband ) {
5464  elog(NOTICE, "Could not modify band for new raster. Returning new "
5465  "raster with the original band");
5466 
5468  PG_FREE_IF_COPY(pgraster, 0);
5469 
5470  /* Serialize created raster */
5471  pgrtn = rt_raster_serialize(newrast);
5472  rt_raster_destroy(newrast);
5473  if (NULL == pgrtn) {
5474  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5475  PG_RETURN_NULL();
5476  }
5477 
5478  SET_VARSIZE(pgrtn, pgrtn->size);
5479  PG_RETURN_POINTER(pgrtn);
5480  }
5481 
5482  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Main computing loop (%d x %d)",
5483  width, height);
5484 
5485  for (x = 0; x < width; x++) {
5486  for(y = 0; y < height; y++) {
5487  ret = rt_band_get_pixel(band, x, y, &r, NULL);
5488 
5493  if (ret == ES_NONE) {
5494  if (FLT_EQ(r, newnodatavalue)) {
5495  if (cbinfo.fn_strict) {
5496  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Strict callbacks cannot accept NULL arguments, skipping NODATA cell.");
5497  continue;
5498  }
5499 #if POSTGIS_PGSQL_VERSION < 120
5500  cbdata.argnull[0] = TRUE;
5501  cbdata.arg[0] = (Datum)NULL;
5502 #else
5503  cbdata->args[0].isnull = TRUE;
5504  cbdata->args[0].value = (Datum)NULL;
5505 #endif
5506  }
5507  else {
5508 #if POSTGIS_PGSQL_VERSION < 120
5509  cbdata.argnull[0] = FALSE;
5510  cbdata.arg[0] = Float8GetDatum(r);
5511 #else
5512  cbdata->args[0].isnull = FALSE;
5513  cbdata->args[0].value = Float8GetDatum(r);
5514 #endif
5515  }
5516 
5517  /* Add pixel positions if callback has proper # of args */
5518  if (cbinfo.fn_nargs == 3) {
5519  Datum d[2];
5520  ArrayType *a;
5521 
5522  d[0] = Int32GetDatum(x+1);
5523  d[1] = Int32GetDatum(y+1);
5524 
5525  a = construct_array(d, 2, INT4OID, sizeof(int32), true, 'i');
5526 
5527 #if POSTGIS_PGSQL_VERSION < 120
5528  cbdata.argnull[1] = FALSE;
5529  cbdata.arg[1] = PointerGetDatum(a);
5530 #else
5531  cbdata->args[1].isnull = FALSE;
5532  cbdata->args[1].value = PointerGetDatum(a);
5533 #endif
5534  }
5535 
5536  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
5537  x, y, r);
5538 
5539 #if POSTGIS_PGSQL_VERSION < 120
5540  tmpnewval = FunctionCallInvoke(&cbdata);
5541 
5542  if (cbdata.isnull) {
5543  newval = newnodatavalue;
5544  }
5545 #else
5546  tmpnewval = FunctionCallInvoke(cbdata);
5547 
5548  if (cbdata->isnull)
5549  {
5550  newval = newnodatavalue;
5551  }
5552 #endif
5553  else {
5554  newval = DatumGetFloat8(tmpnewval);
5555  }
5556 
5557  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: new value = %f",
5558  newval);
5559 
5560  rt_band_set_pixel(newband, x, y, newval, NULL);
5561  }
5562 
5563  }
5564  }
5565 
5566  /* The newrast band has been modified */
5567 
5568  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster modified, serializing it.");
5569  /* Serialize created raster */
5570 
5572  PG_FREE_IF_COPY(pgraster, 0);
5573 
5574  pgrtn = rt_raster_serialize(newrast);
5575  rt_raster_destroy(newrast);
5576  if (NULL == pgrtn)
5577  PG_RETURN_NULL();
5578 
5579  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster serialized");
5580 
5581  POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebraFct: returning raster");
5582 
5583  SET_VARSIZE(pgrtn, pgrtn->size);
5584  PG_RETURN_POINTER(pgrtn);
5585 }
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: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:1376
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:714
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:1375
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:2380
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:1902
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: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:1887
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: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:1362
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:53
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:69
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:73
unsigned int int32
Definition: shpopen.c:54
Struct definitions.
Definition: librtcore.h:2396

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: