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