GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-0b29c8ef57
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  G_debug(3, "Vedit_delete_area(): area=%d", area);
98  centroid = Vect_get_area_centroid(Map, area);
99  if (centroid != 0) {
100  Vect_delete_line(Map, centroid);
101  }
102  else {
103  G_warning(_("Area %d without centroid"), area);
104  return 0;
105  }
106  list = Vect_new_list();
107  Vect_get_area_boundaries(Map, area, list);
108  if (list->n_values > 0) {
109  for (i = 0; i < list->n_values; i++) {
110  line = abs(list->value[i]);
111  Vect_get_line_areas(Map, line, &left, &right);
112  if (left > 0 && right > 0)
113  /* do not delete common boundaries */
114  continue;
115 
116  Vect_delete_line(Map, line);
117  }
118  }
119  else {
120  G_warning(_("Area %d has no boundaries"), area);
122  return 0;
123  }
124 
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 }
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
plus_t Vect_get_num_areas(struct Map_info *)
Get number of areas in vector map.
Definition: level_two.c:87
int Vect_area_alive(struct Map_info *, int)
Check if area is alive or dead (topological level required)
int Vect_get_area_boundaries(struct Map_info *, int, struct ilist *)
Creates list of boundaries for given area.
int Vect_get_centroid_area(struct Map_info *, int)
Get area id the centroid is within.
Definition: level_two.c:429
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
int Vect_delete_line(struct Map_info *, off_t)
Delete existing feature (topological level required)
int Vect_get_area_cat(struct Map_info *, int, int)
Find FIRST category of given field and area.
int Vect_get_area_centroid(struct Map_info *, int)
Returns centroid id for given area.
int Vect_get_line_areas(struct Map_info *, int, int *, int *)
Get area id on the left and right side of the boundary.
Definition: level_two.c:346
#define _(str)
Definition: glocale.h:10
struct list * list
Definition: read_list.c:24
Vector map info.
Definition: dig_structs.h:1243
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
Definition: manage.h:4
int Vedit_delete_areas_cat(struct Map_info *Map, int field, int cat)
Delete vector areas of given category.
int Vedit_delete_area(struct Map_info *Map, int area)
Delete area (centroid + set of boundaries) by id.
int Vedit_delete_lines(struct Map_info *Map, struct ilist *List)
Delete selected features.
int Vedit_delete_area_centroid(struct Map_info *Map, int centroid)
Delete area (centroid and set of boundaries) by centroid.