PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_nMapAlgebraExpr()

Datum RASTER_nMapAlgebraExpr ( PG_FUNCTION_ARGS  )

Definition at line 1332 of file rtpg_mapalgebra.c.

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