raw_stats.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include "global.h"
  5. int raw_stats(int fd[], int with_coordinates, int with_xy, int with_labels)
  6. {
  7. CELL null_cell;
  8. void **rast, **rastp;
  9. char str1[50];
  10. register int i;
  11. int row, col, nulls_found;
  12. double Rast_row_to_northing(), G_col_to_easting();
  13. struct Cell_head window;
  14. char nbuf[100], ebuf[100];
  15. RASTER_MAP_TYPE *map_type;
  16. /* allocate i/o buffers for each raster map */
  17. rast = (void **)G_calloc(nfiles, sizeof(void *));
  18. rastp = (void **)G_calloc(nfiles, sizeof(void *));
  19. map_type = (RASTER_MAP_TYPE *) G_calloc(nfiles, sizeof(RASTER_MAP_TYPE));
  20. for (i = 0; i < nfiles; i++) {
  21. /* if fp map and report real data, not cat indexes, set type to DCELL */
  22. if (is_fp[i] && !raw_output && !as_int)
  23. map_type[i] = DCELL_TYPE;
  24. else
  25. map_type[i] = CELL_TYPE;
  26. rast[i] = Rast_allocate_buf(map_type[i]);
  27. }
  28. /* get window */
  29. if (with_coordinates)
  30. G_get_set_window(&window);
  31. /* here we go */
  32. Rast_set_c_null_value(&null_cell, 1);
  33. for (row = 0; row < nrows; row++) {
  34. G_percent(row, nrows, 2);
  35. /* read the rows and set the pointers */
  36. for (i = 0; i < nfiles; i++) {
  37. Rast_get_row(fd[i], rast[i], row, map_type[i]);
  38. rastp[i] = rast[i];
  39. }
  40. if (with_coordinates)
  41. G_format_northing(Rast_row_to_northing(row + .5, &window), nbuf,
  42. G_projection() == PROJECTION_LL ? -1 : 0);
  43. for (col = 0; col < ncols; col++) {
  44. if (no_nulls || no_nulls_all) {
  45. nulls_found = 0;
  46. for (i = 0; i < nfiles; i++) {
  47. /*
  48. Rast_set_d_value(zero_val, 0.0, map_type[i]);
  49. if (Rast_raster_cmp(rastp[i], zero_val, map_type[i]) != 0)
  50. break;
  51. */
  52. if (Rast_is_null_value(rastp[i], map_type[i]))
  53. nulls_found++;
  54. }
  55. if ((nulls_found == nfiles) || (nulls_found && no_nulls)) {
  56. for (i = 0; i < nfiles; i++)
  57. rastp[i] = G_incr_void_ptr(rastp[i],
  58. Rast_cell_size(map_type
  59. [i]));
  60. continue;
  61. }
  62. }
  63. if (with_coordinates) {
  64. G_format_easting(Rast_col_to_easting(col + .5, &window), ebuf,
  65. G_projection() == PROJECTION_LL ? -1 : 0);
  66. fprintf(stdout, "%s%s%s%s", ebuf, fs, nbuf, fs);
  67. }
  68. if (with_xy)
  69. fprintf(stdout, "%d%s%d%s", col + 1, fs, row + 1, fs);
  70. for (i = 0; i < nfiles; i++) {
  71. if (Rast_is_null_value(rastp[i], map_type[i])) {
  72. fprintf(stdout, "%s%s", i ? fs : "", no_data_str);
  73. if (with_labels)
  74. fprintf(stdout, "%s%s", fs,
  75. Rast_get_c_cat(&null_cell, &labels[i]));
  76. }
  77. else if (map_type[i] == CELL_TYPE) {
  78. fprintf(stdout, "%s%ld", i ? fs : "",
  79. (long)*((CELL *) rastp[i]));
  80. if (with_labels && !is_fp[i])
  81. fprintf(stdout, "%s%s", fs,
  82. Rast_get_c_cat((CELL *) rastp[i], &labels[i]));
  83. }
  84. else { /* floating point cell */
  85. sprintf(str1, "%.10f", *((DCELL *) rastp[i]));
  86. G_trim_decimal(str1);
  87. G_strip(str1);
  88. fprintf(stdout, "%s%s", i ? fs : "", str1);
  89. if (with_labels)
  90. fprintf(stdout, "%s%s", fs,
  91. Rast_get_d_cat((DCELL *) rastp[i],
  92. &labels[i]));
  93. }
  94. rastp[i] =
  95. G_incr_void_ptr(rastp[i], Rast_cell_size(map_type[i]));
  96. }
  97. fprintf(stdout, "\n");
  98. }
  99. }
  100. G_percent(row, nrows, 2);
  101. return 0;
  102. }