PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ RASTER_colorMap()

Datum RASTER_colorMap ( PG_FUNCTION_ARGS  )

Definition at line 4125 of file rtpg_mapalgebra.c.

4126 {
4127  rt_pgraster *pgraster = NULL;
4128  rtpg_colormap_arg arg = NULL;
4129  char *junk = NULL;
4130  rt_raster raster = NULL;
4131 
4132  POSTGIS_RT_DEBUG(3, "RASTER_colorMap: Starting");
4133 
4134  /* pgraster is NULL, return NULL */
4135  if (PG_ARGISNULL(0))
4136  PG_RETURN_NULL();
4137 
4138  /* init arg */
4139  arg = rtpg_colormap_arg_init();
4140  if (arg == NULL) {
4141  elog(ERROR, "RASTER_colorMap: Could not initialize argument structure");
4142  PG_RETURN_NULL();
4143  }
4144 
4145  /* raster (0) */
4146  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
4147 
4148  /* raster object */
4149  arg->raster = rt_raster_deserialize(pgraster, FALSE);
4150  if (!arg->raster) {
4152  PG_FREE_IF_COPY(pgraster, 0);
4153  elog(ERROR, "RASTER_colorMap: Could not deserialize raster");
4154  PG_RETURN_NULL();
4155  }
4156 
4157  /* nband (1) */
4158  if (!PG_ARGISNULL(1))
4159  arg->nband = PG_GETARG_INT32(1);
4160  POSTGIS_RT_DEBUGF(4, "nband = %d", arg->nband);
4161 
4162  /* check that band works */
4163  if (!rt_raster_has_band(arg->raster, arg->nband - 1)) {
4164  elog(NOTICE, "Raster does not have band at index %d. Returning empty raster", arg->nband);
4165 
4166  raster = rt_raster_clone(arg->raster, 0);
4167  if (raster == NULL) {
4169  PG_FREE_IF_COPY(pgraster, 0);
4170  elog(ERROR, "RASTER_colorMap: Could not create empty raster");
4171  PG_RETURN_NULL();
4172  }
4173 
4175  PG_FREE_IF_COPY(pgraster, 0);
4176 
4177  pgraster = rt_raster_serialize(raster);
4179  if (pgraster == NULL)
4180  PG_RETURN_NULL();
4181 
4182  SET_VARSIZE(pgraster, ((rt_pgraster*) pgraster)->size);
4183  PG_RETURN_POINTER(pgraster);
4184  }
4185 
4186  /* get band */
4187  arg->band = rt_raster_get_band(arg->raster, arg->nband - 1);
4188  if (arg->band == NULL) {
4189  int nband = arg->nband;
4191  PG_FREE_IF_COPY(pgraster, 0);
4192  elog(ERROR, "RASTER_colorMap: Could not get band at index %d", nband);
4193  PG_RETURN_NULL();
4194  }
4195 
4196  /* method (3) */
4197  if (!PG_ARGISNULL(3)) {
4198  char *method = NULL;
4199  char *tmp = text_to_cstring(PG_GETARG_TEXT_P(3));
4200  POSTGIS_RT_DEBUGF(4, "raw method = %s", tmp);
4201 
4202  method = rtpg_trim(tmp);
4203  pfree(tmp);
4204  method = rtpg_strtoupper(method);
4205 
4206  if (strcmp(method, "INTERPOLATE") == 0)
4207  arg->colormap->method = CM_INTERPOLATE;
4208  else if (strcmp(method, "EXACT") == 0)
4209  arg->colormap->method = CM_EXACT;
4210  else if (strcmp(method, "NEAREST") == 0)
4211  arg->colormap->method = CM_NEAREST;
4212  else {
4213  elog(NOTICE, "Unknown value provided for method. Defaulting to INTERPOLATE");
4214  arg->colormap->method = CM_INTERPOLATE;
4215  }
4216  }
4217  /* default to INTERPOLATE */
4218  else
4219  arg->colormap->method = CM_INTERPOLATE;
4220  POSTGIS_RT_DEBUGF(4, "method = %d", arg->colormap->method);
4221 
4222  /* colormap (2) */
4223  if (PG_ARGISNULL(2)) {
4225  PG_FREE_IF_COPY(pgraster, 0);
4226  elog(ERROR, "RASTER_colorMap: Value must be provided for colormap");
4227  PG_RETURN_NULL();
4228  }
4229  else {
4230  char *tmp = NULL;
4231  char *colormap = text_to_cstring(PG_GETARG_TEXT_P(2));
4232  char *_entry;
4233  char *_element;
4234  uint32_t i = 0;
4235  uint32_t j = 0;
4236 
4237  POSTGIS_RT_DEBUGF(4, "colormap = %s", colormap);
4238 
4239  /* empty string */
4240  if (!strlen(colormap)) {
4242  PG_FREE_IF_COPY(pgraster, 0);
4243  elog(ERROR, "RASTER_colorMap: Value must be provided for colormap");
4244  PG_RETURN_NULL();
4245  }
4246 
4247  arg->entry = rtpg_strsplit(colormap, "\n", &(arg->nentry));
4248  pfree(colormap);
4249  if (arg->nentry < 1) {
4251  PG_FREE_IF_COPY(pgraster, 0);
4252  elog(ERROR, "RASTER_colorMap: Could not process the value provided for colormap");
4253  PG_RETURN_NULL();
4254  }
4255 
4256  /* allocate the max # of colormap entries */
4257  arg->colormap->entry = palloc(sizeof(struct rt_colormap_entry_t) * arg->nentry);
4258  if (arg->colormap->entry == NULL) {
4260  PG_FREE_IF_COPY(pgraster, 0);
4261  elog(ERROR, "RASTER_colorMap: Could not allocate memory for colormap entries");
4262  PG_RETURN_NULL();
4263  }
4264  memset(arg->colormap->entry, 0, sizeof(struct rt_colormap_entry_t) * arg->nentry);
4265 
4266  /* each entry */
4267  for (i = 0; i < arg->nentry; i++) {
4268  /* substitute space for other delimiters */
4269  tmp = rtpg_strreplace(arg->entry[i], ":", " ", NULL);
4270  _entry = rtpg_strreplace(tmp, ",", " ", NULL);
4271  pfree(tmp);
4272  tmp = rtpg_strreplace(_entry, "\t", " ", NULL);
4273  pfree(_entry);
4274  _entry = rtpg_trim(tmp);
4275  pfree(tmp);
4276 
4277  POSTGIS_RT_DEBUGF(4, "Processing entry[%d] = %s", i, arg->entry[i]);
4278  POSTGIS_RT_DEBUGF(4, "Cleaned entry[%d] = %s", i, _entry);
4279 
4280  /* empty entry, continue */
4281  if (!strlen(_entry)) {
4282  POSTGIS_RT_DEBUGF(3, "Skipping empty entry[%d]", i);
4283  pfree(_entry);
4284  continue;
4285  }
4286 
4287  arg->element = rtpg_strsplit(_entry, " ", &(arg->nelement));
4288  pfree(_entry);
4289  if (arg->nelement < 2) {
4291  PG_FREE_IF_COPY(pgraster, 0);
4292  elog(ERROR, "RASTER_colorMap: Could not process colormap entry %d", i + 1);
4293  PG_RETURN_NULL();
4294  }
4295  else if (arg->nelement > 5) {
4296  elog(NOTICE, "More than five elements in colormap entry %d. Using at most five elements", i + 1);
4297  arg->nelement = 5;
4298  }
4299 
4300  /* smallest # of colors */
4301  if (((int)arg->nelement - 1) < arg->colormap->ncolor)
4302  arg->colormap->ncolor = arg->nelement - 1;
4303 
4304  /* each element of entry */
4305  for (j = 0; j < arg->nelement; j++) {
4306 
4307  _element = rtpg_trim(arg->element[j]);
4308  _element = rtpg_strtoupper(_element);
4309  POSTGIS_RT_DEBUGF(4, "Processing entry[%d][%d] = %s", i, j, arg->element[j]);
4310  POSTGIS_RT_DEBUGF(4, "Cleaned entry[%d][%d] = %s", i, j, _element);
4311 
4312  /* first element is ALWAYS a band value, percentage OR "nv" string */
4313  if (j == 0) {
4314  char *percent = NULL;
4315 
4316  /* NODATA */
4317  if (
4318  strcmp(_element, "NV") == 0 ||
4319  strcmp(_element, "NULL") == 0 ||
4320  strcmp(_element, "NODATA") == 0
4321  ) {
4322  POSTGIS_RT_DEBUG(4, "Processing NODATA string");
4323 
4324  if (arg->nodataentry > -1) {
4325  elog(NOTICE, "More than one NODATA entry found. Using only the first one");
4326  }
4327  else {
4328  arg->colormap->entry[arg->colormap->nentry].isnodata = 1;
4329  /* no need to set value as value comes from band's NODATA */
4330  arg->colormap->entry[arg->colormap->nentry].value = 0;
4331  }
4332  }
4333  /* percent value */
4334  else if ((percent = strchr(_element, '%')) != NULL) {
4335  double value;
4336  POSTGIS_RT_DEBUG(4, "Processing percent string");
4337 
4338  /* get the band stats */
4339  if (arg->bandstats == NULL) {
4340  POSTGIS_RT_DEBUG(4, "Getting band stats");
4341 
4342  arg->bandstats = rt_band_get_summary_stats(arg->band, 1, 1, 0, NULL, NULL, NULL);
4343  if (arg->bandstats == NULL) {
4344  pfree(_element);
4346  PG_FREE_IF_COPY(pgraster, 0);
4347  elog(ERROR, "RASTER_colorMap: Could not get band's summary stats to process percentages");
4348  PG_RETURN_NULL();
4349  }
4350  }
4351 
4352  /* get the string before the percent char */
4353  tmp = palloc(sizeof(char) * (percent - _element + 1));
4354  if (tmp == NULL) {
4355  pfree(_element);
4357  PG_FREE_IF_COPY(pgraster, 0);
4358  elog(ERROR, "RASTER_colorMap: Could not allocate memory for value of percentage");
4359  PG_RETURN_NULL();
4360  }
4361 
4362  memcpy(tmp, _element, percent - _element);
4363  tmp[percent - _element] = '\0';
4364  POSTGIS_RT_DEBUGF(4, "Percent value = %s", tmp);
4365 
4366  /* get percentage value */
4367  errno = 0;
4368  value = strtod(tmp, NULL);
4369  pfree(tmp);
4370  if (errno != 0 || _element == junk) {
4371  pfree(_element);
4373  PG_FREE_IF_COPY(pgraster, 0);
4374  elog(ERROR, "RASTER_colorMap: Could not process percent string to value");
4375  PG_RETURN_NULL();
4376  }
4377 
4378  /* check percentage */
4379  if (value < 0.) {
4380  elog(NOTICE, "Percentage values cannot be less than zero. Defaulting to zero");
4381  value = 0.;
4382  }
4383  else if (value > 100.) {
4384  elog(NOTICE, "Percentage values cannot be greater than 100. Defaulting to 100");
4385  value = 100.;
4386  }
4387 
4388  /* get the true pixel value */
4389  /* TODO: should the percentage be quantile based? */
4390  arg->colormap->entry[arg->colormap->nentry].value = ((value / 100.) * (arg->bandstats->max - arg->bandstats->min)) + arg->bandstats->min;
4391  }
4392  /* straight value */
4393  else {
4394  errno = 0;
4395  arg->colormap->entry[arg->colormap->nentry].value = 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 
4405  }
4406  /* RGB values (0 - 255) */
4407  else {
4408  int value = 0;
4409 
4410  errno = 0;
4411  value = (int) strtod(_element, &junk);
4412  if (errno != 0 || _element == junk) {
4413  pfree(_element);
4415  PG_FREE_IF_COPY(pgraster, 0);
4416  elog(ERROR, "RASTER_colorMap: Could not process string to value");
4417  PG_RETURN_NULL();
4418  }
4419 
4420  if (value > 255) {
4421  elog(NOTICE, "RGBA value cannot be greater than 255. Defaulting to 255");
4422  value = 255;
4423  }
4424  else if (value < 0) {
4425  elog(NOTICE, "RGBA value cannot be less than zero. Defaulting to zero");
4426  value = 0;
4427  }
4428  arg->colormap->entry[arg->colormap->nentry].color[j - 1] = value;
4429  }
4430 
4431  pfree(_element);
4432  }
4433 
4434  POSTGIS_RT_DEBUGF(4, "colormap->entry[%d] (isnodata, value, R, G, B, A) = (%d, %f, %d, %d, %d, %d)",
4435  arg->colormap->nentry,
4436  arg->colormap->entry[arg->colormap->nentry].isnodata,
4437  arg->colormap->entry[arg->colormap->nentry].value,
4438  arg->colormap->entry[arg->colormap->nentry].color[0],
4439  arg->colormap->entry[arg->colormap->nentry].color[1],
4440  arg->colormap->entry[arg->colormap->nentry].color[2],
4441  arg->colormap->entry[arg->colormap->nentry].color[3]
4442  );
4443 
4444  arg->colormap->nentry++;
4445  }
4446 
4447  POSTGIS_RT_DEBUGF(4, "colormap->nentry = %d", arg->colormap->nentry);
4448  POSTGIS_RT_DEBUGF(4, "colormap->ncolor = %d", arg->colormap->ncolor);
4449  }
4450 
4451  /* call colormap */
4452  raster = rt_raster_colormap(arg->raster, arg->nband - 1, arg->colormap);
4453  if (raster == NULL) {
4455  PG_FREE_IF_COPY(pgraster, 0);
4456  elog(ERROR, "RASTER_colorMap: Could not create new raster with applied colormap");
4457  PG_RETURN_NULL();
4458  }
4459 
4461  PG_FREE_IF_COPY(pgraster, 0);
4462  pgraster = rt_raster_serialize(raster);
4464 
4465  POSTGIS_RT_DEBUG(3, "RASTER_colorMap: Done");
4466 
4467  if (pgraster == NULL)
4468  PG_RETURN_NULL();
4469 
4470  SET_VARSIZE(pgraster, ((rt_pgraster*) pgraster)->size);
4471  PG_RETURN_POINTER(pgraster);
4472 }
#define FALSE
Definition: dbfopen.c:168
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:82
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:1347
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:1540
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:381
int value
Definition: genraster.py:61
nband
Definition: pixval.py:52
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
char * text_to_cstring(const text *textptr)
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:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
uint8_t color[4]
Definition: librtcore.h:2484
double value
Definition: librtcore.h:2483
int isnodata
Definition: librtcore.h:2482
Definition: librtcore.h:2481
rt_colormap_entry entry
Definition: librtcore.h:2496
enum rt_colormap_t::@9 method
uint16_t nentry
Definition: librtcore.h:2495
Struct definitions.
Definition: librtcore.h:2250
unsigned int uint32_t
Definition: uthash.h:78

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(), text_to_cstring(), rt_colormap_entry_t::value, and genraster::value.

Here is the call graph for this function: