PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ 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 
813  raster = rt_raster_deserialize(pgraster, FALSE);
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)) {
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);
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:674
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
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:974
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition: rt_band.c:1887
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
band
Definition: ovdump.py:58
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:69
Struct definitions.
Definition: librtcore.h:2403

References ovdump::band, FALSE, POSTGIS_RT_DEBUGF, rtrowdump::raster, 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, TRUE, pixval::x, and pixval::y.

Here is the call graph for this function: