3472 uint32_t numBands = 0;
3492 HeapTupleHeader tup;
3497 text *exprtext = NULL;
3502 char *pixeltype = NULL;
3503 text *pixeltypetext = NULL;
3505 double nodataval = 0;
3506 bool hasnodata =
FALSE;
3508 char **comma_set = NULL;
3509 uint32_t comma_n = 0;
3510 char **colon_set = NULL;
3511 uint32_t colon_n = 0;
3512 char **dash_set = NULL;
3513 uint32_t dash_n = 0;
3518 if (PG_ARGISNULL(0))
3520 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3525 PG_FREE_IF_COPY(pgraster, 0);
3526 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3530 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3534 array = PG_GETARG_ARRAYTYPE_P(1);
3535 etype = ARR_ELEMTYPE(array);
3536 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3538 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3542 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3546 PG_FREE_IF_COPY(pgraster, 0);
3550 SET_VARSIZE(pgrtn, pgrtn->
size);
3551 PG_RETURN_POINTER(pgrtn);
3559 for (i = 0; i < n; i++) {
3560 if (nulls[i])
continue;
3563 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3565 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3569 PG_FREE_IF_COPY(pgraster, 0);
3573 SET_VARSIZE(pgrtn, pgrtn->
size);
3574 PG_RETURN_POINTER(pgrtn);
3578 tupv = GetAttributeByName(tup,
"nband", &isnull);
3580 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3584 PG_FREE_IF_COPY(pgraster, 0);
3588 SET_VARSIZE(pgrtn, pgrtn->
size);
3589 PG_RETURN_POINTER(pgrtn);
3591 nband = DatumGetInt32(tupv);
3595 if (nband < 1 || nband > numBands) {
3596 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3600 PG_FREE_IF_COPY(pgraster, 0);
3604 SET_VARSIZE(pgrtn, pgrtn->
size);
3605 PG_RETURN_POINTER(pgrtn);
3609 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3611 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3615 PG_FREE_IF_COPY(pgraster, 0);
3619 SET_VARSIZE(pgrtn, pgrtn->
size);
3620 PG_RETURN_POINTER(pgrtn);
3622 exprtext = (text *) DatumGetPointer(tupv);
3623 if (NULL == exprtext) {
3624 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3628 PG_FREE_IF_COPY(pgraster, 0);
3632 SET_VARSIZE(pgrtn, pgrtn->
size);
3633 PG_RETURN_POINTER(pgrtn);
3635 expr = text_to_cstring(exprtext);
3644 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3648 PG_FREE_IF_COPY(pgraster, 0);
3652 SET_VARSIZE(pgrtn, pgrtn->
size);
3653 PG_RETURN_POINTER(pgrtn);
3660 for (a = 0, j = 0; a < comma_n; a++) {
3666 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3667 for (k = 0; k < j; k++) pfree(exprset[k]);
3672 PG_FREE_IF_COPY(pgraster, 0);
3676 SET_VARSIZE(pgrtn, pgrtn->
size);
3677 PG_RETURN_POINTER(pgrtn);
3683 for (b = 0; b < colon_n; b++) {
3688 if (dash_n < 1 || dash_n > 3) {
3689 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3690 for (k = 0; k < j; k++) pfree(exprset[k]);
3695 PG_FREE_IF_COPY(pgraster, 0);
3699 SET_VARSIZE(pgrtn, pgrtn->
size);
3700 PG_RETURN_POINTER(pgrtn);
3703 for (c = 0; c < dash_n; c++) {
3707 strlen(dash_set[c]) == 1 && (
3708 strchr(dash_set[c],
'(') != NULL ||
3709 strchr(dash_set[c],
'[') != NULL ||
3710 strchr(dash_set[c],
')') != NULL ||
3711 strchr(dash_set[c],
']') != NULL
3715 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3717 for (k = 0; k <= j; k++) pfree(exprset[k]);
3720 PG_FREE_IF_COPY(pgraster, 0);
3722 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3726 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3728 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3729 strcpy(dash_set[c], junk);
3733 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3734 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3735 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3739 pfree(dash_set[dash_n]);
3740 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3744 if (c < 1 && dash_n > 2) {
3745 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3746 for (k = 0; k < j; k++) pfree(exprset[k]);
3751 PG_FREE_IF_COPY(pgraster, 0);
3755 SET_VARSIZE(pgrtn, pgrtn->
size);
3756 PG_RETURN_POINTER(pgrtn);
3767 strchr(dash_set[c],
')') != NULL ||
3768 strchr(dash_set[c],
']') != NULL
3773 else if (strchr(dash_set[c],
'(') != NULL){
3783 strrchr(dash_set[c],
'(') != NULL ||
3784 strrchr(dash_set[c],
'[') != NULL
3789 else if (strrchr(dash_set[c],
']') != NULL) {
3797 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3805 val = strtod(dash_set[c], &junk);
3806 if (errno != 0 || dash_set[c] == junk) {
3807 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3808 for (k = 0; k < j; k++) pfree(exprset[k]);
3813 PG_FREE_IF_COPY(pgraster, 0);
3817 SET_VARSIZE(pgrtn, pgrtn->
size);
3818 PG_RETURN_POINTER(pgrtn);
3824 junk = strstr(colon_set[b], dash_set[c]);
3829 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3830 b, c, colon_set[b], dash_set[c], junk
3833 if (junk != colon_set[b]) {
3835 if (*(junk - 1) ==
'-') {
3838 ((junk - 1) == colon_set[b]) ||
3839 (*(junk - 2) ==
'-') ||
3840 (*(junk - 2) ==
'[') ||
3841 (*(junk - 2) ==
'(')
3861 exprset[j]->
src.
min = val;
3867 exprset[j]->
src.
max = val;
3877 exprset[j]->
dst.
min = val;
3880 exprset[j]->
dst.
max = val;
3888 , exprset[j]->src.min
3889 , exprset[j]->src.max
3890 , exprset[j]->dst.min
3891 , exprset[j]->dst.max
3898 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3900 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3904 PG_FREE_IF_COPY(pgraster, 0);
3908 SET_VARSIZE(pgrtn, pgrtn->
size);
3909 PG_RETURN_POINTER(pgrtn);
3911 pixeltypetext = (text *) DatumGetPointer(tupv);
3912 if (NULL == pixeltypetext) {
3913 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3917 PG_FREE_IF_COPY(pgraster, 0);
3921 SET_VARSIZE(pgrtn, pgrtn->
size);
3922 PG_RETURN_POINTER(pgrtn);
3924 pixeltype = text_to_cstring(pixeltypetext);
3929 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3935 nodataval = DatumGetFloat8(tupv);
3944 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster",
nband);
3945 for (k = 0; k < j; k++) pfree(exprset[k]);
3950 PG_FREE_IF_COPY(pgraster, 0);
3954 SET_VARSIZE(pgrtn, pgrtn->
size);
3955 PG_RETURN_POINTER(pgrtn);
3959 for (k = 0; k < j; k++) pfree(exprset[k]);
3963 PG_FREE_IF_COPY(pgraster, 0);
3965 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d",
nband);
3971 for (k = 0; k < j; k++) pfree(exprset[k]);
3976 PG_FREE_IF_COPY(pgraster, 0);
3978 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band",
nband);
3986 for (k = 0; k < j; k++) pfree(exprset[k]);
3992 PG_FREE_IF_COPY(pgraster, 0);
3998 SET_VARSIZE(pgrtn, pgrtn->
size);
3999 PG_RETURN_POINTER(pgrtn);
rt_band rt_band_reclass(rt_band srcband, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, rt_reclassexpr *exprset, int exprcount)
Returns new band with values reclassified.
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
void rt_band_destroy(rt_band band)
Destroy a raster band.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
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 * rtpg_removespaces(char *str)
char * rtpg_chartrim(const char *input, char *remove)
char * rtpg_strrstr(const char *s1, const char *s2)
char ** rtpg_strsplit(const char *str, const char *delimiter, uint32_t *n)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)
struct rt_reclassexpr_t::rt_reclassrange src
struct rt_reclassexpr_t::rt_reclassrange dst