1334 MemoryContext mainMemCtx = CurrentMemoryContext;
1337 uint16_t exprpos[3] = {1, 4, 5};
1351 SPITupleTable *tuptable = NULL;
1354 bool isnull =
FALSE;
1360 const int argkwcount = 12;
1376 if (PG_ARGISNULL(0))
1382 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not initialize argument structure");
1389 elog(ERROR,
"RASTER_nMapAlgebra: Could not process rastbandarg");
1393 POSTGIS_RT_DEBUGF(4,
"allnull, allempty, noband = %d, %d, %d", allnull, allempty, noband);
1397 elog(NOTICE,
"All input rasters are NULL. Returning NULL");
1409 if (!PG_ARGISNULL(2)) {
1416 elog(ERROR,
"RASTER_nMapAlgebraExpr: Invalid pixel type: %s", pixtypename);
1423 if (!PG_ARGISNULL(3)) {
1429 if (numraster < 2) {
1430 elog(NOTICE,
"CUSTOM extent type not supported. Defaulting to FIRST");
1434 elog(NOTICE,
"CUSTOM extent type not supported. Defaulting to INTERSECTION");
1438 else if (numraster < 2)
1444 if (!PG_ARGISNULL(6)) {
1452 elog(NOTICE,
"All input rasters are empty. Returning empty raster");
1457 elog(NOTICE,
"All input rasters do not have bands at indicated indexes. Returning empty raster");
1465 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not create empty raster");
1471 if (!pgraster) PG_RETURN_NULL();
1473 SET_VARSIZE(pgraster, pgraster->
size);
1474 PG_RETURN_POINTER(pgraster);
1478 if (SPI_connect() != SPI_OK_CONNECT) {
1480 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not connect to the SPI manager");
1496 char place[12] =
"$1";
1498 if (PG_ARGISNULL(exprpos[i]))
1504 for (j = 0, k = 1; j < argkwcount; j++) {
1516 sprintf(place,
"$%d", k);
1522 len = strlen(
"SELECT (") + strlen(expr) + strlen(
")::double precision");
1523 sql = (
char *) palloc(len + 1);
1527 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not allocate memory for expression parameter %d", exprpos[i]);
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"));
1542 if (argtype == NULL) {
1546 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not allocate memory for prepared plan argtypes of expression parameter %d", exprpos[i]);
1551 for (j = 0, k = 0; j < argkwcount; j++) {
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)
1563 argtype[k] = INT4OID;
1566 argtype[k] = FLOAT8OID;
1578 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not create prepared plan of expression parameter %d", exprpos[i]);
1584 POSTGIS_RT_DEBUGF(3,
"expression parameter %d has no args, simply executing", exprpos[i]);
1585 err = SPI_execute(
sql,
TRUE, 0);
1588 if (err != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
1591 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not evaluate expression parameter %d", exprpos[i]);
1596 tupdesc = SPI_tuptable->tupdesc;
1597 tuptable = SPI_tuptable;
1598 tuple = tuptable->vals[0];
1600 datum = SPI_getbinval(tuple, tupdesc, 1, &isnull);
1601 if (SPI_result == SPI_ERROR_NOATTRIBUTE) {
1602 if (SPI_tuptable) SPI_freetuptable(tuptable);
1605 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not get result of expression parameter %d", exprpos[i]);
1614 if (SPI_tuptable) SPI_freetuptable(tuptable);
1634 for (i = 0; i < numraster; i++) {
1658 if (itrset == NULL) {
1661 elog(ERROR,
"RASTER_nMapAlgebra: Could not allocate memory for iterator arguments");
1666 for (i = 0; i < numraster; i++) {
1690 elog(ERROR,
"RASTER_nMapAlgebraExpr: Could not run raster iterator function");
1693 else if (
raster == NULL) {
1699 MemoryContextSwitchTo(mainMemCtx);
1710 SET_VARSIZE(pgraster, pgraster->
size);
1711 PG_RETURN_POINTER(pgraster);
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
rt_extenttype rt_util_extent_type(const char *name)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
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_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
const char * rt_pixtype_name(rt_pixtype pixtype)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
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)
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,...)
rtpg_nmapalgebraexpr_callback_arg callback
rtpg_nmapalgebra_arg bandarg
struct rtpg_nmapalgebraexpr_callback_arg::@18 expr[3]
struct rtpg_nmapalgebraexpr_callback_arg::@19 nodatanodata