PostGIS  2.1.10dev-r@@SVN_REVISION@@
int rt_raster_generate_new_band ( rt_raster  raster,
rt_pixtype  pixtype,
double  initialvalue,
uint32_t  hasnodata,
double  nodatavalue,
int  index 
)

Generate a new inline band and add it to a raster.

Memory is allocated in this function for band data.

Parameters
raster: the raster to add a band to
pixtype: the pixel type for the new band
initialvalue: initial value for pixels
hasnodata: indicates if the band has a nodata value
nodatavalue: nodata value for the new band
index: position to add the new band in the raster
Returns
identifier (position) for the just-added raster, or -1 on error

Definition at line 5784 of file rt_api.c.

References ovdump::band, FLT_EQ, PT_16BSI, PT_16BUI, PT_1BB, PT_2BUI, PT_32BF, PT_32BSI, PT_32BUI, PT_4BUI, PT_64BF, PT_8BSI, PT_8BUI, rt_band_destroy(), rt_band_new_inline(), rt_band_set_isnodata_flag(), rt_band_set_ownsdata_flag(), rt_pixtype_size(), rt_raster_add_band(), rt_raster_get_height(), rt_raster_get_num_bands(), rt_raster_get_width(), rt_util_clamp_to_16BSI(), rt_util_clamp_to_16BUI(), rt_util_clamp_to_1BB(), rt_util_clamp_to_2BUI(), rt_util_clamp_to_32BSI(), rt_util_clamp_to_32BUI(), rt_util_clamp_to_32F(), rt_util_clamp_to_4BUI(), rt_util_clamp_to_8BSI(), rt_util_clamp_to_8BUI(), rt_util_dbl_trunc_warning(), rtalloc(), rtdealloc(), and rterror().

Referenced by RASTER_addBand(), RASTER_clip(), RASTER_mapAlgebra2(), RASTER_mapAlgebraExpr(), RASTER_mapAlgebraFct(), RASTER_mapAlgebraFctNgb(), RASTER_tile(), RASTER_union_transfn(), rt_raster_from_gdal_dataset(), and rt_raster_iterator().

5788  {
5789  rt_band band = NULL;
5790  int width = 0;
5791  int height = 0;
5792  int numval = 0;
5793  int datasize = 0;
5794  int oldnumbands = 0;
5795  int numbands = 0;
5796  void * mem = NULL;
5797  int32_t checkvalint = 0;
5798  uint32_t checkvaluint = 0;
5799  double checkvaldouble = 0;
5800  float checkvalfloat = 0;
5801  int i;
5802 
5803 
5804  assert(NULL != raster);
5805 
5806  /* Make sure index is in a valid range */
5807  oldnumbands = rt_raster_get_num_bands(raster);
5808  if (index < 0)
5809  index = 0;
5810  else if (index > oldnumbands + 1)
5811  index = oldnumbands + 1;
5812 
5813  /* Determine size of memory block to allocate and allocate it */
5814  width = rt_raster_get_width(raster);
5815  height = rt_raster_get_height(raster);
5816  numval = width * height;
5817  datasize = rt_pixtype_size(pixtype) * numval;
5818 
5819  mem = (int *)rtalloc(datasize);
5820  if (!mem) {
5821  rterror("rt_raster_generate_new_band: Could not allocate memory for band");
5822  return -1;
5823  }
5824 
5825  if (FLT_EQ(initialvalue, 0.0))
5826  memset(mem, 0, datasize);
5827  else {
5828  switch (pixtype)
5829  {
5830  case PT_1BB:
5831  {
5832  uint8_t *ptr = mem;
5833  uint8_t clamped_initval = rt_util_clamp_to_1BB(initialvalue);
5834  for (i = 0; i < numval; i++)
5835  ptr[i] = clamped_initval;
5836  checkvalint = ptr[0];
5837  break;
5838  }
5839  case PT_2BUI:
5840  {
5841  uint8_t *ptr = mem;
5842  uint8_t clamped_initval = rt_util_clamp_to_2BUI(initialvalue);
5843  for (i = 0; i < numval; i++)
5844  ptr[i] = clamped_initval;
5845  checkvalint = ptr[0];
5846  break;
5847  }
5848  case PT_4BUI:
5849  {
5850  uint8_t *ptr = mem;
5851  uint8_t clamped_initval = rt_util_clamp_to_4BUI(initialvalue);
5852  for (i = 0; i < numval; i++)
5853  ptr[i] = clamped_initval;
5854  checkvalint = ptr[0];
5855  break;
5856  }
5857  case PT_8BSI:
5858  {
5859  int8_t *ptr = mem;
5860  int8_t clamped_initval = rt_util_clamp_to_8BSI(initialvalue);
5861  for (i = 0; i < numval; i++)
5862  ptr[i] = clamped_initval;
5863  checkvalint = ptr[0];
5864  break;
5865  }
5866  case PT_8BUI:
5867  {
5868  uint8_t *ptr = mem;
5869  uint8_t clamped_initval = rt_util_clamp_to_8BUI(initialvalue);
5870  for (i = 0; i < numval; i++)
5871  ptr[i] = clamped_initval;
5872  checkvalint = ptr[0];
5873  break;
5874  }
5875  case PT_16BSI:
5876  {
5877  int16_t *ptr = mem;
5878  int16_t clamped_initval = rt_util_clamp_to_16BSI(initialvalue);
5879  for (i = 0; i < numval; i++)
5880  ptr[i] = clamped_initval;
5881  checkvalint = ptr[0];
5882  break;
5883  }
5884  case PT_16BUI:
5885  {
5886  uint16_t *ptr = mem;
5887  uint16_t clamped_initval = rt_util_clamp_to_16BUI(initialvalue);
5888  for (i = 0; i < numval; i++)
5889  ptr[i] = clamped_initval;
5890  checkvalint = ptr[0];
5891  break;
5892  }
5893  case PT_32BSI:
5894  {
5895  int32_t *ptr = mem;
5896  int32_t clamped_initval = rt_util_clamp_to_32BSI(initialvalue);
5897  for (i = 0; i < numval; i++)
5898  ptr[i] = clamped_initval;
5899  checkvalint = ptr[0];
5900  break;
5901  }
5902  case PT_32BUI:
5903  {
5904  uint32_t *ptr = mem;
5905  uint32_t clamped_initval = rt_util_clamp_to_32BUI(initialvalue);
5906  for (i = 0; i < numval; i++)
5907  ptr[i] = clamped_initval;
5908  checkvaluint = ptr[0];
5909  break;
5910  }
5911  case PT_32BF:
5912  {
5913  float *ptr = mem;
5914  float clamped_initval = rt_util_clamp_to_32F(initialvalue);
5915  for (i = 0; i < numval; i++)
5916  ptr[i] = clamped_initval;
5917  checkvalfloat = ptr[0];
5918  break;
5919  }
5920  case PT_64BF:
5921  {
5922  double *ptr = mem;
5923  for (i = 0; i < numval; i++)
5924  ptr[i] = initialvalue;
5925  checkvaldouble = ptr[0];
5926  break;
5927  }
5928  default:
5929  {
5930  rterror("rt_raster_generate_new_band: Unknown pixeltype %d", pixtype);
5931  rtdealloc(mem);
5932  return -1;
5933  }
5934  }
5935  }
5936 
5937  /* Overflow checking */
5939  initialvalue,
5940  checkvalint, checkvaluint,
5941  checkvalfloat, checkvaldouble,
5942  pixtype
5943  );
5944 
5945  band = rt_band_new_inline(width, height, pixtype, hasnodata, nodatavalue, mem);
5946  if (! band) {
5947  rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
5948  rtdealloc(mem);
5949  return -1;
5950  }
5951  rt_band_set_ownsdata_flag(band, 1); /* we DO own this data!!! */
5952  index = rt_raster_add_band(raster, band, index);
5953  numbands = rt_raster_get_num_bands(raster);
5954  if (numbands == oldnumbands || index == -1) {
5955  rterror("rt_raster_generate_new_band: Could not add band to raster. Aborting");
5956  rt_band_destroy(band);
5957  }
5958 
5959  /* set isnodata if hasnodata = TRUE and initial value = nodatavalue */
5960  if (hasnodata && FLT_EQ(initialvalue, nodatavalue))
5961  rt_band_set_isnodata_flag(band, 1);
5962 
5963  return index;
5964 }
uint8_t rt_util_clamp_to_8BUI(double value)
Definition: rt_api.c:109
int rt_raster_get_num_bands(rt_raster raster)
Definition: rt_api.c:5677
uint8_t rt_util_clamp_to_4BUI(double value)
Definition: rt_api.c:99
void rtdealloc(void *mem)
Definition: rt_api.c:882
float rt_util_clamp_to_32F(double value)
Definition: rt_api.c:134
int32_t rt_util_clamp_to_32BSI(double value)
Definition: rt_api.c:124
uint8_t rt_util_clamp_to_1BB(double value)
Definition: rt_api.c:89
tuple band
Definition: ovdump.py:57
int rt_util_dbl_trunc_warning(double initialvalue, int32_t checkvalint, uint32_t checkvaluint, float checkvalfloat, double checkvaldouble, rt_pixtype pixtype)
Definition: rt_api.c:934
uint8_t rt_util_clamp_to_2BUI(double value)
Definition: rt_api.c:94
Definition: rt_api.h:173
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition: rt_api.c:1466
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition: rt_api.c:1097
uint16_t rt_util_clamp_to_16BUI(double value)
Definition: rt_api.c:119
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
Definition: rt_api.c:5706
uint32_t rt_util_clamp_to_32BUI(double value)
Definition: rt_api.c:129
rt_errorstate rt_band_set_isnodata_flag(rt_band band, int flag)
Set isnodata flag value.
Definition: rt_api.c:2023
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition: rt_api.c:1936
#define FLT_EQ(x, y)
Definition: rt_api.h:2159
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition: rt_api.c:1650
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_api.c:5434
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_api.c:895
int8_t rt_util_clamp_to_8BSI(double value)
Definition: rt_api.c:104
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_api.c:5426
int16_t rt_util_clamp_to_16BSI(double value)
Definition: rt_api.c:114

Here is the call graph for this function:

Here is the caller graph for this function: