PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ RASTER_setPixelValue()

Datum RASTER_setPixelValue ( PG_FUNCTION_ARGS  )

Definition at line 767 of file rtpg_pixel.c.

768{
769 rt_pgraster *pgraster = NULL;
770 rt_pgraster *pgrtn = NULL;
771 rt_raster raster = NULL;
772 rt_band band = NULL;
773 double pixvalue = 0;
774 int32_t bandindex = 0;
775 int32_t x = 0;
776 int32_t y = 0;
777 bool skipset = FALSE;
778
779 if (PG_ARGISNULL(0))
780 PG_RETURN_NULL();
781
782 /* Check index is not NULL or < 1 */
783 if (PG_ARGISNULL(1))
784 bandindex = -1;
785 else
786 bandindex = PG_GETARG_INT32(1);
787
788 if (bandindex < 1) {
789 elog(NOTICE, "Invalid band index (must use 1-based). Value not set. Returning original raster");
790 skipset = TRUE;
791 }
792
793 /* Validate pixel coordinates are not null */
794 if (PG_ARGISNULL(2)) {
795 elog(NOTICE, "X coordinate can not be NULL when setting pixel value. Value not set. Returning original raster");
796 skipset = TRUE;
797 }
798 else
799 x = PG_GETARG_INT32(2);
800
801 if (PG_ARGISNULL(3)) {
802 elog(NOTICE, "Y coordinate can not be NULL when setting pixel value. Value not set. Returning original raster");
803 skipset = TRUE;
804 }
805 else
806 y = PG_GETARG_INT32(3);
807
808 POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
809
810 /* Deserialize raster */
811 pgraster = (rt_pgraster *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
812
814 if (!raster) {
815 PG_FREE_IF_COPY(pgraster, 0);
816 elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
817 PG_RETURN_NULL();
818 }
819
820 if (!skipset) {
821 /* Fetch requested band */
822 band = rt_raster_get_band(raster, bandindex - 1);
823 if (!band) {
824 elog(NOTICE, "Could not find raster band of index %d when setting "
825 "pixel value. Value not set. Returning original raster",
826 bandindex);
827 PG_RETURN_POINTER(pgraster);
828 }
829 else {
830 /* Set the pixel value */
831 if (PG_ARGISNULL(4)) {
832 if (!rt_band_get_hasnodata_flag(band)) {
833 elog(NOTICE, "Raster do not have a nodata value defined. "
834 "Set band nodata value first. Nodata value not set. "
835 "Returning original raster");
836 PG_RETURN_POINTER(pgraster);
837 }
838 else {
839 rt_band_get_nodata(band, &pixvalue);
840 rt_band_set_pixel(band, x - 1, y - 1, pixvalue, NULL);
841 }
842 }
843 else {
844 pixvalue = PG_GETARG_FLOAT8(4);
845 rt_band_set_pixel(band, x - 1, y - 1, pixvalue, NULL);
846 }
847 }
848 }
849
850 pgrtn = rt_raster_serialize(raster);
851 rt_raster_destroy(raster);
852 PG_FREE_IF_COPY(pgraster, 0);
853 if (!pgrtn)
854 PG_RETURN_NULL();
855
856 SET_VARSIZE(pgrtn, pgrtn->size);
857 PG_RETURN_POINTER(pgrtn);
858}
#define TRUE
Definition dbfopen.c:73
#define FALSE
Definition dbfopen.c:72
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition rt_band.c:833
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition rt_band.c:1140
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition rt_band.c:2067
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.
Definition rt_raster.c:385
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition rtpostgis.h:69
Struct definitions.
Definition librtcore.h:2452

References FALSE, POSTGIS_RT_DEBUGF, rt_band_get_hasnodata_flag(), rt_band_get_nodata(), rt_band_set_pixel(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_serialize(), rt_raster_serialized_t::size, and TRUE.

Here is the call graph for this function: