GRASS 8 Programmer's Manual 8.6.0dev(2026)-c83afef6d3
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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
12 SPDX-License-Identifier: GPL-2.0-or-later
13
14 \author Radim Blazek
15 */
16
17#include <string.h>
18#include <grass/vector.h>
19#include <grass/glocale.h>
20
21int Vect_overlay_and(struct Map_info *, int, struct ilist *, struct ilist *,
22 struct Map_info *, int, struct ilist *, struct ilist *,
23 struct Map_info *);
24
25/*!
26 \brief Get operator code from string
27
28 \param str operator code string
29
30 \return operator code
31 \return -1 on error
32 */
33int Vect_overlay_str_to_operator(const char *str)
34{
35
36 if (strcmp(str, GV_ON_AND) == 0)
37 return GV_O_AND;
38 else if (strcmp(str, GV_ON_OVERLAP) == 0)
39 return GV_O_OVERLAP;
40
41 return -1;
42}
43
44/*!
45 \brief Overlay 2 vector maps and create new one
46
47 \param AMap vector map A
48 \param atype feature type for A
49 \param AList unused ?
50 \param AAList unused ?
51 \param BMap vector map B
52 \param btype feature type for B
53 \param BList unused ?
54 \param BAList unused ?
55 \param operator operator code
56 \param[out] OMap output vector map
57
58 \return 0 on success
59 */
60int Vect_overlay(struct Map_info *AMap, int atype, struct ilist *AList,
61 struct ilist *AAList, /* map A */
62 struct Map_info *BMap, int btype, struct ilist *BList,
63 struct ilist *BAList, /* map B */
64 int operator, struct Map_info *OMap)
65{ /* output map */
66 switch (operator) {
67 case GV_O_AND:
69 OMap);
70 break;
71 default:
72 G_fatal_error("Vect_overlay(): %s", _("unknown operator"));
73 }
74
75 return 0;
76}
77
78/*!
79 \brief Overlay 2 vector maps with AND.
80
81 AND supports:point line area
82 point + - +
83 line - - -
84 area + - -
85
86 \param AMap vector map A
87 \param atype feature type for A
88 \param AList unused ?
89 \param AAList unused ?
90 \param BMap vector map B
91 \param btype feature type for B
92 \param BList unused ?
93 \param BAList unused ?
94 \param OMap output vector map
95
96 \return 1 on success
97 \return 0 on error
98 */
100 struct ilist *AList G_UNUSED,
101 struct ilist *AAList G_UNUSED, struct Map_info *BMap,
102 int btype, struct ilist *BList G_UNUSED,
103 struct ilist *BAList G_UNUSED, struct Map_info *OMap)
104{
105 int i, j, k, line, altype, bltype, oltype, area, centr;
106 struct line_pnts *Points;
107 struct line_cats *ACats, *BCats, *OCats;
108 struct ilist *AOList, *BOList;
109 struct boxlist *boxlist;
110 struct bound_box box;
111
112 /* TODO: support Lists */
113
114 Points = Vect_new_line_struct();
121
122 /* TODO: support all types; at present only point x point, area x point and
123 * point x area supported */
124 if ((atype & GV_LINES) || (btype & GV_LINES))
125 G_warning(
126 _("Overlay: line/boundary types not supported by AND operator"));
127
128 if ((atype & GV_AREA) && (btype & GV_AREA))
129 G_warning(
130 _("Overlay: area x area types not supported by AND operator"));
131
132 /* TODO: more points in one node in one map */
133
134 /* point x point: select all points with identical coordinates in both maps
135 */
136 if ((atype & GV_POINTS) &&
137 (btype & GV_POINTS)) { /* both points and centroids */
138 G_debug(3, "overlay: AND: point x point");
139 for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
140 altype = Vect_read_line(AMap, Points, ACats, i);
141 if (!(altype & GV_POINTS))
142 continue;
143
144 box.E = box.W = Points->x[0];
145 box.N = box.S = Points->y[0];
146 box.T = box.B = Points->z[0];
148
150
151 for (j = 0; j < boxlist->n_values; j++) {
152 line = boxlist->id[j];
154 if (!(bltype & GV_POINTS))
155 continue;
156
157 /* Identical points found -> write out */
158 /* TODO: do something if fields in ACats and BCats are identical
159 */
160 for (k = 0; k < ACats->n_cats; k++)
161 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
162
163 for (k = 0; k < BCats->n_cats; k++)
164 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
165
166 /* TODO: what to do if one type is GV_POINT and second
167 * GV_CENTROID */
168 oltype = altype;
169 Vect_write_line(OMap, oltype, Points, OCats);
170 Vect_list_append(AOList, i); /* add to list of written lines */
172 break;
173 }
174 }
175 }
176
177 /* TODO: check only labeled areas */
178 /* point x area: select points from A in areas in B */
179 if ((atype & GV_POINTS) &&
180 (btype & GV_AREA)) { /* both points and centroids */
181 G_debug(3, "overlay: AND: point x area");
182
183 for (i = 1; i <= Vect_get_num_lines(AMap); i++) {
184 altype = Vect_read_line(AMap, Points, ACats, i);
185 if (!(altype & GV_POINTS))
186 continue;
187
188 area = Vect_find_area(BMap, Points->x[0], Points->y[0]);
189 if (area == 0)
190 continue;
191
193
194 /* TODO: do something if fields in ACats and BCats are identical */
195 for (k = 0; k < ACats->n_cats; k++)
196 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
197
199 if (centr > 0) {
201 for (k = 0; k < BCats->n_cats; k++)
202 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
203 }
204
205 /* Check if not yet written */
206 if (!(Vect_val_in_list(AOList, i))) {
207 Vect_write_line(OMap, altype, Points, OCats);
209 }
210 }
211 }
212 /* area x point: select points from B in areas in A */
213 if ((btype & GV_POINTS) &&
214 (atype & GV_AREA)) { /* both points and centroids */
215 G_debug(3, "overlay: AND: area x point");
216
217 for (i = 1; i <= Vect_get_num_lines(BMap); i++) {
218 bltype = Vect_read_line(BMap, Points, BCats, i);
219 if (!(bltype & GV_POINTS))
220 continue;
221
222 area = Vect_find_area(AMap, Points->x[0], Points->y[0]);
223 if (area == 0)
224 continue;
225
227
228 /* TODO: do something if fields in ACats and BCats are identical */
229 for (k = 0; k < BCats->n_cats; k++)
230 Vect_cat_set(OCats, BCats->field[k], BCats->cat[k]);
231
233 if (centr > 0) {
235 for (k = 0; k < ACats->n_cats; k++)
236 Vect_cat_set(OCats, ACats->field[k], ACats->cat[k]);
237 }
238
239 /* Check if not yet written */
240 if (!(Vect_val_in_list(BOList, i))) {
241 Vect_write_line(OMap, bltype, Points, OCats);
243 }
244 }
245 }
246
254
255 return 0;
256}
#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:75
plus_t Vect_get_num_lines(struct Map_info *)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition level_two.c:73
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:30
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:43
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 G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
#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:99
int Vect_overlay_str_to_operator(const char *str)
Get operator code from string.
Definition overlay.c:33
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:60
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:712
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.