PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ rt_pixel_set_to_array()

rt_errorstate rt_pixel_set_to_array ( rt_pixel  npixel,
int  count,
rt_mask  mask,
int  x,
int  y,
uint16_t  distancex,
uint16_t  distancey,
double ***  value,
int ***  nodata,
int *  dimx,
int *  dimy 
)

Definition at line 286 of file rt_pixel.c.

References genraster::count, rt_mask_t::dimx, rt_mask_t::dimy, ES_ERROR, ES_NONE, FLT_EQ, rt_mask_t::nodata, RASTER_DEBUGF, rtalloc(), rtdealloc(), rterror(), rt_pixel_t::value, rt_mask_t::values, rt_mask_t::weighted, rt_pixel_t::x, and rt_pixel_t::y.

Referenced by RASTER_neighborhood(), rt_raster_iterator(), test_band_get_nearest_pixel(), and test_pixel_set_to_array().

293  {
294  uint32_t i;
295  uint32_t j;
296  uint32_t dim[2] = {0};
297  double **values = NULL;
298  int **nodatas = NULL;
299  int zero[2] = {0};
300  int _x;
301  int _y;
302 
303  assert(npixel != NULL && count > 0);
304  assert(value != NULL);
305  assert(nodata != NULL);
306 
307  /* dimensions */
308  dim[0] = distancex * 2 + 1;
309  dim[1] = distancey * 2 + 1;
310  RASTER_DEBUGF(4, "dimensions = %d x %d", dim[0], dim[1]);
311 
312  /* make sure that the dimx and dimy match mask */
313  if (mask != NULL) {
314  if (dim[0] != mask->dimx || dim[1] != mask->dimy) {
315  rterror("rt_pixel_set_array: mask dimensions %d x %d do not match given dims %d x %d", mask->dimx, mask->dimy, dim[0], dim[1]);
316  return ES_ERROR;
317  }
318 
319  if (mask->values == NULL || mask->nodata == NULL) {
320  rterror("rt_pixel_set_array: Invalid mask");
321  return ES_ERROR;
322  }
323 
324  }
325 
326  /* establish 2D arrays (Y axis) */
327  values = rtalloc(sizeof(double *) * dim[1]);
328  nodatas = rtalloc(sizeof(int *) * dim[1]);
329 
330  if (values == NULL || nodatas == NULL) {
331  rterror("rt_pixel_set_to_array: Could not allocate memory for 2D array");
332  return ES_ERROR;
333  }
334 
335  /* initialize X axis */
336  for (i = 0; i < dim[1]; i++) {
337  values[i] = rtalloc(sizeof(double) * dim[0]);
338  nodatas[i] = rtalloc(sizeof(int) * dim[0]);
339 
340  if (values[i] == NULL || nodatas[i] == NULL) {
341  rterror("rt_pixel_set_to_array: Could not allocate memory for dimension of 2D array");
342 
343  if (values[i] == NULL) {
344  for (j = 0; j < i; j++) {
345  rtdealloc(values[j]);
346  rtdealloc(nodatas[j]);
347  }
348  }
349  else {
350  for (j = 0; j <= i; j++) {
351  rtdealloc(values[j]);
352  if (j < i)
353  rtdealloc(nodatas[j]);
354  }
355  }
356 
357  rtdealloc(values);
358  rtdealloc(nodatas);
359 
360  return ES_ERROR;
361  }
362 
363  /* set values to 0 */
364  memset(values[i], 0, sizeof(double) * dim[0]);
365 
366  /* set nodatas to 1 */
367  for (j = 0; j < dim[0]; j++)
368  nodatas[i][j] = 1;
369  }
370 
371  /* get 0,0 of grid */
372  zero[0] = x - distancex;
373  zero[1] = y - distancey;
374 
375  /* populate 2D arrays */
376  for (i = 0; i < count; i++) {
377  if (npixel[i].nodata)
378  continue;
379 
380  _x = npixel[i].x - zero[0];
381  _y = npixel[i].y - zero[1];
382 
383  RASTER_DEBUGF(4, "absolute x,y: %d x %d", npixel[i].x, npixel[i].y);
384  RASTER_DEBUGF(4, "relative x,y: %d x %d", _x, _y);
385 
386  /* no mask */
387  if (mask == NULL) {
388  values[_y][_x] = npixel[i].value;
389  nodatas[_y][_x] = 0;
390  }
391  /* mask */
392  else {
393  /* unweighted (boolean) mask */
394  if (mask->weighted == 0) {
395  /* pixel is set to zero or nodata */
396  if (FLT_EQ(mask->values[_y][_x],0) || mask->nodata[_y][_x] == 1) {
397  values[_y][_x] = 0;
398  nodatas[_y][_x] = 1;
399  }
400  /* use pixel */
401  else {
402  values[_y][_x] = npixel[i].value;
403  nodatas[_y][_x] = 0;
404  }
405  }
406  /* weighted mask */
407  else {
408  /* nodata */
409  if(mask->nodata[_y][_x] == 1) {
410  values[_y][_x] = 0;
411  nodatas[_y][_x] = 1;
412  }
413  /* apply weight to pixel value */
414  else {
415  values[_y][_x] = npixel[i].value * mask->values[_y][_x];
416  nodatas[_y][_x] = 0;
417  }
418  }
419  }
420 
421  RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_y][_x], values[_y][_x]);
422  }
423 
424  *value = &(*values);
425  *nodata = &(*nodatas);
426  if (dimx != NULL)
427  *dimx = dim[0];
428  if (dimy != NULL)
429  *dimy = dim[1];
430 
431  return ES_NONE;
432 }
#define FLT_EQ(x, y)
Definition: librtcore.h:2185
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
double value
Definition: librtcore.h:2289
int ** nodata
Definition: librtcore.h:2298
int weighted
Definition: librtcore.h:2299
uint16_t dimy
Definition: librtcore.h:2296
unsigned int uint32_t
Definition: uthash.h:78
double ** values
Definition: librtcore.h:2297
int count
Definition: genraster.py:56
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:299
void rtdealloc(void *mem)
Definition: rt_context.c:186
int value
Definition: genraster.py:61
uint16_t dimx
Definition: librtcore.h:2295
Here is the call graph for this function:
Here is the caller graph for this function: