GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vector/Vlib/area.c
Go to the documentation of this file.
1 
21 #include <stdlib.h>
22 #include <grass/gis.h>
23 #include <grass/Vect.h>
24 #include <grass/glocale.h>
25 
36 int
37 Vect_get_area_points(struct Map_info *Map,
38  int area, struct line_pnts *BPoints)
39 {
40  int i, line, aline, dir;
41  struct Plus_head *Plus;
42  P_AREA *Area;
43  static int first_time = 1;
44  static struct line_pnts *Points;
45 
46  G_debug(3, "Vect_get_area_points(): area = %d", area);
47  BPoints->n_points = 0;
48 
49  Plus = &(Map->plus);
50  Area = Plus->Area[area];
51 
52  if (Area == NULL) { /* dead area */
53  G_warning(_("Attempt to read points of nonexistent area"));
54  return -1; /* error , because we should not read dead areas */
55  }
56 
57  if (first_time == 1) {
58  Points = Vect_new_line_struct();
59  first_time = 0;
60  }
61 
62  G_debug(3, " n_lines = %d", Area->n_lines);
63  for (i = 0; i < Area->n_lines; i++) {
64  line = Area->lines[i];
65  aline = abs(line);
66  G_debug(3, " append line(%d) = %d", i, line);
67 
68  if (0 > Vect_read_line(Map, Points, NULL, aline)) {
69  G_fatal_error("Cannot read line %d", aline);
70  }
71 
72  G_debug(3, " line n_points = %d", Points->n_points);
73 
74  if (line > 0)
75  dir = GV_FORWARD;
76  else
77  dir = GV_BACKWARD;
78 
79  Vect_append_points(BPoints, Points, dir);
80  if (i != (Area->n_lines - 1)) /* all but not last */
81  BPoints->n_points--;
82  G_debug(3, " area n_points = %d", BPoints->n_points);
83  }
84 
85  return (BPoints->n_points);
86 }
87 
97 int
98 Vect_get_isle_points(struct Map_info *Map,
99  int isle, struct line_pnts *BPoints)
100 {
101  int i, line, aline, dir;
102  struct Plus_head *Plus;
103  P_ISLE *Isle;
104  static int first_time = 1;
105  static struct line_pnts *Points;
106 
107  G_debug(3, "Vect_get_isle_points(): isle = %d", isle);
108  BPoints->n_points = 0;
109 
110  Plus = &(Map->plus);
111  Isle = Plus->Isle[isle];
112 
113  if (first_time == 1) {
114  Points = Vect_new_line_struct();
115  first_time = 0;
116  }
117 
118  G_debug(3, " n_lines = %d", Isle->n_lines);
119  for (i = 0; i < Isle->n_lines; i++) {
120  line = Isle->lines[i];
121  aline = abs(line);
122  G_debug(3, " append line(%d) = %d", i, line);
123 
124  if (0 > Vect_read_line(Map, Points, NULL, aline)) {
125  G_fatal_error(_("Unable to read line %d"), aline);
126  }
127 
128  G_debug(3, " line n_points = %d", Points->n_points);
129 
130  if (line > 0)
131  dir = GV_FORWARD;
132  else
133  dir = GV_BACKWARD;
134 
135  Vect_append_points(BPoints, Points, dir);
136  if (i != (Isle->n_lines - 1)) /* all but not last */
137  BPoints->n_points--;
138  G_debug(3, " isle n_points = %d", BPoints->n_points);
139  }
140 
141  return (BPoints->n_points);
142 }
143 
153 int Vect_get_area_centroid(struct Map_info *Map, int area)
154 {
155  struct Plus_head *Plus;
156  P_AREA *Area;
157 
158  G_debug(3, "Vect_get_area_centroid(): area = %d", area);
159 
160  Plus = &(Map->plus);
161  Area = Plus->Area[area];
162 
163  if (Area == NULL)
164  G_fatal_error(_("Attempt to read topo for dead area (%d)"), area);
165 
166  return (Area->centroid);
167 }
168 
178 int
179 Vect_get_area_boundaries(struct Map_info *Map, int area, struct ilist *List)
180 {
181  int i, line;
182  struct Plus_head *Plus;
183  P_AREA *Area;
184 
185  G_debug(3, "Vect_get_area_boundaries(): area = %d", area);
186 
187  Vect_reset_list(List);
188 
189  Plus = &(Map->plus);
190  Area = Plus->Area[area];
191 
192  if (Area == NULL)
193  G_fatal_error(_("Attempt to read topo for dead area (%d)"), area);
194 
195  for (i = 0; i < Area->n_lines; i++) {
196  line = Area->lines[i];
197  Vect_list_append(List, line);
198  }
199 
200  return (List->n_values);
201 }
202 
212 int
213 Vect_get_isle_boundaries(struct Map_info *Map, int isle, struct ilist *List)
214 {
215  int i, line;
216  struct Plus_head *Plus;
217  P_ISLE *Isle;
218 
219  G_debug(3, "Vect_get_isle_boundaries(): isle = %d", isle);
220 
221  Vect_reset_list(List);
222 
223  Plus = &(Map->plus);
224  Isle = Plus->Isle[isle];
225 
226  if (Isle == NULL)
227  G_fatal_error("Attempt to read topo for dead isle (%d)", isle);
228 
229  for (i = 0; i < Isle->n_lines; i++) {
230  line = Isle->lines[i];
231  Vect_list_append(List, line);
232  }
233 
234  return (List->n_values);
235 }
236 
246 int Vect_get_area_num_isles(struct Map_info *Map, int area)
247 {
248  struct Plus_head *Plus;
249  P_AREA *Area;
250 
251  G_debug(3, "Vect_get_area_num_isles(): area = %d", area);
252 
253  Plus = &(Map->plus);
254  Area = Plus->Area[area];
255 
256  if (Area == NULL)
257  G_fatal_error(_("Attempt to read topo for dead area (%d)"), area);
258 
259  G_debug(3, " n_isles = %d", Area->n_isles);
260 
261  return (Area->n_isles);
262 
263 }
264 
275 int Vect_get_area_isle(struct Map_info *Map, int area, int isle)
276 {
277  struct Plus_head *Plus;
278  P_AREA *Area;
279 
280  G_debug(3, "Vect_get_area_isle(): area = %d isle = %d", area, isle);
281 
282  Plus = &(Map->plus);
283  Area = Plus->Area[area];
284 
285  if (Area == NULL)
286  G_fatal_error(_("Attempt to read topo for dead area (%d)"), area);
287 
288  G_debug(3, " -> isle = %d", Area->isles[isle]);
289 
290  return (Area->isles[isle]);
291 }
292 
302 int Vect_get_isle_area(struct Map_info *Map, int isle)
303 {
304  struct Plus_head *Plus;
305  P_ISLE *Isle;
306 
307  G_debug(3, "Vect_get_isle_area(): isle = %d", isle);
308 
309  Plus = &(Map->plus);
310  Isle = Plus->Isle[isle];
311 
312  if (Isle == NULL)
313  G_fatal_error(_("Attempt to read topo for dead isle (%d)"), isle);
314 
315  G_debug(3, " -> area = %d", Isle->area);
316 
317  return (Isle->area);
318 }
319 
320 
328 double Vect_area_perimeter(struct line_pnts *Points)
329 {
330  return Vect_line_length(Points);
331 }
332 
333 
344 int Vect_point_in_area(struct Map_info *Map, int area, double x, double y)
345 {
346  int i, isle;
347  struct Plus_head *Plus;
348  P_AREA *Area;
349  int poly;
350 
351  Plus = &(Map->plus);
352  Area = Plus->Area[area];
353  if (Area == NULL)
354  return 0;
355 
356  poly = Vect_point_in_area_outer_ring(x, y, Map, area);
357  if (poly == 0)
358  return 0; /* includes area boundary (poly == 2), OK? */
359 
360  /* check if in islands */
361  for (i = 0; i < Area->n_isles; i++) {
362  isle = Area->isles[i];
363  poly = Vect_point_in_island(x, y, Map, isle);
364  if (poly >= 1)
365  return 0; /* excludes island boundary (poly == 2), OK? */
366  }
367 
368  return 1;
369 }
370 
379 double Vect_get_area_area(struct Map_info *Map, int area)
380 {
381  struct Plus_head *Plus;
382  P_AREA *Area;
383  struct line_pnts *Points;
384  double size;
385  int i;
386  static int first_time = 1;
387 
388  G_debug(3, "Vect_get_area_area(): area = %d", area);
389 
390  if (first_time == 1) {
392  first_time = 0;
393  }
394 
395  Points = Vect_new_line_struct();
396  Plus = &(Map->plus);
397  Area = Plus->Area[area];
398 
399  Vect_get_area_points(Map, area, Points);
400  size = G_area_of_polygon(Points->x, Points->y, Points->n_points);
401 
402  /* substructing island areas */
403  for (i = 0; i < Area->n_isles; i++) {
404  Vect_get_isle_points(Map, Area->isles[i], Points);
405  size -= G_area_of_polygon(Points->x, Points->y, Points->n_points);
406  }
407 
408  Vect_destroy_line_struct(Points);
409 
410  G_debug(3, " area = %f", size);
411 
412  return (size);
413 }
414 
425 int Vect_get_area_cats(struct Map_info *Map, int area, struct line_cats *Cats)
426 {
427  int centroid;
428 
429  Vect_reset_cats(Cats);
430 
431  centroid = Vect_get_area_centroid(Map, area);
432  if (centroid > 0) {
433  Vect_read_line(Map, NULL, Cats, centroid);
434  }
435  else {
436  return 1; /* no centroid */
437  }
438 
439 
440  return 0;
441 }
442 
453 int Vect_get_area_cat(struct Map_info *Map, int area, int field)
454 {
455  int i;
456  static struct line_cats *Cats = NULL;
457 
458  if (!Cats)
459  Cats = Vect_new_cats_struct();
460  else
461  Vect_reset_cats(Cats);
462 
463  if (Vect_get_area_cats(Map, area, Cats) == 1 || Cats->n_cats == 0) {
464  return -1;
465  }
466 
467  for (i = 0; i < Cats->n_cats; i++) {
468  if (Cats->field[i] == field) {
469  return Cats->cat[i];
470  }
471  }
472 
473  return -1;
474 }
int Vect_get_area_centroid(struct Map_info *Map, int area)
Returns centroid number of area.
int Vect_get_area_boundaries(struct Map_info *Map, int area, struct ilist *List)
Creates list of boundaries for area.
int Vect_get_isle_points(struct Map_info *Map, int isle, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
double G_area_of_polygon(const double *x, const double *y, int n)
Area in square meters of polygon.
Definition: gis/area.c:165
int Vect_get_isle_area(struct Map_info *Map, int isle)
Returns area for isle.
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
int Vect_point_in_island(double X, double Y, struct Map_info *Map, int isle)
Determines if a point (X,Y) is inside an island.
Definition: Vlib/poly.c:760
int Vect_append_points(struct line_pnts *Points, struct line_pnts *APoints, int direction)
Appends points to the end of a line.
Definition: line.c:312
int Vect_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_get_area_num_isles(struct Map_info *Map, int area)
Returns number of isles for area.
int Vect_get_area_isle(struct Map_info *Map, int area, int isle)
Returns isle for area.
int y
Definition: plot.c:34
int Vect_get_area_cat(struct Map_info *Map, int area, int field)
Find FIRST category of given field and area.
int Vect_get_area_points(struct Map_info *Map, int area, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int Vect_point_in_area_outer_ring(double X, double Y, struct Map_info *Map, int area)
Determines if a point (X,Y) is inside an area outer ring. Islands are not considered.
Definition: Vlib/poly.c:695
int G_begin_polygon_area_calculations(void)
Begin polygon area calculations.
Definition: gis/area.c:123
int Vect_point_in_area(struct Map_info *Map, int area, double x, double y)
Returns 1 if point is in area.
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_get_isle_boundaries(struct Map_info *Map, int isle, struct ilist *List)
Creates list of boundaries for isle.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_get_area_cats(struct Map_info *Map, int area, struct line_cats *Cats)
Get area categories.
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
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
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
double Vect_area_perimeter(struct line_pnts *Points)
Calculate area perimeter.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
Definition: line.c:90
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.