PostGIS 3.7.0dev-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 301 of file rt_pixel.c.

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

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: