GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
quant_rw.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/glocale.h>
3 #include <string.h>
4 
5 /*********************************************************************
6 *
7 * G_quantize_fp_map(name, mapset, min, max)
8 * char *name, *mapset; name of the map
9 * CELL min, max; resulting int range
10 *
11 * Writes necessary quant rules for map <name> so that
12 * a floating range of <name> is mapped into integer range (min, max)
13 *
14 **********************************************************************
15 *
16 * G_quantize_fp_map_range(name, mapset, d_min, d_max, min, max)
17 * char *name, *mapset; name of the map
18 * CELL min, max; resulting int range
19 * DCELL d_min, d_max; floating point range
20 *
21 * Make a rule for map <name> that maps floating range (d_min, d_max)
22 * into integer range (min, max)
23 * This function is useful when the quant rule doesn't depend of the
24 * range of produced float data, for example the slope map whould
25 * want to have a quant rule: 0.0, 90.0 -> 0 , 90
26 * no matter what the min and max slope of this map is.
27 *
28 **********************************************************************
29 *
30 * G_write_quant(name, mapset, quant)
31 * char *name, *mapset;
32 * struct Quant *quant;
33 * writes the quant rule table for the map <name>
34 *
35 **********************************************************************
36 *
37 * G_read_quant(name, mapset, quant)
38 * char *name, *mapset;
39 *
40 * reads the quant table for name@mapset
41 *
42 **********************************************************************
43 *
44 * G_truncate_fp_map(name, mapset)
45 * char *name, *mapset;
46 * struct Quant *quant;
47 *
48 * writes the quant rules which indicate that all floating numbers
49 * should be truncated instead of applying any quant rules from
50 * floats to integers
51 *
52 **********************************************************************
53 *
54 * G_round_fp_map(name, mapset)
55 * char *name, *mapset;
56 * struct Quant *quant;
57 *
58 * writes the quant rules which indicate that all floating numbers
59 * should be rounded instead of applying any quant rules from
60 * floats to integers
61 *
62 **********************************************************************/
63 
64 int G_truncate_fp_map(const char *name, const char *mapset)
65 {
66  char buf[300];
67  struct Quant quant;
68 
69  G_quant_init(&quant);
70  G_quant_truncate(&quant);
71  /* quantize the map */
72  if (G_write_quant(name, mapset, &quant) < 0) {
73  sprintf(buf, "G_truncate_fp_map: can't write quant rules for map %s",
74  name);
75  G_warning(buf);
76  return -1;
77  }
78  return 1;
79 }
80 
81 int G_round_fp_map(const char *name, const char *mapset)
82 {
83  char buf[300];
84  struct Quant quant;
85 
86  G_quant_init(&quant);
87  G_quant_round(&quant);
88  /* round the map */
89  if (G_write_quant(name, mapset, &quant) < 0) {
90  sprintf(buf, "G_truncate_fp_map: can't write quant rules for map %s",
91  name);
92  G_warning(buf);
93  return -1;
94  }
95  return 1;
96 }
97 
98 
113 int G_quantize_fp_map(const char *name, const char *mapset,
114  CELL min, CELL max)
115 {
116  char buf[300];
117  DCELL d_min, d_max;
118  struct FPRange fp_range;
119 
120  if (G_read_fp_range(name, mapset, &fp_range) < 0) {
121  sprintf(buf, "G_quantize_fp_map: can't read fp range for map %s",
122  name);
123  G_warning(buf);
124  return -1;
125  }
126  G_get_fp_range_min_max(&fp_range, &d_min, &d_max);
127  if (G_is_d_null_value(&d_min) || G_is_d_null_value(&d_max)) {
128  sprintf(buf, "G_quantize_fp_map: raster map %s is empty", name);
129  G_warning(buf);
130  return -1;
131  }
132  return G_quantize_fp_map_range(name, mapset, d_min, d_max, min, max);
133 }
134 
135 /*-------------------------------------------------------------------------*/
136 
137 
159 int G_quantize_fp_map_range(const char *name, const char *mapset,
160  DCELL d_min, DCELL d_max, CELL min, CELL max)
161 {
162  char buf[300];
163  struct Quant quant;
164 
165  G_quant_init(&quant);
166  G_quant_add_rule(&quant, d_min, d_max, min, max);
167  /* quantize the map */
168  if (G_write_quant(name, mapset, &quant) < 0) {
169  sprintf(buf,
170  "G_quantize_fp_map_range: can't write quant rules for map %s",
171  name);
172  G_warning(buf);
173  return -1;
174  }
175  return 1;
176 }
177 
178 /*-------------------------------------------------------------------------*/
179 
180 
181 
198 int G_write_quant(const char *name, const char *mapset,
199  const struct Quant *quant)
200 {
201  CELL cell_min, cell_max;
202  DCELL d_min, d_max;
203  char buf[300];
204 
205  if (G_raster_map_type(name, mapset) == CELL_TYPE) {
206  sprintf(buf, _("Cannot write quant rules: map %s is integer"), name);
207  G_warning(buf);
208  return -1;
209  }
210 
211  G_quant_get_limits(quant, &d_min, &d_max, &cell_min, &cell_max);
212 
213  /* first actually write the rules */
214  if (G__quant_export(name, mapset, quant) < 0) {
215  sprintf(buf, _("Cannot write quant rules for map %s"), name);
216  G_warning(buf);
217  return -1;
218  }
219 
220  return 1;
221 }
222 
223 /*-------------------------------------------------------------------------*/
224 
225 
245 int G_read_quant(const char *name, const char *mapset, struct Quant *quant)
246 {
247  G_quant_init(quant);
248  return G__quant_import(name, mapset, quant);
249 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_truncate_fp_map(const char *name, const char *mapset)
Definition: quant_rw.c:64
int G_quantize_fp_map(const char *name, const char *mapset, CELL min, CELL max)
Writes the f_quant file for the raster map name with one rule. The rule is generated using the floati...
Definition: quant_rw.c:113
string name
Definition: render.py:1314
#define min(x, y)
Definition: draw2.c:68
int G_read_quant(const char *name, const char *mapset, struct Quant *quant)
reads quantization rules for &quot;name&quot; in &quot;mapset&quot; and stores them in the quantization structure &quot;quant&quot;...
Definition: quant_rw.c:245
#define max(x, y)
Definition: draw2.c:69
int G_round_fp_map(const char *name, const char *mapset)
Definition: quant_rw.c:81
int G__quant_import(const char *name, const char *mapset, struct Quant *quant)
Definition: quant_io.c:113
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
Definition: null_val.c:306
int G_quant_get_limits(const struct Quant *q, DCELL *dMin, DCELL *dMax, CELL *cMin, CELL *cMax)
Extracts the minimum and maximum floating-point and integer values from all the rules (except the &quot;in...
Definition: quant.c:534
int G__quant_export(const char *name, const char *mapset, const struct Quant *quant)
Definition: quant_io.c:281
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
void G_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
Definition: quant.c:655
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_get_fp_range_min_max(const struct FPRange *range, DCELL *min, DCELL *max)
Extract the min/max from the range structure r. If the range structure has no defined min/max (first!...
Definition: range.c:667
int G_quant_init(struct Quant *quant)
Initializes the q struct.
Definition: quant.c:422
int G_read_fp_range(const char *name, const char *mapset, struct FPRange *drange)
Read the floating point range file f_range. This file is written in binary using XDR format...
Definition: range.c:139
RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
Determine raster data type.
Definition: opencell.c:1001
int G_quantize_fp_map_range(const char *name, const char *mapset, DCELL d_min, DCELL d_max, CELL min, CELL max)
Writes the f_quant file for the raster map name with one rule. The rule is generated using the floati...
Definition: quant_rw.c:159
int G_write_quant(const char *name, const char *mapset, const struct Quant *quant)
Writes the f_quant file for the raster map name from q. if mapset==G_mapset() i.e. the map is in current mapset, then the original quant file in cell_misc/map/f_quant is written. Otherwise q is written into quant2/mapset/name (much like colr2 element). This results in map being read using quant rules stored in q from G_mapset(). See G_read_quant() for detailes.
Definition: quant_rw.c:198
int G_quant_round(struct Quant *quant)
Definition: quant.c:479
int G_quant_truncate(struct Quant *quant)
sets the quant for q rules to perform simple truncation on floats.
Definition: quant.c:471