3515 HeapTupleHeader tup;
3520 text *exprtext = NULL;
3525 char *pixeltype = NULL;
3526 text *pixeltypetext = NULL;
3528 double nodataval = 0;
3529 bool hasnodata =
FALSE;
3531 char **comma_set = NULL;
3533 char **colon_set = NULL;
3535 char **dash_set = NULL;
3541 if (PG_ARGISNULL(0))
3543 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3548 PG_FREE_IF_COPY(pgraster, 0);
3549 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3553 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3557 array = PG_GETARG_ARRAYTYPE_P(1);
3558 etype = ARR_ELEMTYPE(array);
3559 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3561 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
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);
3582 for (i = 0; i < n; i++) {
3583 if (nulls[i])
continue;
3586 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3588 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3592 PG_FREE_IF_COPY(pgraster, 0);
3596 SET_VARSIZE(pgrtn, pgrtn->
size);
3597 PG_RETURN_POINTER(pgrtn);
3601 tupv = GetAttributeByName(tup,
"nband", &isnull);
3603 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3607 PG_FREE_IF_COPY(pgraster, 0);
3611 SET_VARSIZE(pgrtn, pgrtn->
size);
3612 PG_RETURN_POINTER(pgrtn);
3614 nband = DatumGetInt32(tupv);
3618 if (nband < 1 || nband > numBands) {
3619 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3623 PG_FREE_IF_COPY(pgraster, 0);
3627 SET_VARSIZE(pgrtn, pgrtn->
size);
3628 PG_RETURN_POINTER(pgrtn);
3632 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3634 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3638 PG_FREE_IF_COPY(pgraster, 0);
3642 SET_VARSIZE(pgrtn, pgrtn->
size);
3643 PG_RETURN_POINTER(pgrtn);
3645 exprtext = (text *) DatumGetPointer(tupv);
3646 if (NULL == exprtext) {
3647 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3651 PG_FREE_IF_COPY(pgraster, 0);
3655 SET_VARSIZE(pgrtn, pgrtn->
size);
3656 PG_RETURN_POINTER(pgrtn);
3658 expr = text_to_cstring(exprtext);
3667 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3671 PG_FREE_IF_COPY(pgraster, 0);
3675 SET_VARSIZE(pgrtn, pgrtn->
size);
3676 PG_RETURN_POINTER(pgrtn);
3683 for (a = 0, j = 0; a < comma_n; a++) {
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);
3706 for (b = 0; b < colon_n; b++) {
3711 if (dash_n < 1 || dash_n > 3) {
3712 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3713 for (k = 0; k < j; k++) pfree(exprset[k]);
3718 PG_FREE_IF_COPY(pgraster, 0);
3722 SET_VARSIZE(pgrtn, pgrtn->
size);
3723 PG_RETURN_POINTER(pgrtn);
3726 for (c = 0; c < dash_n; c++) {
3730 strlen(dash_set[c]) == 1 && (
3731 strchr(dash_set[c],
'(') != NULL ||
3732 strchr(dash_set[c],
'[') != NULL ||
3733 strchr(dash_set[c],
')') != NULL ||
3734 strchr(dash_set[c],
']') != NULL
3737 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3739 for (k = 0; k <= j; k++) pfree(exprset[k]);
3742 PG_FREE_IF_COPY(pgraster, 0);
3744 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3748 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3750 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3751 strcpy(dash_set[c], junk);
3755 for (k = 1; k < dash_n; k++) {
3756 dash_set[k - 1] = repalloc(dash_set[k - 1], (strlen(dash_set[k]) + 1) *
sizeof(
char));
3757 strcpy(dash_set[k - 1], dash_set[k]);
3761 pfree(dash_set[dash_n]);
3762 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3766 if (c < 1 && dash_n > 2) {
3767 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3768 for (k = 0; k < j; k++) pfree(exprset[k]);
3773 PG_FREE_IF_COPY(pgraster, 0);
3777 SET_VARSIZE(pgrtn, pgrtn->
size);
3778 PG_RETURN_POINTER(pgrtn);
3789 strchr(dash_set[c],
')') != NULL ||
3790 strchr(dash_set[c],
']') != NULL
3795 else if (strchr(dash_set[c],
'(') != NULL){
3805 strrchr(dash_set[c],
'(') != NULL ||
3806 strrchr(dash_set[c],
'[') != NULL
3811 else if (strrchr(dash_set[c],
']') != NULL) {
3819 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3827 val = strtod(dash_set[c], &junk);
3828 if (errno != 0 || dash_set[c] == junk) {
3829 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3830 for (k = 0; k < j; k++) pfree(exprset[k]);
3835 PG_FREE_IF_COPY(pgraster, 0);
3839 SET_VARSIZE(pgrtn, pgrtn->
size);
3840 PG_RETURN_POINTER(pgrtn);
3846 junk = strstr(colon_set[b], dash_set[c]);
3851 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3852 b, c, colon_set[b], dash_set[c], junk
3855 if (junk != colon_set[b]) {
3857 if (*(junk - 1) ==
'-') {
3860 ((junk - 1) == colon_set[b]) ||
3861 (*(junk - 2) ==
'-') ||
3862 (*(junk - 2) ==
'[') ||
3863 (*(junk - 2) ==
'(')
3883 exprset[j]->
src.
min = val;
3889 exprset[j]->
src.
max = val;
3899 exprset[j]->
dst.
min = val;
3902 exprset[j]->
dst.
max = val;
3910 , exprset[j]->src.min
3920 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3922 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3926 PG_FREE_IF_COPY(pgraster, 0);
3930 SET_VARSIZE(pgrtn, pgrtn->
size);
3931 PG_RETURN_POINTER(pgrtn);
3933 pixeltypetext = (text *) DatumGetPointer(tupv);
3934 if (NULL == pixeltypetext) {
3935 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3939 PG_FREE_IF_COPY(pgraster, 0);
3943 SET_VARSIZE(pgrtn, pgrtn->
size);
3944 PG_RETURN_POINTER(pgrtn);
3946 pixeltype = text_to_cstring(pixeltypetext);
3951 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3957 nodataval = DatumGetFloat8(tupv);
3966 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster", nband);
3967 for (k = 0; k < j; k++) pfree(exprset[k]);
3972 PG_FREE_IF_COPY(pgraster, 0);
3976 SET_VARSIZE(pgrtn, pgrtn->
size);
3977 PG_RETURN_POINTER(pgrtn);
3979 newband =
rt_band_reclass(band, pixtype, hasnodata, nodataval, exprset, j);
3981 for (k = 0; k < j; k++) pfree(exprset[k]);
3985 PG_FREE_IF_COPY(pgraster, 0);
3987 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d", nband);
3993 for (k = 0; k < j; k++) pfree(exprset[k]);
3998 PG_FREE_IF_COPY(pgraster, 0);
4000 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band", nband);
4008 for (k = 0; k < j; k++) pfree(exprset[k]);
4014 PG_FREE_IF_COPY(pgraster, 0);
4020 SET_VARSIZE(pgrtn, pgrtn->
size);
4021 PG_RETURN_POINTER(pgrtn);
char ** rtpg_strsplit(const char *str, const char *delimiter, int *n)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_raster_get_num_bands(rt_raster raster)
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
struct rt_reclassexpr_t::rt_reclassrange dst
void rt_band_destroy(rt_band band)
Destroy a raster band.
#define POSTGIS_RT_DEBUGF(level, msg,...)
struct rt_reclassexpr_t::rt_reclassrange src
rt_pixtype rt_pixtype_index_from_name(const char *pixname)
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
char * rtpg_chartrim(const char *input, char *remove)
char * rtpg_removespaces(char *str)
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.
#define POSTGIS_RT_DEBUG(level, msg)
char * rtpg_strrstr(const char *s1, const char *s2)
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.