GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
overlay.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/overlay.c
3
4 \brief Vector library - overlays
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 This is file is just example and starting point for writing overlay
9 functions!!!
10
11 (C) 2001-2009 by the GRASS Development Team
12
13 This program is free software under the GNU General Public License
14 (>=v2). Read the file COPYING that comes with GRASS for details.
15
16 \author Radim Blazek
17 */
18
19#include <string.h>
20#include <grass/vector.h>
21#include <grass/glocale.h>
22
23int Vect_overlay_and(struct Map_info *, int, struct ilist *, struct ilist *,
24 struct Map_info *, int, struct ilist *, struct ilist *,
25 struct Map_info *);
26
27/*!
28 \brief Get operator code from string
29
30 \param str operator code string
31
32 \return operator code
33 \return -1 on error
34 */
35int Vect_overlay_str_to_operator(const char *str)
36{
37
38 if (strcmp(str, GV_ON_AND) == 0)
39 return GV_O_AND;
40 else if (strcmp(str, GV_ON_OVERLAP) == 0)
41 return GV_O_OVERLAP;
42
43 return -1;
44}
45
46/*!
47 \brief Overlay 2 vector maps and create new one
48
49 \param AMap vector map A
50 \param atype feature type for A
51 \param AList unused ?
52 \param AAList unused ?
53 \param BMap vector map B
54 \param btype feature type for B
55 \param BList unused ?
56 \param BAList unused ?
57 \param operator operator code
58 \param[out] OMap output vector map
59
60 \return 0 on success
61 */
62int Vect_overlay(struct Map_info *AMap, int atype, struct ilist *AList,
63 struct ilist *AAList, /* map A */
64 struct Map_info *BMap, int btype, struct ilist *BList,
65 struct ilist *BAList, /* map B */
66 int operator, struct Map_info *OMap)
67{ /* output map */
68 switch (operator) {
69 case GV_O_AND:
71 OMap);
72 break;
73 default:
74 G_fatal_error("Vect_overlay(): %s", _("unknown operator"));
75 }
76
77 return 0;
78}
79
80/*!
81 \brief Overlay 2 vector maps with AND.
82
83 AND supports:point line area
84 point + - +
85 line - - -
86 area + - -
87
88 \param AMap vector map A
89 \param atype feature type for A
90 \param AList unused ?
91 \param AAList unused ?
92 \param BMap vector map B
93 \param btype feature type for B
94 \param BList unused ?
95 \param BAList unused ?
96 \param OMap output vector map
97
98 \return 1 on success
99 \return 0 on error
100 */
102 struct ilist *AList UNUSED, struct ilist *AAList UNUSED,
103 struct Map_info *BMap, int btype,
104 struct ilist *BList UNUSED, struct ilist *BAList UNUSED,
105 struct Map_info *OMap)
106{
107 int i, j, k, line, altype, bltype, oltype, area, centr;
108 struct line_pnts *Points;
109 struct line_cats *ACats, *BCats, *OCats;
110 struct ilist *AOList, *BOList;
111 struct boxlist *boxlist;
112 struct bound_box box;
113
114 /* TODO: support Lists */
115
116 Points = Vect_new_line_struct();
123
124 /* TODO: support all types; at present only point x point, area x point and
125 * point x area supported */
126 if ((atype & GV_LINES) || (btype & GV_LINES))
127 G_warning(
128 _("Overlay: line/boundary types not supported by AND operator"));
129
130 if ((atype & GV_AREA) && (btype & GV_AREA))
131 G_warning(
132 _("Overlay: area x area types not supported by AND operator"));
133
134 /* TODO: more points in one node in one map */
135
136 /* point x point: select all points with identical coordinates in both maps
137 */
138 if ((atype & GV_POINTS) &&
139 (btype & GV_POINTS)) { /* both points and centroids */
140 G_debug(3, "overlay: AND: point x point");
141 for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
142 altype = Vect_read_line(AMap, Points, ACats, i);
143 if (!(altype & GV_POINTS))
144 continue;
145
146 box.E = box.W = Points->x[0];
147 box.N = box.S = Points->y[0];
148 box.T = box.B = Points->z[0];
150
152
153 for (j = 0; j < boxlist->n_values; j++) {
154 line = boxlist->id[j];
156 if (!(bltype & GV_POINTS))
157 continue;
158
159 /* Identical points found -> write out */
160 /* TODO: do something if fields in ACats and BCats are identical
161 */
162 for (k = 0; k < ACats->n_cats; k++)
163 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
164
165 for (k = 0; k < BCats->n_cats; k++)
166 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
167
168 /* TODO: what to do if one type is GV_POINT and second
169 * GV_CENTROID */
170 oltype = altype;
171 Vect_write_line(OMap, oltype, Points, OCats);
172 Vect_list_append(AOList, i); /* add to list of written lines */
174 break;
175 }
176 }
177 }
178
179 /* TODO: check only labeled areas */
180 /* point x area: select points from A in areas in B */
181 if ((atype & GV_POINTS) &&
182 (btype & GV_AREA)) { /* both points and centroids */
183 G_debug(3, "overlay: AND: point x area");
184
185 for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
186 altype = Vect_read_line(AMap, Points, ACats, i);
187 if (!(altype & GV_POINTS))
188 continue;
189
190 area = Vect_find_area(BMap, Points->x[0], Points->y[0]);
191 if (area == 0)
192 continue;
193
195
196 /* TODO: do something if fields in ACats and BCats are identical */
197 for (k = 0; k < ACats->n_cats; k++)
198 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
199
201 if (centr > 0) {
203 for (k = 0; k < BCats->n_cats; k++)
204 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
205 }
206
207 /* Check if not yet written */
208 if (!(Vect_val_in_list(AOList, i))) {
209 Vect_write_line(OMap, altype, Points, OCats);
211 }
212 }
213 }
214 /* area x point: select points from B in areas in A */
215 if ((btype & GV_POINTS) &&
216 (atype & GV_AREA)) { /* both points and centroids */
217 G_debug(3, "overlay: AND: area x point");
218
219 for (i = 1; i <= Vect_get_num_lines(BMap); i++) {
220 bltype = Vect_read_line(BMap, Points, BCats, i);
221 if (!(bltype & GV_POINTS))
222 continue;
223
224 area = Vect_find_area(AMap, Points->x[0], Points->y[0]);
225 if (area == 0)
226 continue;
227
229
230 /* TODO: do something if fields in ACats and BCats are identical */
231 for (k = 0; k < BCats->n_cats; k++)
232 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
233
235 if (centr > 0) {
237 for (k = 0; k < ACats->n_cats; k++)
238 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
239 }
240
241 /* Check if not yet written */
242 if (!(Vect_val_in_list(BOList, i))) {
243 Vect_write_line(OMap, bltype, Points, OCats);
245 }
246 }
247 }
248
256
257 return 0;
258}
#define NULL
Definition ccmath.h:32
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition line.c:77
plus_t Vect_get_num_lines(struct Map_info *)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition level_two.c:75
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn't exist yet.
struct boxlist * Vect_new_boxlist(int)
Creates and initializes a struct boxlist.
void Vect_destroy_boxlist(struct boxlist *)
Frees all memory associated with a struct boxlist, including the struct itself.
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_list_append(struct ilist *, int)
Append new item to the end of list if not yet present.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
int Vect_select_lines_by_box(struct Map_info *, const struct bound_box *, int, struct boxlist *)
Select lines with bounding boxes by box.
Definition sindex.c:32
int Vect_val_in_list(const struct ilist *, int)
Find a given item in the list.
int Vect_get_area_centroid(struct Map_info *, int)
Returns centroid id for given area.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition line.c:45
int Vect_find_area(struct Map_info *, double, double)
Find the nearest area.
#define GV_ON_OVERLAP
#define GV_LINES
#define GV_ON_AND
Overlay operators.
@ GV_O_AND
@ GV_O_OVERLAP
#define GV_POINTS
#define GV_AREA
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:46
#define _(str)
Definition glocale.h:10
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:101
int Vect_overlay_str_to_operator(const char *str)
Get operator code from string.
Definition overlay.c:35
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:62
Vector map info.
Bounding box.
Definition dig_structs.h:62
double W
West.
Definition dig_structs.h:78
double T
Top.
Definition dig_structs.h:82
double S
South.
Definition dig_structs.h:70
double N
North.
Definition dig_structs.h:66
double E
East.
Definition dig_structs.h:74
double B
Bottom.
Definition dig_structs.h:86
List of bounding boxes with id.
int * id
Array of ids.
int n_values
Number of items in the list.
List of integers.
Definition gis.h:715
Feature category info.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
double * z
Array of Z coordinates.