GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71917
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
CELL cat
Definition: raster3d/cats.c:82
int Vedit_modify_cats(struct Map_info *Map, struct ilist *List, int layer, int del, struct cat_list *Clist)
Add / remove categories.
int Vect_field_cat_del(struct line_cats *Cats, int field, int cat)
Delete field/cat from line_cats structure.
off_t Vect_rewrite_line(struct Map_info *Map, off_t line, int type, const struct line_pnts *points, const struct line_cats *cats)
Rewrites existing feature (topological level required)
struct line_pnts * Vect_new_line_struct()
Creates and initializes a line_pnts structure.
Definition: line.c:45
int Vect_line_alive(const struct Map_info *Map, int line)
Check if feature is alive or dead (topological level required)
int n_values
Number of values in the list.
Definition: gis.h:659
int * min
Array of minimum values.
Definition: dig_structs.h:1732
void Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
void Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
#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
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn&#39;t exist yet.
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
Category list.
Definition: dig_structs.h:1723
Vector map info.
Definition: dig_structs.h:1259
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
#define _(str)
Definition: glocale.h:13
List of integers.
Definition: gis.h:650
int Vect_read_line(const struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature (topological level required)
int * value
Array of values.
Definition: gis.h:655
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203