GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/cats.c
Go to the documentation of this file.
1 
2 /**********************************************************************
3  *
4  * Code in this file works with category files. There are two formats:
5  * Pre 3.0 direct category encoding form:
6  *
7  * 2 categories
8  * Map Title
9  * Elevation: 1000.00 to 1005.00 feet
10  * Elevation: 1005.00 to 1010.00 feet
11  * Elevation: 1010.00 to 1015.00 feet
12  *
13  * 3.0 format
14  *
15  * # 2 categories
16  * Map Title
17  * Elevation: $1.2 to $2.2 feet ## Format Statement
18  * 5.0 1000 5.0 1005 ## Coefficients
19  *
20  * The coefficient line can be followed by explicit category labels
21  * which override the format label generation.
22  * 0:no data
23  * 2: .
24  * 5: . ## explicit category labels
25  * 7: .
26  * explicit labels can be also of the form:
27  * 5.5:5:9 label description
28  * or
29  * 15:30 label description
30  *
31  * In the format line
32  * $1 refers to the value num*5.0+1000 (ie, using the first 2 coefficients)
33  * $2 refers to the value num*5.0+1005 (ie, using the last 2 coefficients)
34  *
35  * $1.2 will print $1 with 2 decimal places.
36  *
37  * Also, the form $?xxx$yyy$ translates into yyy if the category is 1, xxx
38  * otherwise. The $yyy$ is optional. Thus
39  *
40  * $1 meter$?s
41  *
42  * will become: 1 meter (for category 1)
43  * 2 meters (for category 2), etc.
44  *
45  * The format and coefficients above would be used to generate the
46  * following statement in creation of the format appropriate category
47  * string for category "num":
48  *
49  * sprintf(buff,"Elevation: %.2f to %.2f feet", num*5.0+1000, num*5.0*1005)
50  *
51  * Note: while both the format and coefficent lins must be present
52  * a blank line for the fmt will effectively suppress automatic
53  * label generation
54  *
55  * Note: quant rules of Categories structures are heavily dependent
56  * on the fact that rules are stored in the same order they are entered.
57  * since i-th rule and i-th label are entered at the same time, we
58  * know that i-th rule maps fp range to i, thus we know for sure
59  * that cats.labels[i] corresponds to i-th quant rule
60  *
61  **********************************************************************
62  *
63  * G_read_[raster]_cats (name, mapset, pcats)
64  * char *name name of cell file
65  * char *mapset mapset that cell file belongs to
66  * struct Categories *pcats structure to hold category info
67  *
68  * Reads the category information associated with cell file "name"
69  * in mapset "mapset" into the structure "pcats".
70  *
71  * returns: 0 if successful
72  * -1 on fail
73  *
74  * note: a warning message is printed if the file is
75  * "missing" or "invalid".
76  **********************************************************************
77  *
78  * G_copy_raster_cats (pcats_to, pcats_from)
79  * struct Categories *pcats_to
80  * const struct Categories *pcats_from
81  *
82  * Allocates NEW space for quant rules and labels and copies
83  * all info from "from" cats to "to" cats
84  *
85  * returns: 0 if successful
86  * -1 on fail
87  *
88  **********************************************************************
89  *
90  * G_read_vector_cats (name, mapset, pcats)
91  * char *name name of vector map
92  * char *mapset mapset that vector map belongs to
93  * struct Categories *pcats structure to hold category info
94  *
95  *
96  * returns: 0 if successful
97  * -1 on fail
98  *
99  * note: a warning message is printed if the file is
100  * "missing" or "invalid".
101  **********************************************************************
102  * Returns pointer to a string describing category.
103  **********************************************************************
104  *
105  * G_number_of_cats(name, mapset)
106  * returns the largest category number in the map.
107  * -1 on error
108  * WARING: do not use for fp maps!
109  **********************************************************************
110  *
111  * char *
112  * G_get_c/f/d_raster_cat (num, pcats)
113  * [F/D]CELL *val pointer to cell value
114  * struct Categories *pcats structure to hold category info
115  *
116  * Returns pointer to a string describing category.
117  *
118  **********************************************************************
119  *
120  * char *
121  * G_get_raster_cat (val, pcats, data_type)
122  * void *val pointer to cell value
123  * struct Categories *pcats structure to hold category info
124  * RASTER_MAP_TYPE data_type type of raster cell
125  *
126  * Returns pointer to a string describing category.
127  *
128  **********************************************************************
129  *
130  * char *
131  * G_get_ith_c/f/d_raster_cat (pcats, i, rast1, rast2)
132  * [F/D]CELL *rast1, *rast2 cat range
133  * struct Categories *pcats structure to hold category info
134  *
135  * Returns pointer to a string describing category.
136  *
137  **********************************************************************
138  *
139  * int
140  * G_number_of_raster_cats (pcats)
141  * struct Categories *pcats structure to hold category info
142  *
143  * Returns pcats->ncats number of labels
144  *
145  **********************************************************************
146  *
147  * char *
148  * G_get_ith_raster_cat (pcats, i, rast1, rast2, data_type)
149  * void *rast1, *rast2 cat range
150  * struct Categories *pcats structure to hold category info
151  * RASTER_MAP_TYPE data_type type of raster cell
152  *
153  * Returns pointer to a string describing category.
154  *
155  **********************************************************************
156  *
157  * char *
158  * G_get_[raster]_cats_title (pcats)
159  * struct Categories *pcats structure to hold category info
160  *
161  * Returns pointer to a string with title
162  *
163  **********************************************************************
164  *
165  * G_init_cats (ncats, title, pcats)
166  * CELL ncats number of categories
167  * char *title cell title
168  * struct Categories *pcats structure to hold category info
169  *
170  * Initializes the cats structure for subsequent calls to G_set_cat()
171  **********************************************************************
172  *
173  * G_unmark_raster_cats (pcats)
174  * struct Categories *pcats structure to hold category info
175  *
176  * initialize cats.marks: the statistics of how many values of map
177  * have each label
178  *
179  **********************************************************************
180  *
181  * G_rewind_raster_cats (pcats)
182  * struct Categories *pcats structure to hold category info
183  *
184  * after calll to this function G_get_next_marked_raster_cat() returns
185  * rhe first marked cat label.
186  *
187  **********************************************************************
188  *
189  * char* G_get_next_marked_raster_cat(pcats, rast1, rast2, stats, data_type)
190  * struct Categories *pcats structure to hold category info
191  * void *rast1, *rast2; pointers to raster range
192  * long *stats;
193  * RASTER_MAP_TYPE data_type
194  *
195  * returns the next marked label.
196  * NULL if none found
197  *
198  **********************************************************************
199  *
200  * char* G_get_next_marked_f/d/craster_cat(pcats, rast1, rast2, stats)
201  * struct Categories *pcats structure to hold category info
202  * [D/F]CELL *rast1, *rast2; pointers to raster range
203  * long *stats;
204  *
205  * returns the next marked label.
206  * NULL if none found
207  *
208  **********************************************************************
209  *
210  * G_mark_raster_cats (rast_row, ncols, pcats, data_type)
211  * void *raster_row; raster row to update stats
212  * struct Categories *pcats structure to hold category info
213  * RASTER_MAP_TYPE data_type;
214  * Finds the index of label for each raster cell in a row, and
215  * increases pcats->marks[index]
216  * Note: if there are no explicit cats: only rules for cats, no
217  * marking is done.
218  *
219  **********************************************************************
220  *
221  * G_mark_c/d/f_raster_cats (rast_row, ncols, pcats)
222  * int ncols;
223  * [D?F]CELL *raster_row; raster row to update stats
224  * struct Categories *pcats structure to hold category info
225  *
226  * Finds the index of label for each raster cell in a row, and
227  * increases pcats->marks[index]
228  *
229  **********************************************************************
230  *
231  * G_init_raster_cats (title, pcats)
232  * char *title cell title
233  * struct Categories *pcats structure to hold category info
234  *
235  * Initializes the cats structure for subsequent calls to G_set_cat()
236  *
237  **********************************************************************
238  *
239  * G_set_[raster]_cats_fmt (fmt, m1, a1, m2, a2, pcats)
240  * char *fmt user form of the equation format
241  * float m1,a1,m2,a2 coefficients
242  * struct Categories *pcats structure to hold category info
243  *
244  * configures the cats structure for the equation. Must be called
245  * after G_init_cats().
246  *
247  **********************************************************************
248  *
249  * G_set_[raster]_cats_title (title, pcats)
250  * char *title cell file title
251  * struct Categories *pcats structure holding category info
252  *
253  * Store title as cell file in cats structure
254  * Returns nothing.
255  *
256  **********************************************************************
257  *
258  * G_set_cat (num, label, pcats)
259  * CELL num category number
260  * char *label category label
261  * struct Categories *pcats structure to hold category info
262  *
263  * Adds the string buff to represent category "num" in category structure
264  * pcats.
265  *
266  * Returns: 0 is cat is null value -1 too many cats, 1 ok.
267  *
268  **********************************************************************
269  *
270  * G_set_[f/d/c]_raster_cat (&val1, &val2, label, pcats)
271  * [D/F]CELL *val1, *val2; pointers to raster values
272  * char *label category label
273  * struct Categories *pcats structure to hold category info
274  *
275  * Adds the label for range val1 through val2 in category structure
276  * pcats.
277  *
278  * Returns: 0 if cat is null value -1 too many cats, 1 ok.
279  *
280  **********************************************************************
281  *
282  * G_set_raster_cat (val1, val2, label, pcats, data_type)
283  * void *val1, *val2; pointers to raster values
284  * char *label category label
285  * struct Categories *pcats structure to hold category info
286  * RASTER_MAP_TYPE data_type type of raster cell
287  *
288  * Adds the label for range val1 through val2 in category structure
289  * pcats.
290  *
291  * Returns: 0 if cat is null value -1 too many cats, 1 ok.
292  *
293  **********************************************************************
294  *
295  * G_write_[raster]_cats (name, pcats)
296  * char *name name of cell file
297  * struct Categories *pcats structure holding category info
298  *
299  * Writes the category information associated with cell file "name"
300  * into current mapset from the structure "pcats".
301  *
302  * returns: 1 if successful
303  * -1 on fail
304  **********************************************************************
305  *
306  * G_write_vector_cats (name, pcats)
307  * char *name name of vector map
308  * struct Categories *pcats structure holding category info
309  *
310  * Writes the category information associated with vector map "name"
311  * into current mapset from the structure "pcats".
312  *
313  * returns: 1 if successful
314  * -1 on fail
315  **********************************************************************
316  *
317  * G_free_[raster]_cats (pcats)
318  * struct Categories *pcats structure holding category info
319  *
320  * Releases memory allocated for the cats structure
321  **********************************************************************/
322 
323 #include <stdlib.h>
324 #include <string.h>
325 #include <grass/gis.h>
326 #include <grass/glocale.h>
327 
328 static int get_cond(char **, char *, DCELL);
329 static int get_fmt(char **, char *, int *);
330 static int cmp(const void *, const void *);
331 
332 
347 int G_read_cats(const char *name,
348  const char *mapset, struct Categories *pcats)
349 {
350  return G_read_raster_cats(name, mapset, pcats);
351 }
352 
353 
365 int G_read_raster_cats(const char *name,
366  const char *mapset, struct Categories *pcats)
367 {
368  char *type;
369 
370  switch (G__read_cats("cats", name, mapset, pcats, 1)) {
371  case -2:
372  type = "missing";
373  break;
374  case -1:
375  type = "invalid";
376  break;
377  default:
378  return 0;
379  }
380 
381  G_warning(_("category support for [%s] in mapset [%s] %s"),
382  name, mapset, type);
383  return -1;
384 }
385 
386 
401 int G_read_vector_cats(const char *name,
402  const char *mapset, struct Categories *pcats)
403 {
404  char *type;
405 
406  switch (G__read_cats("dig_cats", name, mapset, pcats, 1)) {
407  case -2:
408  type = "missing";
409  break;
410  case -1:
411  type = "invalid";
412  break;
413  default:
414  return 0;
415  }
416 
417  G_warning(_("category support for vector map [%s] in mapset [%s] %s"),
418  name, mapset, type);
419  return -1;
420 }
421 
422 CELL G_number_of_cats(const char *name, const char *mapset)
423 {
424  struct Range range;
425  CELL min, max;
426 
427  /* return the max category number */
428  if (G_read_range(name, mapset, &range) < 0)
429  return -1;
430  G_get_range_min_max(&range, &min, &max);
431  if (G_is_c_null_value(&max))
432  max = 0;
433  return max;
434 }
435 
436 CELL G__read_cats(const char *element,
437  const char *name,
438  const char *mapset, struct Categories * pcats, int full)
439 {
440  FILE *fd;
441  char buff[1024];
442  CELL cat;
443  DCELL val1, val2;
444  int old = 0, fp_map;
445  long num = -1;
446 
447 
448  if (strncmp(element, "dig", 3) == 0)
449  fp_map = 0;
450  else
451  fp_map = G_raster_map_is_fp(name, mapset);
452 
453  if (!(fd = G_fopen_old(element, name, mapset)))
454  return -2;
455 
456  /* Read the number of categories */
457  if (G_getl(buff, sizeof buff, fd) == 0)
458  goto error;
459 
460  if (sscanf(buff, "# %ld", &num) == 1)
461  old = 0;
462  else if (sscanf(buff, "%ld", &num) == 1)
463  old = 1;
464 
465  if (!full) {
466  fclose(fd);
467  if (num < 0)
468  return 0; /* coorect */
469  return (CELL) num;
470  }
471 
472  /* Read the title for the file */
473  if (G_getl(buff, sizeof buff, fd) == 0)
474  goto error;
475  G_strip(buff);
476  /* G_ascii_check(buff) ; */
477 
478  G_init_raster_cats(buff, pcats);
479  if (num >= 0)
480  pcats->num = num;
481 
482  if (!old) {
483  char fmt[256];
484  float m1, a1, m2, a2;
485 
486  if (G_getl(fmt, sizeof fmt, fd) == 0)
487  goto error;
488  /* next line contains equation coefficients */
489  if (G_getl(buff, sizeof buff, fd) == 0)
490  goto error;
491  if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
492  goto error;
493  G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats);
494  }
495 
496  /* Read all category names */
497  for (cat = 0;; cat++) {
498  char label[1024];
499 
500  if (G_getl(buff, sizeof buff, fd) == 0)
501  break;
502  if (old)
503  G_set_cat(cat, buff, pcats);
504  else {
505  *label = 0;
506  if (sscanf(buff, "%1s", label) != 1)
507  continue;
508  if (*label == '#')
509  continue;
510  *label = 0;
511  /* for fp maps try to read a range of data */
512  if (fp_map
513  && sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
514  G_set_raster_cat(&val1, &val2, label, pcats, DCELL_TYPE);
515  else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
516  G_set_raster_cat(&cat, &cat, label, pcats, CELL_TYPE);
517  else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
518  G_set_raster_cat(&val1, &val1, label, pcats, DCELL_TYPE);
519  else
520  goto error;
521  }
522  }
523 
524  fclose(fd);
525  return 0;
526  error:
527  fclose(fd);
528  return -1;
529 }
530 
531 
545 char *G_get_cats_title(const struct Categories *pcats)
546 {
547  return G_get_raster_cats_title(pcats);
548 }
549 
550 
560 char *G_get_raster_cats_title(const struct Categories *pcats)
561 {
562  static char *none = "";
563 
564  return pcats->title ? pcats->title : none;
565 }
566 
567 
583 char *G_get_cat(CELL num, struct Categories *pcats)
584 {
585  return G_get_c_raster_cat(&num, pcats);
586 }
587 
588 
600 char *G_get_c_raster_cat(CELL * rast, struct Categories *pcats)
601 {
602  return G_get_raster_cat(rast, pcats, CELL_TYPE);
603 }
604 
605 
617 char *G_get_f_raster_cat(FCELL * rast, struct Categories *pcats)
618 {
619  return G_get_raster_cat(rast, pcats, FCELL_TYPE);
620 }
621 
622 
634 char *G_get_d_raster_cat(DCELL * rast, struct Categories *pcats)
635 {
636  return G_get_raster_cat(rast, pcats, DCELL_TYPE);
637 }
638 
639 
652 char *G_get_raster_cat(void *rast,
653  struct Categories *pcats, RASTER_MAP_TYPE data_type)
654 {
655  static char label[1024];
656  char *f, *l, *v;
657  CELL i;
658  DCELL val;
659  float a[2];
660  char fmt[30], value_str[30];
661 
662  if (G_is_null_value(rast, data_type)) {
663  sprintf(label, "no data");
664  return label;
665  }
666 
667  /* first search the list of labels */
668  *label = 0;
669  val = G_get_raster_value_d(rast, data_type);
670  i = G_quant_get_cell_value(&pcats->q, val);
671 
672  G_debug(5, "G_get_raster_cat(): val %lf found i %d", val, i);
673 
674  if (!G_is_c_null_value(&i) && i < pcats->ncats) {
675  if (pcats->labels[i] != NULL)
676  return pcats->labels[i];
677  return label;
678  }
679 
680  /* generate the label */
681  if ((f = pcats->fmt) == NULL)
682  return label;
683 
684  a[0] = (float)val *pcats->m1 + pcats->a1;
685  a[1] = (float)val *pcats->m2 + pcats->a2;
686 
687  l = label;
688  while (*f) {
689  if (*f == '$') {
690  f++;
691  if (*f == '$')
692  *l++ = *f++;
693  else if (*f == '?') {
694  f++;
695  get_cond(&f, v = value_str, val);
696  while (*v)
697  *l++ = *v++;
698  }
699  else if (get_fmt(&f, fmt, &i)) {
700  sprintf(v = value_str, fmt, a[i]);
701  while (*v)
702  *l++ = *v++;
703  }
704  else
705  *l++ = '$';
706  }
707  else {
708  *l++ = *f++;
709  }
710  }
711  *l = 0;
712  return label;
713 }
714 
715 
731 int G_unmark_raster_cats(struct Categories *pcats)
732 { /* structure to hold category info */
733  register int i;
734 
735  for (i = 0; i < pcats->ncats; i++)
736  pcats->marks[i] = 0;
737  return 0;
738 }
739 
740 
755 int G_mark_c_raster_cats(CELL * rast_row, /* raster row to update stats */
756  int ncols, struct Categories *pcats)
757 { /* structure to hold category info */
758  G_mark_raster_cats(rast_row, ncols, pcats, CELL_TYPE);
759  return 0;
760 }
761 
762 
777 int G_mark_f_raster_cats(FCELL * rast_row, /* raster row to update stats */
778  int ncols, struct Categories *pcats)
779 { /* structure to hold category info */
780  G_mark_raster_cats(rast_row, ncols, pcats, FCELL_TYPE);
781  return 0;
782 }
783 
784 
799 int G_mark_d_raster_cats(DCELL * rast_row, /* raster row to update stats */
800  int ncols, struct Categories *pcats)
801 { /* structure to hold category info */
802  G_mark_raster_cats(rast_row, ncols, pcats, DCELL_TYPE);
803  return 0;
804 }
805 
806 
823 int G_mark_raster_cats(void *rast_row, /* raster row to update stats */
824  int ncols, struct Categories *pcats, /* structure to hold category info */
825  RASTER_MAP_TYPE data_type)
826 {
827  CELL i;
828 
829  while (ncols-- > 0) {
830  i = G_quant_get_cell_value(&pcats->q,
831  G_get_raster_value_d(rast_row, data_type));
832  if (G_is_c_null_value(&i))
833  continue;
834  if (i > pcats->ncats)
835  return -1;
836  pcats->marks[i]++;
837  rast_row = G_incr_void_ptr(rast_row, G_raster_size(data_type));
838  }
839  return 1;
840 }
841 
842 
854 int G_rewind_raster_cats(struct Categories *pcats)
855 {
856  pcats->last_marked_rule = -1;
857  return 0;
858 }
859 
860 char *G_get_next_marked_d_raster_cat(struct Categories *pcats, /* structure to hold category info */
861  DCELL * rast1, DCELL * rast2, /* pointers to raster range */
862  long *count)
863 {
864  char *descr = NULL;
865  int found, i;
866 
867  found = 0;
868  /* pcats->ncats should be == G_quant_nof_rules(&pcats->q) */
869  /* DEBUG
870  fprintf (stderr, "last marked %d nrules %d\n", pcats->last_marked_rule, G_quant_nof_rules(&pcats->q));
871  */
872  for (i = pcats->last_marked_rule + 1; i < G_quant_nof_rules(&pcats->q);
873  i++) {
874  descr = G_get_ith_d_raster_cat(pcats, i, rast1, rast2);
875  /* DEBUG fprintf (stderr, "%d %d\n", i, pcats->marks[i]); */
876  if (pcats->marks[i]) {
877  found = 1;
878  break;
879  }
880  }
881 
882  if (!found)
883  return NULL;
884 
885  *count = pcats->marks[i];
886  pcats->last_marked_rule = i;
887  return descr;
888 }
889 
890 char *G_get_next_marked_c_raster_cat(struct Categories *pcats, /* structure to hold category info */
891  CELL * rast1, CELL * rast2, /* pointers to raster range */
892  long *count)
893 {
894  return G_get_next_marked_raster_cat(pcats, rast1, rast2, count,
895  CELL_TYPE);
896 }
897 
898 char *G_get_next_marked_f_raster_cat(struct Categories *pcats, /* structure to hold category info */
899  FCELL * rast1, FCELL * rast2, /* pointers to raster range */
900  long *count)
901 {
902  return G_get_next_marked_raster_cat(pcats, rast1, rast2, count,
903  FCELL_TYPE);
904 }
905 
906 char *G_get_next_marked_raster_cat(struct Categories *pcats, /* structure to hold category info */
907  void *rast1, void *rast2, /* pointers to raster range */
908  long *count, RASTER_MAP_TYPE data_type)
909 {
910  DCELL val1, val2;
911  char *lab;
912 
913  lab = G_get_next_marked_d_raster_cat(pcats, &val1, &val2, count);
914  G_set_raster_value_d(rast1, val1, data_type);
915  G_set_raster_value_d(rast2, val2, data_type);
916  return lab;
917 }
918 
919 static int get_fmt(char **f, char *fmt, int *i)
920 {
921  char *ff;
922 
923  ff = *f;
924  if (*ff == 0)
925  return 0;
926  if (*ff == '$') {
927  *f = ff + 1;
928  return 0;
929  }
930  switch (*ff++) {
931  case '1':
932  *i = 0;
933  break;
934  case '2':
935  *i = 1;
936  break;
937  default:
938  return 0;
939  }
940  *fmt++ = '%';
941  *fmt++ = '.';
942  if (*ff++ != '.') {
943  *f = ff - 1;
944  *fmt++ = '0';
945  *fmt++ = 'f';
946  *fmt = 0;
947  return 1;
948  }
949  *fmt = '0';
950  while (*ff >= '0' && *ff <= '9')
951  *fmt++ = *ff++;
952  *fmt++ = 'f';
953  *fmt = 0;
954  *f = ff;
955  return 1;
956 }
957 
958 static int get_cond(char **f, char *value, DCELL val)
959 {
960  char *ff;
961 
962  ff = *f;
963  if (val == 1.) {
964  while (*ff)
965  if (*ff++ == '$')
966  break;
967  }
968 
969  while (*ff)
970  if (*ff == '$') {
971  ff++;
972  break;
973  }
974  else
975  *value++ = *ff++;
976 
977  if (val != 1.) {
978  while (*ff)
979  if (*ff++ == '$')
980  break;
981  }
982  *value = 0;
983  *f = ff;
984 
985  return 0;
986 }
987 
988 
1001 int G_set_cat(CELL num, char *label, struct Categories *pcats)
1002 {
1003  CELL tmp = num;
1004 
1005  return G_set_c_raster_cat(&tmp, &tmp, label, pcats);
1006 }
1007 
1008 
1021 int G_set_c_raster_cat(CELL * rast1, CELL * rast2,
1022  char *label, struct Categories *pcats)
1023 {
1024  return G_set_raster_cat(rast1, rast2, label, pcats, CELL_TYPE);
1025 }
1026 
1027 
1040 int G_set_f_raster_cat(FCELL * rast1, FCELL * rast2,
1041  char *label, struct Categories *pcats)
1042 {
1043  return G_set_raster_cat(rast1, rast2, label, pcats, FCELL_TYPE);
1044 }
1045 
1046 
1059 int G_set_d_raster_cat(DCELL * rast1, DCELL * rast2,
1060  char *label, struct Categories *pcats)
1061 {
1062  long len;
1063  DCELL dtmp1, dtmp2;
1064  int i;
1065  char *descr;
1066 
1067  /* DEBUG fprintf(stderr,"G_set_d_raster_cat(rast1 = %p,rast2 = %p,label = '%s',pcats = %p)\n",
1068  rast1,rast2,label,pcats); */
1069  if (G_is_d_null_value(rast1))
1070  return 0;
1071  if (G_is_d_null_value(rast2))
1072  return 0;
1073  /* DEBUG fprintf (stderr, "G_set_d_raster_cat(): adding quant rule: %f %f %d %d\n", *rast1, *rast2, pcats->ncats, pcats->ncats); */
1074  /* the set_cat() functions are used in many places to reset the labels
1075  for the range (or cat) with existing label. In this case we don't
1076  want to store both rules with identical range even though the result
1077  of get_cat() will be correct, since it will use rule added later.
1078  we don't want to overuse memory and we don't want rules which are
1079  not used to be writen out in cats file. So we first look if
1080  the label for this range has been sen, and if it has, overwrite it */
1081 
1082  for (i = 0; i < pcats->ncats; i++) {
1083  descr = G_get_ith_d_raster_cat(pcats, i, &dtmp1, &dtmp2);
1084  if ((dtmp1 == *rast1 && dtmp2 == *rast2)
1085  || (dtmp1 == *rast2 && dtmp2 == *rast1)) {
1086  if (pcats->labels[i] != NULL)
1087  G_free(pcats->labels[i]);
1088  pcats->labels[i] = G_store(label);
1089  G_newlines_to_spaces(pcats->labels[i]);
1090  G_strip(pcats->labels[i]);
1091  return 1;
1092  }
1093  }
1094  /* when rule for this range does not exist */
1095  /* DEBUG fprintf (stderr, "G_set_d_raster_cat(): New rule: adding %d %p\n", i, pcats->labels); */
1096  G_quant_add_rule(&pcats->q, *rast1, *rast2, pcats->ncats, pcats->ncats);
1097  pcats->ncats++;
1098  if (pcats->nalloc < pcats->ncats) {
1099  /* DEBUG fprintf (stderr, "G_set_d_raster_cat(): need more space nalloc = %d ncats = %d\n", pcats->nalloc,pcats->ncats); */
1100  len = (pcats->nalloc + 256) * sizeof(char *);
1101  /* DEBUG fprintf (stderr, "G_set_d_raster_cat(): allocating %d labels(%d)\n", pcats->nalloc + 256,(int)len); */
1102  if (len != (int)len) { /* make sure len doesn't overflow int */
1103  pcats->ncats--;
1104  return -1;
1105  }
1106  /* DEBUG fprintf(stderr,"G_set_d_raster_cat(): pcats->nalloc = %d, pcats->labels = (%p), len = %d\n",pcats->nalloc,pcats->labels,(int)len); */
1107  if (pcats->nalloc) {
1108  /* DEBUG fprintf(stderr,"G_set_d_raster_cat(): Realloc-ing pcats->labels (%p)\n",pcats->labels); */
1109  pcats->labels =
1110  (char **)G_realloc((char *)pcats->labels, (int)len);
1111  }
1112  else {
1113  /* DEBUG fprintf(stderr,"G_set_d_raster_cat(): alloc-ing new labels pointer array\n"); */
1114  pcats->labels = (char **)G_malloc((int)len);
1115  }
1116  /* fflush(stderr); */
1117  /* DEBUG fprintf (stderr, "G_set_d_raster_cats(): allocating %d marks(%d)\n", pcats->nalloc + 256,(int)len); */
1118  len = (pcats->nalloc + 256) * sizeof(int);
1119  if (len != (int)len) { /* make sure len doesn't overflow int */
1120  pcats->ncats--;
1121  return -1;
1122  }
1123  if (pcats->nalloc)
1124  pcats->marks = (int *)G_realloc((char *)pcats->marks, (int)len);
1125  else
1126  pcats->marks = (int *)G_malloc((int)len);
1127  pcats->nalloc += 256;
1128  }
1129  /* DEBUG fprintf(stderr,"G_set_d_raster_cats(): store new label\n"); */
1130  pcats->labels[pcats->ncats - 1] = G_store(label);
1131  G_newlines_to_spaces(pcats->labels[pcats->ncats - 1]);
1132  G_strip(pcats->labels[pcats->ncats - 1]);
1133  /* DEBUG
1134  fprintf (stderr, "%d %s\n", pcats->ncats - 1, pcats->labels[pcats->ncats - 1]);
1135  */
1136  /* updates cats.num = max cat values. This is really just used in old
1137  raster programs, and I am doing it for backwards cmpatibility (Olga) */
1138  if ((CELL) * rast1 > pcats->num)
1139  pcats->num = (CELL) * rast1;
1140  if ((CELL) * rast2 > pcats->num)
1141  pcats->num = (CELL) * rast2;
1142  /* DEBUG fprintf(stderr,"G_set_d_raster_cat(): done\n"); */
1143  /* DEBUG fflush(stderr); */
1144  return 1;
1145 }
1146 
1147 
1160 int G_set_raster_cat(void *rast1, void *rast2,
1161  char *label,
1162  struct Categories *pcats, RASTER_MAP_TYPE data_type)
1163 {
1164  DCELL val1, val2;
1165 
1166  val1 = G_get_raster_value_d(rast1, data_type);
1167  val2 = G_get_raster_value_d(rast2, data_type);
1168  return G_set_d_raster_cat(&val1, &val2, label, pcats);
1169 }
1170 
1171 
1185 int G_write_cats(char *name, struct Categories *cats)
1186 {
1187  return G__write_cats("cats", name, cats);
1188 }
1189 
1190 
1201 int G_write_raster_cats(char *name, struct Categories *cats)
1202 {
1203  return G__write_cats("cats", name, cats);
1204 }
1205 
1206 
1221 int G_write_vector_cats(char *name, struct Categories *cats)
1222 {
1223  return G__write_cats("dig_cats", name, cats);
1224 }
1225 
1226 int G__write_cats(char *element, char *name, struct Categories *cats)
1227 {
1228  FILE *fd;
1229  int i, fp_map;
1230  char *descr;
1231  DCELL val1, val2;
1232  char str1[100], str2[100];
1233 
1234  /* DEBUG fprintf(stderr,"G__write_cats(*element = '%s', name = '%s', cats = %p\n",element,name,cats); */
1235  if (!(fd = G_fopen_new(element, name)))
1236  return -1;
1237 
1238  /* write # cats - note # indicate 3.0 or later */
1239  fprintf(fd, "# %ld categories\n", (long)cats->num);
1240 
1241  /* title */
1242  fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
1243 
1244  /* write format and coefficients */
1245  fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
1246  fprintf(fd, "%.2f %.2f %.2f %.2f\n",
1247  cats->m1, cats->a1, cats->m2, cats->a2);
1248 
1249  /* if the map is integer or if this is a vector map, sort labels */
1250  if (strncmp(element, "dig", 3) == 0)
1251  fp_map = 0;
1252  else
1253  fp_map = G_raster_map_is_fp(name, G_mapset());
1254  /* DEBUG fprintf(stderr,"G__write_cats(): fp_map = %d\n",fp_map); */
1255  if (!fp_map)
1256  G_sort_cats(cats);
1257 
1258  /* write the cat numbers:label */
1259  for (i = 0; i < G_quant_nof_rules(&cats->q); i++) {
1260  descr = G_get_ith_d_raster_cat(cats, i, &val1, &val2);
1261  if ((cats->fmt && cats->fmt[0])
1262  || (descr && descr[0])) {
1263  if (val1 == val2) {
1264  sprintf(str1, "%.10f", val1);
1265  G_trim_decimal(str1);
1266  fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
1267  }
1268  else {
1269  sprintf(str1, "%.10f", val1);
1270  G_trim_decimal(str1);
1271  sprintf(str2, "%.10f", val2);
1272  G_trim_decimal(str2);
1273  fprintf(fd, "%s:%s:%s\n", str1, str2,
1274  descr != NULL ? descr : "");
1275  }
1276  }
1277  }
1278  fclose(fd);
1279  /* DEBUG fprintf(stderr,"G__write_cats(): done\n"); */
1280  return (1);
1281 }
1282 
1283 
1298 char *G_get_ith_d_raster_cat(const struct Categories *pcats,
1299  int i, DCELL * rast1, DCELL * rast2)
1300 {
1301  int index;
1302 
1303  if (i > pcats->ncats) {
1304  G_set_d_null_value(rast1, 1);
1305  G_set_d_null_value(rast2, 1);
1306  return "";
1307  }
1308  G_quant_get_ith_rule(&pcats->q, i, rast1, rast2, &index, &index);
1309  return pcats->labels[index];
1310 }
1311 
1312 
1327 char *G_get_ith_f_raster_cat(const struct Categories *pcats,
1328  int i, void *rast1, void *rast2)
1329 {
1330  RASTER_MAP_TYPE data_type = FCELL_TYPE;
1331  char *tmp;
1332  DCELL val1, val2;
1333 
1334  tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
1335  G_set_raster_value_d(rast1, val1, data_type);
1336  G_set_raster_value_d(rast2, val2, data_type);
1337  return tmp;
1338 }
1339 
1340 
1355 char *G_get_ith_c_raster_cat(const struct Categories *pcats,
1356  int i, void *rast1, void *rast2)
1357 {
1358  RASTER_MAP_TYPE data_type = CELL_TYPE;
1359  char *tmp;
1360  DCELL val1, val2;
1361 
1362  tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
1363  G_set_raster_value_d(rast1, val1, data_type);
1364  G_set_raster_value_d(rast2, val2, data_type);
1365  return tmp;
1366 }
1367 
1368 
1385 char *G_get_ith_raster_cat(const struct Categories *pcats, int i, void *rast1,
1386  void *rast2, RASTER_MAP_TYPE data_type)
1387 {
1388  char *tmp;
1389  DCELL val1, val2;
1390 
1391  tmp = G_get_ith_d_raster_cat(pcats, i, &val1, &val2);
1392  G_set_raster_value_d(rast1, val1, data_type);
1393  G_set_raster_value_d(rast2, val2, data_type);
1394  return tmp;
1395 }
1396 
1397 
1417 int G_init_cats(CELL num, const char *title, struct Categories *pcats)
1418 {
1419  G_init_raster_cats(title, pcats);
1420  pcats->num = num;
1421  return 0;
1422 }
1423 
1424 
1437 int G_init_raster_cats(const char *title, struct Categories *pcats)
1438 {
1439  G_set_raster_cats_title(title, pcats);
1440  pcats->labels = NULL;
1441  pcats->nalloc = 0;
1442  pcats->ncats = 0;
1443  pcats->num = 0;
1444  pcats->fmt = NULL;
1445  pcats->m1 = 0.0;
1446  pcats->a1 = 0.0;
1447  pcats->m2 = 0.0;
1448  pcats->a2 = 0.0;
1449  pcats->last_marked_rule = -1;
1450  G_quant_init(&pcats->q);
1451  return 0;
1452 }
1453 
1454 
1466 int G_set_cats_title(const char *title, struct Categories *pcats)
1467 {
1468  G_set_raster_cats_title(title, pcats);
1469  return 0;
1470 }
1471 
1472 
1483 int G_set_raster_cats_title(const char *title, struct Categories *pcats)
1484 {
1485  if (title == NULL)
1486  title = "";
1487  pcats->title = G_store(title);
1488  G_newlines_to_spaces(pcats->title);
1489  G_strip(pcats->title);
1490  return 0;
1491 }
1492 
1493 int G_set_cats_fmt(const char *fmt, double m1, double a1, double m2,
1494  double a2, struct Categories *pcats)
1495 {
1496  G_set_raster_cats_fmt(fmt, m1, a1, m2, a2, pcats);
1497  return 0;
1498 }
1499 
1500 
1515 int G_set_raster_cats_fmt(const char *fmt, double m1, double a1, double m2,
1516  double a2, struct Categories *pcats)
1517 {
1518  pcats->m1 = m1;
1519  pcats->a1 = a1;
1520  pcats->m2 = m2;
1521  pcats->a2 = a2;
1522 
1523  pcats->fmt = G_store(fmt);
1524  G_newlines_to_spaces(pcats->fmt);
1525  G_strip(pcats->fmt);
1526  return 0;
1527 }
1528 
1529 
1540 int G_free_cats(struct Categories *pcats)
1541 {
1542  G_free_raster_cats(pcats);
1543  return 0;
1544 }
1545 
1546 
1556 int G_free_raster_cats(struct Categories *pcats)
1557 {
1558  int i;
1559 
1560  if (pcats->title != NULL) {
1561  G_free(pcats->title);
1562  pcats->title = NULL;
1563  }
1564  if (pcats->fmt != NULL) {
1565  G_free(pcats->fmt);
1566  pcats->fmt = NULL;
1567  }
1568  if (pcats->ncats > 0) {
1569  for (i = 0; i < pcats->ncats; i++)
1570  if (pcats->labels[i] != NULL)
1571  G_free(pcats->labels[i]);
1572  G_free(pcats->labels);
1573  G_free(pcats->marks);
1574  pcats->labels = NULL;
1575  }
1576  G_quant_free(&pcats->q);
1577  pcats->ncats = 0;
1578  pcats->nalloc = 0;
1579  return 0;
1580 }
1581 
1582 
1597 int
1598 G_copy_raster_cats(struct Categories *pcats_to,
1599  const struct Categories *pcats_from)
1600 {
1601  int i;
1602  char *descr;
1603  DCELL d1, d2;
1604 
1605  G_init_raster_cats(pcats_from->title, pcats_to);
1606  for (i = 0; i < pcats_from->ncats; i++) {
1607  descr = G_get_ith_d_raster_cat(pcats_from, i, &d1, &d2);
1608  G_set_d_raster_cat(&d1, &d2, descr, pcats_to);
1609  }
1610  return 0;
1611 }
1612 
1613 int G_number_of_raster_cats(struct Categories *pcats)
1614 {
1615  return pcats->ncats;
1616 }
1617 
1618 static struct Categories save_cats;
1619 
1620 int G_sort_cats(struct Categories *pcats)
1621 {
1622  int *indexes, i, ncats;
1623  char *descr;
1624  DCELL d1, d2;
1625 
1626  if (pcats->ncats <= 1)
1627  return -1;
1628 
1629  ncats = pcats->ncats;
1630  /* DEBUG fprintf(stderr,"G_sort_cats(): Copying to save cats buffer\n"); */
1631  G_copy_raster_cats(&save_cats, pcats);
1632  G_free_raster_cats(pcats);
1633 
1634  indexes = (int *)G_malloc(sizeof(int) * ncats);
1635  for (i = 0; i < ncats; i++)
1636  indexes[i] = i;
1637 
1638  qsort(indexes, ncats, sizeof(int), cmp);
1639  G_init_raster_cats(save_cats.title, pcats);
1640  for (i = 0; i < ncats; i++) {
1641  descr = G_get_ith_d_raster_cat(&save_cats, indexes[i], &d1, &d2);
1642  /* DEBUG fprintf(stderr,"G_sort_cats(): Write sorted cats, pcats = %p pcats->labels = %p\n",pcats,pcats->labels); */
1643  G_set_d_raster_cat(&d1, &d2, descr, pcats);
1644  /* DEBUG fflush(stderr); */
1645  }
1646  G_free_raster_cats(&save_cats);
1647  /* DEBUG fprintf(stderr,"G_sort_cats(): Done\n"); */
1648  /* fflush(stderr); */
1649 
1650  return 0;
1651 }
1652 
1653 static int cmp(const void *aa, const void *bb)
1654 {
1655  const int *a = aa, *b = bb;
1656  DCELL min_rast1, min_rast2, max_rast1, max_rast2;
1657  CELL index;
1658 
1659  G_quant_get_ith_rule(&(save_cats.q), *a,
1660  &min_rast1, &max_rast1, &index, &index);
1661  G_quant_get_ith_rule(&(save_cats.q), *b,
1662  &min_rast2, &max_rast2, &index, &index);
1663  if (min_rast1 < min_rast2)
1664  return -1;
1665  if (min_rast1 > min_rast2)
1666  return 1;
1667  return 0;
1668 }
char * G_get_f_raster_cat(FCELL *rast, struct Categories *pcats)
given a FCELL value val Returns pointer to a string describing category.
Definition: gis/cats.c:617
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int G_is_c_null_value(const CELL *cellVal)
Returns 1 if cell is NULL, 0 otherwise. This will test if the value cell is the largest int...
Definition: null_val.c:244
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int G_set_cats_title(const char *title, struct Categories *pcats)
set title in category structure
Definition: gis/cats.c:1466
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
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_set_raster_cats_title(const char *title, struct Categories *pcats)
Same as existing G_set_cats_title()
Definition: gis/cats.c:1483
int l
Definition: dataquad.c:292
float b
Definition: named_colr.c:8
void G_quant_free(struct Quant *q)
Definition: quant.c:316
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
DCELL val1
Definition: g3dcats.c:91
int G_number_of_raster_cats(struct Categories *pcats)
Definition: gis/cats.c:1613
char * G_get_ith_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2, RASTER_MAP_TYPE data_type)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
Definition: gis/cats.c:1385
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
int G_read_raster_cats(const char *name, const char *mapset, struct Categories *pcats)
Is the same as existing G_read_cats()
Definition: gis/cats.c:365
char * G_get_next_marked_raster_cat(struct Categories *pcats, void *rast1, void *rast2, long *count, RASTER_MAP_TYPE data_type)
Definition: gis/cats.c:906
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
#define min(x, y)
Definition: draw2.c:68
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
int G_write_cats(char *name, struct Categories *cats)
write raster category file
Definition: gis/cats.c:1185
int G_init_cats(CELL num, const char *title, struct Categories *pcats)
initialize category structure
Definition: gis/cats.c:1417
int count
long num
Definition: g3dcats.c:93
int G_write_raster_cats(char *name, struct Categories *cats)
Same as existing G_write_cats()
Definition: gis/cats.c:1201
CELL G_quant_get_cell_value(struct Quant *q, DCELL dcellVal)
Returns a CELL category for the floating-point value based on the quantization rules in q...
Definition: quant.c:775
char * G_get_next_marked_c_raster_cat(struct Categories *pcats, CELL *rast1, CELL *rast2, long *count)
Definition: gis/cats.c:890
CELL G_number_of_cats(const char *name, const char *mapset)
Definition: gis/cats.c:422
int G_rewind_raster_cats(struct Categories *pcats)
after call to this function G_get_next_marked_raster_cat() returns the first marked cat label...
Definition: gis/cats.c:854
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
int G_write_vector_cats(char *name, struct Categories *cats)
write vector category file
Definition: gis/cats.c:1221
int G_set_c_raster_cat(CELL *rast1, CELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
Definition: gis/cats.c:1021
#define max(x, y)
Definition: draw2.c:69
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
CELL G__read_cats(const char *element, const char *name, const char *mapset, struct Categories *pcats, int full)
Definition: gis/cats.c:436
void G_newlines_to_spaces(char *s)
Definition: nl_to_spaces.c:3
int G_read_range(const char *name, const char *mapset, struct Range *range)
read raster range
Definition: range.c:226
int G_read_cats(const char *name, const char *mapset, struct Categories *pcats)
read raster category file
Definition: gis/cats.c:347
int G_raster_map_is_fp(const char *name, const char *mapset)
Check if raster map is floating-point.
Definition: opencell.c:969
char * G_get_cats_title(const struct Categories *pcats)
get title from category structure struct
Definition: gis/cats.c:545
char buff[1024]
Definition: g3dcats.c:89
int G_free_cats(struct Categories *pcats)
free category structure memory
Definition: gis/cats.c:1540
DCELL val2
Definition: g3dcats.c:91
int G_read_vector_cats(const char *name, const char *mapset, struct Categories *pcats)
read vector category file
Definition: gis/cats.c:401
void G_quant_get_ith_rule(const struct Quant *q, int i, DCELL *dLow, DCELL *dHigh, CELL *cLow, CELL *cHigh)
Definition: quant.c:562
int G_get_range_min_max(const struct Range *range, CELL *min, CELL *max)
get range min and max
Definition: range.c:608
char * G_get_cat(CELL num, struct Categories *pcats)
get a category label
Definition: gis/cats.c:583
int G_mark_c_raster_cats(CELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
Definition: gis/cats.c:755
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
size_t G_raster_size(RASTER_MAP_TYPE data_type)
Returns size of a raster CELL in bytes.
Definition: alloc_cell.c:38
char * G_get_next_marked_d_raster_cat(struct Categories *pcats, DCELL *rast1, DCELL *rast2, long *count)
Definition: gis/cats.c:860
int G_set_cats_fmt(const char *fmt, double m1, double a1, double m2, double a2, struct Categories *pcats)
Definition: gis/cats.c:1493
struct field_info * ff
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
int G_unmark_raster_cats(struct Categories *pcats)
Sets marks for all categories to 0. This initializes Categories structure for subsequest calls to G_m...
Definition: gis/cats.c:731
char * G_get_next_marked_f_raster_cat(struct Categories *pcats, FCELL *rast1, FCELL *rast2, long *count)
Definition: gis/cats.c:898
int
Definition: g3dcolor.c:48
char * value
Definition: env.c:30
int G_set_f_raster_cat(FCELL *rast1, FCELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
Definition: gis/cats.c:1040
int G_set_raster_value_d(void *rast, DCELL dval, RASTER_MAP_TYPE data_type)
Places a DCELL raster value.
Definition: gis/raster.c:201
char * G_get_ith_f_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
Definition: gis/cats.c:1327
int G_mark_f_raster_cats(FCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
Definition: gis/cats.c:777
int G__write_cats(char *element, char *name, struct Categories *cats)
Definition: gis/cats.c:1226
char * G_get_d_raster_cat(DCELL *rast, struct Categories *pcats)
given a DCELL value val Returns pointer to a string describing category.
Definition: gis/cats.c:634
int G_quant_nof_rules(const struct Quant *q)
Definition: quant.c:555
void G_quant_add_rule(struct Quant *q, DCELL dLow, DCELL dHigh, CELL cLow, CELL cHigh)
Definition: quant.c:655
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
char * G_get_c_raster_cat(CELL *rast, struct Categories *pcats)
given a CELL value val Returns pointer to a string describing category.
Definition: gis/cats.c:600
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_quant_init(struct Quant *quant)
Initializes the q struct.
Definition: quant.c:422
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
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 * G_get_raster_cat(void *rast, struct Categories *pcats, RASTER_MAP_TYPE data_type)
given a raster value val of type data_type Returns pointer to a string describing category...
Definition: gis/cats.c:652
int G_free_raster_cats(struct Categories *pcats)
Same as existing G_free_cats()
Definition: gis/cats.c:1556
DCELL G_get_raster_value_d(const void *rast, RASTER_MAP_TYPE data_type)
Retrieves the value of type data_type from pointer p,.
Definition: gis/raster.c:313
char * G_get_ith_c_raster_cat(const struct Categories *pcats, int i, void *rast1, void *rast2)
Returns i-th description and i-th data range from the list of category descriptions with correspondin...
Definition: gis/cats.c:1355
int G_set_d_raster_cat(DCELL *rast1, DCELL *rast2, char *label, struct Categories *pcats)
Adds the label for range rast1 through rast2 in category structure pcats.
Definition: gis/cats.c:1059
int G_copy_raster_cats(struct Categories *pcats_to, const struct Categories *pcats_from)
Allocates NEW space for quant rules and labels n pcats_to and copies all info from pcats_from cats to...
Definition: gis/cats.c:1598
int G_mark_d_raster_cats(DCELL *rast_row, int ncols, struct Categories *pcats)
Looks up the category label for each raster value in the rast_row and updates the marks for labels fo...
Definition: gis/cats.c:799
char * G_get_raster_cats_title(const struct Categories *pcats)
get raster cats title
Definition: gis/cats.c:560
int G_sort_cats(struct Categories *pcats)
Definition: gis/cats.c:1620
int G_is_null_value(const void *rast, RASTER_MAP_TYPE data_type)
If the data_type is CELL_TYPE, calls G_is_c_null_value ((CELL *) rast); If the data_type is FCELL_TYP...
Definition: null_val.c:207
int G_mark_raster_cats(void *rast_row, int ncols, struct Categories *pcats, RASTER_MAP_TYPE data_type)
Looks up the category label for each raster value in the rast_row (row of raster cell value) and upda...
Definition: gis/cats.c:823