3500 {
3506 uint32_t numBands = 0;
3507
3508 ArrayType *array;
3509 Oid etype;
3510 Datum *e;
3511 bool *nulls;
3512 int16 typlen;
3513 bool typbyval;
3514 char typalign;
3515 int n = 0;
3516
3517 int i = 0;
3518 int j = 0;
3519 int k = 0;
3520
3521 uint32_t a = 0;
3522 uint32_t b = 0;
3523 uint32_t c = 0;
3524
3526 HeapTupleHeader tup;
3527 bool isnull;
3528 Datum tupv;
3530 char *expr = NULL;
3531 text *exprtext = NULL;
3532 double val = 0;
3533 char *junk = NULL;
3534 int inc_val = 0;
3535 int exc_val = 0;
3536 char *pixeltype = NULL;
3537 text *pixeltypetext = NULL;
3539 double nodataval = 0;
3540 bool hasnodata =
FALSE;
3541
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;
3548
3550
3551
3552 if (PG_ARGISNULL(0))
3553 PG_RETURN_NULL();
3554 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3555
3556
3558 if (!raster) {
3559 PG_FREE_IF_COPY(pgraster, 0);
3560 elog(ERROR, "RASTER_reclass: Could not deserialize raster");
3561 PG_RETURN_NULL();
3562 }
3564 POSTGIS_RT_DEBUGF(3,
"RASTER_reclass: %d possible bands to be reclassified", numBands);
3565
3566
3568 array = PG_GETARG_ARRAYTYPE_P(1);
3569 etype = ARR_ELEMTYPE(array);
3570 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3571
3572 deconstruct_array(array, etype, typlen, typbyval, typalign, &e,
3573 &nulls, &n);
3574
3575 if (!n) {
3576 elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
3577
3580 PG_FREE_IF_COPY(pgraster, 0);
3581 if (!pgrtn)
3582 PG_RETURN_NULL();
3583
3584 SET_VARSIZE(pgrtn, pgrtn->
size);
3585 PG_RETURN_POINTER(pgrtn);
3586 }
3587
3588
3589
3590
3591
3592
3593 for (i = 0; i < n; i++) {
3594 if (nulls[i]) continue;
3595
3596
3597 tup = (HeapTupleHeader) DatumGetPointer(e[i]);
3598 if (NULL == tup) {
3599 elog(NOTICE, "Invalid argument for reclassargset. Returning original raster");
3600
3603 PG_FREE_IF_COPY(pgraster, 0);
3604 if (!pgrtn)
3605 PG_RETURN_NULL();
3606
3607 SET_VARSIZE(pgrtn, pgrtn->
size);
3608 PG_RETURN_POINTER(pgrtn);
3609 }
3610
3611
3612 tupv = GetAttributeByName(tup, "nband", &isnull);
3613 if (isnull) {
3614 elog(NOTICE, "Invalid argument for reclassargset. Missing value of nband for reclassarg of index %d . Returning original raster", i);
3615
3618 PG_FREE_IF_COPY(pgraster, 0);
3619 if (!pgrtn)
3620 PG_RETURN_NULL();
3621
3622 SET_VARSIZE(pgrtn, pgrtn->
size);
3623 PG_RETURN_POINTER(pgrtn);
3624 }
3625 nband = DatumGetInt32(tupv);
3627
3628
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);
3631
3634 PG_FREE_IF_COPY(pgraster, 0);
3635 if (!pgrtn)
3636 PG_RETURN_NULL();
3637
3638 SET_VARSIZE(pgrtn, pgrtn->
size);
3639 PG_RETURN_POINTER(pgrtn);
3640 }
3641
3642
3643 tupv = GetAttributeByName(tup, "reclassexpr", &isnull);
3644 if (isnull) {
3645 elog(NOTICE, "Invalid argument for reclassargset. Missing value of reclassexpr for reclassarg of index %d . Returning original raster", i);
3646
3649 PG_FREE_IF_COPY(pgraster, 0);
3650 if (!pgrtn)
3651 PG_RETURN_NULL();
3652
3653 SET_VARSIZE(pgrtn, pgrtn->
size);
3654 PG_RETURN_POINTER(pgrtn);
3655 }
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);
3659
3662 PG_FREE_IF_COPY(pgraster, 0);
3663 if (!pgrtn)
3664 PG_RETURN_NULL();
3665
3666 SET_VARSIZE(pgrtn, pgrtn->
size);
3667 PG_RETURN_POINTER(pgrtn);
3668 }
3673
3674
3675
3677 if (comma_n < 1) {
3678 elog(NOTICE, "Invalid argument for reclassargset. Invalid expression of reclassexpr for reclassarg of index %d . Returning original raster", i);
3679
3682 PG_FREE_IF_COPY(pgraster, 0);
3683 if (!pgrtn)
3684 PG_RETURN_NULL();
3685
3686 SET_VARSIZE(pgrtn, pgrtn->
size);
3687 PG_RETURN_POINTER(pgrtn);
3688 }
3689
3690
3693
3694 for (a = 0, j = 0; a < comma_n; a++) {
3696
3697
3699 if (colon_n != 2) {
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]);
3702 pfree(exprset);
3703
3706 PG_FREE_IF_COPY(pgraster, 0);
3707 if (!pgrtn)
3708 PG_RETURN_NULL();
3709
3710 SET_VARSIZE(pgrtn, pgrtn->
size);
3711 PG_RETURN_POINTER(pgrtn);
3712 }
3713
3714
3716
3717 for (b = 0; b < colon_n; b++) {
3719
3720
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]);
3725 pfree(exprset);
3726
3729 PG_FREE_IF_COPY(pgraster, 0);
3730 if (!pgrtn)
3731 PG_RETURN_NULL();
3732
3733 SET_VARSIZE(pgrtn, pgrtn->
size);
3734 PG_RETURN_POINTER(pgrtn);
3735 }
3736
3737 for (c = 0; c < dash_n; c++) {
3738
3739 if (
3740 c < 1 &&
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
3746 )
3747 ) {
3748 uint32_t dash_it;
3749 junk = palloc(sizeof(char) * (strlen(dash_set[c + 1]) + 2));
3750 if (NULL == junk) {
3751 for (k = 0; k <= j; k++) pfree(exprset[k]);
3752 pfree(exprset);
3754 PG_FREE_IF_COPY(pgraster, 0);
3755
3756 elog(ERROR, "RASTER_reclass: Could not allocate memory");
3757 PG_RETURN_NULL();
3758 }
3759
3760 sprintf(junk, "%s%s", dash_set[c], dash_set[c + 1]);
3761 c++;
3762 dash_set[c] = repalloc(dash_set[c], sizeof(char) * (strlen(junk) + 1));
3763 strcpy(dash_set[c], junk);
3764 pfree(junk);
3765
3766
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]);
3770 }
3771 dash_n--;
3772 c--;
3773 pfree(dash_set[dash_n]);
3774 dash_set = repalloc(dash_set, sizeof(char *) * dash_n);
3775 }
3776
3777
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]);
3781 pfree(exprset);
3782
3785 PG_FREE_IF_COPY(pgraster, 0);
3786 if (!pgrtn)
3787 PG_RETURN_NULL();
3788
3789 SET_VARSIZE(pgrtn, pgrtn->
size);
3790 PG_RETURN_POINTER(pgrtn);
3791 }
3792
3793
3794 exc_val = 0;
3795 inc_val = 1;
3796
3797 if (dash_n != 1) {
3798
3799 if (c < 1) {
3800 if (
3801 strchr(dash_set[c], ')') != NULL ||
3802 strchr(dash_set[c], ']') != NULL
3803 ) {
3804 exc_val = 1;
3805 inc_val = 1;
3806 }
3807 else if (strchr(dash_set[c], '(') != NULL){
3808 inc_val = 0;
3809 }
3810 else {
3811 inc_val = 1;
3812 }
3813 }
3814
3815 else {
3816 if (
3817 strrchr(dash_set[c], '(') != NULL ||
3818 strrchr(dash_set[c], '[') != NULL
3819 ) {
3820 exc_val = 1;
3821 inc_val = 0;
3822 }
3823 else if (strrchr(dash_set[c], ']') != NULL) {
3824 inc_val = 1;
3825 }
3826 else {
3827 inc_val = 0;
3828 }
3829 }
3830 }
3831 POSTGIS_RT_DEBUGF(4,
"RASTER_reclass: exc_val %d inc_val %d", exc_val, inc_val);
3832
3833
3836
3837
3838 errno = 0;
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]);
3843 pfree(exprset);
3844
3847 PG_FREE_IF_COPY(pgraster, 0);
3848 if (!pgrtn)
3849 PG_RETURN_NULL();
3850
3851 SET_VARSIZE(pgrtn, pgrtn->
size);
3852 PG_RETURN_POINTER(pgrtn);
3853 }
3855
3856
3857 if (c < 1)
3858 junk = strstr(colon_set[b], dash_set[c]);
3859 else
3862 4,
3863 "(colon_set[%d], dash_set[%d], junk) = (%s, %s, %s)",
3864 b, c, colon_set[b], dash_set[c], junk
3865 );
3866
3867 if (junk != colon_set[b]) {
3868
3869 if (*(junk - 1) == '-') {
3870
3871 if (
3872 ((junk - 1) == colon_set[b]) ||
3873 (*(junk - 2) == '-') ||
3874 (*(junk - 2) == '[') ||
3875 (*(junk - 2) == '(')
3876 ) {
3877 val *= -1.;
3878 }
3879 }
3880 }
3882
3883
3884 if (b < 1) {
3885
3886 if (dash_n == 1) {
3890 }
3891
3892 else if (c < 1) {
3895 exprset[j]->
src.
min = val;
3896 }
3897
3898 else {
3901 exprset[j]->
src.
max = val;
3902 }
3903 }
3904
3905 else {
3906
3907 if (dash_n == 1)
3909
3910 else if (c < 1)
3911 exprset[j]->
dst.
min = val;
3912
3913 else
3914 exprset[j]->
dst.
max = val;
3915 }
3916 }
3917 pfree(dash_set);
3918 }
3919 pfree(colon_set);
3920
3922 , exprset[j]->src.min
3923 , exprset[j]->src.max
3924 , exprset[j]->dst.min
3925 , exprset[j]->dst.max
3926 );
3927 j++;
3928 }
3929 pfree(comma_set);
3930
3931
3932 tupv = GetAttributeByName(tup, "pixeltype", &isnull);
3933 if (isnull) {
3934 elog(NOTICE, "Invalid argument for reclassargset. Missing value of pixeltype for reclassarg of index %d . Returning original raster", i);
3935
3938 PG_FREE_IF_COPY(pgraster, 0);
3939 if (!pgrtn)
3940 PG_RETURN_NULL();
3941
3942 SET_VARSIZE(pgrtn, pgrtn->
size);
3943 PG_RETURN_POINTER(pgrtn);
3944 }
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);
3948
3951 PG_FREE_IF_COPY(pgraster, 0);
3952 if (!pgrtn)
3953 PG_RETURN_NULL();
3954
3955 SET_VARSIZE(pgrtn, pgrtn->
size);
3956 PG_RETURN_POINTER(pgrtn);
3957 }
3961
3962
3963 tupv = GetAttributeByName(tup, "nodataval", &isnull);
3964 if (isnull) {
3965 nodataval = 0;
3967 }
3968 else {
3969 nodataval = DatumGetFloat8(tupv);
3971 }
3974
3975
3977 if (!band) {
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]);
3980 pfree(exprset);
3981
3984 PG_FREE_IF_COPY(pgraster, 0);
3985 if (!pgrtn)
3986 PG_RETURN_NULL();
3987
3988 SET_VARSIZE(pgrtn, pgrtn->
size);
3989 PG_RETURN_POINTER(pgrtn);
3990 }
3991 newband =
rt_band_reclass(band, pixtype, hasnodata, nodataval, exprset, j);
3992 if (!newband) {
3993 for (k = 0; k < j; k++) pfree(exprset[k]);
3994 pfree(exprset);
3995
3997 PG_FREE_IF_COPY(pgraster, 0);
3998
3999 elog(ERROR, "RASTER_reclass: Could not reclassify raster band of index %d", nband);
4000 PG_RETURN_NULL();
4001 }
4002
4003
4005 for (k = 0; k < j; k++) pfree(exprset[k]);
4006 pfree(exprset);
4007
4010 PG_FREE_IF_COPY(pgraster, 0);
4011
4012 elog(ERROR, "RASTER_reclass: Could not replace raster band of index %d with reclassified band", nband);
4013 PG_RETURN_NULL();
4014 }
4015
4016
4018
4019
4020 for (k = 0; k < j; k++) pfree(exprset[k]);
4021 pfree(exprset);
4022 }
4023
4026 PG_FREE_IF_COPY(pgraster, 0);
4027 if (!pgrtn)
4028 PG_RETURN_NULL();
4029
4031
4032 SET_VARSIZE(pgrtn, pgrtn->
size);
4033 PG_RETURN_POINTER(pgrtn);
4034}
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 * text_to_cstring(const text *textptr)
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