GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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, int layer,
31  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(
64  _("Unable to set category %d for (feature id %d)"),
65  cat, line);
66  }
67  else {
68  rewrite = 1;
69  }
70  }
71  else { /* delete old category */
72  if (Vect_field_cat_del(Cats, layer, cat) > 0) {
73  rewrite = 1;
74  }
75  }
76  }
77  }
78 
79  if (rewrite == 0)
80  continue;
81 
82  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
83  return -1;
84  }
85 
86  nlines_modified++;
87  }
88 
89  /* destroy structures */
92 
93  return nlines_modified;
94 }
void G_warning(const char *,...) __attribute__((format(printf
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
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 Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn't exist yet.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
int Vect_field_cat_del(struct line_cats *, int, int)
Delete field/cat from line_cats structure.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
#define max(x, y)
Definition: draw2.c:30
#define _(str)
Definition: glocale.h:10
Vector map info.
Definition: dig_structs.h:1243
Category list.
Definition: dig_structs.h:1697
int n_ranges
Number of ranges.
Definition: dig_structs.h:1713
int * min
Array of minimum values.
Definition: dig_structs.h:1705
List of integers.
Definition: gis.h:709
int n_values
Number of values in the list.
Definition: gis.h:717
int * value
Array of values.
Definition: gis.h:713
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
int Vedit_modify_cats(struct Map_info *Map, struct ilist *List, int layer, int del, struct cat_list *Clist)
Add / remove categories.