GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
remove_areas.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 
22 #include <grass/gis.h>
23 #include <grass/Vect.h>
24 #include <grass/glocale.h>
25 
40 int
41 Vect_remove_small_areas(struct Map_info *Map, double thresh,
42  struct Map_info *Err, double *removed_area)
43 {
44  int area, nareas;
45  int nremoved = 0;
46  struct ilist *List;
47  struct ilist *AList;
48  struct line_pnts *Points;
49  struct line_cats *Cats;
50  double size_removed = 0.0;
51 
52  List = Vect_new_list();
53  AList = Vect_new_list();
54  Points = Vect_new_line_struct();
55  Cats = Vect_new_cats_struct();
56 
57  nareas = Vect_get_num_areas(Map);
58  for (area = 1; area <= nareas; area++) {
59  int i, j, centroid, dissolve_neighbour;
60  double length, size;
61 
62  G_percent(area, nareas, 1);
63  G_debug(3, "area = %d", area);
64  if (!Vect_area_alive(Map, area))
65  continue;
66 
67  size = Vect_get_area_area(Map, area);
68  if (size > thresh)
69  continue;
70  size_removed += size;
71 
72  /* The area is smaller than the limit -> remove */
73 
74  /* Remove centroid */
75  centroid = Vect_get_area_centroid(Map, area);
76  if (centroid > 0) {
77  if (Err) {
78  Vect_read_line(Map, Points, Cats, centroid);
79  Vect_write_line(Err, GV_CENTROID, Points, Cats);
80  }
81  Vect_delete_line(Map, centroid);
82  }
83 
84  /* Find the adjacent area with which the longest boundary is shared */
85 
86  Vect_get_area_boundaries(Map, area, List);
87 
88  /* Create a list of neighbour areas */
89  Vect_reset_list(AList);
90  for (i = 0; i < List->n_values; i++) {
91  int line, left, right, neighbour;
92 
93  line = List->value[i];
94 
95  if (!Vect_line_alive(Map, abs(line))) /* Should not happen */
96  G_fatal_error(_("Area is composed of dead boundary"));
97 
98  Vect_get_line_areas(Map, abs(line), &left, &right);
99  if (line > 0)
100  neighbour = left;
101  else
102  neighbour = right;
103 
104  G_debug(4, " line = %d left = %d right = %d neighbour = %d",
105  line, left, right, neighbour);
106 
107  Vect_list_append(AList, neighbour); /* this checks for duplicity */
108  }
109  G_debug(3, "num neighbours = %d", AList->n_values);
110 
111  /* Go through the list of neghours and find that with the longest boundary */
112  dissolve_neighbour = 0;
113  length = -1.0;
114  for (i = 0; i < AList->n_values; i++) {
115  int neighbour1;
116  double l = 0.0;
117 
118  neighbour1 = AList->value[i];
119  G_debug(4, " neighbour1 = %d", neighbour1);
120 
121  for (j = 0; j < List->n_values; j++) {
122  int line, left, right, neighbour2;
123 
124  line = List->value[j];
125  Vect_get_line_areas(Map, abs(line), &left, &right);
126  if (line > 0)
127  neighbour2 = left;
128  else
129  neighbour2 = right;
130 
131  if (neighbour2 == neighbour1) {
132  Vect_read_line(Map, Points, NULL, abs(line));
133  l += Vect_line_length(Points);
134  }
135  }
136  if (l > length) {
137  length = l;
138  dissolve_neighbour = neighbour1;
139  }
140  }
141 
142  G_debug(3, "dissolve_neighbour = %d", dissolve_neighbour);
143 
144  /* Make list of boundaries to be removed */
145  Vect_reset_list(AList);
146  for (i = 0; i < List->n_values; i++) {
147  int line, left, right, neighbour;
148 
149  line = List->value[i];
150  Vect_get_line_areas(Map, abs(line), &left, &right);
151  if (line > 0)
152  neighbour = left;
153  else
154  neighbour = right;
155 
156  G_debug(3, " neighbour = %d", neighbour);
157 
158  if (neighbour == dissolve_neighbour) {
159  Vect_list_append(AList, abs(line));
160  }
161  }
162 
163  /* Remove boundaries */
164  for (i = 0; i < AList->n_values; i++) {
165  int line;
166 
167  line = AList->value[i];
168 
169  if (Err) {
170  Vect_read_line(Map, Points, Cats, line);
171  Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
172  }
173  Vect_delete_line(Map, line);
174  }
175 
176  nremoved++;
177  nareas = Vect_get_num_areas(Map);
178  }
179 
180  if (removed_area)
181  *removed_area = size_removed;
182 
183  return (nremoved);
184 }
int Vect_get_area_centroid(struct Map_info *Map, int area)
Returns centroid number of area.
int l
Definition: dataquad.c:292
int Vect_get_area_boundaries(struct Map_info *Map, int area, struct ilist *List)
Creates list of boundaries for area.
int Vect_get_line_areas(struct Map_info *Map, int line, int *left, int *right)
Get area/isle ids on the left and right.
Definition: level_two.c:272
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_get_num_areas(struct Map_info *map)
Get number of areas in vector map.
Definition: level_two.c:81
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int G_percent(long n, long d, int s)
Print percent complete messages.
Definition: percent.c:63
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
return NULL
Definition: dbfopen.c:1394
double Vect_line_length(struct line_pnts *Points)
Calculate line length, 3D-length in case of 3D vector line.
Definition: line.c:555
int Vect_remove_small_areas(struct Map_info *Map, double thresh, struct Map_info *Err, double *removed_area)
Remove small areas from the map map.
Definition: remove_areas.c:41
double Vect_get_area_area(struct Map_info *Map, int area)
Returns area of area without areas of isles.
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int Vect_delete_line(struct Map_info *Map, int line)
Delete feature.
long Vect_write_line(struct Map_info *Map, int type, struct line_pnts *points, struct line_cats *cats)
Writes new feature to the end of file (table)
int Vect_area_alive(struct Map_info *Map, int area)
Check if area is alive or dead.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.