iclass.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 the first band */
  51. Rast_get_cellhd(refer->file[0].name, refer->file[0].mapset, &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 into the Ref structure.
  73. \param group_name name of imagery group
  74. \param[out] refer pointer to band files structure
  75. \return 1 on success
  76. \return 0 on failure
  77. */
  78. int I_iclass_init_group(const char *group_name, struct Ref *refer)
  79. {
  80. int n;
  81. G_debug(3, "init_group(): group = %s", group_name);
  82. I_init_group_ref(refer); /* called in I_get_group_ref */
  83. I_get_group_ref(group_name, refer);
  84. for (n = 0; n < refer->nfiles; n++) {
  85. if (G_find_raster(refer->file[n].name, refer->file[n].mapset) == NULL) {
  86. G_warning(_("Raster map <%s@%s> in group "
  87. "<%s> do not exist"), refer->file[n].name,
  88. refer->file[n].mapset, group_name);
  89. I_free_group_ref(refer);
  90. return 0;
  91. }
  92. }
  93. if (refer->nfiles <= 1) {
  94. G_warning(_("Group <%s> does not have enough files (it has %d files)"),
  95. group_name, refer->nfiles);
  96. I_free_group_ref(refer);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. /*!
  102. \brief Create raster map based on statistics.
  103. \param statistics pointer to bands statistics
  104. \param refer pointer to band files structure
  105. \param raster_name name of temporary raster map (to be created)
  106. */
  107. void I_iclass_create_raster(IClass_statistics * statistics, struct Ref *refer,
  108. const char *raster_name)
  109. {
  110. CELL **band_buffer;
  111. int *band_fd;
  112. int b;
  113. for (b = 0; b < statistics->nbands; b++) {
  114. band_range(statistics, b);
  115. }
  116. open_band_files(refer, &band_buffer, &band_fd);
  117. create_raster(statistics, band_buffer, band_fd, raster_name);
  118. close_band_files(refer, band_buffer, band_fd);
  119. }