GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
raster3d/cats.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 
6 #include <grass/gis.h>
7 #include <grass/raster.h>
8 
9 #include "raster3d_intern.h"
10 
11 /*---------------------------------------------------------------------------*/
12 
13 /*!
14  * \brief
15  *
16  * Writes the
17  * categories stored in the <em>cats</em> structure into the categories file for
18  * map <em>name</em> in the current mapset. See <em>Rast_write_cats</em>
19  * (Raster_Category_File) for details and return values.
20  *
21  * \param name
22  * \param cats
23  * \return int
24  */
25 
26 int Rast3d_write_cats(const char *name, struct Categories *cats)
27 /* adapted from Rast_write_cats */
28 {
29  FILE *fd;
30  int i;
31  const char *descr;
32  DCELL val1, val2;
33  char str1[100], str2[100];
34 
36  if (!fd)
37  return -1;
38 
39  /* write # cats - note # indicate 3.0 or later */
40  fprintf(fd, "# %ld categories\n", (long)cats->num);
41 
42  /* title */
43  fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
44 
45  /* write format and coefficients */
46  fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
47  fprintf(fd, "%.2f %.2f %.2f %.2f\n", cats->m1, cats->a1, cats->m2,
48  cats->a2);
49 
50  /* write the cat numbers:label */
51  for (i = 0; i < Rast_quant_nof_rules(&cats->q); i++) {
52  descr = Rast_get_ith_d_cat(cats, i, &val1, &val2);
53  if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) {
54  if (val1 == val2) {
55  sprintf(str1, "%.10f", val1);
56  G_trim_decimal(str1);
57  fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
58  }
59  else {
60  sprintf(str1, "%.10f", val1);
61  G_trim_decimal(str1);
62  sprintf(str2, "%.10f", val2);
63  G_trim_decimal(str2);
64  fprintf(fd, "%s:%s:%s\n", str1, str2,
65  descr != NULL ? descr : "");
66  }
67  }
68  }
69  fclose(fd);
70  return 1;
71 }
72 
73 /*---------------------------------------------------------------------------*/
74 
75 static int read_cats(const char *name, const char *mapset,
76  struct Categories *pcats)
77 /* adapted from G__read_cats */
78 {
79  FILE *fd;
80  char buff[1024];
81  CELL cat;
82  DCELL val1, val2;
83  int old;
84  long num = -1;
85 
87  mapset);
88  if (!fd)
89  return -2;
90 
91  /* Read the number of categories */
92  if (G_getl(buff, sizeof(buff), fd) == 0)
93  goto error;
94 
95  if (sscanf(buff, "# %ld", &num) == 1)
96  old = 0;
97  else if (sscanf(buff, "%ld", &num) == 1)
98  old = 1;
99 
100  /* Read the title for the file */
101  if (G_getl(buff, sizeof(buff), fd) == 0)
102  goto error;
103  G_strip(buff);
104 
105  Rast_init_cats(buff, pcats);
106  if (num >= 0)
107  pcats->num = num;
108 
109  if (!old) {
110  char fmt[256];
111  float m1, a1, m2, a2;
112 
113  if (G_getl(fmt, sizeof(fmt), fd) == 0)
114  goto error;
115  /* next line contains equation coefficients */
116  if (G_getl(buff, sizeof(buff), fd) == 0)
117  goto error;
118  if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
119  goto error;
120  Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
121  }
122 
123  /* Read all category names */
124  for (cat = 0;; cat++) {
125  char label[1024];
126 
127  if (G_getl(buff, sizeof(buff), fd) == 0)
128  break;
129 
130  if (old)
131  Rast_set_c_cat(&cat, &cat, buff, pcats);
132  else {
133  *label = 0;
134  if (sscanf(buff, "%1s", label) != 1)
135  continue;
136  if (*label == '#')
137  continue;
138  *label = 0;
139 
140  /* try to read a range of data */
141  if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
142  Rast_set_cat(&val1, &val2, label, pcats, DCELL_TYPE);
143  else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
144  Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
145  else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
146  Rast_set_cat(&val1, &val1, label, pcats, DCELL_TYPE);
147  else
148  goto error;
149  }
150  }
151 
152  fclose(fd);
153  return 0;
154 
155 error:
156  fclose(fd);
157  return -1;
158 }
159 
160 /*---------------------------------------------------------------------------*/
161 
162 /*!
163  * \brief
164  *
165  * Reads the categories file for map <em>name</em> in <em>mapset</em> and
166  * stores the categories in the <em>pcats</em> structure. See
167  * <em>Rast_read_cats</em> (Raster_Category_File) for details and return values.
168  *
169  * \param name
170  * \param mapset
171  * \param pcats
172  * \return int
173  */
174 
175 int Rast3d_read_cats(const char *name, const char *mapset,
176  struct Categories *pcats)
177 /* adapted from Rast_read_cats */
178 {
179  const char *type;
180 
181  switch (read_cats(name, mapset, pcats)) {
182  case -2:
183  type = "missing";
184  break;
185  case -1:
186  type = "invalid";
187  break;
188  default:
189  return 0;
190  }
191 
192  G_warning("category support for [%s] in mapset [%s] %s", name, mapset,
193  type);
194  return -1;
195 }
#define NULL
Definition: ccmath.h:32
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database misc file for reading
Definition: open_misc.c:205
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database misc file
Definition: open_misc.c:178
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition: strings.c:300
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
Definition: getl.c:33
char * Rast_get_ith_d_cat(const struct Categories *, int, DCELL *, DCELL *)
Get category description (DCELL)
Definition: raster/cats.c:1029
void Rast_set_cats_fmt(const char *, double, double, double, double, struct Categories *)
Set category fmt (?)
Definition: raster/cats.c:1190
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
Definition: raster/cats.c:1145
int Rast_set_cat(const void *, const void *, const char *, struct Categories *, RASTER_MAP_TYPE)
Set a raster category label.
Definition: raster/cats.c:916
int Rast_set_c_cat(const CELL *, const CELL *, const char *, struct Categories *)
Set a raster category label (CELL)
Definition: raster/cats.c:766
int Rast_quant_nof_rules(const struct Quant *)
Returns the number of quantization rules defined.
Definition: quant.c:309
double DCELL
Definition: gis.h:629
int CELL
Definition: gis.h:628
const char * name
Definition: named_colr.c:6
int Rast3d_write_cats(const char *name, struct Categories *cats)
Writes the categories stored in the cats structure into the categories file for map name in the curre...
Definition: raster3d/cats.c:26
int Rast3d_read_cats(const char *name, const char *mapset, struct Categories *pcats)
Reads the categories file for map name in mapset and stores the categories in the pcats structure....
#define RASTER3D_DIRECTORY
Definition: raster3d.h:31
#define RASTER3D_CATS_ELEMENT
Definition: raster3d.h:33
#define DCELL_TYPE
Definition: raster.h:13
#define CELL_TYPE
Definition: raster.h:11
float a2
Definition: raster.h:131
float m2
Definition: raster.h:130
CELL num
Definition: raster.h:123
char * fmt
Definition: raster.h:127
float m1
Definition: raster.h:128
char * title
Definition: raster.h:126
float a1
Definition: raster.h:129
struct Quant q
Definition: raster.h:132