GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dcats.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 #include <rpc/types.h>
6 #include <rpc/xdr.h>
7 #include <grass/gis.h>
8 #include "G3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 
26 int G3d_writeCats(const char *name, struct Categories *cats)
27  /* adapted from G_write_cats */
28 {
29  FILE *fd;
30  int i;
31  const char *descr;
32  DCELL val1, val2;
33  char str1[100], str2[100], buf[200], buf2[200], xname[GNAME_MAX],
34  xmapset[GMAPSET_MAX];
35 
36  if (G__name_is_fully_qualified(name, xname, xmapset)) {
37  sprintf(buf, "%s/%s", G3D_DIRECTORY, xname);
38  sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats@mapset */
39  }
40  else {
41  sprintf(buf, "%s/%s", G3D_DIRECTORY, name);
42  sprintf(buf2, "%s", G3D_CATS_ELEMENT);
43  }
44 
45  if (!(fd = G_fopen_new(buf, buf2)))
46  return -1;
47 
48  /* write # cats - note # indicate 3.0 or later */
49  fprintf(fd, "# %ld categories\n", (long)cats->num);
50 
51  /* title */
52  fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
53 
54  /* write format and coefficients */
55  fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
56  fprintf(fd, "%.2f %.2f %.2f %.2f\n",
57  cats->m1, cats->a1, cats->m2, cats->a2);
58 
59  /* write the cat numbers:label */
60  for (i = 0; i < G_quant_nof_rules(&cats->q); i++) {
61  descr = G_get_ith_d_raster_cat(cats, i, &val1, &val2);
62  if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) {
63  if (val1 == val2) {
64  sprintf(str1, "%.10f", val1);
65  G_trim_decimal(str1);
66  fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
67  }
68  else {
69  sprintf(str1, "%.10f", val1);
70  G_trim_decimal(str1);
71  sprintf(str2, "%.10f", val2);
72  G_trim_decimal(str2);
73  fprintf(fd, "%s:%s:%s\n", str1, str2,
74  descr != NULL ? descr : "");
75  }
76  }
77  }
78  fclose(fd);
79  return 1;
80 }
81 
82 /*---------------------------------------------------------------------------*/
83 
84 static int
85 read_cats(const char *name, const char *mapset, struct Categories *pcats)
86  /* adapted from G__read_cats */
87 {
88  FILE *fd;
89  char buff[1024], buf2[200], xname[512], xmapset[512];
90  CELL cat;
91  DCELL val1, val2;
92  int old;
93  long num = -1;
94 
95  if (G__name_is_fully_qualified(name, xname, xmapset)) {
96  sprintf(buff, "%s/%s", G3D_DIRECTORY, xname);
97  sprintf(buf2, "%s@%s", G3D_CATS_ELEMENT, xmapset); /* == cats@mapset */
98  }
99  else {
100  sprintf(buff, "%s/%s", G3D_DIRECTORY, name);
101  sprintf(buf2, "%s", G3D_CATS_ELEMENT);
102  }
103 
104  if (!(fd = G_fopen_old(buff, buf2, mapset)))
105  return -2;
106 
107  /* Read the number of categories */
108  if (G_getl(buff, sizeof(buff), fd) == 0)
109  goto error;
110 
111  if (sscanf(buff, "# %ld", &num) == 1)
112  old = 0;
113  else if (sscanf(buff, "%ld", &num) == 1)
114  old = 1;
115 
116  /* Read the title for the file */
117  if (G_getl(buff, sizeof(buff), fd) == 0)
118  goto error;
119  G_strip(buff);
120 
121  G_init_raster_cats(buff, pcats);
122  if (num >= 0)
123  pcats->num = num;
124 
125  if (!old) {
126  char fmt[256];
127  float m1, a1, m2, a2;
128 
129  if (G_getl(fmt, sizeof(fmt), fd) == 0)
130  goto error;
131  /* next line contains equation coefficients */
132  if (G_getl(buff, sizeof(buff), fd) == 0)
133  goto error;
134  if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
135  goto error;
136  G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats);
137  }
138 
139  /* Read all category names */
140  for (cat = 0;; cat++) {
141  char label[1024];
142 
143  if (G_getl(buff, sizeof(buff), fd) == 0)
144  break;
145 
146  if (old)
147  G_set_cat(cat, buff, pcats);
148  else {
149  *label = 0;
150  if (sscanf(buff, "%1s", label) != 1)
151  continue;
152  if (*label == '#')
153  continue;
154  *label = 0;
155 
156  /* try to read a range of data */
157  if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
158  G_set_raster_cat(&val1, &val2, label, pcats, DCELL_TYPE);
159  else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
160  G_set_raster_cat(&cat, &cat, label, pcats, CELL_TYPE);
161  else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
162  G_set_raster_cat(&val1, &val1, label, pcats, DCELL_TYPE);
163  else
164  goto error;
165  }
166  }
167 
168  fclose(fd);
169  return 0;
170 
171  error:
172  fclose(fd);
173  return -1;
174 }
175 
176 /*---------------------------------------------------------------------------*/
177 
178 
192 int
193 G3d_readCats(const char *name, const char *mapset, struct Categories *pcats)
194  /* adapted from G_read_cats */
195 {
196  const char *type;
197 
198  switch (read_cats(name, mapset, pcats)) {
199  case -2:
200  type = "missing";
201  break;
202  case -1:
203  type = "invalid";
204  break;
205  default:
206  return 0;
207  }
208 
209  G_warning("category support for [%s] in mapset [%s] %s",
210  name, mapset, type);
211  return -1;
212 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_set_raster_cat(void *rast1, void *rast2, char *label, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Adds the label for range rast1 through rast2 in category structure pcats.
Definition: gis/cats.c:1160
char xmapset[512]
Definition: g3dcats.c:89
DCELL val1
Definition: g3dcats.c:91
int G_set_raster_cats_fmt(const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
Same as existing G_set_cats_fmt()
Definition: gis/cats.c:1515
tuple label
gridSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY, label = _(&quot;width&quot;)), pos = (1, 1), flag = wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT)
Definition: tools.py:1366
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
long num
Definition: g3dcats.c:93
int G_set_cat(CELL num, char *label, struct Categories *pcats)
set a category label
Definition: gis/cats.c:1001
int G_trim_decimal(char *buf)
Removes trailing zeros from decimal number.
Definition: trim_dec.c:30
char buff[1024]
Definition: g3dcats.c:89
DCELL val2
Definition: g3dcats.c:91
int G_init_raster_cats(const char *title, struct Categories *pcats)
Same as existing G_init_raster_cats() only ncats argument is missign. ncats has no meaning in new Cat...
Definition: gis/cats.c:1437
int G_strip(char *buf)
Removes all leading and trailing white space from string.
Definition: strings.c:389
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int G_quant_nof_rules(const struct Quant *q)
Definition: quant.c:555
int G_getl(char *buf, int n, FILE *fd)
gets a line of text from a file
Definition: getl.c:17
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:197
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
fclose(fd)
char buf2[200]
Definition: g3dcats.c:89
int old
Definition: g3dcats.c:92
CELL cat
Definition: g3dcats.c:90
char * G_get_ith_d_raster_cat(const struct Categories *pcats, int i, DCELL *rast1, DCELL *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
Definition: gis/cats.c:1298
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
char xname[512]
Definition: g3dcats.c:89
int G3d_writeCats(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: g3dcats.c:26
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57