GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 /*!
33  \brief Calculates statistical values for one class and multiple bands based 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 
67 
68  G_debug(1, "iclass_analysis(): group = %s", group);
69 
70  category = statistics->cat;
71 
72  /* region set to current workin region */
73  G_get_set_window(&band_region);
74 
75  /* find perimeter points from vector map */
76  ret =
77  vector2perimeters(map_info, layer_name, category, &perimeters,
78  &band_region);
79  if (ret < 0) {
80  return -1;
81  }
82  else if (ret == 0) {
83  G_warning(_("No areas in category %d"), category);
84  return 0;
85  }
86 
87  open_band_files(refer, &band_buffer, &band_fd);
88  alloc_statistics(statistics, refer->nfiles);
89  make_all_statistics(statistics, &perimeters, band_buffer, band_fd);
90  create_raster(statistics, band_buffer, band_fd, raster_name);
91  close_band_files(refer, band_buffer, band_fd);
92 
93  free_perimeters(&perimeters);
94  return ret;
95 }
96 
97 
98 
99 /*!
100  \brief Read files for the specified group subgroup into the Ref structure.
101 
102  \param group_name name of imagery group
103  \param subgroup_name name of imagery subgroup
104  \param subgroup_name if it is NULL, bands from group will be used
105  \param[out] refer pointer to band files structure
106 
107  \return 1 on success
108  \return 0 on failure
109  */
110 int I_iclass_init_group(const char *group_name, const char *subgroup_name,
111  struct Ref *refer)
112 {
113  int n;
114 
115  G_debug(3, "I_iclass_init_group(): group_name = %s, subgroup_name = %s",
116  group_name, subgroup_name);
117  I_init_group_ref(refer); /* called in I_get_group_ref */
118 
119  if (subgroup_name)
120  I_get_subgroup_ref(group_name, subgroup_name, refer);
121  else
122  I_get_group_ref(group_name, refer);
123 
124  for (n = 0; n < refer->nfiles; n++) {
125  if (G_find_raster(refer->file[n].name, refer->file[n].mapset) == NULL) {
126  if (subgroup_name)
127  G_warning(_("Raster map <%s@%s> in subgroup "
128  "<%s> does not exist"), refer->file[n].name,
129  refer->file[n].mapset, subgroup_name);
130  else
131  G_warning(_("Raster map <%s@%s> in group "
132  "<%s> does not exist"), refer->file[n].name,
133  refer->file[n].mapset, group_name);
134 
135  I_free_group_ref(refer);
136  return 0;
137  }
138  }
139 
140  if (refer->nfiles <= 1) {
141  if (subgroup_name)
142  G_warning(_
143  ("Subgroup <%s> does not have enough files (it has %d files)"),
144  subgroup_name, refer->nfiles);
145  else
146  G_warning(_
147  ("Group <%s> does not have enough files (it has %d files)"),
148  group_name, refer->nfiles);
149  I_free_group_ref(refer);
150  return 0;
151  }
152 
153  return 1;
154 }
155 
156 /*!
157  \brief Create raster map based on statistics.
158 
159  \param statistics pointer to bands statistics
160  \param refer pointer to band files structure
161  \param raster_name name of temporary raster map (to be created)
162  */
163 void I_iclass_create_raster(IClass_statistics * statistics, struct Ref *refer,
164  const char *raster_name)
165 {
166  CELL **band_buffer;
167 
168  int *band_fd;
169 
170  int b;
171 
172  for (b = 0; b < statistics->nbands; b++) {
173  band_range(statistics, b);
174  }
175 
176  open_band_files(refer, &band_buffer, &band_fd);
177  create_raster(statistics, band_buffer, band_fd, raster_name);
178  close_band_files(refer, band_buffer, band_fd);
179 }
char name[INAME_LEN]
Definition: imagery.h:22
void I_iclass_create_raster(IClass_statistics *statistics, struct Ref *refer, const char *raster_name)
Create raster map based on statistics.
Definition: iclass.c:163
2D/3D raster map header (used also for region)
Definition: gis.h:423
int I_free_group_ref(struct Ref *)
free Ref structure
Definition: group.c:502
Definition: imagery.h:26
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 create_raster(IClass_statistics *statistics, CELL **band_buffer, int *band_fd, const char *raster_name)
Create raster map based on statistics.
void alloc_statistics(IClass_statistics *statistics, int nbands)
Allocate space for statistics.
#define NULL
Definition: ccmath.h:32
void band_range(IClass_statistics *statistics, int band)
Helper function for computing min and max range in one band.
void free_perimeters(IClass_perimeter_list *perimeters)
Frees all perimeters in list of perimeters.
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:110
double b
Definition: r_raster.c:39
int make_all_statistics(IClass_statistics *statistics, IClass_perimeter_list *perimeters, CELL **band_buffer, int *band_fd)
Calculate statistics for all training areas.
char mapset[INAME_LEN]
Definition: imagery.h:23
int I_get_subgroup_ref(const char *, const char *, struct Ref *)
read subgroup REF file
Definition: group.c:153
void G_get_set_window(struct Cell_head *)
Get the current working window (region)
int nfiles
Definition: imagery.h:28
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
Vector map info.
Definition: dig_structs.h:1259
const char * G_find_raster(char *, const char *)
Find a raster map.
Definition: find_rast.c:55
struct Ref_Files * file
Definition: imagery.h:29
void G_warning(const char *,...) __attribute__((format(printf
int CELL
Definition: gis.h:613
#define _(str)
Definition: glocale.h:10
int I_init_group_ref(struct Ref *)
initialize Ref structure
Definition: group.c:483
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 I_get_group_ref(const char *, struct Ref *)
read group REF file
Definition: group.c:114
int G_debug(int, const char *,...) __attribute__((format(printf
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