3506 uint32_t numBands = 0;
3526 HeapTupleHeader tup;
3531 text *exprtext = NULL;
3536 char *pixeltype = NULL;
3537 text *pixeltypetext = NULL;
3539 double nodataval = 0;
3540 bool hasnodata =
FALSE;
3542 char **comma_set = NULL;
3543 uint32_t comma_n = 0;
3544 char **colon_set = NULL;
3545 uint32_t colon_n = 0;
3546 char **dash_set = NULL;
3547 uint32_t dash_n = 0;
3552 if (PG_ARGISNULL(0))
3554 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3559 PG_FREE_IF_COPY(pgraster, 0);
3560 elog(ERROR,
"RASTER_reclass: Could not deserialize raster");
3564 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3568 array = PG_GETARG_ARRAYTYPE_P(1);
3569 etype = ARR_ELEMTYPE(array);
3570 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3572 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3576 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3580 PG_FREE_IF_COPY(pgraster, 0);
3584 SET_VARSIZE(pgrtn, pgrtn->
size);
3585 PG_RETURN_POINTER(pgrtn);
3593 for (i = 0; i < n; i++) {
3594 if (nulls[i])
continue;
3597 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3599 elog(NOTICE,
"Invalid argument for reclassargset. Returning original raster");
3603 PG_FREE_IF_COPY(pgraster, 0);
3607 SET_VARSIZE(pgrtn, pgrtn->
size);
3608 PG_RETURN_POINTER(pgrtn);
3612 tupv = GetAttributeByName(tup,
"nband", &isnull);
3614 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of nband 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);
3625 nband = DatumGetInt32(tupv);
3629 if (nband < 1 || nband > numBands) {
3630 elog(NOTICE,
"Invalid argument for reclassargset. Invalid band index (must use 1-based) 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);
3643 tupv = GetAttributeByName(tup,
"reclassexpr", &isnull);
3645 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3649 PG_FREE_IF_COPY(pgraster, 0);
3653 SET_VARSIZE(pgrtn, pgrtn->
size);
3654 PG_RETURN_POINTER(pgrtn);
3656 exprtext = (text *) DatumGetPointer(tupv);
3657 if (NULL == exprtext) {
3658 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3662 PG_FREE_IF_COPY(pgraster, 0);
3666 SET_VARSIZE(pgrtn, pgrtn->
size);
3667 PG_RETURN_POINTER(pgrtn);
3678 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3682 PG_FREE_IF_COPY(pgraster, 0);
3686 SET_VARSIZE(pgrtn, pgrtn->
size);
3687 PG_RETURN_POINTER(pgrtn);
3694 for (a = 0, j = 0; a < comma_n; a++) {
3700 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3701 for (k = 0; k < j; k++) pfree(exprset[k]);
3706 PG_FREE_IF_COPY(pgraster, 0);
3710 SET_VARSIZE(pgrtn, pgrtn->
size);
3711 PG_RETURN_POINTER(pgrtn);
3717 for (b = 0; b < colon_n; b++) {
3722 if (dash_n < 1 || dash_n > 3) {
3723 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3724 for (k = 0; k < j; k++) pfree(exprset[k]);
3729 PG_FREE_IF_COPY(pgraster, 0);
3733 SET_VARSIZE(pgrtn, pgrtn->
size);
3734 PG_RETURN_POINTER(pgrtn);
3737 for (c = 0; c < dash_n; c++) {
3741 strlen(dash_set[c]) == 1 && (
3742 strchr(dash_set[c],
'(') != NULL ||
3743 strchr(dash_set[c],
'[') != NULL ||
3744 strchr(dash_set[c],
')') != NULL ||
3745 strchr(dash_set[c],
']') != NULL
3749 junk = palloc(
sizeof(
char) * (strlen(dash_set[c + 1]) + 2));
3751 for (k = 0; k <= j; k++) pfree(exprset[k]);
3754 PG_FREE_IF_COPY(pgraster, 0);
3756 elog(ERROR,
"RASTER_reclass: Could not allocate memory");
3760 sprintf(junk,
"%s%s", dash_set[c], dash_set[c + 1]);
3762 dash_set[c] = repalloc(dash_set[c],
sizeof(
char) * (strlen(junk) + 1));
3763 strcpy(dash_set[c], junk);
3767 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3768 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) *
sizeof(
char));
3769 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3773 pfree(dash_set[dash_n]);
3774 dash_set = repalloc(dash_set,
sizeof(
char *) * dash_n);
3778 if (c < 1 && dash_n > 2) {
3779 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3780 for (k = 0; k < j; k++) pfree(exprset[k]);
3785 PG_FREE_IF_COPY(pgraster, 0);
3789 SET_VARSIZE(pgrtn, pgrtn->
size);
3790 PG_RETURN_POINTER(pgrtn);
3801 strchr(dash_set[c],
')') != NULL ||
3802 strchr(dash_set[c],
']') != NULL
3807 else if (strchr(dash_set[c],
'(') != NULL){
3817 strrchr(dash_set[c],
'(') != NULL ||
3818 strrchr(dash_set[c],
'[') != NULL
3823 else if (strrchr(dash_set[c],
']') != NULL) {
3831 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3839 val = strtod(dash_set[c], &junk);
3840 if (errno != 0 || dash_set[c] == junk) {
3841 elog(NOTICE,
"Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3842 for (k = 0; k < j; k++) pfree(exprset[k]);
3847 PG_FREE_IF_COPY(pgraster, 0);
3851 SET_VARSIZE(pgrtn, pgrtn->
size);
3852 PG_RETURN_POINTER(pgrtn);
3858 junk = strstr(colon_set[b], dash_set[c]);
3863 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3864 b, c, colon_set[b], dash_set[c], junk
3867 if (junk != colon_set[b]) {
3869 if (*(junk - 1) ==
'-') {
3872 ((junk - 1) == colon_set[b]) ||
3873 (*(junk - 2) ==
'-') ||
3874 (*(junk - 2) ==
'[') ||
3875 (*(junk - 2) ==
'(')
3895 exprset[j]->
src.
min = val;
3901 exprset[j]->
src.
max = val;
3911 exprset[j]->
dst.
min = val;
3914 exprset[j]->
dst.
max = val;
3922 , exprset[j]->src.min
3923 , exprset[j]->src.max
3924 , exprset[j]->dst.min
3925 , exprset[j]->dst.max
3932 tupv = GetAttributeByName(tup,
"pixeltype", &isnull);
3934 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3938 PG_FREE_IF_COPY(pgraster, 0);
3942 SET_VARSIZE(pgrtn, pgrtn->
size);
3943 PG_RETURN_POINTER(pgrtn);
3945 pixeltypetext = (text *) DatumGetPointer(tupv);
3946 if (NULL == pixeltypetext) {
3947 elog(NOTICE,
"Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3951 PG_FREE_IF_COPY(pgraster, 0);
3955 SET_VARSIZE(pgrtn, pgrtn->
size);
3956 PG_RETURN_POINTER(pgrtn);
3963 tupv = GetAttributeByName(tup,
"nodataval", &isnull);
3969 nodataval = DatumGetFloat8(tupv);
3978 elog(NOTICE,
"Could not find raster band of index %d. Returning original raster",
nband);
3979 for (k = 0; k < j; k++) pfree(exprset[k]);
3984 PG_FREE_IF_COPY(pgraster, 0);
3988 SET_VARSIZE(pgrtn, pgrtn->
size);
3989 PG_RETURN_POINTER(pgrtn);
3993 for (k = 0; k < j; k++) pfree(exprset[k]);
3997 PG_FREE_IF_COPY(pgraster, 0);
3999 elog(ERROR,
"RASTER_reclass: Could not reclassify raster band of index %d",
nband);
4005 for (k = 0; k < j; k++) pfree(exprset[k]);
4010 PG_FREE_IF_COPY(pgraster, 0);
4012 elog(ERROR,
"RASTER_reclass: Could not replace raster band of index %d with reclassified band",
nband);
4020 for (k = 0; k < j; k++) pfree(exprset[k]);
4026 PG_FREE_IF_COPY(pgraster, 0);
4032 SET_VARSIZE(pgrtn, pgrtn->
size);
4033 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 * text_to_cstring(const text *textptr)
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