GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
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 */
25int Rast3d_write_cats(const char *name, struct Categories *cats)
26/* adapted from Rast_write_cats */
27{
28 FILE *fd;
29 int i;
30 const char *descr;
32 char str1[100], str2[100];
33
35 if (!fd)
36 return -1;
37
38 /* write # cats - note # indicate 3.0 or later */
39 fprintf(fd, "# %ld categories\n", (long)cats->num);
40
41 /* title */
42 fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
43
44 /* write format and coefficients */
45 fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
46 fprintf(fd, "%.2f %.2f %.2f %.2f\n", cats->m1, cats->a1, cats->m2,
47 cats->a2);
48
49 /* write the cat numbers:label */
50 for (i = 0; i < Rast_quant_nof_rules(&cats->q); i++) {
51 descr = Rast_get_ith_d_cat(cats, i, &val1, &val2);
52 if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) {
53 if (val1 == val2) {
54 snprintf(str1, sizeof(str1), "%.10f", val1);
56 fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
57 }
58 else {
59 snprintf(str1, sizeof(str1), "%.10f", val1);
61 snprintf(str2, sizeof(str2), "%.10f", val2);
63 fprintf(fd, "%s:%s:%s\n", str1, str2,
64 descr != NULL ? descr : "");
65 }
66 }
67 }
68 fclose(fd);
69 return 1;
70}
71
72/*---------------------------------------------------------------------------*/
73
74static int read_cats(const char *name, const char *mapset,
75 struct Categories *pcats)
76/* adapted from G__read_cats */
77{
78 FILE *fd;
79 char buff[1024];
80 CELL cat;
82 int old;
83 long num = -1;
84
86 mapset);
87 if (!fd)
88 return -2;
89
90 /* Read the number of categories */
91 if (G_getl(buff, sizeof(buff), fd) == 0)
92 goto error;
93
94 if (sscanf(buff, "# %ld", &num) == 1)
95 old = 0;
96 else if (sscanf(buff, "%ld", &num) == 1)
97 old = 1;
98
99 /* Read the title for the file */
100 if (G_getl(buff, sizeof(buff), fd) == 0)
101 goto error;
102 G_strip(buff);
103
104 Rast_init_cats(buff, pcats);
105 if (num >= 0)
106 pcats->num = num;
107
108 if (!old) {
109 char fmt[256];
110 float m1, a1, m2, a2;
111
112 if (G_getl(fmt, sizeof(fmt), fd) == 0)
113 goto error;
114 /* next line contains equation coefficients */
115 if (G_getl(buff, sizeof(buff), fd) == 0)
116 goto error;
117 if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
118 goto error;
119 Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
120 }
121
122 /* Read all category names */
123 for (cat = 0;; cat++) {
124 char label[1024];
125
126 if (G_getl(buff, sizeof(buff), fd) == 0)
127 break;
128
129 if (old)
130 Rast_set_c_cat(&cat, &cat, buff, pcats);
131 else {
132 *label = 0;
133 if (sscanf(buff, "%1s", label) != 1)
134 continue;
135 if (*label == '#')
136 continue;
137 *label = 0;
138
139 /* try to read a range of data */
140 if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
142 else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
143 Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
144 else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
146 else
147 goto error;
148 }
149 }
150
151 fclose(fd);
152 return 0;
153
154error:
155 fclose(fd);
156 return -1;
157}
158
159/*---------------------------------------------------------------------------*/
160
161/*!
162 * \brief
163 *
164 * Reads the categories file for map <em>name</em> in <em>mapset</em> and
165 * stores the categories in the <em>pcats</em> structure. See
166 * <em>Rast_read_cats</em> (Raster_Category_File) for details and return values.
167 *
168 * \param name
169 * \param mapset
170 * \param pcats
171 * \return int
172 */
173int Rast3d_read_cats(const char *name, const char *mapset,
174 struct Categories *pcats)
175/* adapted from Rast_read_cats */
176{
177 const char *type;
178
179 switch (read_cats(name, mapset, pcats)) {
180 case -2:
181 type = "missing";
182 break;
183 case -1:
184 type = "invalid";
185 break;
186 default:
187 return 0;
188 }
189
190 G_warning("category support for [%s] in mapset [%s] %s", name, mapset,
191 type);
192 return -1;
193}
#define NULL
Definition ccmath.h:32
void G_warning(const char *,...) __attribute__((format(printf
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
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database misc file for reading
Definition open_misc.c:210
int G_getl(char *, int, FILE *)
Gets a line of text from a file.
Definition getl.c:33
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database misc file
Definition open_misc.c:183
void Rast_set_cats_fmt(const char *, double, double, double, double, struct Categories *)
Set category fmt (?)
void Rast_init_cats(const char *, struct Categories *)
Initialize category structure.
char * Rast_get_ith_d_cat(const struct Categories *, int, DCELL *, DCELL *)
Get category description (DCELL)
int Rast_set_cat(const void *, const void *, const char *, struct Categories *, RASTER_MAP_TYPE)
Set a raster category label.
int Rast_set_c_cat(const CELL *, const CELL *, const char *, struct Categories *)
Set a raster category label (CELL)
int Rast_quant_nof_rules(const struct Quant *)
Returns the number of quantization rules defined.
Definition quant.c:309
double DCELL
Definition gis.h:635
int CELL
Definition gis.h:634
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...
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