GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
vector/vedit/cats.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/cats.c
3 
4  \brief Vedit library - category manipulation
5 
6  (C) 2006-2008 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Jachym Cepicky <jachym.cepicky gmail.com>
12  \author Martin Landa <landa.martin gmail.com>
13 */
14 
15 #include <grass/glocale.h>
16 #include <grass/vedit.h>
17 
18 /*!
19  \brief Add / remove categories
20 
21  \param Map pointer to Map_info
22  \param List list of selected primitives
23  \param layer layer number
24  \param del action (non-zero for delete otherwise add)
25  \param Clist list of category numbers
26 
27  \return number of modified primitives
28  \return -1 on error
29 */
30 int Vedit_modify_cats(struct Map_info *Map, struct ilist *List,
31  int layer, int del, struct cat_list *Clist)
32 {
33  int i, j;
34  struct line_cats *Cats;
35  struct line_pnts *Points;
36  int line, type, cat;
37  int nlines_modified, rewrite;
38 
39  /* features defined by cats */
40  if (Clist->n_ranges <= 0) {
41  return 0;
42  }
43 
44  nlines_modified = 0;
45 
46  Cats = Vect_new_cats_struct();
47  Points = Vect_new_line_struct();
48 
49  /* for each line, set new category */
50  for (i = 0; i < List->n_values; i++) {
51  line = List->value[i];
52  type = Vect_read_line(Map, Points, Cats, line);
53 
54  if (!Vect_line_alive(Map, line))
55  continue;
56 
57  rewrite = 0;
58  for (j = 0; j < Clist->n_ranges; j++) {
59  for (cat = Clist->min[j]; cat <= Clist->max[j]; cat++) {
60  /* add new category */
61  if (!del) {
62  if (Vect_cat_set(Cats, layer, cat) < 1) {
63  G_warning(_("Unable to set category %d for (feature id %d)"),
64  cat, line);
65  }
66  else {
67  rewrite = 1;
68  }
69  }
70  else { /* delete old category */
71  if (Vect_field_cat_del(Cats, layer, cat) > 0) {
72  rewrite = 1;
73  }
74  }
75  }
76  }
77 
78  if (rewrite == 0)
79  continue;
80 
81  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
82  return -1;
83  }
84 
85  nlines_modified++;
86 
87  }
88 
89  /* destroy structures */
92 
93  return nlines_modified;
94 }
int Vedit_modify_cats(struct Map_info *Map, struct ilist *List, int layer, int del, struct cat_list *Clist)
Add / remove categories.
off_t Vect_rewrite_line(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites existing feature (topological level required)
int n_values
Number of values in the list.
Definition: gis.h:698
int * min
Array of minimum values.
Definition: dig_structs.h:1732
#define max(x, y)
Definition: draw2.c:32
int n_ranges
Number of ranges.
Definition: dig_structs.h:1740
Feature category info.
Definition: dig_structs.h:1702
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
Category list.
Definition: dig_structs.h:1723
Vector map info.
Definition: dig_structs.h:1259
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
void G_warning(const char *,...) __attribute__((format(printf
int Vect_line_alive(const struct Map_info *, int)
Check if feature is alive or dead (topological level required)
#define _(str)
Definition: glocale.h:10
List of integers.
Definition: gis.h:689
int * value
Array of values.
Definition: gis.h:694
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
int Vect_read_line(const struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_field_cat_del(struct line_cats *, int, int)
Delete field/cat from line_cats structure.
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn&#39;t exist yet.