3466 {
3472 uint32_t numBands = 0;
3473
3474 ArrayType *array;
3475 Oid etype;
3476 Datum *e;
3477 bool *nulls;
3478 int16 typlen;
3479 bool typbyval;
3480 char typalign;
3481 int n = 0;
3482
3483 int i = 0;
3484 int j = 0;
3485 int k = 0;
3486
3487 uint32_t a = 0;
3488 uint32_t b = 0;
3489 uint32_t c = 0;
3490
3492 HeapTupleHeader tup;
3493 bool isnull;
3494 Datum tupv;
3496 char *expr = NULL;
3497 text *exprtext = NULL;
3498 double val = 0;
3499 char *junk = NULL;
3500 int inc_val = 0;
3501 int exc_val = 0;
3502 char *pixeltype = NULL;
3503 text *pixeltypetext = NULL;
3505 double nodataval = 0;
3506 bool hasnodata =
FALSE;
3507
3508 char **comma_set = NULL;
3509 uint32_t comma_n = 0;
3510 char **colon_set = NULL;
3511 uint32_t colon_n = 0;
3512 char **dash_set = NULL;
3513 uint32_t dash_n = 0;
3514
3516
3517
3518 if (PG_ARGISNULL(0))
3519 PG_RETURN_NULL();
3520 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3521
3522
3524 if (!raster) {
3525 PG_FREE_IF_COPY(pgraster, 0);
3526 elog(ERROR, "RASTER_reclass: Could not deserialize raster");
3527 PG_RETURN_NULL();
3528 }
3530 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3531
3532
3534 array = PG_GETARG_ARRAYTYPE_P(1);
3535 etype = ARR_ELEMTYPE(array);
3536 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3537
3538 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3539 &nulls, &n);
3540
3541 if (!n) {
3542 elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
3543
3546 PG_FREE_IF_COPY(pgraster, 0);
3547 if (!pgrtn)
3548 PG_RETURN_NULL();
3549
3550 SET_VARSIZE(pgrtn, pgrtn->
size);
3551 PG_RETURN_POINTER(pgrtn);
3552 }
3553
3554
3555
3556
3557
3558
3559 for (i = 0; i < n; i++) {
3560 if (nulls[i]) continue;
3561
3562
3563 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3564 if (NULL == tup) {
3565 elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
3566
3569 PG_FREE_IF_COPY(pgraster, 0);
3570 if (!pgrtn)
3571 PG_RETURN_NULL();
3572
3573 SET_VARSIZE(pgrtn, pgrtn->
size);
3574 PG_RETURN_POINTER(pgrtn);
3575 }
3576
3577
3578 tupv = GetAttributeByName(tup, "nband", &isnull);
3579 if (isnull) {
3580 elog(NOTICE, "Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3581
3584 PG_FREE_IF_COPY(pgraster, 0);
3585 if (!pgrtn)
3586 PG_RETURN_NULL();
3587
3588 SET_VARSIZE(pgrtn, pgrtn->
size);
3589 PG_RETURN_POINTER(pgrtn);
3590 }
3591 nband = DatumGetInt32(tupv);
3593
3594
3595 if (nband < 1 || nband > numBands) {
3596 elog(NOTICE, "Invalid argument for reclassargset. Invalid band index (must use 1-based) for reclassarg of index %d . Returning original raster", i);
3597
3600 PG_FREE_IF_COPY(pgraster, 0);
3601 if (!pgrtn)
3602 PG_RETURN_NULL();
3603
3604 SET_VARSIZE(pgrtn, pgrtn->
size);
3605 PG_RETURN_POINTER(pgrtn);
3606 }
3607
3608
3609 tupv = GetAttributeByName(tup, "reclassexpr", &isnull);
3610 if (isnull) {
3611 elog(NOTICE, "Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3612
3615 PG_FREE_IF_COPY(pgraster, 0);
3616 if (!pgrtn)
3617 PG_RETURN_NULL();
3618
3619 SET_VARSIZE(pgrtn, pgrtn->
size);
3620 PG_RETURN_POINTER(pgrtn);
3621 }
3622 exprtext = (text *) DatumGetPointer(tupv);
3623 if (NULL == exprtext) {
3624 elog(NOTICE, "Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3625
3628 PG_FREE_IF_COPY(pgraster, 0);
3629 if (!pgrtn)
3630 PG_RETURN_NULL();
3631
3632 SET_VARSIZE(pgrtn, pgrtn->
size);
3633 PG_RETURN_POINTER(pgrtn);
3634 }
3635 expr = text_to_cstring(exprtext);
3639
3640
3641
3643 if (comma_n < 1) {
3644 elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3645
3648 PG_FREE_IF_COPY(pgraster, 0);
3649 if (!pgrtn)
3650 PG_RETURN_NULL();
3651
3652 SET_VARSIZE(pgrtn, pgrtn->
size);
3653 PG_RETURN_POINTER(pgrtn);
3654 }
3655
3656
3659
3660 for (a = 0, j = 0; a < comma_n; a++) {
3662
3663
3665 if (colon_n != 2) {
3666 elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3667 for (k = 0; k < j; k++) pfree(exprset[k]);
3668 pfree(exprset);
3669
3672 PG_FREE_IF_COPY(pgraster, 0);
3673 if (!pgrtn)
3674 PG_RETURN_NULL();
3675
3676 SET_VARSIZE(pgrtn, pgrtn->
size);
3677 PG_RETURN_POINTER(pgrtn);
3678 }
3679
3680
3682
3683 for (b = 0; b < colon_n; b++) {
3685
3686
3688 if (dash_n < 1 || dash_n > 3) {
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]);
3691 pfree(exprset);
3692
3695 PG_FREE_IF_COPY(pgraster, 0);
3696 if (!pgrtn)
3697 PG_RETURN_NULL();
3698
3699 SET_VARSIZE(pgrtn, pgrtn->
size);
3700 PG_RETURN_POINTER(pgrtn);
3701 }
3702
3703 for (c = 0; c < dash_n; c++) {
3704
3705 if (
3706 c < 1 &&
3707 strlen(dash_set[c]) == 1 && (
3708 strchr(dash_set[c], '(') != NULL ||
3709 strchr(dash_set[c], '[') != NULL ||
3710 strchr(dash_set[c], ')') != NULL ||
3711 strchr(dash_set[c], ']') != NULL
3712 )
3713 ) {
3714 uint32_t dash_it;
3715 junk = palloc(sizeof(char) * (strlen(dash_set[c + 1]) + 2));
3716 if (NULL == junk) {
3717 for (k = 0; k <= j; k++) pfree(exprset[k]);
3718 pfree(exprset);
3720 PG_FREE_IF_COPY(pgraster, 0);
3721
3722 elog(ERROR, "RASTER_reclass: Could not allocate memory");
3723 PG_RETURN_NULL();
3724 }
3725
3726 sprintf(junk, "%s%s", dash_set[c], dash_set[c + 1]);
3727 c++;
3728 dash_set[c] = repalloc(dash_set[c], sizeof(char) * (strlen(junk) + 1));
3729 strcpy(dash_set[c], junk);
3730 pfree(junk);
3731
3732
3733 for (dash_it = 1; dash_it < dash_n; dash_it++) {
3734 dash_set[dash_it - 1] = repalloc(dash_set[dash_it - 1], (strlen(dash_set[dash_it]) + 1) * sizeof(char));
3735 strcpy(dash_set[dash_it - 1], dash_set[dash_it]);
3736 }
3737 dash_n--;
3738 c--;
3739 pfree(dash_set[dash_n]);
3740 dash_set = repalloc(dash_set, sizeof(char *) * dash_n);
3741 }
3742
3743
3744 if (c < 1 && dash_n > 2) {
3745 elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3746 for (k = 0; k < j; k++) pfree(exprset[k]);
3747 pfree(exprset);
3748
3751 PG_FREE_IF_COPY(pgraster, 0);
3752 if (!pgrtn)
3753 PG_RETURN_NULL();
3754
3755 SET_VARSIZE(pgrtn, pgrtn->
size);
3756 PG_RETURN_POINTER(pgrtn);
3757 }
3758
3759
3760 exc_val = 0;
3761 inc_val = 1;
3762
3763 if (dash_n != 1) {
3764
3765 if (c < 1) {
3766 if (
3767 strchr(dash_set[c], ')') != NULL ||
3768 strchr(dash_set[c], ']') != NULL
3769 ) {
3770 exc_val = 1;
3771 inc_val = 1;
3772 }
3773 else if (strchr(dash_set[c], '(') != NULL){
3774 inc_val = 0;
3775 }
3776 else {
3777 inc_val = 1;
3778 }
3779 }
3780
3781 else {
3782 if (
3783 strrchr(dash_set[c], '(') != NULL ||
3784 strrchr(dash_set[c], '[') != NULL
3785 ) {
3786 exc_val = 1;
3787 inc_val = 0;
3788 }
3789 else if (strrchr(dash_set[c], ']') != NULL) {
3790 inc_val = 1;
3791 }
3792 else {
3793 inc_val = 0;
3794 }
3795 }
3796 }
3797 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3798
3799
3802
3803
3804 errno = 0;
3805 val = strtod(dash_set[c], &junk);
3806 if (errno != 0 || dash_set[c] == junk) {
3807 elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3808 for (k = 0; k < j; k++) pfree(exprset[k]);
3809 pfree(exprset);
3810
3813 PG_FREE_IF_COPY(pgraster, 0);
3814 if (!pgrtn)
3815 PG_RETURN_NULL();
3816
3817 SET_VARSIZE(pgrtn, pgrtn->
size);
3818 PG_RETURN_POINTER(pgrtn);
3819 }
3821
3822
3823 if (c < 1)
3824 junk = strstr(colon_set[b], dash_set[c]);
3825 else
3828 4,
3829 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3830 b, c, colon_set[b], dash_set[c], junk
3831 );
3832
3833 if (junk != colon_set[b]) {
3834
3835 if (*(junk - 1) == '-') {
3836
3837 if (
3838 ((junk - 1) == colon_set[b]) ||
3839 (*(junk - 2) == '-') ||
3840 (*(junk - 2) == '[') ||
3841 (*(junk - 2) == '(')
3842 ) {
3843 val *= -1.;
3844 }
3845 }
3846 }
3848
3849
3850 if (b < 1) {
3851
3852 if (dash_n == 1) {
3856 }
3857
3858 else if (c < 1) {
3861 exprset[j]->
src.
min = val;
3862 }
3863
3864 else {
3867 exprset[j]->
src.
max = val;
3868 }
3869 }
3870
3871 else {
3872
3873 if (dash_n == 1)
3875
3876 else if (c < 1)
3877 exprset[j]->
dst.
min = val;
3878
3879 else
3880 exprset[j]->
dst.
max = val;
3881 }
3882 }
3883 pfree(dash_set);
3884 }
3885 pfree(colon_set);
3886
3888 , exprset[j]->src.min
3889 , exprset[j]->src.max
3890 , exprset[j]->dst.min
3891 , exprset[j]->dst.max
3892 );
3893 j++;
3894 }
3895 pfree(comma_set);
3896
3897
3898 tupv = GetAttributeByName(tup, "pixeltype", &isnull);
3899 if (isnull) {
3900 elog(NOTICE, "Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3901
3904 PG_FREE_IF_COPY(pgraster, 0);
3905 if (!pgrtn)
3906 PG_RETURN_NULL();
3907
3908 SET_VARSIZE(pgrtn, pgrtn->
size);
3909 PG_RETURN_POINTER(pgrtn);
3910 }
3911 pixeltypetext = (text *) DatumGetPointer(tupv);
3912 if (NULL == pixeltypetext) {
3913 elog(NOTICE, "Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3914
3917 PG_FREE_IF_COPY(pgraster, 0);
3918 if (!pgrtn)
3919 PG_RETURN_NULL();
3920
3921 SET_VARSIZE(pgrtn, pgrtn->
size);
3922 PG_RETURN_POINTER(pgrtn);
3923 }
3924 pixeltype = text_to_cstring(pixeltypetext);
3927
3928
3929 tupv = GetAttributeByName(tup, "nodataval", &isnull);
3930 if (isnull) {
3931 nodataval = 0;
3933 }
3934 else {
3935 nodataval = DatumGetFloat8(tupv);
3937 }
3940
3941
3943 if (!band) {
3944 elog(NOTICE, "Could not find raster band of index %d. Returning original raster", nband);
3945 for (k = 0; k < j; k++) pfree(exprset[k]);
3946 pfree(exprset);
3947
3950 PG_FREE_IF_COPY(pgraster, 0);
3951 if (!pgrtn)
3952 PG_RETURN_NULL();
3953
3954 SET_VARSIZE(pgrtn, pgrtn->
size);
3955 PG_RETURN_POINTER(pgrtn);
3956 }
3957 newband =
rt_band_reclass(band, pixtype, hasnodata, nodataval, exprset, j);
3958 if (!newband) {
3959 for (k = 0; k < j; k++) pfree(exprset[k]);
3960 pfree(exprset);
3961
3963 PG_FREE_IF_COPY(pgraster, 0);
3964
3965 elog(ERROR, "RASTER_reclass: Could not reclassify raster band of index %d", nband);
3966 PG_RETURN_NULL();
3967 }
3968
3969
3971 for (k = 0; k < j; k++) pfree(exprset[k]);
3972 pfree(exprset);
3973
3976 PG_FREE_IF_COPY(pgraster, 0);
3977
3978 elog(ERROR, "RASTER_reclass: Could not replace raster band of index %d with reclassified band", nband);
3979 PG_RETURN_NULL();
3980 }
3981
3982
3984
3985
3986 for (k = 0; k < j; k++) pfree(exprset[k]);
3987 pfree(exprset);
3988 }
3989
3992 PG_FREE_IF_COPY(pgraster, 0);
3993 if (!pgrtn)
3994 PG_RETURN_NULL();
3995
3997
3998 SET_VARSIZE(pgrtn, pgrtn->
size);
3999 PG_RETURN_POINTER(pgrtn);
4000}
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.
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)
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
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_strsplit(const char *str, const char *delimiter, uint32_t *n)
char * rtpg_removespaces(char *str)
char * rtpg_chartrim(const char *input, char *remove)
char * rtpg_strrstr(const char *s1, const char *s2)
#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