3491 uint32_t numBands = 0;
3511 HeapTupleHeader tup;
3516 text *exprtext = NULL;
3521 char *pixeltype = NULL;
3522 text *pixeltypetext = NULL;
3524 double nodataval = 0;
3525 bool hasnodata =
FALSE;
3527 char **comma_set = NULL;
3528 uint32_t comma_n = 0;
3529 char **colon_set = NULL;
3530 uint32_t colon_n = 0;
3531 char **dash_set = NULL;
3532 uint32_t dash_n = 0;
3537 if (PG_ARGISNULL(0))
3539 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3544 PG_FREE_IF_COPY(pgraster, 0);
3545 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3549 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3553 array = PG_GETARG_ARRAYTYPE_P(1);
3554 etype = ARR_ELEMTYPE(array);
3555 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3557 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3561 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3565 PG_FREE_IF_COPY(pgraster, 0);
3569 SET_VARSIZE(pgrtn, pgrtn->
size);
3570 PG_RETURN_POINTER(pgrtn);
3578 for (i = 0; i < n; i++) {
3579 if (nulls[i])
continue;
3582 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3584 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3588 PG_FREE_IF_COPY(pgraster, 0);
3592 SET_VARSIZE(pgrtn, pgrtn->
size);
3593 PG_RETURN_POINTER(pgrtn);
3597 tupv = GetAttributeByName(tup,
"nband", &isnull);
3599 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3603 PG_FREE_IF_COPY(pgraster, 0);
3607 SET_VARSIZE(pgrtn, pgrtn->
size);
3608 PG_RETURN_POINTER(pgrtn);
3610 nband = DatumGetInt32(tupv);
3614 if (nband < 1 || nband > numBands) {
3615 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3619 PG_FREE_IF_COPY(pgraster, 0);
3623 SET_VARSIZE(pgrtn, pgrtn->
size);
3624 PG_RETURN_POINTER(pgrtn);
3628 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3630 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3634 PG_FREE_IF_COPY(pgraster, 0);
3638 SET_VARSIZE(pgrtn, pgrtn->
size);
3639 PG_RETURN_POINTER(pgrtn);
3641 exprtext = (text *) DatumGetPointer(tupv);
3642 if (NULL == exprtext) {
3643 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3647 PG_FREE_IF_COPY(pgraster, 0);
3651 SET_VARSIZE(pgrtn, pgrtn->
size);
3652 PG_RETURN_POINTER(pgrtn);
3654 expr = text_to_cstring(exprtext);
3663 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3667 PG_FREE_IF_COPY(pgraster, 0);
3671 SET_VARSIZE(pgrtn, pgrtn->
size);
3672 PG_RETURN_POINTER(pgrtn);
3679 for (a = 0, j = 0; a < comma_n; a++) {
3685 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3686 for (k = 0; k < j; k++) pfree(exprset[k]);
3691 PG_FREE_IF_COPY(pgraster, 0);
3695 SET_VARSIZE(pgrtn, pgrtn->
size);
3696 PG_RETURN_POINTER(pgrtn);
3702 for (b = 0; b < colon_n; b++) {
3707 if (dash_n < 1 || dash_n > 3) {
3708 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3709 for (k = 0; k < j; k++) pfree(exprset[k]);
3714 PG_FREE_IF_COPY(pgraster, 0);
3718 SET_VARSIZE(pgrtn, pgrtn->
size);
3719 PG_RETURN_POINTER(pgrtn);
3722 for (c = 0; c < dash_n; c++) {
3726 strlen(dash_set[c]) == 1 && (
3727 strchr(dash_set[c],
'(') != NULL ||
3728 strchr(dash_set[c],
'[') != NULL ||
3729 strchr(dash_set[c],
')') != NULL ||
3730 strchr(dash_set[c],
']') != NULL
3734 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3736 for (k = 0; k <= j; k++) pfree(exprset[k]);
3739 PG_FREE_IF_COPY(pgraster, 0);
3741 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3745 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3747 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3748 strcpy(dash_set[c], junk);
3752 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3753 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3754 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3758 pfree(dash_set[dash_n]);
3759 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3763 if (c < 1 && dash_n > 2) {
3764 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3765 for (k = 0; k < j; k++) pfree(exprset[k]);
3770 PG_FREE_IF_COPY(pgraster, 0);
3774 SET_VARSIZE(pgrtn, pgrtn->
size);
3775 PG_RETURN_POINTER(pgrtn);
3786 strchr(dash_set[c],
')') != NULL ||
3787 strchr(dash_set[c],
']') != NULL
3792 else if (strchr(dash_set[c],
'(') != NULL){
3802 strrchr(dash_set[c],
'(') != NULL ||
3803 strrchr(dash_set[c],
'[') != NULL
3808 else if (strrchr(dash_set[c],
']') != NULL) {
3816 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3824 val = strtod(dash_set[c], &junk);
3825 if (errno != 0 || dash_set[c] == junk) {
3826 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3827 for (k = 0; k < j; k++) pfree(exprset[k]);
3832 PG_FREE_IF_COPY(pgraster, 0);
3836 SET_VARSIZE(pgrtn, pgrtn->
size);
3837 PG_RETURN_POINTER(pgrtn);
3843 junk = strstr(colon_set[b], dash_set[c]);
3848 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3849 b, c, colon_set[b], dash_set[c], junk
3852 if (junk != colon_set[b]) {
3854 if (*(junk - 1) ==
'-') {
3857 ((junk - 1) == colon_set[b]) ||
3858 (*(junk - 2) ==
'-') ||
3859 (*(junk - 2) ==
'[') ||
3860 (*(junk - 2) ==
'(')
3880 exprset[j]->
src.
min = val;
3886 exprset[j]->
src.
max = val;
3896 exprset[j]->
dst.
min = val;
3899 exprset[j]->
dst.
max = val;
3907 , exprset[j]->src.min
3908 , exprset[j]->src.max
3909 , exprset[j]->dst.min
3910 , exprset[j]->dst.max
3917 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3919 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3923 PG_FREE_IF_COPY(pgraster, 0);
3927 SET_VARSIZE(pgrtn, pgrtn->
size);
3928 PG_RETURN_POINTER(pgrtn);
3930 pixeltypetext = (text *) DatumGetPointer(tupv);
3931 if (NULL == pixeltypetext) {
3932 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3936 PG_FREE_IF_COPY(pgraster, 0);
3940 SET_VARSIZE(pgrtn, pgrtn->
size);
3941 PG_RETURN_POINTER(pgrtn);
3943 pixeltype = text_to_cstring(pixeltypetext);
3948 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3954 nodataval = DatumGetFloat8(tupv);
3963 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster",
nband);
3964 for (k = 0; k < j; k++) pfree(exprset[k]);
3969 PG_FREE_IF_COPY(pgraster, 0);
3973 SET_VARSIZE(pgrtn, pgrtn->
size);
3974 PG_RETURN_POINTER(pgrtn);
3978 for (k = 0; k < j; k++) pfree(exprset[k]);
3982 PG_FREE_IF_COPY(pgraster, 0);
3984 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d",
nband);
3990 for (k = 0; k < j; k++) pfree(exprset[k]);
3995 PG_FREE_IF_COPY(pgraster, 0);
3997 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band",
nband);
4005 for (k = 0; k < j; k++) pfree(exprset[k]);
4011 PG_FREE_IF_COPY(pgraster, 0);
4017 SET_VARSIZE(pgrtn, pgrtn->
size);
4018 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