3512 uint32_t numBands = 0;
3532 HeapTupleHeader tup;
3537 text *exprtext = NULL;
3542 char *pixeltype = NULL;
3543 text *pixeltypetext = NULL;
3545 double nodataval = 0;
3546 bool hasnodata =
FALSE;
3548 char **comma_set = NULL;
3549 uint32_t comma_n = 0;
3550 char **colon_set = NULL;
3551 uint32_t colon_n = 0;
3552 char **dash_set = NULL;
3553 uint32_t dash_n = 0;
3558 if (PG_ARGISNULL(0))
3560 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3565 PG_FREE_IF_COPY(pgraster, 0);
3566 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3570 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3574 array = PG_GETARG_ARRAYTYPE_P(1);
3575 etype = ARR_ELEMTYPE(array);
3576 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3578 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3582 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3586 PG_FREE_IF_COPY(pgraster, 0);
3590 SET_VARSIZE(pgrtn, pgrtn->
size);
3591 PG_RETURN_POINTER(pgrtn);
3599 for (i = 0; i < n; i++) {
3600 if (nulls[i])
continue;
3603 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3605 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3609 PG_FREE_IF_COPY(pgraster, 0);
3613 SET_VARSIZE(pgrtn, pgrtn->
size);
3614 PG_RETURN_POINTER(pgrtn);
3618 tupv = GetAttributeByName(tup,
"nband", &isnull);
3620 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3624 PG_FREE_IF_COPY(pgraster, 0);
3628 SET_VARSIZE(pgrtn, pgrtn->
size);
3629 PG_RETURN_POINTER(pgrtn);
3631 nband = DatumGetInt32(tupv);
3635 if (nband < 1 || nband > numBands) {
3636 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3640 PG_FREE_IF_COPY(pgraster, 0);
3644 SET_VARSIZE(pgrtn, pgrtn->
size);
3645 PG_RETURN_POINTER(pgrtn);
3649 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3651 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3655 PG_FREE_IF_COPY(pgraster, 0);
3659 SET_VARSIZE(pgrtn, pgrtn->
size);
3660 PG_RETURN_POINTER(pgrtn);
3662 exprtext = (text *) DatumGetPointer(tupv);
3663 if (NULL == exprtext) {
3664 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3668 PG_FREE_IF_COPY(pgraster, 0);
3672 SET_VARSIZE(pgrtn, pgrtn->
size);
3673 PG_RETURN_POINTER(pgrtn);
3675 expr = text_to_cstring(exprtext);
3684 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3688 PG_FREE_IF_COPY(pgraster, 0);
3692 SET_VARSIZE(pgrtn, pgrtn->
size);
3693 PG_RETURN_POINTER(pgrtn);
3700 for (a = 0, j = 0; a < comma_n; a++) {
3706 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3707 for (k = 0; k < j; k++) pfree(exprset[k]);
3712 PG_FREE_IF_COPY(pgraster, 0);
3716 SET_VARSIZE(pgrtn, pgrtn->
size);
3717 PG_RETURN_POINTER(pgrtn);
3723 for (b = 0; b < colon_n; b++) {
3728 if (dash_n < 1 || dash_n > 3) {
3729 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3730 for (k = 0; k < j; k++) pfree(exprset[k]);
3735 PG_FREE_IF_COPY(pgraster, 0);
3739 SET_VARSIZE(pgrtn, pgrtn->
size);
3740 PG_RETURN_POINTER(pgrtn);
3743 for (c = 0; c < dash_n; c++) {
3747 strlen(dash_set[c]) == 1 && (
3748 strchr(dash_set[c],
'(') != NULL ||
3749 strchr(dash_set[c],
'[') != NULL ||
3750 strchr(dash_set[c],
')') != NULL ||
3751 strchr(dash_set[c],
']') != NULL
3755 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3757 for (k = 0; k <= j; k++) pfree(exprset[k]);
3760 PG_FREE_IF_COPY(pgraster, 0);
3762 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3766 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3768 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3769 strcpy(dash_set[c], junk);
3773 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3774 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3775 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3779 pfree(dash_set[dash_n]);
3780 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3784 if (c < 1 && dash_n > 2) {
3785 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3786 for (k = 0; k < j; k++) pfree(exprset[k]);
3791 PG_FREE_IF_COPY(pgraster, 0);
3795 SET_VARSIZE(pgrtn, pgrtn->
size);
3796 PG_RETURN_POINTER(pgrtn);
3807 strchr(dash_set[c],
')') != NULL ||
3808 strchr(dash_set[c],
']') != NULL
3813 else if (strchr(dash_set[c],
'(') != NULL){
3823 strrchr(dash_set[c],
'(') != NULL ||
3824 strrchr(dash_set[c],
'[') != NULL
3829 else if (strrchr(dash_set[c],
']') != NULL) {
3837 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3845 val = strtod(dash_set[c], &junk);
3846 if (errno != 0 || dash_set[c] == junk) {
3847 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3848 for (k = 0; k < j; k++) pfree(exprset[k]);
3853 PG_FREE_IF_COPY(pgraster, 0);
3857 SET_VARSIZE(pgrtn, pgrtn->
size);
3858 PG_RETURN_POINTER(pgrtn);
3864 junk = strstr(colon_set[b], dash_set[c]);
3869 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3870 b, c, colon_set[b], dash_set[c], junk
3873 if (junk != colon_set[b]) {
3875 if (*(junk - 1) ==
'-') {
3878 ((junk - 1) == colon_set[b]) ||
3879 (*(junk - 2) ==
'-') ||
3880 (*(junk - 2) ==
'[') ||
3881 (*(junk - 2) ==
'(')
3901 exprset[j]->
src.
min = val;
3907 exprset[j]->
src.
max = val;
3917 exprset[j]->
dst.
min = val;
3920 exprset[j]->
dst.
max = val;
3928 , exprset[j]->src.min
3929 , exprset[j]->src.max
3930 , exprset[j]->dst.min
3931 , exprset[j]->dst.max
3938 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3940 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3944 PG_FREE_IF_COPY(pgraster, 0);
3948 SET_VARSIZE(pgrtn, pgrtn->
size);
3949 PG_RETURN_POINTER(pgrtn);
3951 pixeltypetext = (text *) DatumGetPointer(tupv);
3952 if (NULL == pixeltypetext) {
3953 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3957 PG_FREE_IF_COPY(pgraster, 0);
3961 SET_VARSIZE(pgrtn, pgrtn->
size);
3962 PG_RETURN_POINTER(pgrtn);
3964 pixeltype = text_to_cstring(pixeltypetext);
3969 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3975 nodataval = DatumGetFloat8(tupv);
3984 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster",
nband);
3985 for (k = 0; k < j; k++) pfree(exprset[k]);
3990 PG_FREE_IF_COPY(pgraster, 0);
3994 SET_VARSIZE(pgrtn, pgrtn->
size);
3995 PG_RETURN_POINTER(pgrtn);
3999 for (k = 0; k < j; k++) pfree(exprset[k]);
4003 PG_FREE_IF_COPY(pgraster, 0);
4005 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d",
nband);
4011 for (k = 0; k < j; k++) pfree(exprset[k]);
4016 PG_FREE_IF_COPY(pgraster, 0);
4018 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band",
nband);
4026 for (k = 0; k < j; k++) pfree(exprset[k]);
4032 PG_FREE_IF_COPY(pgraster, 0);
4038 SET_VARSIZE(pgrtn, pgrtn->
size);
4039 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