PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ RASTER_colorMap()

Datum RASTER_colorMap ( PG_FUNCTION_ARGS  )

Definition at line 4109 of file rtpg_mapalgebra.c.

4110 {
4111  rt_pgraster *pgraster = NULL;
4112  rtpg_colormap_arg arg = NULL;
4113  char *junk = NULL;
4114  rt_raster raster = NULL;
4115 
4116  POSTGIS_RT_DEBUG(3, "RASTER_colorMap: Starting");
4117 
4118  /* pgraster is NULL, return NULL */
4119  if (PG_ARGISNULL(0))
4120  PG_RETURN_NULL();
4121 
4122  /* init arg */
4123  arg = rtpg_colormap_arg_init();
4124  if (arg == NULL) {
4125  elog(ERROR, "RASTER_colorMap: Could not initialize argument structure");
4126  PG_RETURN_NULL();
4127  }
4128 
4129  /* raster (0) */
4130  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
4131 
4132  /* raster object */
4133  arg->raster = rt_raster_deserialize(pgraster, FALSE);
4134  if (!arg->raster) {
4136  PG_FREE_IF_COPY(pgraster, 0);
4137  elog(ERROR, "RASTER_colorMap: Could not deserialize raster");
4138  PG_RETURN_NULL();
4139  }
4140 
4141  /* nband (1) */
4142  if (!PG_ARGISNULL(1))
4143  arg->nband = PG_GETARG_INT32(1);
4144  POSTGIS_RT_DEBUGF(4, "nband = %d", arg->nband);
4145 
4146  /* check that band works */
4147  if (!rt_raster_has_band(arg->raster, arg->nband - 1)) {
4148  elog(NOTICE, "Raster does not have band at index %d. Returning empty raster", arg->nband);
4149 
4150  raster = rt_raster_clone(arg->raster, 0);
4151  if (raster == NULL) {
4153  PG_FREE_IF_COPY(pgraster, 0);
4154  elog(ERROR, "RASTER_colorMap: Could not create empty raster");
4155  PG_RETURN_NULL();
4156  }
4157 
4159  PG_FREE_IF_COPY(pgraster, 0);
4160 
4161  pgraster = rt_raster_serialize(raster);
4163  if (pgraster == NULL)
4164  PG_RETURN_NULL();
4165 
4166  SET_VARSIZE(pgraster, ((rt_pgraster*) pgraster)->size);
4167  PG_RETURN_POINTER(pgraster);
4168  }
4169 
4170  /* get band */
4171  arg->band = rt_raster_get_band(arg->raster, arg->nband - 1);
4172  if (arg->band == NULL) {
4173  int nband = arg->nband;
4175  PG_FREE_IF_COPY(pgraster, 0);
4176  elog(ERROR, "RASTER_colorMap: Could not get band at index %d", nband);
4177  PG_RETURN_NULL();
4178  }
4179 
4180  /* method (3) */
4181  if (!PG_ARGISNULL(3)) {
4182  char *method = NULL;
4183  char *tmp = text_to_cstring(PG_GETARG_TEXT_P(3));
4184  POSTGIS_RT_DEBUGF(4, "raw method = %s", tmp);
4185 
4186  method = rtpg_trim(tmp);
4187  pfree(tmp);
4188  method = rtpg_strtoupper(method);
4189 
4190  if (strcmp(method, "INTERPOLATE") == 0)
4191  arg->colormap->method = CM_INTERPOLATE;
4192  else if (strcmp(method, "EXACT") == 0)
4193  arg->colormap->method = CM_EXACT;
4194  else if (strcmp(method, "NEAREST") == 0)
4195  arg->colormap->method = CM_NEAREST;
4196  else {
4197  elog(NOTICE, "Unknown value provided for method. Defaulting to INTERPOLATE");
4198  arg->colormap->method = CM_INTERPOLATE;
4199  }
4200  }
4201  /* default to INTERPOLATE */
4202  else
4203  arg->colormap->method = CM_INTERPOLATE;
4204  POSTGIS_RT_DEBUGF(4, "method = %d", arg->colormap->method);
4205 
4206  /* colormap (2) */
4207  if (PG_ARGISNULL(2)) {
4209  PG_FREE_IF_COPY(pgraster, 0);
4210  elog(ERROR, "RASTER_colorMap: Value must be provided for colormap");
4211  PG_RETURN_NULL();
4212  }
4213  else {
4214  char *tmp = NULL;
4215  char *colormap = text_to_cstring(PG_GETARG_TEXT_P(2));
4216  char *_entry;
4217  char *_element;
4218  uint32_t i = 0;
4219  uint32_t j = 0;
4220 
4221  POSTGIS_RT_DEBUGF(4, "colormap = %s", colormap);
4222 
4223  /* empty string */
4224  if (!strlen(colormap)) {
4226  PG_FREE_IF_COPY(pgraster, 0);
4227  elog(ERROR, "RASTER_colorMap: Value must be provided for colormap");
4228  PG_RETURN_NULL();
4229  }
4230 
4231  arg->entry = rtpg_strsplit(colormap, "\n", &(arg->nentry));
4232  pfree(colormap);
4233  if (arg->nentry < 1) {
4235  PG_FREE_IF_COPY(pgraster, 0);
4236  elog(ERROR, "RASTER_colorMap: Could not process the value provided for colormap");
4237  PG_RETURN_NULL();
4238  }
4239 
4240  /* allocate the max # of colormap entries */
4241  arg->colormap->entry = palloc(sizeof(struct rt_colormap_entry_t) * arg->nentry);
4242  if (arg->colormap->entry == NULL) {
4244  PG_FREE_IF_COPY(pgraster, 0);
4245  elog(ERROR, "RASTER_colorMap: Could not allocate memory for colormap entries");
4246  PG_RETURN_NULL();
4247  }
4248  memset(arg->colormap->entry, 0, sizeof(struct rt_colormap_entry_t) * arg->nentry);
4249 
4250  /* each entry */
4251  for (i = 0; i < arg->nentry; i++) {
4252  /* substitute space for other delimiters */
4253  tmp = rtpg_strreplace(arg->entry[i], ":", " ", NULL);
4254  _entry = rtpg_strreplace(tmp, ",", " ", NULL);
4255  pfree(tmp);
4256  tmp = rtpg_strreplace(_entry, "\t", " ", NULL);
4257  pfree(_entry);
4258  _entry = rtpg_trim(tmp);
4259  pfree(tmp);
4260 
4261  POSTGIS_RT_DEBUGF(4, "Processing entry[%d] = %s", i, arg->entry[i]);
4262  POSTGIS_RT_DEBUGF(4, "Cleaned entry[%d] = %s", i, _entry);
4263 
4264  /* empty entry, continue */
4265  if (!strlen(_entry)) {
4266  POSTGIS_RT_DEBUGF(3, "Skipping empty entry[%d]", i);
4267  pfree(_entry);
4268  continue;
4269  }
4270 
4271  arg->element = rtpg_strsplit(_entry, " ", &(arg->nelement));
4272  pfree(_entry);
4273  if (arg->nelement < 2) {
4275  PG_FREE_IF_COPY(pgraster, 0);
4276  elog(ERROR, "RASTER_colorMap: Could not process colormap entry %d", i + 1);
4277  PG_RETURN_NULL();
4278  }
4279  else if (arg->nelement > 5) {
4280  elog(NOTICE, "More than five elements in colormap entry %d. Using at most five elements", i + 1);
4281  arg->nelement = 5;
4282  }
4283 
4284  /* smallest # of colors */
4285  if (((int)arg->nelement - 1) < arg->colormap->ncolor)
4286  arg->colormap->ncolor = arg->nelement - 1;
4287 
4288  /* each element of entry */
4289  for (j = 0; j < arg->nelement; j++) {
4290 
4291  _element = rtpg_trim(arg->element[j]);
4292  _element = rtpg_strtoupper(_element);
4293  POSTGIS_RT_DEBUGF(4, "Processing entry[%d][%d] = %s", i, j, arg->element[j]);
4294  POSTGIS_RT_DEBUGF(4, "Cleaned entry[%d][%d] = %s", i, j, _element);
4295 
4296  /* first element is ALWAYS a band value, percentage OR "nv" string */
4297  if (j == 0) {
4298  char *percent = NULL;
4299 
4300  /* NODATA */
4301  if (
4302  strcmp(_element, "NV") == 0 ||
4303  strcmp(_element, "NULL") == 0 ||
4304  strcmp(_element, "NODATA") == 0
4305  ) {
4306  POSTGIS_RT_DEBUG(4, "Processing NODATA string");
4307 
4308  if (arg->nodataentry > -1) {
4309  elog(NOTICE, "More than one NODATA entry found. Using only the first one");
4310  }
4311  else {
4312  arg->colormap->entry[arg->colormap->nentry].isnodata = 1;
4313  /* no need to set value as value comes from band's NODATA */
4314  arg->colormap->entry[arg->colormap->nentry].value = 0;
4315  }
4316  }
4317  /* percent value */
4318  else if ((percent = strchr(_element, '%')) != NULL) {
4319  double value;
4320  POSTGIS_RT_DEBUG(4, "Processing percent string");
4321 
4322  /* get the band stats */
4323  if (arg->bandstats == NULL) {
4324  POSTGIS_RT_DEBUG(4, "Getting band stats");
4325 
4326  arg->bandstats = rt_band_get_summary_stats(arg->band, 1, 1, 0, NULL, NULL, NULL);
4327  if (arg->bandstats == NULL) {
4328  pfree(_element);
4330  PG_FREE_IF_COPY(pgraster, 0);
4331  elog(ERROR, "RASTER_colorMap: Could not get band's summary stats to process percentages");
4332  PG_RETURN_NULL();
4333  }
4334  }
4335 
4336  /* get the string before the percent char */
4337  tmp = palloc(sizeof(char) * (percent - _element + 1));
4338  if (tmp == NULL) {
4339  pfree(_element);
4341  PG_FREE_IF_COPY(pgraster, 0);
4342  elog(ERROR, "RASTER_colorMap: Could not allocate memory for value of percentage");
4343  PG_RETURN_NULL();
4344  }
4345 
4346  memcpy(tmp, _element, percent - _element);
4347  tmp[percent - _element] = '\0';
4348  POSTGIS_RT_DEBUGF(4, "Percent value = %s", tmp);
4349 
4350  /* get percentage value */
4351  errno = 0;
4352  value = strtod(tmp, NULL);
4353  pfree(tmp);
4354  if (errno != 0 || _element == junk) {
4355  pfree(_element);
4357  PG_FREE_IF_COPY(pgraster, 0);
4358  elog(ERROR, "RASTER_colorMap: Could not process percent string to value");
4359  PG_RETURN_NULL();
4360  }
4361 
4362  /* check percentage */
4363  if (value < 0.) {
4364  elog(NOTICE, "Percentage values cannot be less than zero. Defaulting to zero");
4365  value = 0.;
4366  }
4367  else if (value > 100.) {
4368  elog(NOTICE, "Percentage values cannot be greater than 100. Defaulting to 100");
4369  value = 100.;
4370  }
4371 
4372  /* get the true pixel value */
4373  /* TODO: should the percentage be quantile based? */
4374  arg->colormap->entry[arg->colormap->nentry].value = ((value / 100.) * (arg->bandstats->max - arg->bandstats->min)) + arg->bandstats->min;
4375  }
4376  /* straight value */
4377  else {
4378  errno = 0;
4379  arg->colormap->entry[arg->colormap->nentry].value = strtod(_element, &junk);
4380  if (errno != 0 || _element == junk) {
4381  pfree(_element);
4383  PG_FREE_IF_COPY(pgraster, 0);
4384  elog(ERROR, "RASTER_colorMap: Could not process string to value");
4385  PG_RETURN_NULL();
4386  }
4387  }
4388 
4389  }
4390  /* RGB values (0 - 255) */
4391  else {
4392  int value = 0;
4393 
4394  errno = 0;
4395  value = (int) strtod(_element, &junk);
4396  if (errno != 0 || _element == junk) {
4397  pfree(_element);
4399  PG_FREE_IF_COPY(pgraster, 0);
4400  elog(ERROR, "RASTER_colorMap: Could not process string to value");
4401  PG_RETURN_NULL();
4402  }
4403 
4404  if (value > 255) {
4405  elog(NOTICE, "RGBA value cannot be greater than 255. Defaulting to 255");
4406  value = 255;
4407  }
4408  else if (value < 0) {
4409  elog(NOTICE, "RGBA value cannot be less than zero. Defaulting to zero");
4410  value = 0;
4411  }
4412  arg->colormap->entry[arg->colormap->nentry].color[j - 1] = value;
4413  }
4414 
4415  pfree(_element);
4416  }
4417 
4418  POSTGIS_RT_DEBUGF(4, "colormap->entry[%d] (isnodata, value, R, G, B, A) = (%d, %f, %d, %d, %d, %d)",
4419  arg->colormap->nentry,
4420  arg->colormap->entry[arg->colormap->nentry].isnodata,
4421  arg->colormap->entry[arg->colormap->nentry].value,
4422  arg->colormap->entry[arg->colormap->nentry].color[0],
4423  arg->colormap->entry[arg->colormap->nentry].color[1],
4424  arg->colormap->entry[arg->colormap->nentry].color[2],
4425  arg->colormap->entry[arg->colormap->nentry].color[3]
4426  );
4427 
4428  arg->colormap->nentry++;
4429  }
4430 
4431  POSTGIS_RT_DEBUGF(4, "colormap->nentry = %d", arg->colormap->nentry);
4432  POSTGIS_RT_DEBUGF(4, "colormap->ncolor = %d", arg->colormap->ncolor);
4433  }
4434 
4435  /* call colormap */
4436  raster = rt_raster_colormap(arg->raster, arg->nband - 1, arg->colormap);
4437  if (raster == NULL) {
4439  PG_FREE_IF_COPY(pgraster, 0);
4440  elog(ERROR, "RASTER_colorMap: Could not create new raster with applied colormap");
4441  PG_RETURN_NULL();
4442  }
4443 
4445  PG_FREE_IF_COPY(pgraster, 0);
4446  pgraster = rt_raster_serialize(raster);
4448 
4449  POSTGIS_RT_DEBUG(3, "RASTER_colorMap: Done");
4450 
4451  if (pgraster == NULL)
4452  PG_RETURN_NULL();
4453 
4454  SET_VARSIZE(pgraster, ((rt_pgraster*) pgraster)->size);
4455  PG_RETURN_POINTER(pgraster);
4456 }
#define FALSE
Definition: dbfopen.c:72
rt_raster rt_raster_colormap(rt_raster raster, int nband, rt_colormap colormap)
Returns a new raster with up to four 8BUI bands (RGBA) from applying a colormap to the user-specified...
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
Definition: rt_raster.c:1375
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
rt_raster rt_raster_clone(rt_raster raster, uint8_t deep)
Clone an existing raster.
Definition: rt_raster.c:1568
rt_bandstats rt_band_get_summary_stats(rt_band band, int exclude_nodata_value, double sample, int inc_vals, uint64_t *cK, double *cM, double *cQ)
Compute summary statistics for a band.
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
int value
Definition: genraster.py:62
nband
Definition: pixval.py:53
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
char * rtpg_strtoupper(char *str)
char * rtpg_trim(const char *input)
char ** rtpg_strsplit(const char *str, const char *delimiter, uint32_t *n)
char * rtpg_strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
Definition: rtpg_internal.c:55
static void rtpg_colormap_arg_destroy(rtpg_colormap_arg arg)
static rtpg_colormap_arg rtpg_colormap_arg_init()
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:69
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:73
uint8_t color[4]
Definition: librtcore.h:2630
double value
Definition: librtcore.h:2629
int isnodata
Definition: librtcore.h:2628
Definition: librtcore.h:2627
enum rt_colormap_t::@10 method
rt_colormap_entry entry
Definition: librtcore.h:2642
uint16_t nentry
Definition: librtcore.h:2641
Struct definitions.
Definition: librtcore.h:2396

References rtpg_colormap_arg_t::band, rtpg_colormap_arg_t::bandstats, rt_colormap_entry_t::color, rtpg_colormap_arg_t::colormap, rtpg_colormap_arg_t::element, rt_colormap_t::entry, rtpg_colormap_arg_t::entry, FALSE, rt_colormap_entry_t::isnodata, rt_bandstats_t::max, rt_colormap_t::method, rt_bandstats_t::min, rtpg_colormap_arg_t::nband, pixval::nband, rt_colormap_t::ncolor, rtpg_colormap_arg_t::nelement, rt_colormap_t::nentry, rtpg_colormap_arg_t::nentry, rtpg_colormap_arg_t::nodataentry, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtpg_colormap_arg_t::raster, rtrowdump::raster, rt_band_get_summary_stats(), rt_raster_clone(), rt_raster_colormap(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_band(), rt_raster_has_band(), rt_raster_serialize(), rtpg_colormap_arg_destroy(), rtpg_colormap_arg_init(), rtpg_strreplace(), rtpg_strsplit(), rtpg_strtoupper(), rtpg_trim(), rt_colormap_entry_t::value, and genraster::value.

Here is the call graph for this function: