GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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 /*!
15  * \brief
16  *
17  * Writes the
18  * categories stored in the <em>cats</em> structure into the categories file for
19  * map <em>name</em> in the current mapset. See <em>Rast_write_cats</em>
20  * (Raster_Category_File) for details and return values.
21  *
22  * \param name
23  * \param cats
24  * \return int
25  */
26 
27 int Rast3d_write_cats(const char *name, struct Categories *cats)
28  /* adapted from Rast_write_cats */
29 {
30  FILE *fd;
31  int i;
32  const char *descr;
33  DCELL val1, val2;
34  char str1[100], str2[100];
35 
37  if (!fd)
38  return -1;
39 
40  /* write # cats - note # indicate 3.0 or later */
41  fprintf(fd, "# %ld categories\n", (long)cats->num);
42 
43  /* title */
44  fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
45 
46  /* write format and coefficients */
47  fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
48  fprintf(fd, "%.2f %.2f %.2f %.2f\n",
49  cats->m1, cats->a1, cats->m2, cats->a2);
50 
51  /* write the cat numbers:label */
52  for (i = 0; i < Rast_quant_nof_rules(&cats->q); i++) {
53  descr = Rast_get_ith_d_cat(cats, i, &val1, &val2);
54  if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) {
55  if (val1 == val2) {
56  sprintf(str1, "%.10f", val1);
57  G_trim_decimal(str1);
58  fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
59  }
60  else {
61  sprintf(str1, "%.10f", val1);
62  G_trim_decimal(str1);
63  sprintf(str2, "%.10f", val2);
64  G_trim_decimal(str2);
65  fprintf(fd, "%s:%s:%s\n", str1, str2,
66  descr != NULL ? descr : "");
67  }
68  }
69  }
70  fclose(fd);
71  return 1;
72 }
73 
74 /*---------------------------------------------------------------------------*/
75 
76 static int
77 read_cats(const char *name, const char *mapset, struct Categories *pcats)
78  /* adapted from G__read_cats */
79 {
80  FILE *fd;
81  char buff[1024];
82  CELL cat;
83  DCELL val1, val2;
84  int old;
85  long num = -1;
86 
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 /*!
164  * \brief
165  *
166  * Reads the categories file for map <em>name</em> in <em>mapset</em> and
167  * stores the categories in the <em>pcats</em> structure. See <em>Rast_read_cats</em>
168  * (Raster_Category_File) for details and return values.
169  *
170  * \param name
171  * \param mapset
172  * \param pcats
173  * \return int
174  */
175 
176 int
177 Rast3d_read_cats(const char *name, const char *mapset, struct Categories *pcats)
178  /* adapted from Rast_read_cats */
179 {
180  const char *type;
181 
182  switch (read_cats(name, mapset, pcats)) {
183  case -2:
184  type = "missing";
185  break;
186  case -1:
187  type = "invalid";
188  break;
189  default:
190  return 0;
191  }
192 
193  G_warning("category support for [%s] in mapset [%s] %s",
194  name, mapset, type);
195  return -1;
196 }
#define CELL_TYPE
Definition: raster.h:11
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
Definition: getl.c:31
float m2
Definition: raster.h:137
float a1
Definition: raster.h:136
double DCELL
Definition: gis.h:603
#define RASTER3D_CATS_ELEMENT
Definition: raster3d.h:33
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:27
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition: strings.c:300
#define NULL
Definition: ccmath.h:32
int Rast_set_cat(const void *, const void *, const char *, struct Categories *, RASTER_MAP_TYPE)
Set a raster category label.
Definition: raster/cats.c:914
int Rast_quant_nof_rules(const struct Quant *)
Returns the number of quantization rules defined.
Definition: quant.c:315
#define DCELL_TYPE
Definition: raster.h:13
CELL num
Definition: raster.h:130
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...
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database file for reading
Definition: open_misc.c:210
char * title
Definition: raster.h:133
char * fmt
Definition: raster.h:134
char * Rast_get_ith_d_cat(const struct Categories *, int, DCELL *, DCELL *)
Get category description (DCELL)
Definition: raster/cats.c:1029
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
Definition: raster/cats.c:1145
int Rast_set_c_cat(const CELL *, const CELL *, const char *, struct Categories *)
Set a raster category label (CELL)
Definition: raster/cats.c:770
#define RASTER3D_DIRECTORY
Definition: raster3d.h:31
float m1
Definition: raster.h:135
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database file
Definition: open_misc.c:182
void G_warning(const char *,...) __attribute__((format(printf
int CELL
Definition: gis.h:602
void Rast_set_cats_fmt(const char *, double, double, double, double, struct Categories *)
Set category fmt (?)
Definition: raster/cats.c:1190
const char * name
Definition: named_colr.c:7
float a2
Definition: raster.h:138
struct Quant q
Definition: raster.h:139
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:24