iclass.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. \file lib/imagery/iclass.c
  3. \brief Imagery library - functions for wx.iclass
  4. Computation based on training areas for supervised classification.
  5. Based on i.class module (GRASS 6).
  6. Copyright (C) 1999-2007, 2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. \author David Satnik, Central Washington University (original author)
  10. \author Markus Neteler <neteler itc.it> (i.class module)
  11. \author Bernhard Reiter <bernhard intevation.de> (i.class module)
  12. \author Brad Douglas <rez touchofmadness.com>(i.class module)
  13. \author Glynn Clements <glynn gclements.plus.com> (i.class module)
  14. \author Hamish Bowman <hamish_b yahoo.com> (i.class module)
  15. \author Jan-Oliver Wagner <jan intevation.de> (i.class module)
  16. \author Anna Kratochvilova <kratochanna gmail.com> (rewriting for wx.iclass)
  17. \author Vaclav Petras <wenzeslaus gmail.com> (rewriting for wx.iclass)
  18. */
  19. #include <grass/imagery.h>
  20. #include <grass/glocale.h>
  21. #include <grass/vector.h>
  22. #include "iclass_local_proto.h"
  23. /*!
  24. \brief Calculates statistical values for one class and multiple bands based on training areas.
  25. Calculates values statistical based on the cells
  26. that are within training areas. Creates raster map
  27. to display the cells of the image bands which fall
  28. within standard deviations from the means.
  29. \param statistics pointer to bands statistics
  30. \param refer pointer to band files structure
  31. \param map_info vector map with training areas
  32. \param layer_name vector layer
  33. \param group name of imagery group
  34. \param raster_name name of temporary raster map (to be created)
  35. \return number of processed training areas
  36. \return -1 on failure
  37. */
  38. int I_iclass_analysis(IClass_statistics * statistics, struct Ref *refer,
  39. struct Map_info *map_info, const char *layer_name,
  40. const char *group, const char *raster_name)
  41. {
  42. int ret;
  43. int category;
  44. struct Cell_head band_region;
  45. CELL **band_buffer;
  46. int *band_fd;
  47. IClass_perimeter_list perimeters;
  48. G_debug(1, "iclass_analysis(): group = %s", group);
  49. category = statistics->cat;
  50. /* region set to current workin region */
  51. G_get_set_window(&band_region);
  52. /* find perimeter points from vector map */
  53. ret =
  54. vector2perimeters(map_info, layer_name, category, &perimeters,
  55. &band_region);
  56. if (ret < 0) {
  57. return -1;
  58. }
  59. else if (ret == 0) {
  60. G_warning(_("No areas in category %d"), category);
  61. return 0;
  62. }
  63. open_band_files(refer, &band_buffer, &band_fd);
  64. alloc_statistics(statistics, refer->nfiles);
  65. make_all_statistics(statistics, &perimeters, band_buffer, band_fd);
  66. create_raster(statistics, band_buffer, band_fd, raster_name);
  67. close_band_files(refer, band_buffer, band_fd);
  68. free_perimeters(&perimeters);
  69. return ret;
  70. }
  71. /*!
  72. \brief Read files for the specified group subgroup into the Ref structure.
  73. \param group_name name of imagery group
  74. \param subgroup_name name of imagery subgroup
  75. \param subgroup_name if it is NULL, bands from group will be used
  76. \param[out] refer pointer to band files structure
  77. \return 1 on success
  78. \return 0 on failure
  79. */
  80. int I_iclass_init_group(const char *group_name, const char *subgroup_name,
  81. struct Ref *refer)
  82. {
  83. int n;
  84. G_debug(3, "I_iclass_init_group(): group_name = %s, subgroup_name = %s",
  85. group_name, subgroup_name);
  86. I_init_group_ref(refer); /* called in I_get_group_ref */
  87. if (subgroup_name)
  88. I_get_subgroup_ref(group_name, subgroup_name, refer);
  89. else
  90. I_get_group_ref(group_name, refer);
  91. for (n = 0; n < refer->nfiles; n++) {
  92. if (G_find_raster(refer->file[n].name, refer->file[n].mapset) == NULL) {
  93. if (subgroup_name)
  94. G_warning(_("Raster map <%s@%s> in subgroup "
  95. "<%s> does not exist"), refer->file[n].name,
  96. refer->file[n].mapset, subgroup_name);
  97. else
  98. G_warning(_("Raster map <%s@%s> in group "
  99. "<%s> does not exist"), refer->file[n].name,
  100. refer->file[n].mapset, group_name);
  101. I_free_group_ref(refer);
  102. return 0;
  103. }
  104. }
  105. if (refer->nfiles <= 1) {
  106. if (subgroup_name)
  107. G_warning(_
  108. ("Subgroup <%s> does not have enough files (it has %d files)"),
  109. subgroup_name, refer->nfiles);
  110. else
  111. G_warning(_
  112. ("Group <%s> does not have enough files (it has %d files)"),
  113. group_name, refer->nfiles);
  114. I_free_group_ref(refer);
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. /*!
  120. \brief Create raster map based on statistics.
  121. \param statistics pointer to bands statistics
  122. \param refer pointer to band files structure
  123. \param raster_name name of temporary raster map (to be created)
  124. */
  125. void I_iclass_create_raster(IClass_statistics * statistics, struct Ref *refer,
  126. const char *raster_name)
  127. {
  128. CELL **band_buffer;
  129. int *band_fd;
  130. int b;
  131. for (b = 0; b < statistics->nbands; b++) {
  132. band_range(statistics, b);
  133. }
  134. open_band_files(refer, &band_buffer, &band_fd);
  135. create_raster(statistics, band_buffer, band_fd, raster_name);
  136. close_band_files(refer, band_buffer, band_fd);
  137. }