nearest.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 "global.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 *row_idx, /* row index in input matrix */
  13. double *col_idx, /* column 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. DCELL *cellp;
  19. /* cut indices to integer and get nearest cell */
  20. /* the row_idx, col_idx correction for bilinear/bicubic does not apply here */
  21. row = (int)floor(*row_idx);
  22. col = (int)floor(*col_idx);
  23. /* check for out of bounds - if out of bounds set NULL value */
  24. if (row < 0 || row >= cellhd->rows || col < 0 || col >= cellhd->cols) {
  25. Rast_set_null_value(obufptr, 1, cell_type);
  26. return;
  27. }
  28. cellp = CPTR(ibuffer, row, col);
  29. if (Rast_is_d_null_value(cellp)) {
  30. Rast_set_null_value(obufptr, 1, cell_type);
  31. return;
  32. }
  33. Rast_set_d_value(obufptr, *cellp, cell_type);
  34. }