3445 uint32_t numBands = 0;
3465 HeapTupleHeader tup;
3470 text *exprtext = NULL;
3475 char *pixeltype = NULL;
3476 text *pixeltypetext = NULL;
3478 double nodataval = 0;
3479 bool hasnodata =
FALSE;
3481 char **comma_set = NULL;
3482 uint32_t comma_n = 0;
3483 char **colon_set = NULL;
3484 uint32_t colon_n = 0;
3485 char **dash_set = NULL;
3486 uint32_t dash_n = 0;
3491 if (PG_ARGISNULL(0))
3493 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3498 PG_FREE_IF_COPY(pgraster, 0);
3499 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3503 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3507 array = PG_GETARG_ARRAYTYPE_P(1);
3508 etype = ARR_ELEMTYPE(array);
3509 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3511 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3515 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3519 PG_FREE_IF_COPY(pgraster, 0);
3523 SET_VARSIZE(pgrtn, pgrtn->
size);
3524 PG_RETURN_POINTER(pgrtn);
3532 for (i = 0; i < n; i++) {
3533 if (nulls[i])
continue;
3536 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3538 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3542 PG_FREE_IF_COPY(pgraster, 0);
3546 SET_VARSIZE(pgrtn, pgrtn->
size);
3547 PG_RETURN_POINTER(pgrtn);
3551 tupv = GetAttributeByName(tup,
"nband", &isnull);
3553 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3557 PG_FREE_IF_COPY(pgraster, 0);
3561 SET_VARSIZE(pgrtn, pgrtn->
size);
3562 PG_RETURN_POINTER(pgrtn);
3564 nband = DatumGetInt32(tupv);
3568 if (nband < 1 || nband > numBands) {
3569 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3573 PG_FREE_IF_COPY(pgraster, 0);
3577 SET_VARSIZE(pgrtn, pgrtn->
size);
3578 PG_RETURN_POINTER(pgrtn);
3582 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3584 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3588 PG_FREE_IF_COPY(pgraster, 0);
3592 SET_VARSIZE(pgrtn, pgrtn->
size);
3593 PG_RETURN_POINTER(pgrtn);
3595 exprtext = (text *) DatumGetPointer(tupv);
3596 if (NULL == exprtext) {
3597 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3601 PG_FREE_IF_COPY(pgraster, 0);
3605 SET_VARSIZE(pgrtn, pgrtn->
size);
3606 PG_RETURN_POINTER(pgrtn);
3608 expr = text_to_cstring(exprtext);
3617 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3621 PG_FREE_IF_COPY(pgraster, 0);
3625 SET_VARSIZE(pgrtn, pgrtn->
size);
3626 PG_RETURN_POINTER(pgrtn);
3633 for (a = 0, j = 0; a < comma_n; a++) {
3639 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3640 for (k = 0; k < j; k++) pfree(exprset[k]);
3645 PG_FREE_IF_COPY(pgraster, 0);
3649 SET_VARSIZE(pgrtn, pgrtn->
size);
3650 PG_RETURN_POINTER(pgrtn);
3656 for (b = 0; b < colon_n; b++) {
3661 if (dash_n < 1 || dash_n > 3) {
3662 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3663 for (k = 0; k < j; k++) pfree(exprset[k]);
3668 PG_FREE_IF_COPY(pgraster, 0);
3672 SET_VARSIZE(pgrtn, pgrtn->
size);
3673 PG_RETURN_POINTER(pgrtn);
3676 for (c = 0; c < dash_n; c++) {
3680 strlen(dash_set[c]) == 1 && (
3681 strchr(dash_set[c],
'(') != NULL ||
3682 strchr(dash_set[c],
'[') != NULL ||
3683 strchr(dash_set[c],
')') != NULL ||
3684 strchr(dash_set[c],
']') != NULL
3688 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3690 for (k = 0; k <= j; k++) pfree(exprset[k]);
3693 PG_FREE_IF_COPY(pgraster, 0);
3695 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3699 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3701 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3702 strcpy(dash_set[c], junk);
3706 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3707 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3708 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3712 pfree(dash_set[dash_n]);
3713 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3717 if (c < 1 && dash_n > 2) {
3718 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3719 for (k = 0; k < j; k++) pfree(exprset[k]);
3724 PG_FREE_IF_COPY(pgraster, 0);
3728 SET_VARSIZE(pgrtn, pgrtn->
size);
3729 PG_RETURN_POINTER(pgrtn);
3740 strchr(dash_set[c],
')') != NULL ||
3741 strchr(dash_set[c],
']') != NULL
3746 else if (strchr(dash_set[c],
'(') != NULL){
3756 strrchr(dash_set[c],
'(') != NULL ||
3757 strrchr(dash_set[c],
'[') != NULL
3762 else if (strrchr(dash_set[c],
']') != NULL) {
3770 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3778 val = strtod(dash_set[c], &junk);
3779 if (errno != 0 || dash_set[c] == junk) {
3780 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3781 for (k = 0; k < j; k++) pfree(exprset[k]);
3786 PG_FREE_IF_COPY(pgraster, 0);
3790 SET_VARSIZE(pgrtn, pgrtn->
size);
3791 PG_RETURN_POINTER(pgrtn);
3797 junk = strstr(colon_set[b], dash_set[c]);
3802 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3803 b, c, colon_set[b], dash_set[c], junk
3806 if (junk != colon_set[b]) {
3808 if (*(junk - 1) ==
'-') {
3811 ((junk - 1) == colon_set[b]) ||
3812 (*(junk - 2) ==
'-') ||
3813 (*(junk - 2) ==
'[') ||
3814 (*(junk - 2) ==
'(')
3834 exprset[j]->
src.
min = val;
3840 exprset[j]->
src.
max = val;
3850 exprset[j]->
dst.
min = val;
3853 exprset[j]->
dst.
max = val;
3861 , exprset[j]->src.min
3862 , exprset[j]->src.max
3863 , exprset[j]->dst.min
3864 , exprset[j]->dst.max
3871 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3873 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3877 PG_FREE_IF_COPY(pgraster, 0);
3881 SET_VARSIZE(pgrtn, pgrtn->
size);
3882 PG_RETURN_POINTER(pgrtn);
3884 pixeltypetext = (text *) DatumGetPointer(tupv);
3885 if (NULL == pixeltypetext) {
3886 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3890 PG_FREE_IF_COPY(pgraster, 0);
3894 SET_VARSIZE(pgrtn, pgrtn->
size);
3895 PG_RETURN_POINTER(pgrtn);
3897 pixeltype = text_to_cstring(pixeltypetext);
3902 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3908 nodataval = DatumGetFloat8(tupv);
3917 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster",
nband);
3918 for (k = 0; k < j; k++) pfree(exprset[k]);
3923 PG_FREE_IF_COPY(pgraster, 0);
3927 SET_VARSIZE(pgrtn, pgrtn->
size);
3928 PG_RETURN_POINTER(pgrtn);
3932 for (k = 0; k < j; k++) pfree(exprset[k]);
3936 PG_FREE_IF_COPY(pgraster, 0);
3938 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d",
nband);
3944 for (k = 0; k < j; k++) pfree(exprset[k]);
3949 PG_FREE_IF_COPY(pgraster, 0);
3951 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band",
nband);
3959 for (k = 0; k < j; k++) pfree(exprset[k]);
3965 PG_FREE_IF_COPY(pgraster, 0);
3971 SET_VARSIZE(pgrtn, pgrtn->
size);
3972 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