points.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Written by Bill Brown, USA-CERL, NCSA, UI GMSL.
  2. */
  3. #include <stdlib.h>
  4. #include <strings.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/glocale.h>
  8. #include <grass/dbmi.h>
  9. #include <grass/vector.h>
  10. #include "global.h"
  11. int extract_points(int z_flag)
  12. {
  13. struct line_pnts *points = Vect_new_line_struct();
  14. CELL *cellbuf;
  15. FCELL *fcellbuf;
  16. DCELL *dcellbuf;
  17. int row, col;
  18. double x, y;
  19. int count;
  20. switch (data_type) {
  21. case CELL_TYPE:
  22. cellbuf = Rast_allocate_c_buf();
  23. break;
  24. case FCELL_TYPE:
  25. fcellbuf = Rast_allocate_f_buf();
  26. break;
  27. case DCELL_TYPE:
  28. dcellbuf = Rast_allocate_d_buf();
  29. break;
  30. }
  31. G_message(_("Extracting points..."));
  32. count = 1;
  33. for (row = 0; row < cell_head.rows; row++) {
  34. G_percent(row, n_rows, 2);
  35. y = Rast_row_to_northing((double)(row + .5), &cell_head);
  36. switch (data_type) {
  37. case CELL_TYPE:
  38. Rast_get_c_row(input_fd, cellbuf, row);
  39. break;
  40. case FCELL_TYPE:
  41. Rast_get_f_row(input_fd, fcellbuf, row);
  42. break;
  43. case DCELL_TYPE:
  44. Rast_get_d_row(input_fd, dcellbuf, row);
  45. break;
  46. }
  47. for (col = 0; col < cell_head.cols; col++) {
  48. int cat, val;
  49. double dval;
  50. x = Rast_col_to_easting((double)(col + .5), &cell_head);
  51. switch (data_type) {
  52. case CELL_TYPE:
  53. if (Rast_is_c_null_value(cellbuf + col))
  54. continue;
  55. val = cellbuf[col];
  56. dval = val;
  57. break;
  58. case FCELL_TYPE:
  59. if (Rast_is_f_null_value(fcellbuf + col))
  60. continue;
  61. dval = fcellbuf[col];
  62. break;
  63. case DCELL_TYPE:
  64. if (Rast_is_d_null_value(dcellbuf + col))
  65. continue;
  66. dval = dcellbuf[col];
  67. break;
  68. }
  69. /* value_flag is used only for CELL type */
  70. cat = (value_flag) ? val : count;
  71. Vect_reset_line(points);
  72. Vect_reset_cats(Cats);
  73. Vect_cat_set(Cats, 1, cat);
  74. Vect_append_point(points, x, y, dval);
  75. Vect_write_line(&Map, GV_POINT, points, Cats);
  76. if ((driver != NULL) && !value_flag) {
  77. insert_value(cat, val, dval);
  78. }
  79. count++;
  80. }
  81. }
  82. G_percent(row, n_rows, 2);
  83. return (1);
  84. }