nearest.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * nearest.c - returns the nearest neighbor to a given
  3. * x,y position
  4. */
  5. #include <math.h>
  6. #include <grass/gis.h>
  7. #include <grass/raster.h>
  8. #include "r.proj.h"
  9. void p_nearest(struct cache *ibuffer, /* input buffer */
  10. void *obufptr, /* ptr in output buffer */
  11. int cell_type, /* raster map type of obufptr */
  12. double col_idx, /* column index in input matrix */
  13. double row_idx, /* row index in input matrix */
  14. struct Cell_head *cellhd /* cell header of input layer */
  15. )
  16. {
  17. int row, col; /* row/col of nearest neighbor */
  18. FCELL cell;
  19. /* cut indices to integer */
  20. row = (int)floor(row_idx);
  21. col = (int)floor(col_idx);
  22. /* check for out of bounds - if out of bounds set NULL value */
  23. if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
  24. Rast_set_null_value(obufptr, 1, cell_type);
  25. return;
  26. }
  27. cell = CVAL(ibuffer, row, col);
  28. if (Rast_is_f_null_value(&cell)) {
  29. Rast_set_null_value(obufptr, 1, cell_type);
  30. return;
  31. }
  32. Rast_set_f_value(obufptr, cell, cell_type);
  33. }