3462 HeapTupleHeader tup;
3467 text *exprtext = NULL;
3472 char *pixeltype = NULL;
3473 text *pixeltypetext = NULL;
3475 double nodataval = 0;
3476 bool hasnodata =
FALSE;
3478 char **comma_set = NULL;
3480 char **colon_set = NULL;
3482 char **dash_set = NULL;
3488 if (PG_ARGISNULL(0))
3490 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3495 PG_FREE_IF_COPY(pgraster, 0);
3496 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3500 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3504 array = PG_GETARG_ARRAYTYPE_P(1);
3505 etype = ARR_ELEMTYPE(array);
3506 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3508 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3512 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3516 PG_FREE_IF_COPY(pgraster, 0);
3520 SET_VARSIZE(pgrtn, pgrtn->
size);
3521 PG_RETURN_POINTER(pgrtn);
3529 for (i = 0; i < n; i++) {
3530 if (nulls[i])
continue;
3533 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3535 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3539 PG_FREE_IF_COPY(pgraster, 0);
3543 SET_VARSIZE(pgrtn, pgrtn->
size);
3544 PG_RETURN_POINTER(pgrtn);
3548 tupv = GetAttributeByName(tup,
"nband", &isnull);
3550 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3554 PG_FREE_IF_COPY(pgraster, 0);
3558 SET_VARSIZE(pgrtn, pgrtn->
size);
3559 PG_RETURN_POINTER(pgrtn);
3561 nband = DatumGetInt32(tupv);
3565 if (nband < 1 || nband > numBands) {
3566 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3570 PG_FREE_IF_COPY(pgraster, 0);
3574 SET_VARSIZE(pgrtn, pgrtn->
size);
3575 PG_RETURN_POINTER(pgrtn);
3579 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3581 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3585 PG_FREE_IF_COPY(pgraster, 0);
3589 SET_VARSIZE(pgrtn, pgrtn->
size);
3590 PG_RETURN_POINTER(pgrtn);
3592 exprtext = (text *) DatumGetPointer(tupv);
3593 if (NULL == exprtext) {
3594 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3598 PG_FREE_IF_COPY(pgraster, 0);
3602 SET_VARSIZE(pgrtn, pgrtn->
size);
3603 PG_RETURN_POINTER(pgrtn);
3614 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3618 PG_FREE_IF_COPY(pgraster, 0);
3622 SET_VARSIZE(pgrtn, pgrtn->
size);
3623 PG_RETURN_POINTER(pgrtn);
3630 for (a = 0, j = 0; a < comma_n; a++) {
3636 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3637 for (k = 0; k < j; k++) pfree(exprset[k]);
3642 PG_FREE_IF_COPY(pgraster, 0);
3646 SET_VARSIZE(pgrtn, pgrtn->
size);
3647 PG_RETURN_POINTER(pgrtn);
3653 for (b = 0; b < colon_n; b++) {
3658 if (dash_n < 1 || dash_n > 3) {
3659 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3660 for (k = 0; k < j; k++) pfree(exprset[k]);
3665 PG_FREE_IF_COPY(pgraster, 0);
3669 SET_VARSIZE(pgrtn, pgrtn->
size);
3670 PG_RETURN_POINTER(pgrtn);
3673 for (c = 0; c < dash_n; c++) {
3677 strlen(dash_set[c]) == 1 && (
3678 strchr(dash_set[c],
'(') != NULL ||
3679 strchr(dash_set[c],
'[') != NULL ||
3680 strchr(dash_set[c],
')') != NULL ||
3681 strchr(dash_set[c],
']') != NULL
3685 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3687 for (k = 0; k <= j; k++) pfree(exprset[k]);
3690 PG_FREE_IF_COPY(pgraster, 0);
3692 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3696 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3698 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3699 strcpy(dash_set[c], junk);
3703 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3704 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3705 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3709 pfree(dash_set[dash_n]);
3710 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3714 if (c < 1 && dash_n > 2) {
3715 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3716 for (k = 0; k < j; k++) pfree(exprset[k]);
3721 PG_FREE_IF_COPY(pgraster, 0);
3725 SET_VARSIZE(pgrtn, pgrtn->
size);
3726 PG_RETURN_POINTER(pgrtn);
3737 strchr(dash_set[c],
')') != NULL ||
3738 strchr(dash_set[c],
']') != NULL
3743 else if (strchr(dash_set[c],
'(') != NULL){
3753 strrchr(dash_set[c],
'(') != NULL ||
3754 strrchr(dash_set[c],
'[') != NULL
3759 else if (strrchr(dash_set[c],
']') != NULL) {
3767 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3775 val = strtod(dash_set[c], &junk);
3776 if (errno != 0 || dash_set[c] == junk) {
3777 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3778 for (k = 0; k < j; k++) pfree(exprset[k]);
3783 PG_FREE_IF_COPY(pgraster, 0);
3787 SET_VARSIZE(pgrtn, pgrtn->
size);
3788 PG_RETURN_POINTER(pgrtn);
3794 junk = strstr(colon_set[b], dash_set[c]);
3799 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3800 b, c, colon_set[b], dash_set[c], junk
3803 if (junk != colon_set[b]) {
3805 if (*(junk - 1) ==
'-') {
3808 ((junk - 1) == colon_set[b]) ||
3809 (*(junk - 2) ==
'-') ||
3810 (*(junk - 2) ==
'[') ||
3811 (*(junk - 2) ==
'(')
3831 exprset[j]->
src.
min = val;
3837 exprset[j]->
src.
max = val;
3847 exprset[j]->
dst.
min = val;
3850 exprset[j]->
dst.
max = val;
3858 , exprset[j]->src.min
3868 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3870 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3874 PG_FREE_IF_COPY(pgraster, 0);
3878 SET_VARSIZE(pgrtn, pgrtn->
size);
3879 PG_RETURN_POINTER(pgrtn);
3881 pixeltypetext = (text *) DatumGetPointer(tupv);
3882 if (NULL == pixeltypetext) {
3883 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3887 PG_FREE_IF_COPY(pgraster, 0);
3891 SET_VARSIZE(pgrtn, pgrtn->
size);
3892 PG_RETURN_POINTER(pgrtn);
3899 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3905 nodataval = DatumGetFloat8(tupv);
3914 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster", nband);
3915 for (k = 0; k < j; k++) pfree(exprset[k]);
3920 PG_FREE_IF_COPY(pgraster, 0);
3924 SET_VARSIZE(pgrtn, pgrtn->
size);
3925 PG_RETURN_POINTER(pgrtn);
3927 newband =
rt_band_reclass(band, pixtype, hasnodata, nodataval, exprset, j);
3929 for (k = 0; k < j; k++) pfree(exprset[k]);
3933 PG_FREE_IF_COPY(pgraster, 0);
3935 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d", nband);
3941 for (k = 0; k < j; k++) pfree(exprset[k]);
3946 PG_FREE_IF_COPY(pgraster, 0);
3948 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band", nband);
3956 for (k = 0; k < j; k++) pfree(exprset[k]);
3962 PG_FREE_IF_COPY(pgraster, 0);
3968 SET_VARSIZE(pgrtn, pgrtn->
size);
3969 PG_RETURN_POINTER(pgrtn);
char * text_to_cstring(const text *textptr)
char ** rtpg_strsplit(const char *str, const char *delimiter, uint32_t *n)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
uint16_t 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.