GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
overlay.c
Go to the documentation of this file.
1 
23 #include <string.h>
24 #include <grass/Vect.h>
25 #include <grass/glocale.h>
26 
27 int Vect_overlay_and(struct Map_info *, int, struct ilist *, struct ilist *,
28  struct Map_info *, int, struct ilist *, struct ilist *,
29  struct Map_info *);
30 
39 int Vect_overlay_str_to_operator(const char *str)
40 {
41 
42  if (strcmp(str, GV_ON_AND) == 0)
43  return GV_O_AND;
44  else if (strcmp(str, GV_ON_OVERLAP) == 0)
45  return GV_O_OVERLAP;
46 
47  return -1;
48 }
49 
64 int Vect_overlay(struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList, /* map A */
65  struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList, /* map B */
66  int operator, struct Map_info *OMap)
67 { /* output map */
68  switch (operator) {
69  case GV_O_AND:
70  Vect_overlay_and(AMap, atype, AList, AAList, BMap, btype, BList,
71  BAList, OMap);
72  break;
73  default:
74  G_fatal_error("Vect_overlay(): %s", _("unknown operator"));
75  }
76 
77  return 0;
78 }
79 
101 int
102 Vect_overlay_and(struct Map_info *AMap, int atype, struct ilist *AList,
103  struct ilist *AAList, struct Map_info *BMap, int btype,
104  struct ilist *BList, struct ilist *BAList,
105  struct Map_info *OMap)
106 {
107  int i, j, k, node, line, altype, bltype, oltype, area, centr;
108  struct line_pnts *Points;
109  struct line_cats *ACats, *BCats, *OCats;
110  struct ilist *AOList, *BOList;
111 
112  /* TODO: support Lists */
113 
114  Points = Vect_new_line_struct();
115  ACats = Vect_new_cats_struct();
116  BCats = Vect_new_cats_struct();
117  OCats = Vect_new_cats_struct();
118  AOList = Vect_new_list();
119  BOList = Vect_new_list();
120 
121  /* TODO: support all types; at present only point x point, area x point and point x area supported */
122  if ((atype & GV_LINES) || (btype & GV_LINES))
123  G_warning(_("Overlay: line/boundary types not supported by AND operator"));
124 
125  if ((atype & GV_AREA) && (btype & GV_AREA))
126  G_warning(_("Overlay: area x area types not supported by AND operator"));
127 
128  /* TODO: more points in one node in one map */
129 
130  /* point x point: select all points with identical coordinates in both maps */
131  if ((atype & GV_POINTS) && (btype & GV_POINTS)) { /* both points and centroids */
132  G_debug(3, "overlay: AND: point x point");
133  for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
134  altype = Vect_read_line(AMap, Points, ACats, i);
135  if (!(altype & GV_POINTS))
136  continue;
137 
138  node =
139  Vect_find_node(BMap, Points->x[0], Points->y[0], Points->z[0],
140  0, 1);
141  G_debug(3, "overlay: node = %d", node);
142  if (node == 0)
143  continue;
144 
145  Vect_reset_cats(OCats);
146 
147  for (j = 0; j < Vect_get_node_n_lines(BMap, node); j++) {
148  line = Vect_get_node_line(BMap, node, j);
149  bltype = Vect_read_line(BMap, NULL, BCats, line);
150  if (!(bltype & GV_POINTS))
151  continue;
152 
153  /* Identical points found -> write out */
154  /* TODO: do something if fields in ACats and BCats are identical */
155  for (k = 0; k < ACats->n_cats; k++)
156  Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
157 
158  for (k = 0; k < BCats->n_cats; k++)
159  Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
160 
161  /* TODO: what to do if one type is GV_POINT and second GV_CENTROID */
162  oltype = altype;
163  Vect_write_line(OMap, oltype, Points, OCats);
164  Vect_list_append(AOList, i); /* add to list of written lines */
165  Vect_list_append(BOList, line);
166  break;
167  }
168  }
169  }
170 
171  /* TODO: check only labeled areas */
172  /* point x area: select points from A in areas in B */
173  if ((atype & GV_POINTS) && (btype & GV_AREA)) { /* both points and centroids */
174  G_debug(3, "overlay: AND: point x area");
175 
176  for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
177  altype = Vect_read_line(AMap, Points, ACats, i);
178  if (!(altype & GV_POINTS))
179  continue;
180 
181  area = Vect_find_area(BMap, Points->x[0], Points->y[0]);
182  if (area == 0)
183  continue;
184 
185  Vect_reset_cats(OCats);
186 
187  /* TODO: do something if fields in ACats and BCats are identical */
188  for (k = 0; k < ACats->n_cats; k++)
189  Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
190 
191  centr = Vect_get_area_centroid(BMap, area);
192  if (centr > 0) {
193  bltype = Vect_read_line(BMap, NULL, BCats, centr);
194  for (k = 0; k < BCats->n_cats; k++)
195  Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
196  }
197 
198  /* Check if not yet written */
199  if (!(Vect_val_in_list(AOList, i))) {
200  Vect_write_line(OMap, altype, Points, OCats);
201  Vect_list_append(AOList, i);
202  }
203 
204  }
205  }
206  /* area x point: select points from B in areas in A */
207  if ((btype & GV_POINTS) && (atype & GV_AREA)) { /* both points and centroids */
208  G_debug(3, "overlay: AND: area x point");
209 
210  for (i = 1; i <= Vect_get_num_lines(BMap); i++) {
211  bltype = Vect_read_line(BMap, Points, BCats, i);
212  if (!(bltype & GV_POINTS))
213  continue;
214 
215  area = Vect_find_area(AMap, Points->x[0], Points->y[0]);
216  if (area == 0)
217  continue;
218 
219  Vect_reset_cats(OCats);
220 
221  /* TODO: do something if fields in ACats and BCats are identical */
222  for (k = 0; k < BCats->n_cats; k++)
223  Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
224 
225  centr = Vect_get_area_centroid(AMap, area);
226  if (centr > 0) {
227  altype = Vect_read_line(AMap, NULL, ACats, centr);
228  for (k = 0; k < ACats->n_cats; k++)
229  Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
230  }
231 
232  /* Check if not yet written */
233  if (!(Vect_val_in_list(BOList, i))) {
234  Vect_write_line(OMap, bltype, Points, OCats);
235  Vect_list_append(BOList, i);
236  }
237 
238  }
239  }
240 
241  return 0;
242 }
int Vect_get_area_centroid(struct Map_info *Map, int area)
Returns centroid number of area.
int Vect_get_node_line(struct Map_info *Map, int node, int line)
Get line id for node line index.
Definition: level_two.c:316
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_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
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.
int Vect_find_area(struct Map_info *Map, double x, double y)
Find the nearest area.
int Vect_overlay_and(struct Map_info *, int, struct ilist *, struct ilist *, struct Map_info *, int, struct ilist *, struct ilist *, struct Map_info *)
Overlay 2 vector maps with AND.
Definition: overlay.c:102
int GV_LINES
Definition: vdigit/main.py:24
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_find_node(struct Map_info *Map, double ux, double uy, double uz, double maxdist, int with_z)
Find the nearest node.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int Vect_get_num_lines(struct Map_info *map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition: level_two.c:69
int Vect_overlay(struct Map_info *AMap, int atype, struct ilist *AList, struct ilist *AAList, struct Map_info *BMap, int btype, struct ilist *BList, struct ilist *BAList, int operator, struct Map_info *OMap)
Overlay 2 vector maps and create new one.
Definition: overlay.c:64
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
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_val_in_list(struct ilist *list, int val)
Find a given item in the list.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_overlay_str_to_operator(const char *str)
Get operator code from string.
Definition: overlay.c:39
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.
int Vect_get_node_n_lines(struct Map_info *Map, int node)
Get number of lines for node.
Definition: level_two.c:296