PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ RASTER_nMapAlgebraExpr()

Datum RASTER_nMapAlgebraExpr ( PG_FUNCTION_ARGS  )

Definition at line 1323 of file rtpg_mapalgebra.c.

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, rtpg_nmapalgebra_arg_t::nband, rt_iterator_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, rtpg_nmapalgebra_arg_t::raster, rtrowdump::raster, rt_iterator_t::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, TRUE, and rtpg_nmapalgebraexpr_callback_arg::val.

Referenced by rtpg_nmapalgebraexpr_callback().

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