PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ 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 286 of file rt_pixel.c.

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.0) || mask->nodata[_y][_x] == 1)
397 {
398 values[_y][_x] = 0;
399 nodatas[_y][_x] = 1;
400 }
401 /* use pixel */
402 else {
403 values[_y][_x] = npixel[i].value;
404 nodatas[_y][_x] = 0;
405 }
406 }
407 /* weighted mask */
408 else {
409 /* nodata */
410 if(mask->nodata[_y][_x] == 1) {
411 values[_y][_x] = 0;
412 nodatas[_y][_x] = 1;
413 }
414 /* apply weight to pixel value */
415 else {
416 values[_y][_x] = npixel[i].value * mask->values[_y][_x];
417 nodatas[_y][_x] = 0;
418 }
419 }
420 }
421
422 RASTER_DEBUGF(4, "(x, y, nodata, value) = (%d, %d, %d, %f)", _x, _y, nodatas[_y][_x], values[_y][_x]);
423 }
424
425 *value = &(*values);
426 *nodata = &(*nodatas);
427 if (dimx != NULL)
428 *dimx = dim[0];
429 if (dimy != NULL)
430 *dimy = dim[1];
431
432 return ES_NONE;
433}
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
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:299
#define FLT_EQ(x, y)
Definition librtcore.h:2235
@ ES_NONE
Definition librtcore.h:180
@ ES_ERROR
Definition librtcore.h:181
void rtdealloc(void *mem)
Definition rt_context.c:186
int value
Definition genraster.py:62
int count
Definition genraster.py:57
double ** values
Definition librtcore.h:2347
uint16_t dimy
Definition librtcore.h:2346
uint16_t dimx
Definition librtcore.h:2345
int ** nodata
Definition librtcore.h:2348
double value
Definition librtcore.h:2339

References 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().

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