GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
iclass.c
Go to the documentation of this file.
1 /*!
2  \file lib/imagery/iclass.c
3 
4  \brief Imagery library - functions for wx.iclass
5 
6  Computation based on training areas for supervised classification.
7  Based on i.class module (GRASS 6).
8 
9  Copyright (C) 1999-2007, 2011 by the GRASS Development Team
10 
11  This program is free software under the GNU General Public License
12  (>=v2). Read the file COPYING that comes with GRASS for details.
13 
14  \author David Satnik, Central Washington University (original author)
15  \author Markus Neteler <neteler itc.it> (i.class module)
16  \author Bernhard Reiter <bernhard intevation.de> (i.class module)
17  \author Brad Douglas <rez touchofmadness.com>(i.class module)
18  \author Glynn Clements <glynn gclements.plus.com> (i.class module)
19  \author Hamish Bowman <hamish_b yahoo.com> (i.class module)
20  \author Jan-Oliver Wagner <jan intevation.de> (i.class module)
21  \author Anna Kratochvilova <kratochanna gmail.com> (rewriting for wx.iclass)
22  \author Vaclav Petras <wenzeslaus gmail.com> (rewriting for wx.iclass)
23  */
24 
25 #include <grass/imagery.h>
26 #include <grass/glocale.h>
27 #include <grass/vector.h>
28 
29 #include "iclass_local_proto.h"
30 
31 /*!
32  \brief Calculates statistical values for one class and multiple bands based
33  on training areas.
34 
35  Calculates values statistical based on the cells
36  that are within training areas. Creates raster map
37  to display the cells of the image bands which fall
38  within standard deviations from the means.
39 
40  \param statistics pointer to bands statistics
41  \param refer pointer to band files structure
42  \param map_info vector map with training areas
43  \param layer_name vector layer
44  \param group name of imagery group
45  \param raster_name name of temporary raster map (to be created)
46 
47  \return number of processed training areas
48  \return -1 on failure
49  */
50 int I_iclass_analysis(IClass_statistics *statistics, struct Ref *refer,
51  struct Map_info *map_info, const char *layer_name,
52  const char *group, const char *raster_name)
53 {
54  int ret;
55 
56  int category;
57 
58  struct Cell_head band_region;
59 
60  CELL **band_buffer;
61 
62  int *band_fd;
63 
64  IClass_perimeter_list perimeters;
65 
66  G_debug(1, "iclass_analysis(): group = %s", group);
67 
68  category = statistics->cat;
69 
70  /* region set to current workin region */
71  G_get_set_window(&band_region);
72 
73  /* find perimeter points from vector map */
74  ret = vector2perimeters(map_info, layer_name, category, &perimeters,
75  &band_region);
76  if (ret < 0) {
77  return -1;
78  }
79  else if (ret == 0) {
80  G_warning(_("No areas in category %d"), category);
81  return 0;
82  }
83 
84  open_band_files(refer, &band_buffer, &band_fd);
85  alloc_statistics(statistics, refer->nfiles);
86  make_all_statistics(statistics, &perimeters, band_buffer, band_fd);
87  create_raster(statistics, band_buffer, band_fd, raster_name);
88  close_band_files(refer, band_buffer, band_fd);
89 
90  free_perimeters(&perimeters);
91  return ret;
92 }
93 
94 /*!
95  \brief Read files for the specified group subgroup into the Ref structure.
96 
97  \param group_name name of imagery group
98  \param subgroup_name name of imagery subgroup
99  \param subgroup_name if it is NULL, bands from group will be used
100  \param[out] refer pointer to band files structure
101 
102  \return 1 on success
103  \return 0 on failure
104  */
105 int I_iclass_init_group(const char *group_name, const char *subgroup_name,
106  struct Ref *refer)
107 {
108  int n;
109 
110  G_debug(3, "I_iclass_init_group(): group_name = %s, subgroup_name = %s",
111  group_name, subgroup_name);
112  I_init_group_ref(refer); /* called in I_get_group_ref */
113 
114  if (subgroup_name)
115  I_get_subgroup_ref(group_name, subgroup_name, refer);
116  else
117  I_get_group_ref(group_name, refer);
118 
119  for (n = 0; n < refer->nfiles; n++) {
120  if (G_find_raster(refer->file[n].name, refer->file[n].mapset) == NULL) {
121  if (subgroup_name)
122  G_warning(_("Raster map <%s@%s> in subgroup "
123  "<%s> does not exist"),
124  refer->file[n].name, refer->file[n].mapset,
125  subgroup_name);
126  else
127  G_warning(_("Raster map <%s@%s> in group "
128  "<%s> does not exist"),
129  refer->file[n].name, refer->file[n].mapset,
130  group_name);
131 
132  I_free_group_ref(refer);
133  return 0;
134  }
135  }
136 
137  if (refer->nfiles <= 1) {
138  if (subgroup_name)
139  G_warning(
140  _("Subgroup <%s> does not have enough files (it has %d files)"),
141  subgroup_name, refer->nfiles);
142  else
143  G_warning(
144  _("Group <%s> does not have enough files (it has %d files)"),
145  group_name, refer->nfiles);
146  I_free_group_ref(refer);
147  return 0;
148  }
149 
150  return 1;
151 }
152 
153 /*!
154  \brief Create raster map based on statistics.
155 
156  \param statistics pointer to bands statistics
157  \param refer pointer to band files structure
158  \param raster_name name of temporary raster map (to be created)
159  */
160 void I_iclass_create_raster(IClass_statistics *statistics, struct Ref *refer,
161  const char *raster_name)
162 {
163  CELL **band_buffer;
164 
165  int *band_fd;
166 
167  int b;
168 
169  for (b = 0; b < statistics->nbands; b++) {
170  band_range(statistics, b);
171  }
172 
173  open_band_files(refer, &band_buffer, &band_fd);
174  create_raster(statistics, band_buffer, band_fd, raster_name);
175  close_band_files(refer, band_buffer, band_fd);
176 }
#define NULL
Definition: ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
void G_get_set_window(struct Cell_head *)
Get the current working window (region)
const char * G_find_raster(char *, const char *)
Find a raster map.
Definition: find_rast.c:55
int G_debug(int, const char *,...) __attribute__((format(printf
int I_init_group_ref(struct Ref *)
initialize Ref structure
Definition: group.c:466
int I_get_group_ref(const char *, struct Ref *)
read group REF file
Definition: group.c:112
int I_free_group_ref(struct Ref *)
free Ref structure
Definition: group.c:484
int I_get_subgroup_ref(const char *, const char *, struct Ref *)
read subgroup REF file
Definition: group.c:149
int CELL
Definition: gis.h:625
#define _(str)
Definition: glocale.h:10
void I_iclass_create_raster(IClass_statistics *statistics, struct Ref *refer, const char *raster_name)
Create raster map based on statistics.
Definition: iclass.c:160
int I_iclass_init_group(const char *group_name, const char *subgroup_name, struct Ref *refer)
Read files for the specified group subgroup into the Ref structure.
Definition: iclass.c:105
int I_iclass_analysis(IClass_statistics *statistics, struct Ref *refer, struct Map_info *map_info, const char *layer_name, const char *group, const char *raster_name)
Calculates statistical values for one class and multiple bands based on training areas.
Definition: iclass.c:50
void close_band_files(struct Ref *refer, CELL **band_buffer, int *band_fd)
Close and free space for the group band files.
Definition: iclass_bands.c:67
void open_band_files(struct Ref *refer, CELL ***band_buffer, int **band_fd)
Open and allocate space for the group band files.
Definition: iclass_bands.c:39
int vector2perimeters(struct Map_info *Map, const char *layer_name, int category, IClass_perimeter_list *perimeters, struct Cell_head *band_region)
Creates perimeters from vector areas of given category.
void free_perimeters(IClass_perimeter_list *perimeters)
Frees all perimeters in list of perimeters.
void alloc_statistics(IClass_statistics *statistics, int nbands)
Allocate space for statistics.
void band_range(IClass_statistics *statistics, int band)
Helper function for computing min and max range in one band.
void create_raster(IClass_statistics *statistics, CELL **band_buffer, int *band_fd, const char *raster_name)
Create raster map based on statistics.
int make_all_statistics(IClass_statistics *statistics, IClass_perimeter_list *perimeters, CELL **band_buffer, int *band_fd)
Calculate statistics for all training areas.
double b
Definition: r_raster.c:39
2D/3D raster map header (used also for region)
Definition: gis.h:437
Vector map info.
Definition: dig_structs.h:1243
char name[INAME_LEN]
Definition: imagery.h:20
char mapset[INAME_LEN]
Definition: imagery.h:21
Definition: imagery.h:24
int nfiles
Definition: imagery.h:25
struct Ref_Files * file
Definition: imagery.h:26