GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
quant_rw.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/quant_rw.c
3  *
4  * \brief Raster Library - Quantization rules (read/write).
5  *
6  * (C) 1999-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author USACERL and many others
13  */
14 
15 #include <string.h>
16 
17 #include <grass/gis.h>
18 #include <grass/raster.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Writes the quant rules.
23 
24  Writes the quant rules which indicate that all floating numbers
25  should be truncated instead of applying any quant rules from
26  floats to integers.
27 
28  \param name map name
29  \param mapset mapset name
30  */
31 void Rast_truncate_fp_map(const char *name, const char *mapset)
32 {
33  struct Quant quant;
34 
35  Rast_quant_init(&quant);
36  Rast_quant_truncate(&quant);
37  /* quantize the map */
38  Rast_write_quant(name, mapset, &quant);
39 }
40 
41 /*!
42  \brief Writes the quant rules.
43 
44  Writes the quant rules which indicate that all floating numbers
45  should be rounded instead of applying any quant rules from
46  floats to integers.
47 
48  \param name map name
49  \param mapset mapset name
50  */
51 void Rast_round_fp_map(const char *name, const char *mapset)
52 {
53  struct Quant quant;
54 
55  Rast_quant_init(&quant);
56  Rast_quant_round(&quant);
57  /* round the map */
58  Rast_write_quant(name, mapset, &quant);
59 }
60 
61 /*!
62  * \brief Write quant rules (f_quant) for floating-point raster map.
63  *
64  * Writes the <tt>f_quant</tt> file for the raster map <em>name</em>
65  * with one rule. The rule is generated using the floating-point range
66  * in <tt>f_range</tt> producing the integer range
67  * [<em>cmin,cmax</em>].
68  *
69  * Make a rule for map <name> that maps floating range (d_min, d_max)
70  * into integer range (min, max)
71  * This function is useful when the quant rule doesn't depend of the
72  * range of produced float data, for example the slope map whould
73  * want to have a quant rule: 0.0, 90.0 -> 0 , 90
74  * no matter what the min and max slope of this map is.
75 
76  * \param name map name
77  * \param mapset mapset name
78  * \param cmin minimum value
79  * \param cmax maximum value
80  */
81 void Rast_quantize_fp_map(const char *name, const char *mapset,
82  CELL min, CELL max)
83 {
84  DCELL d_min, d_max;
85  struct FPRange fp_range;
86 
87  if (Rast_read_fp_range(name, mapset, &fp_range) < 0)
88  G_fatal_error(_("Unable to read fp range for raster map <%s>"),
89  G_fully_qualified_name(name, mapset));
90  Rast_get_fp_range_min_max(&fp_range, &d_min, &d_max);
91  if (Rast_is_d_null_value(&d_min) || Rast_is_d_null_value(&d_max))
92  G_fatal_error(_("Raster map <%s> is empty"),
93  G_fully_qualified_name(name, mapset));
94  Rast_quantize_fp_map_range(name, mapset, d_min, d_max, min, max);
95 }
96 
97 /*!
98  * \brief Write quant rules (f_quant) for floating-point raster map.
99  *
100  * Writes the <tt>f_quant</tt> file for the raster map
101  * <em>name</em> with one rule. The rule is generated using the floating-point
102  * range [<em>dmin,dmax</em>] and the integer range
103  * [<em>min,max</em>].
104  * This routine differs from the one above in that the application controls the
105  * floating-point range. For example, r.slope.aspect will use this routine to
106  * quantize the slope map from [0.0, 90.0] to [0,
107  * 90] even if the range of slopes is not 0-90. The aspect map would be
108  * quantized from [0.0, 360.0] to [0, 360].
109  *
110  * Make a rule for map <name> that maps floating range (d_min, d_max)
111  * into integer range (min, max)
112  * This function is useful when the quant rule doesn't depend of the
113  * range of produced float data, for example the slope map whould
114  * want to have a quant rule: 0.0, 90.0 -> 0 , 90
115  * no matter what the min and max slope of this map is.
116  *
117  * \param name map name
118  * \param mapset mapset name
119  * \param d_min minimum fp value
120  * \param d_max maximum fp value
121  * \param min minimum value
122  * \param max maximum value
123  */
124 void Rast_quantize_fp_map_range(const char *name, const char *mapset,
125  DCELL d_min, DCELL d_max, CELL min, CELL max)
126 {
127  struct Quant quant;
128 
129  Rast_quant_init(&quant);
130  Rast_quant_add_rule(&quant, d_min, d_max, min, max);
131  /* quantize the map */
132  Rast_write_quant(name, mapset, &quant);
133 }
134 
135 /*!
136  * \brief Writes the quant rule table for the raster map
137  *
138  * Writes the <tt>f_quant</tt> file for the raster map <em>name</em>
139  * from <em>q</em>. if mapset==G_mapset() i.e. the map is in current
140  * mapset, then the original quant file in cell_misc/map/f_quant is
141  * written. Otherwise <em>q</em> is written into quant2/mapset/name
142  * (much like colr2 element). This results in map@mapset being read
143  * using quant rules stored in <em>q</em> from G_mapset(). See
144  * Rast_read_quant() for detailes.
145  *
146  * \param name map name
147  * \param mapset mapset name
148  * \param quant pointer to Quant structure which hold quant rules info
149  */
150 void Rast_write_quant(const char *name, const char *mapset,
151  const struct Quant *quant)
152 {
153  CELL cell_min, cell_max;
154  DCELL d_min, d_max;
155 
156  if (Rast_map_type(name, mapset) == CELL_TYPE) {
157  G_warning(_("Unable to write quant rules: raster map <%s> is integer"),
158  name);
159  return;
160  }
161 
162  Rast_quant_get_limits(quant, &d_min, &d_max, &cell_min, &cell_max);
163 
164  /* first actually write the rules */
165  if (Rast__quant_export(name, mapset, quant) < 0)
166  G_fatal_error(_("Unable to write quant rules for raster map <%s>"), name);
167 }
168 
169 /*!
170  * \brief
171  *
172  * Reads quantization rules for <i>name</i> in <i>mapset</i> and
173  * stores them in the quantization structure. If the map is in another
174  * mapset, first checks for quant2 table for this map in current
175  * mapset.
176  * \param name
177  * \param mapset
178  * \param q
179  *
180  * \return -2 if raster map is of type integer
181  * \return -1 if (!G_name_is_fully_qualified())
182  * \return 0 if quantization file does not exist, or the file is empty or has wrong format
183  * \return 1 if non-empty quantization file exists
184  *
185  */
186 int Rast_read_quant(const char *name, const char *mapset, struct Quant *quant)
187 {
188  Rast_quant_init(quant);
189  return Rast__quant_import(name, mapset, quant);
190 }
#define CELL_TYPE
Definition: raster.h:11
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void Rast_round_fp_map(const char *name, const char *mapset)
Writes the quant rules.
Definition: quant_rw.c:51
void Rast_quantize_fp_map(const char *name, const char *mapset, CELL min, CELL max)
Write quant rules (f_quant) for floating-point raster map.
Definition: quant_rw.c:81
#define Rast_is_d_null_value(dcellVal)
Definition: defs/raster.h:414
#define min(x, y)
Definition: draw2.c:31
double DCELL
Definition: gis.h:614
void Rast_quantize_fp_map_range(const char *name, const char *mapset, DCELL d_min, DCELL d_max, CELL min, CELL max)
Write quant rules (f_quant) for floating-point raster map.
Definition: quant_rw.c:124
#define max(x, y)
Definition: draw2.c:32
void Rast_quant_init(struct Quant *)
Initialize the structure.
Definition: quant.c:174
void Rast_truncate_fp_map(const char *name, const char *mapset)
Writes the quant rules.
Definition: quant_rw.c:31
void Rast_get_fp_range_min_max(const struct FPRange *, DCELL *, DCELL *)
Get minimum and maximum value from fp range.
Definition: range.c:764
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition: nme_in_mps.c:101
Definition: raster.h:84
int Rast__quant_import(const char *, const char *, struct Quant *)
Reads quantization rules (internal use only)
Definition: quant_io.c:94
int Rast__quant_export(const char *, const char *, const struct Quant *)
Writes the quantization rules (internal use only)
Definition: quant_io.c:268
int Rast_read_quant(const char *name, const char *mapset, struct Quant *quant)
Reads quantization rules for name in mapset and stores them in the quantization structure. If the map is in another mapset, first checks for quant2 table for this map in current mapset.
Definition: quant_rw.c:186
int Rast_quant_get_limits(const struct Quant *, DCELL *, DCELL *, CELL *, CELL *)
Returns the minimum and maximum cell and dcell values of all the ranges defined.
Definition: quant.c:281
int Rast_read_fp_range(const char *, const char *, struct FPRange *)
Read floating-point range.
Definition: range.c:71
void Rast_write_quant(const char *name, const char *mapset, const struct Quant *quant)
Writes the quant rule table for the raster map.
Definition: quant_rw.c:150
void Rast_quant_add_rule(struct Quant *, DCELL, DCELL, CELL, CELL)
Adds a new rule to the set of quantization rules.
Definition: quant.c:473
void G_warning(const char *,...) __attribute__((format(printf
int CELL
Definition: gis.h:613
#define _(str)
Definition: glocale.h:10
void Rast_quant_round(struct Quant *)
Sets the quant rules to perform simple rounding on floats.
Definition: quant.c:229
const char * name
Definition: named_colr.c:7
void Rast_quant_truncate(struct Quant *)
Sets the quant rules to perform simple truncation on floats.
Definition: quant.c:216
RASTER_MAP_TYPE Rast_map_type(const char *, const char *)
Determine raster data type.
Definition: raster/open.c:880