PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ rt_pixel_set_to_array()

rt_errorstate rt_pixel_set_to_array ( rt_pixel  npixel,
uint32_t  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 288 of file rt_pixel.c.

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

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, genraster::value, rt_mask_t::values, rt_mask_t::weighted, rt_pixel_t::x, pixval::x, rt_pixel_t::y, and pixval::y.

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

Here is the call graph for this function:
Here is the caller graph for this function: