PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ RASTER_nMapAlgebraExpr()

Datum RASTER_nMapAlgebraExpr ( PG_FUNCTION_ARGS  )

Definition at line 1330 of file rtpg_mapalgebra.c.

1331 {
1332  MemoryContext mainMemCtx = CurrentMemoryContext;
1333  rtpg_nmapalgebraexpr_arg arg = NULL;
1334  rt_iterator itrset;
1335  uint16_t exprpos[3] = {1, 4, 5};
1336 
1337  int i = 0;
1338  int j = 0;
1339  int k = 0;
1340 
1341  int numraster = 0;
1342  int err = 0;
1343  int allnull = 0;
1344  int allempty = 0;
1345  int noband = 0;
1346  int len = 0;
1347 
1348  TupleDesc tupdesc;
1349  SPITupleTable *tuptable = NULL;
1350  HeapTuple tuple;
1351  Datum datum;
1352  bool isnull = FALSE;
1353 
1354  rt_raster raster = NULL;
1355  rt_band band = NULL;
1356  rt_pgraster *pgraster = NULL;
1357 
1358  const int argkwcount = 12;
1359  char *argkw[] = {
1360  "[rast.x]",
1361  "[rast.y]",
1362  "[rast.val]",
1363  "[rast]",
1364  "[rast1.x]",
1365  "[rast1.y]",
1366  "[rast1.val]",
1367  "[rast1]",
1368  "[rast2.x]",
1369  "[rast2.y]",
1370  "[rast2.val]",
1371  "[rast2]"
1372  };
1373 
1374  if (PG_ARGISNULL(0))
1375  PG_RETURN_NULL();
1376 
1377  /* init argument struct */
1378  arg = rtpg_nmapalgebraexpr_arg_init(argkwcount, argkw);
1379  if (arg == NULL) {
1380  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not initialize argument structure");
1381  PG_RETURN_NULL();
1382  }
1383 
1384  /* let helper function process rastbandarg (0) */
1385  if (!rtpg_nmapalgebra_rastbandarg_process(arg->bandarg, PG_GETARG_ARRAYTYPE_P(0), &allnull, &allempty, &noband)) {
1387  elog(ERROR, "RASTER_nMapAlgebra: Could not process rastbandarg");
1388  PG_RETURN_NULL();
1389  }
1390 
1391  POSTGIS_RT_DEBUGF(4, "allnull, allempty, noband = %d, %d, %d", allnull, allempty, noband);
1392 
1393  /* all rasters are NULL, return NULL */
1394  if (allnull == arg->bandarg->numraster) {
1395  elog(NOTICE, "All input rasters are NULL. Returning NULL");
1397  PG_RETURN_NULL();
1398  }
1399 
1400  /* only work on one or two rasters */
1401  if (arg->bandarg->numraster > 1)
1402  numraster = 2;
1403  else
1404  numraster = 1;
1405 
1406  /* pixel type (2) */
1407  if (!PG_ARGISNULL(2)) {
1408  char *pixtypename = text_to_cstring(PG_GETARG_TEXT_P(2));
1409 
1410  /* Get the pixel type index */
1411  arg->bandarg->pixtype = rt_pixtype_index_from_name(pixtypename);
1412  if (arg->bandarg->pixtype == PT_END) {
1414  elog(ERROR, "RASTER_nMapAlgebraExpr: Invalid pixel type: %s", pixtypename);
1415  PG_RETURN_NULL();
1416  }
1417  }
1418  POSTGIS_RT_DEBUGF(4, "pixeltype: %d", arg->bandarg->pixtype);
1419 
1420  /* extent type (3) */
1421  if (!PG_ARGISNULL(3)) {
1422  char *extenttypename = rtpg_strtoupper(rtpg_trim(text_to_cstring(PG_GETARG_TEXT_P(3))));
1423  arg->bandarg->extenttype = rt_util_extent_type(extenttypename);
1424  }
1425 
1426  if (arg->bandarg->extenttype == ET_CUSTOM) {
1427  if (numraster < 2) {
1428  elog(NOTICE, "CUSTOM extent type not supported. Defaulting to FIRST");
1429  arg->bandarg->extenttype = ET_FIRST;
1430  }
1431  else {
1432  elog(NOTICE, "CUSTOM extent type not supported. Defaulting to INTERSECTION");
1434  }
1435  }
1436  else if (numraster < 2)
1437  arg->bandarg->extenttype = ET_FIRST;
1438 
1439  POSTGIS_RT_DEBUGF(4, "extenttype: %d", arg->bandarg->extenttype);
1440 
1441  /* nodatanodataval (6) */
1442  if (!PG_ARGISNULL(6)) {
1443  arg->callback.nodatanodata.hasval = 1;
1444  arg->callback.nodatanodata.val = PG_GETARG_FLOAT8(6);
1445  }
1446 
1447  err = 0;
1448  /* all rasters are empty, return empty raster */
1449  if (allempty == arg->bandarg->numraster) {
1450  elog(NOTICE, "All input rasters are empty. Returning empty raster");
1451  err = 1;
1452  }
1453  /* all rasters don't have indicated band, return empty raster */
1454  else if (noband == arg->bandarg->numraster) {
1455  elog(NOTICE, "All input rasters do not have bands at indicated indexes. Returning empty raster");
1456  err = 1;
1457  }
1458  if (err) {
1460 
1461  raster = rt_raster_new(0, 0);
1462  if (raster == NULL) {
1463  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not create empty raster");
1464  PG_RETURN_NULL();
1465  }
1466 
1467  pgraster = rt_raster_serialize(raster);
1469  if (!pgraster) PG_RETURN_NULL();
1470 
1471  SET_VARSIZE(pgraster, pgraster->size);
1472  PG_RETURN_POINTER(pgraster);
1473  }
1474 
1475  /* connect SPI */
1476  if (SPI_connect() != SPI_OK_CONNECT) {
1478  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not connect to the SPI manager");
1479  PG_RETURN_NULL();
1480  }
1481 
1482  /*
1483  process expressions
1484 
1485  exprpos elements are:
1486  1 - expression => spi_plan[0]
1487  4 - nodata1expr => spi_plan[1]
1488  5 - nodata2expr => spi_plan[2]
1489  */
1490  for (i = 0; i < arg->callback.exprcount; i++) {
1491  char *expr = NULL;
1492  char *tmp = NULL;
1493  char *sql = NULL;
1494  char place[12] = "$1";
1495 
1496  if (PG_ARGISNULL(exprpos[i]))
1497  continue;
1498 
1499  expr = text_to_cstring(PG_GETARG_TEXT_P(exprpos[i]));
1500  POSTGIS_RT_DEBUGF(3, "raw expr of argument #%d: %s", exprpos[i], expr);
1501 
1502  for (j = 0, k = 1; j < argkwcount; j++) {
1503  /* attempt to replace keyword with placeholder */
1504  len = 0;
1505  tmp = rtpg_strreplace(expr, argkw[j], place, &len);
1506  pfree(expr);
1507  expr = tmp;
1508 
1509  if (len) {
1510  POSTGIS_RT_DEBUGF(4, "kw #%d (%s) at pos $%d", j, argkw[j], k);
1511  arg->callback.expr[i].spi_argcount++;
1512  arg->callback.expr[i].spi_argpos[j] = k++;
1513 
1514  sprintf(place, "$%d", k);
1515  }
1516  else
1517  arg->callback.expr[i].spi_argpos[j] = 0;
1518  }
1519 
1520  len = strlen("SELECT (") + strlen(expr) + strlen(")::double precision");
1521  sql = (char *) palloc(len + 1);
1522  if (sql == NULL) {
1524  SPI_finish();
1525  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not allocate memory for expression parameter %d", exprpos[i]);
1526  PG_RETURN_NULL();
1527  }
1528 
1529  memcpy(sql, "SELECT (", strlen("SELECT ("));
1530  memcpy(sql + strlen("SELECT ("), expr, strlen(expr));
1531  memcpy(sql + strlen("SELECT (") + strlen(expr), ")::double precision", strlen(")::double precision"));
1532  sql[len] = '\0';
1533 
1534  POSTGIS_RT_DEBUGF(3, "sql #%d: %s", exprpos[i], sql);
1535 
1536  /* prepared plan */
1537  if (arg->callback.expr[i].spi_argcount) {
1538  Oid *argtype = (Oid *) palloc(arg->callback.expr[i].spi_argcount * sizeof(Oid));
1539  POSTGIS_RT_DEBUGF(3, "expression parameter %d is a prepared plan", exprpos[i]);
1540  if (argtype == NULL) {
1541  pfree(sql);
1543  SPI_finish();
1544  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not allocate memory for prepared plan argtypes of expression parameter %d", exprpos[i]);
1545  PG_RETURN_NULL();
1546  }
1547 
1548  /* specify datatypes of parameters */
1549  for (j = 0, k = 0; j < argkwcount; j++) {
1550  if (arg->callback.expr[i].spi_argpos[j] < 1) continue;
1551 
1552  /* positions are INT4 */
1553  if (
1554  (strstr(argkw[j], "[rast.x]") != NULL) ||
1555  (strstr(argkw[j], "[rast.y]") != NULL) ||
1556  (strstr(argkw[j], "[rast1.x]") != NULL) ||
1557  (strstr(argkw[j], "[rast1.y]") != NULL) ||
1558  (strstr(argkw[j], "[rast2.x]") != NULL) ||
1559  (strstr(argkw[j], "[rast2.y]") != NULL)
1560  )
1561  argtype[k] = INT4OID;
1562  /* everything else is FLOAT8 */
1563  else
1564  argtype[k] = FLOAT8OID;
1565 
1566  k++;
1567  }
1568 
1569  arg->callback.expr[i].spi_plan = SPI_prepare(sql, arg->callback.expr[i].spi_argcount, argtype);
1570  pfree(argtype);
1571  pfree(sql);
1572 
1573  if (arg->callback.expr[i].spi_plan == NULL) {
1575  SPI_finish();
1576  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not create prepared plan of expression parameter %d", exprpos[i]);
1577  PG_RETURN_NULL();
1578  }
1579  }
1580  /* no args, just execute query */
1581  else {
1582  POSTGIS_RT_DEBUGF(3, "expression parameter %d has no args, simply executing", exprpos[i]);
1583  err = SPI_execute(sql, TRUE, 0);
1584  pfree(sql);
1585 
1586  if (err != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
1588  SPI_finish();
1589  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not evaluate expression parameter %d", exprpos[i]);
1590  PG_RETURN_NULL();
1591  }
1592 
1593  /* get output of prepared plan */
1594  tupdesc = SPI_tuptable->tupdesc;
1595  tuptable = SPI_tuptable;
1596  tuple = tuptable->vals[0];
1597 
1598  datum = SPI_getbinval(tuple, tupdesc, 1, &isnull);
1599  if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
1600  if (SPI_tuptable) SPI_freetuptable(tuptable);
1602  SPI_finish();
1603  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not get result of expression parameter %d", exprpos[i]);
1604  PG_RETURN_NULL();
1605  }
1606 
1607  if (!isnull) {
1608  arg->callback.expr[i].hasval = 1;
1609  arg->callback.expr[i].val = DatumGetFloat8(datum);
1610  }
1611 
1612  if (SPI_tuptable) SPI_freetuptable(tuptable);
1613  }
1614  }
1615 
1616  /* determine nodataval and possibly pixtype */
1617  /* band to check */
1618  switch (arg->bandarg->extenttype) {
1619  case ET_LAST:
1620  case ET_SECOND:
1621  if (numraster > 1)
1622  i = 1;
1623  else
1624  i = 0;
1625  break;
1626  default:
1627  i = 0;
1628  break;
1629  }
1630  /* find first viable band */
1631  if (!arg->bandarg->hasband[i]) {
1632  for (i = 0; i < numraster; i++) {
1633  if (arg->bandarg->hasband[i])
1634  break;
1635  }
1636  if (i >= numraster)
1637  i = numraster - 1;
1638  }
1639  band = rt_raster_get_band(arg->bandarg->raster[i], arg->bandarg->nband[i]);
1640 
1641  /* set pixel type if PT_END */
1642  if (arg->bandarg->pixtype == PT_END)
1644 
1645  /* set hasnodata and nodataval */
1646  arg->bandarg->hasnodata = 1;
1649  else
1651 
1652  POSTGIS_RT_DEBUGF(4, "pixtype, hasnodata, nodataval: %s, %d, %f", rt_pixtype_name(arg->bandarg->pixtype), arg->bandarg->hasnodata, arg->bandarg->nodataval);
1653 
1654  /* init itrset */
1655  itrset = palloc(sizeof(struct rt_iterator_t) * numraster);
1656  if (itrset == NULL) {
1658  SPI_finish();
1659  elog(ERROR, "RASTER_nMapAlgebra: Could not allocate memory for iterator arguments");
1660  PG_RETURN_NULL();
1661  }
1662 
1663  /* set itrset */
1664  for (i = 0; i < numraster; i++) {
1665  itrset[i].raster = arg->bandarg->raster[i];
1666  itrset[i].nband = arg->bandarg->nband[i];
1667  itrset[i].nbnodata = 1;
1668  }
1669 
1670  /* pass everything to iterator */
1671  err = rt_raster_iterator(
1672  itrset, numraster,
1673  arg->bandarg->extenttype, arg->bandarg->cextent,
1674  arg->bandarg->pixtype,
1675  arg->bandarg->hasnodata, arg->bandarg->nodataval,
1676  0, 0,
1677  NULL,
1678  &(arg->callback),
1680  &raster
1681  );
1682 
1683  pfree(itrset);
1685 
1686  if (err != ES_NONE) {
1687  SPI_finish();
1688  elog(ERROR, "RASTER_nMapAlgebraExpr: Could not run raster iterator function");
1689  PG_RETURN_NULL();
1690  }
1691  else if (raster == NULL) {
1692  SPI_finish();
1693  PG_RETURN_NULL();
1694  }
1695 
1696  /* switch to prior memory context to ensure memory allocated in correct context */
1697  MemoryContextSwitchTo(mainMemCtx);
1698 
1699  pgraster = rt_raster_serialize(raster);
1701 
1702  /* finish SPI */
1703  SPI_finish();
1704 
1705  if (!pgraster)
1706  PG_RETURN_NULL();
1707 
1708  SET_VARSIZE(pgraster, pgraster->size);
1709  PG_RETURN_POINTER(pgraster);
1710 }
#define TRUE
Definition: dbfopen.c:169
#define FALSE
Definition: dbfopen.c:168
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
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
@ PT_END
Definition: librtcore.h:197
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition: rt_raster.c:48
rt_extenttype rt_util_extent_type(const char *name)
Definition: rt_util.c:193
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
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
@ ES_NONE
Definition: librtcore.h:180
rt_errorstate rt_raster_iterator(rt_iterator itrset, uint16_t itrcount, rt_extenttype extenttype, rt_raster customextent, rt_pixtype pixtype, uint8_t hasnodata, double nodataval, uint16_t distancex, uint16_t distancey, rt_mask mask, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
@ ET_CUSTOM
Definition: librtcore.h:206
@ ET_LAST
Definition: librtcore.h:205
@ ET_INTERSECTION
Definition: librtcore.h:201
@ ET_SECOND
Definition: librtcore.h:204
@ ET_FIRST
Definition: librtcore.h:203
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
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:58
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)
char * rtpg_strtoupper(char *str)
char * rtpg_trim(const char *input)
char * rtpg_strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
Definition: rtpg_internal.c:55
static void rtpg_nmapalgebraexpr_arg_destroy(rtpg_nmapalgebraexpr_arg arg)
static rtpg_nmapalgebraexpr_arg rtpg_nmapalgebraexpr_arg_init(int cnt, char **kw)
static int rtpg_nmapalgebra_rastbandarg_process(rtpg_nmapalgebra_arg arg, ArrayType *array, int *allnull, int *allempty, int *noband)
static int rtpg_nmapalgebraexpr_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
rt_raster raster
Definition: librtcore.h:2444
uint16_t nband
Definition: librtcore.h:2445
uint8_t nbnodata
Definition: librtcore.h:2446
Struct definitions.
Definition: librtcore.h:2251
rtpg_nmapalgebraexpr_callback_arg callback
rtpg_nmapalgebra_arg bandarg
struct rtpg_nmapalgebraexpr_callback_arg::@18 expr[3]
struct rtpg_nmapalgebraexpr_callback_arg::@19 nodatanodata

References ovdump::band, rtpg_nmapalgebraexpr_arg_t::bandarg, rtpg_nmapalgebraexpr_arg_t::callback, rtpg_nmapalgebra_arg_t::cextent, ES_NONE, ET_CUSTOM, ET_FIRST, ET_INTERSECTION, ET_LAST, ET_SECOND, rtpg_nmapalgebraexpr_callback_arg::expr, rtpg_nmapalgebraexpr_callback_arg::exprcount, rtpg_nmapalgebra_arg_t::extenttype, FALSE, rtpg_nmapalgebra_arg_t::hasband, rtpg_nmapalgebra_arg_t::hasnodata, rtpg_nmapalgebraexpr_callback_arg::hasval, rt_iterator_t::nband, rtpg_nmapalgebra_arg_t::nband, rt_iterator_t::nbnodata, rtpg_nmapalgebraexpr_callback_arg::nodatanodata, rtpg_nmapalgebra_arg_t::nodataval, rtpg_nmapalgebra_arg_t::numraster, rtpg_nmapalgebra_arg_t::pixtype, POSTGIS_RT_DEBUGF, PT_END, rt_iterator_t::raster, rtpg_nmapalgebra_arg_t::raster, rtrowdump::raster, rt_band_get_hasnodata_flag(), rt_band_get_min_value(), rt_band_get_nodata(), rt_band_get_pixtype(), rt_pixtype_index_from_name(), rt_pixtype_name(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_iterator(), rt_raster_new(), rt_raster_serialize(), rt_util_extent_type(), rtpg_nmapalgebra_rastbandarg_process(), rtpg_nmapalgebraexpr_arg_destroy(), rtpg_nmapalgebraexpr_arg_init(), rtpg_nmapalgebraexpr_callback(), rtpg_strreplace(), rtpg_strtoupper(), rtpg_trim(), rt_raster_serialized_t::size, rtpg_nmapalgebraexpr_callback_arg::spi_argcount, rtpg_nmapalgebraexpr_callback_arg::spi_argpos, rtpg_nmapalgebraexpr_callback_arg::spi_plan, rtgdalraster::sql, text_to_cstring(), TRUE, and rtpg_nmapalgebraexpr_callback_arg::val.

Here is the call graph for this function: