PostGIS  2.1.10dev-r@@SVN_REVISION@@
rt_errorstate rt_pixel_set_to_array ( rt_pixel  npixel,
int  count,
int  x,
int  y,
uint16_t  distancex,
uint16_t  distancey,
double ***  value,
int ***  nodata,
int *  dimx,
int *  dimy 
)

Definition at line 1341 of file rt_api.c.

References genraster::count, ES_ERROR, ES_NONE, RASTER_DEBUGF, rtalloc(), rtdealloc(), rterror(), rt_pixel_t::value, rt_pixel_t::x, and rt_pixel_t::y.

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

1348  {
1349  uint32_t i;
1350  uint32_t j;
1351  uint32_t dim[2] = {0};
1352  double **values = NULL;
1353  int **nodatas = NULL;
1354  int zero[2] = {0};
1355  int _x;
1356  int _y;
1357 
1358  assert(npixel != NULL && count > 0);
1359  assert(value != NULL);
1360  assert(nodata != NULL);
1361 
1362  /* dimensions */
1363  dim[0] = distancex * 2 + 1;
1364  dim[1] = distancey * 2 + 1;
1365  RASTER_DEBUGF(4, "dimensions = %d x %d", dim[0], dim[1]);
1366 
1367  /* establish 2D arrays (Y axis) */
1368  values = rtalloc(sizeof(double *) * dim[1]);
1369  nodatas = rtalloc(sizeof(int *) * dim[1]);
1370 
1371  if (values == NULL || nodatas == NULL) {
1372  rterror("rt_pixel_set_to_array: Could not allocate memory for 2D array");
1373  return ES_ERROR;
1374  }
1375 
1376  /* initialize X axis */
1377  for (i = 0; i < dim[1]; i++) {
1378  values[i] = rtalloc(sizeof(double) * dim[0]);
1379  nodatas[i] = rtalloc(sizeof(int) * dim[0]);
1380 
1381  if (values[i] == NULL || nodatas[i] == NULL) {
1382  rterror("rt_pixel_set_to_array: Could not allocate memory for dimension of 2D array");
1383 
1384  if (values[i] == NULL) {
1385  for (j = 0; j < i; j++) {
1386  rtdealloc(values[j]);
1387  rtdealloc(nodatas[j]);
1388  }
1389  }
1390  else {
1391  for (j = 0; j <= i; j++) {
1392  rtdealloc(values[j]);
1393  if (j < i)
1394  rtdealloc(nodatas[j]);
1395  }
1396  }
1397 
1398  rtdealloc(values);
1399  rtdealloc(nodatas);
1400 
1401  return ES_ERROR;
1402  }
1403 
1404  /* set values to 0 */
1405  memset(values[i], 0, sizeof(double) * dim[0]);
1406 
1407  /* set nodatas to 1 */
1408  for (j = 0; j < dim[0]; j++)
1409  nodatas[i][j] = 1;
1410  }
1411 
1412  /* get 0,0 of grid */
1413  zero[0] = x - distancex;
1414  zero[1] = y - distancey;
1415 
1416  /* populate 2D arrays */
1417  for (i = 0; i < count; i++) {
1418  if (npixel[i].nodata)
1419  continue;
1420 
1421  _x = npixel[i].x - zero[0];
1422  _y = npixel[i].y - zero[1];
1423 
1424  RASTER_DEBUGF(4, "absolute x,y: %d x %d", npixel[i].x, npixel[i].y);
1425  RASTER_DEBUGF(4, "relative x,y: %d x %d", _x, _y);
1426 
1427  values[_y][_x] = npixel[i].value;
1428  nodatas[_y][_x] = 0;
1429 
1430  RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_y][_x], values[_y][_x]);
1431  }
1432 
1433  *value = &(*values);
1434  *nodata = &(*nodatas);
1435  if (dimx != NULL)
1436  *dimx = dim[0];
1437  if (dimy != NULL)
1438  *dimy = dim[1];
1439 
1440  return ES_NONE;
1441 }
void rtdealloc(void *mem)
Definition: rt_api.c:882
double value
Definition: rt_api.h:2263
#define RASTER_DEBUGF(level, msg,...)
Definition: rt_api.h:285
int count
Definition: genraster.py:57
tuple x
Definition: pixval.py:53
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
tuple y
Definition: pixval.py:54

Here is the call graph for this function:

Here is the caller graph for this function: