PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_setPixelValue()

Datum RASTER_setPixelValue ( PG_FUNCTION_ARGS  )

Definition at line 568 of file rtpg_pixel.c.

569 {
570  rt_pgraster *pgraster = NULL;
571  rt_pgraster *pgrtn = NULL;
572  rt_raster raster = NULL;
573  rt_band band = NULL;
574  double pixvalue = 0;
575  int32_t bandindex = 0;
576  int32_t x = 0;
577  int32_t y = 0;
578  bool skipset = FALSE;
579 
580  if (PG_ARGISNULL(0))
581  PG_RETURN_NULL();
582 
583  /* Check index is not NULL or < 1 */
584  if (PG_ARGISNULL(1))
585  bandindex = -1;
586  else
587  bandindex = PG_GETARG_INT32(1);
588 
589  if (bandindex < 1) {
590  elog(NOTICE, "Invalid band index (must use 1-based). Value not set. Returning original raster");
591  skipset = TRUE;
592  }
593 
594  /* Validate pixel coordinates are not null */
595  if (PG_ARGISNULL(2)) {
596  elog(NOTICE, "X coordinate can not be NULL when setting pixel value. Value not set. Returning original raster");
597  skipset = TRUE;
598  }
599  else
600  x = PG_GETARG_INT32(2);
601 
602  if (PG_ARGISNULL(3)) {
603  elog(NOTICE, "Y coordinate can not be NULL when setting pixel value. Value not set. Returning original raster");
604  skipset = TRUE;
605  }
606  else
607  y = PG_GETARG_INT32(3);
608 
609  POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
610 
611  /* Deserialize raster */
612  pgraster = (rt_pgraster *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
613 
614  raster = rt_raster_deserialize(pgraster, FALSE);
615  if (!raster) {
616  PG_FREE_IF_COPY(pgraster, 0);
617  elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
618  PG_RETURN_NULL();
619  }
620 
621  if (!skipset) {
622  /* Fetch requested band */
623  band = rt_raster_get_band(raster, bandindex - 1);
624  if (!band) {
625  elog(NOTICE, "Could not find raster band of index %d when setting "
626  "pixel value. Value not set. Returning original raster",
627  bandindex);
628  PG_RETURN_POINTER(pgraster);
629  }
630  else {
631  /* Set the pixel value */
632  if (PG_ARGISNULL(4)) {
634  elog(NOTICE, "Raster do not have a nodata value defined. "
635  "Set band nodata value first. Nodata value not set. "
636  "Returning original raster");
637  PG_RETURN_POINTER(pgraster);
638  }
639  else {
640  rt_band_get_nodata(band, &pixvalue);
641  rt_band_set_pixel(band, x - 1, y - 1, pixvalue, NULL);
642  }
643  }
644  else {
645  pixvalue = PG_GETARG_FLOAT8(4);
646  rt_band_set_pixel(band, x - 1, y - 1, pixvalue, NULL);
647  }
648  }
649  }
650 
651  pgrtn = rt_raster_serialize(raster);
653  PG_FREE_IF_COPY(pgraster, 0);
654  if (!pgrtn)
655  PG_RETURN_NULL();
656 
657  SET_VARSIZE(pgrtn, pgrtn->size);
658  PG_RETURN_POINTER(pgrtn);
659 }
#define TRUE
Definition: dbfopen.c:169
#define FALSE
Definition: dbfopen.c:168
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:82
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:1730
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:381
band
Definition: ovdump.py:57
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:65
Struct definitions.
Definition: librtcore.h:2250

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: