PostGIS  2.4.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 5126 of file rtpg_mapalgebra.c.

References ovdump::band, ES_NONE, FALSE, FLT_EQ, rt_raster_serialized_t::height, pixval::nband, PG_FUNCTION_INFO_V1(), POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, PT_END, r, rtrowdump::raster, RASTER_mapAlgebraFctNgb(), 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, rt_raster_serialized_t::width, pixval::x, and pixval::y.

Referenced by RASTER_mapAlgebraExpr().

5127 {
5128  rt_pgraster *pgraster = NULL;
5129  rt_pgraster *pgrtn = NULL;
5130  rt_raster raster = NULL;
5131  rt_raster newrast = NULL;
5132  rt_band band = NULL;
5133  rt_band newband = NULL;
5134  int x, y, nband, width, height;
5135  double r;
5136  double newnodatavalue = 0.0;
5137  double newinitialvalue = 0.0;
5138  double newval = 0.0;
5139  rt_pixtype newpixeltype;
5140  int ret = -1;
5141  Oid oid;
5142  FmgrInfo cbinfo;
5143 #if POSTGIS_PGSQL_VERSION < 120
5144  FunctionCallInfoData cbdata;
5145 #else
5146  LOCAL_FCINFO(cbdata, FUNC_MAX_ARGS); /* Could be optimized */
5147 #endif
5148  Datum tmpnewval;
5149  char * strFromText = NULL;
5150  int k = 0;
5151 
5152  POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebraFct: STARTING...");
5153 
5154  /* Check raster */
5155  if (PG_ARGISNULL(0)) {
5156  elog(WARNING, "Raster is NULL. Returning NULL");
5157  PG_RETURN_NULL();
5158  }
5159 
5160 
5161  /* Deserialize raster */
5162  pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
5163  raster = rt_raster_deserialize(pgraster, FALSE);
5164  if (NULL == raster) {
5165  PG_FREE_IF_COPY(pgraster, 0);
5166  elog(ERROR, "RASTER_mapAlgebraFct: Could not deserialize raster");
5167  PG_RETURN_NULL();
5168  }
5169 
5170  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting arguments...");
5171 
5172  /* Get the rest of the arguments */
5173 
5174  if (PG_ARGISNULL(1))
5175  nband = 1;
5176  else
5177  nband = PG_GETARG_INT32(1);
5178 
5179  if (nband < 1)
5180  nband = 1;
5181 
5182  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Creating new empty raster...");
5183 
5188  width = rt_raster_get_width(raster);
5189  height = rt_raster_get_height(raster);
5190 
5191  newrast = rt_raster_new(width, height);
5192 
5193  if ( NULL == newrast ) {
5194 
5195  rt_raster_destroy(raster);
5196  PG_FREE_IF_COPY(pgraster, 0);
5197 
5198  elog(ERROR, "RASTER_mapAlgebraFct: Could not create a new raster");
5199  PG_RETURN_NULL();
5200  }
5201 
5202  rt_raster_set_scale(newrast,
5203  rt_raster_get_x_scale(raster),
5204  rt_raster_get_y_scale(raster));
5205 
5206  rt_raster_set_offsets(newrast,
5207  rt_raster_get_x_offset(raster),
5208  rt_raster_get_y_offset(raster));
5209 
5210  rt_raster_set_skews(newrast,
5211  rt_raster_get_x_skew(raster),
5212  rt_raster_get_y_skew(raster));
5213 
5214  rt_raster_set_srid(newrast, rt_raster_get_srid(raster));
5215 
5216 
5221  if (rt_raster_is_empty(newrast))
5222  {
5223  elog(NOTICE, "Raster is empty. Returning an empty raster");
5224  rt_raster_destroy(raster);
5225  PG_FREE_IF_COPY(pgraster, 0);
5226 
5227  pgrtn = rt_raster_serialize(newrast);
5228  rt_raster_destroy(newrast);
5229  if (NULL == pgrtn) {
5230  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5231  PG_RETURN_NULL();
5232  }
5233 
5234  SET_VARSIZE(pgrtn, pgrtn->size);
5235  PG_RETURN_POINTER(pgrtn);
5236  }
5237 
5238  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Getting raster band %d...", nband);
5239 
5244  if (!rt_raster_has_band(raster, nband - 1)) {
5245  elog(NOTICE, "Raster does not have the required band. Returning a raster "
5246  "without a band");
5247  rt_raster_destroy(raster);
5248  PG_FREE_IF_COPY(pgraster, 0);
5249 
5250  pgrtn = rt_raster_serialize(newrast);
5251  rt_raster_destroy(newrast);
5252  if (NULL == pgrtn) {
5253  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5254  PG_RETURN_NULL();
5255  }
5256 
5257  SET_VARSIZE(pgrtn, pgrtn->size);
5258  PG_RETURN_POINTER(pgrtn);
5259  }
5260 
5261  /* Get the raster band */
5262  band = rt_raster_get_band(raster, nband - 1);
5263  if ( NULL == band ) {
5264  elog(NOTICE, "Could not get the required band. Returning a raster "
5265  "without a band");
5266  rt_raster_destroy(raster);
5267  PG_FREE_IF_COPY(pgraster, 0);
5268 
5269  pgrtn = rt_raster_serialize(newrast);
5270  rt_raster_destroy(newrast);
5271  if (NULL == pgrtn) {
5272  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5273  PG_RETURN_NULL();
5274  }
5275 
5276  SET_VARSIZE(pgrtn, pgrtn->size);
5277  PG_RETURN_POINTER(pgrtn);
5278  }
5279 
5280  /*
5281  * Get NODATA value
5282  */
5283  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Getting NODATA value for band...");
5284 
5285  if (rt_band_get_hasnodata_flag(band)) {
5286  rt_band_get_nodata(band, &newnodatavalue);
5287  }
5288 
5289  else {
5290  newnodatavalue = rt_band_get_min_value(band);
5291  }
5292 
5293  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: NODATA value for band: %f",
5294  newnodatavalue);
5300  newinitialvalue = newnodatavalue;
5301 
5305  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Setting pixeltype...");
5306 
5307  if (PG_ARGISNULL(2)) {
5308  newpixeltype = rt_band_get_pixtype(band);
5309  }
5310 
5311  else {
5312  strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
5313  newpixeltype = rt_pixtype_index_from_name(strFromText);
5314  pfree(strFromText);
5315  if (newpixeltype == PT_END)
5316  newpixeltype = rt_band_get_pixtype(band);
5317  }
5318 
5319  if (newpixeltype == PT_END) {
5320 
5321  rt_raster_destroy(raster);
5322  PG_FREE_IF_COPY(pgraster, 0);
5323  rt_raster_destroy(newrast);
5324 
5325  elog(ERROR, "RASTER_mapAlgebraFct: Invalid pixeltype");
5326  PG_RETURN_NULL();
5327  }
5328 
5329  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Pixeltype set to %s",
5330  rt_pixtype_name(newpixeltype));
5331 
5332  /* Get the name of the callback user function for raster values */
5333  if (PG_ARGISNULL(3)) {
5334 
5335  rt_raster_destroy(raster);
5336  PG_FREE_IF_COPY(pgraster, 0);
5337  rt_raster_destroy(newrast);
5338 
5339  elog(ERROR, "RASTER_mapAlgebraFct: Required function is missing. Returning NULL");
5340  PG_RETURN_NULL();
5341  }
5342 
5343  oid = PG_GETARG_OID(3);
5344  if (oid == InvalidOid) {
5345 
5346  rt_raster_destroy(raster);
5347  PG_FREE_IF_COPY(pgraster, 0);
5348  rt_raster_destroy(newrast);
5349 
5350  elog(ERROR, "RASTER_mapAlgebraFct: Got invalid function object id. Returning NULL");
5351  PG_RETURN_NULL();
5352  }
5353 
5354  fmgr_info(oid, &cbinfo);
5355 
5356  /* function cannot return set */
5357  if (cbinfo.fn_retset) {
5358 
5359  rt_raster_destroy(raster);
5360  PG_FREE_IF_COPY(pgraster, 0);
5361  rt_raster_destroy(newrast);
5362 
5363  elog(ERROR, "RASTER_mapAlgebraFct: Function provided must return double precision not resultset");
5364  PG_RETURN_NULL();
5365  }
5366  /* function should have correct # of args */
5367  else if (cbinfo.fn_nargs < 2 || cbinfo.fn_nargs > 3) {
5368 
5369  rt_raster_destroy(raster);
5370  PG_FREE_IF_COPY(pgraster, 0);
5371  rt_raster_destroy(newrast);
5372 
5373  elog(ERROR, "RASTER_mapAlgebraFct: Function does not have two or three input parameters");
5374  PG_RETURN_NULL();
5375  }
5376 
5377  if (cbinfo.fn_nargs == 2)
5378  k = 1;
5379  else
5380  k = 2;
5381 
5382  if (func_volatile(oid) == 'v') {
5383  elog(NOTICE, "Function provided is VOLATILE. Unless required and for best performance, function should be IMMUTABLE or STABLE");
5384  }
5385 
5386  /* prep function call data */
5387 #if POSTGIS_PGSQL_VERSION < 120
5388  InitFunctionCallInfoData(cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5389 
5390  cbdata.argnull[0] = FALSE;
5391  cbdata.argnull[1] = FALSE;
5392  cbdata.argnull[2] = FALSE;
5393 #else
5394  InitFunctionCallInfoData(*cbdata, &cbinfo, 2, InvalidOid, NULL, NULL);
5395 
5396  cbdata->args[0].isnull = FALSE;
5397  cbdata->args[1].isnull = FALSE;
5398  cbdata->args[2].isnull = FALSE;
5399 #endif
5400 
5401  /* check that the function isn't strict if the args are null. */
5402  if (PG_ARGISNULL(4)) {
5403  if (cbinfo.fn_strict) {
5404 
5405  rt_raster_destroy(raster);
5406  PG_FREE_IF_COPY(pgraster, 0);
5407  rt_raster_destroy(newrast);
5408 
5409  elog(ERROR, "RASTER_mapAlgebraFct: Strict callback functions cannot have null parameters");
5410  PG_RETURN_NULL();
5411  }
5412 
5413 #if POSTGIS_PGSQL_VERSION < 120
5414  cbdata.arg[k] = (Datum)NULL;
5415  cbdata.argnull[k] = TRUE;
5416 #else
5417  cbdata->args[k].value = (Datum)NULL;
5418  cbdata->args[k].isnull = TRUE;
5419 #endif
5420  }
5421  else {
5422 #if POSTGIS_PGSQL_VERSION < 120
5423  cbdata.arg[k] = PG_GETARG_DATUM(4);
5424 #else
5425  cbdata->args[k].value = PG_GETARG_DATUM(4);
5426 #endif
5427  }
5428 
5434  if (rt_band_get_isnodata_flag(band)) {
5435 
5436  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Band is a nodata band, returning "
5437  "a raster filled with nodata");
5438 
5439  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5440  newinitialvalue, TRUE, newnodatavalue, 0);
5441 
5442  rt_raster_destroy(raster);
5443  PG_FREE_IF_COPY(pgraster, 0);
5444 
5445  /* Serialize created raster */
5446  pgrtn = rt_raster_serialize(newrast);
5447  rt_raster_destroy(newrast);
5448  if (NULL == pgrtn) {
5449  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5450  PG_RETURN_NULL();
5451  }
5452 
5453  SET_VARSIZE(pgrtn, pgrtn->size);
5454  PG_RETURN_POINTER(pgrtn);
5455  }
5456 
5457 
5462  ret = rt_raster_generate_new_band(newrast, newpixeltype,
5463  newinitialvalue, TRUE, newnodatavalue, 0);
5464 
5465  /* Get the new raster band */
5466  newband = rt_raster_get_band(newrast, 0);
5467  if ( NULL == newband ) {
5468  elog(NOTICE, "Could not modify band for new raster. Returning new "
5469  "raster with the original band");
5470 
5471  rt_raster_destroy(raster);
5472  PG_FREE_IF_COPY(pgraster, 0);
5473 
5474  /* Serialize created raster */
5475  pgrtn = rt_raster_serialize(newrast);
5476  rt_raster_destroy(newrast);
5477  if (NULL == pgrtn) {
5478  elog(ERROR, "RASTER_mapAlgebraFct: Could not serialize raster");
5479  PG_RETURN_NULL();
5480  }
5481 
5482  SET_VARSIZE(pgrtn, pgrtn->size);
5483  PG_RETURN_POINTER(pgrtn);
5484  }
5485 
5486  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: Main computing loop (%d x %d)",
5487  width, height);
5488 
5489  for (x = 0; x < width; x++) {
5490  for(y = 0; y < height; y++) {
5491  ret = rt_band_get_pixel(band, x, y, &r, NULL);
5492 
5497  if (ret == ES_NONE) {
5498  if (FLT_EQ(r, newnodatavalue)) {
5499  if (cbinfo.fn_strict) {
5500  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: Strict callbacks cannot accept NULL arguments, skipping NODATA cell.");
5501  continue;
5502  }
5503 #if POSTGIS_PGSQL_VERSION < 120
5504  cbdata.argnull[0] = TRUE;
5505  cbdata.arg[0] = (Datum)NULL;
5506 #else
5507  cbdata->args[0].isnull = TRUE;
5508  cbdata->args[0].value = (Datum)NULL;
5509 #endif
5510  }
5511  else {
5512 #if POSTGIS_PGSQL_VERSION < 120
5513  cbdata.argnull[0] = FALSE;
5514  cbdata.arg[0] = Float8GetDatum(r);
5515 #else
5516  cbdata->args[0].isnull = FALSE;
5517  cbdata->args[0].value = Float8GetDatum(r);
5518 #endif
5519  }
5520 
5521  /* Add pixel positions if callback has proper # of args */
5522  if (cbinfo.fn_nargs == 3) {
5523  Datum d[2];
5524  ArrayType *a;
5525 
5526  d[0] = Int32GetDatum(x+1);
5527  d[1] = Int32GetDatum(y+1);
5528 
5529  a = construct_array(d, 2, INT4OID, sizeof(int32), true, 'i');
5530 
5531 #if POSTGIS_PGSQL_VERSION < 120
5532  cbdata.argnull[1] = FALSE;
5533  cbdata.arg[1] = PointerGetDatum(a);
5534 #else
5535  cbdata->args[1].isnull = FALSE;
5536  cbdata->args[1].value = PointerGetDatum(a);
5537 #endif
5538  }
5539 
5540  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: (%dx%d), r = %f",
5541  x, y, r);
5542 
5543 #if POSTGIS_PGSQL_VERSION < 120
5544  tmpnewval = FunctionCallInvoke(&cbdata);
5545 
5546  if (cbdata.isnull) {
5547  newval = newnodatavalue;
5548  }
5549 #else
5550  tmpnewval = FunctionCallInvoke(cbdata);
5551 
5552  if (cbdata->isnull)
5553  {
5554  newval = newnodatavalue;
5555  }
5556 #endif
5557  else {
5558  newval = DatumGetFloat8(tmpnewval);
5559  }
5560 
5561  POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraFct: new value = %f",
5562  newval);
5563 
5564  rt_band_set_pixel(newband, x, y, newval, NULL);
5565  }
5566 
5567  }
5568  }
5569 
5570  /* The newrast band has been modified */
5571 
5572  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster modified, serializing it.");
5573  /* Serialize created raster */
5574 
5575  rt_raster_destroy(raster);
5576  PG_FREE_IF_COPY(pgraster, 0);
5577 
5578  pgrtn = rt_raster_serialize(newrast);
5579  rt_raster_destroy(newrast);
5580  if (NULL == pgrtn)
5581  PG_RETURN_NULL();
5582 
5583  POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebraFct: raster serialized");
5584 
5585  POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebraFct: returning raster");
5586 
5587  SET_VARSIZE(pgrtn, pgrtn->size);
5588  PG_RETURN_POINTER(pgrtn);
5589 }
unsigned int int32
Definition: shpopen.c:273
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
Definition: rt_raster.c:213
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:190
char * r
Definition: cu_in_wkt.c:24
raster
Be careful!! Zeros function&#39;s input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
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
band
Definition: ovdump.py:57
#define FLT_EQ(x, y)
Definition: librtcore.h:2185
rt_pixtype
Definition: librtcore.h:185
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition: rt_raster.c:1338
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1597
nband
Definition: pixval.py:52
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition: rt_band.c:1088
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition: rt_raster.c:137
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
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
Definition: rt_pixel.c:80
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition: rt_raster.c:199
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:381
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition: rt_band.c:541
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
Definition: rt_raster.c:150
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:1351
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster&#39;s SRID.
Definition: rt_raster.c:363
int32_t rt_raster_get_srid(rt_raster raster)
Get raster&#39;s SRID.
Definition: rt_raster.c:356
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
Definition: rt_raster.c:159
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:181
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
#define FALSE
Definition: dbfopen.c:168
Struct definitions.
Definition: librtcore.h:2201
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:1612
const char * rt_pixtype_name(rt_pixtype pixtype)
Definition: rt_pixel.c:110
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:498
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition: rt_band.c:581
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel&#39;s value.
Definition: rt_band.c:841
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define TRUE
Definition: dbfopen.c:169
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
Definition: rt_raster.c:222
Here is the call graph for this function:
Here is the caller graph for this function: