GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
vector/vedit/delete.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/delete.c
3 
4  \brief Vedit library - delete vector features
5 
6  (C) 2007-2008, 2012 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 Martin Landa <landa.martin gmail.com>
12 */
13 
14 #include <stdlib.h>
15 
16 #include <grass/vedit.h>
17 #include <grass/dbmi.h>
18 #include <grass/glocale.h>
19 
20 /*!
21  \brief Delete selected features
22 
23  \param Map pointer to Map_info
24  \param List list of features to be deleted
25 
26  \return number of deleted features
27  \return -1 on on error
28 */
29 int Vedit_delete_lines(struct Map_info *Map, struct ilist *List)
30 {
31  int i, line;
32  int nlines_removed;
33 
34  nlines_removed = 0;
35 
36  for (i = 0; i < List->n_values; i++) {
37  line = List->value[i];
38 
39  if (!Vect_line_alive(Map, line)) {
40  G_warning(_("Attempt to delete dead feature (%d)"), line);
41  continue;
42  }
43 
44  if (Vect_delete_line(Map, line) < 0) {
45  return -1;
46  }
47 
48  G_debug(3, "Vedit_delete_lines(): line=%d", line);
49  nlines_removed++;
50  }
51 
52  return nlines_removed;
53 }
54 
55 /*!
56  \brief Delete area (centroid and set of boundaries) by centroid
57 
58  \param Map pointer to Map_info struct
59  \param centroid
60 
61  \return 0 no area deleted
62  \return 1 area deleted
63 */
64 int Vedit_delete_area_centroid(struct Map_info *Map, int centroid)
65 {
66  int area;
67 
68  G_debug(1, "Vedit_delete_area_centroid(): centroid = %d", centroid);
69 
70  area = Vect_get_centroid_area(Map, centroid);
71  if (area == 0) {
72  G_warning(_("No area found for centroid %d"), centroid);
73  return 0;
74  }
75  if (area < 0) {
76  G_warning(_("Duplicate centroid %d, unable to delete area"), centroid);
77  return 0;
78  }
79 
80  return Vedit_delete_area(Map, area);
81 }
82 
83 /*!
84  \brief Delete area (centroid + set of boundaries) by id
85 
86  \param Map pointer to Map_info struct
87  \param area id
88 
89  \return 0 no area deleted
90  \return 1 area deleted
91 */
92 int Vedit_delete_area(struct Map_info *Map, int area)
93 {
94  int i, line, centroid, left, right;
95  struct ilist *list;
96 
97  list = Vect_new_list();
98 
99  G_debug(3, "Vedit_delete_area(): area=%d", area);
100  centroid = Vect_get_area_centroid(Map, area);
101  if (centroid != 0) {
102  Vect_delete_line(Map, centroid);
103  }
104  else {
105  G_warning(_("Area %d without centroid"), area);
106  return 0;
107  }
108  Vect_get_area_boundaries(Map, area, list);
109  if (list->n_values > 0) {
110  for (i = 0; i < list->n_values; i++) {
111  line = abs(list->value[i]);
112  Vect_get_line_areas(Map, line, &left, &right);
113  if (left > 0 && right > 0)
114  /* do not delete common boundaries */
115  continue;
116 
117  Vect_delete_line(Map, line);
118  }
119  }
120  else {
121  G_warning(_("Area %d has no boundaries"), area);
122  return 0;
123  }
124 
125  Vect_destroy_list(list);
126 
127  return 1;
128 }
129 
130 /*!
131  \brief Delete vector areas of given category
132 
133  \param Map pointer to Map_info struct
134  \param field layer number
135  \param cat category number
136 
137  \return number of deleted areas
138 */
139 int Vedit_delete_areas_cat(struct Map_info *Map, int field, int cat)
140 {
141  int area, nareas, nremoved;
142 
143  G_debug(1, "Vedit_delete_areas(): field = %d cat = %d", field, cat);
144  nareas = Vect_get_num_areas(Map);
145  nremoved = 0;
146  for (area = 1; area <= nareas; area++) {
147  if (!Vect_area_alive(Map, area))
148  continue;
149  if (Vect_get_area_cat(Map, area, field) != cat)
150  continue;
151 
152  if (Vedit_delete_area(Map, area))
153  nremoved++;
154  }
155 
156  return nremoved;
157 }
int Vedit_delete_area_centroid(struct Map_info *Map, int centroid)
Delete area (centroid and set of boundaries) by centroid.
int Vedit_delete_area(struct Map_info *Map, int area)
Delete area (centroid + set of boundaries) by id.
int Vect_get_area_centroid(const struct Map_info *, int)
Returns centroid id for given area.
int n_values
Number of values in the list.
Definition: gis.h:698
int Vect_get_centroid_area(const struct Map_info *, int)
Get area id the centroid is within.
Definition: level_two.c:434
int Vedit_delete_lines(struct Map_info *Map, struct ilist *List)
Delete selected features.
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
struct list * list
Definition: read_list.c:24
int Vect_get_area_boundaries(const struct Map_info *, int, struct ilist *)
Creates list of boundaries for given area.
plus_t Vect_get_num_areas(const struct Map_info *)
Get number of areas in vector map.
Definition: level_two.c:86
int Vect_area_alive(const struct Map_info *, int)
Check if area is alive or dead (topological level required)
Vector map info.
Definition: dig_structs.h:1259
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_delete_line(struct Map_info *, off_t)
Delete existing feature (topological level required)
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 Vedit_delete_areas_cat(struct Map_info *Map, int field, int cat)
Delete vector areas of given category.
int * value
Array of values.
Definition: gis.h:694
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_get_line_areas(const struct Map_info *, int, int *, int *)
Get area id on the left and right side of the boundary.
Definition: level_two.c:350
int Vect_get_area_cat(const struct Map_info *, int, int)
Find FIRST category of given field and area.