GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
level_two.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/level_two.c
3
4 \brief Vector library - topology level functions
5
6 SPDX-FileCopyrightText: 2001-2009, 2011-2012 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
10 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
11 \author Update to GRASS 7 by Martin Landa <landa.martin gmail.com>
12 */
13
14#include <stdlib.h>
15#include <grass/vector.h>
16#include <grass/glocale.h>
17
18static void check_level(struct Map_info *Map)
19{
20 if (Map->level < 2)
21 G_fatal_error(_("Vector map <%s> is not open at topological level"),
23}
24
25/*!
26 \brief Get number of nodes in vector map
27
28 \param Map pointer to Map_info struct
29
30 \return number of nodes
31 */
33{
34 return (Map->plus.n_nodes);
35}
36
37/*!
38 \brief Get number of primitives in vector map
39
40 \param Map pointer to Map_info struct
41 \param type feature type
42
43 \return number of primitives
44 */
46{
47 plus_t num = 0;
48
49 if (type & GV_POINT)
50 num += Map->plus.n_plines;
51 if (type & GV_LINE)
52 num += Map->plus.n_llines;
53 if (type & GV_BOUNDARY)
54 num += Map->plus.n_blines;
55 if (type & GV_CENTROID)
56 num += Map->plus.n_clines;
57 if (type & GV_FACE)
58 num += Map->plus.n_flines;
59 if (type & GV_KERNEL)
60 num += Map->plus.n_klines;
61
62 return num;
63}
64
65/*!
66 \brief Fetch number of features (points, lines, boundaries, centroids) in
67 vector map
68
69 \param Map pointer to Map_info struct
70
71 \return number of features
72 */
74{
75 return (Map->plus.n_lines);
76}
77
78/*!
79 \brief Get number of areas in vector map
80
81 \param Map pointer to Map_info struct
82
83 \return number of areas
84 */
86{
87 return (Map->plus.n_areas);
88}
89
90/*!
91 \brief Fetch number of kernels in vector map
92
93 \param Map pointer to Map_info struct
94
95 \return number of kernels
96 */
98{
99 return (Map->plus.n_klines);
100}
101
102/*!
103 \brief Get number of faces in vector map
104
105 \param Map pointer to Map_info struct
106
107 \return number of faces
108 */
110{
111 return (Map->plus.n_flines);
112}
113
114/*!
115 \brief Fetch number of volumes in vector map
116
117 \param Map pointer to Map_info struct
118
119 \return number of volumes
120 */
122{
123 return (Map->plus.n_volumes);
124}
125
126/*!
127 \brief Get number of islands in vector map
128
129 \param Map pointer to Map_info struct
130
131 \return number of islands
132 */
134{
135 return (Map->plus.n_isles);
136}
137
138/*!
139 \brief Fetch number of holes in vector map
140
141 \param Map pointer to Map_info struct
142
143 \return number of holes
144 */
146{
147 return (Map->plus.n_holes);
148}
149
150/*!
151 \brief Get number of defined dblinks
152
153 \param Map pointer to Map_info struct
154
155 \return number of dblinks
156 */
158{
159 /* available on level 1 ? */
160 return (Map->dblnk->n_fields);
161}
162
163/*!
164 \brief Get number of updated features
165
166 Note: Vect_set_updated() must be called to maintain list of updated
167 features
168
169 \param Map pointer to Map_info struct
170
171 \return number of updated features
172 */
174{
175 return (Map->plus.uplist.n_uplines);
176}
177
178/*!
179 \brief Get updated line by index
180
181 Note: Vect_set_updated() must be called to maintain list of updated
182 features
183
184 \param Map pointer to Map_info struct
185 \param idx index
186
187 \return updated line
188 */
189int Vect_get_updated_line(struct Map_info *Map, int idx)
190{
191 return (Map->plus.uplist.uplines[idx]);
192}
193
194/*!
195 \brief Get updated line offset by index
196
197 Note: Vect_set_updated() must be called to maintain list of updated
198 features
199
200 \param Map pointer to Map_info struct
201 \param idx index
202
203 \return updated line
204 */
206{
207 return (Map->plus.uplist.uplines_offset[idx]);
208}
209
210/*!
211 \brief Get number of updated nodes
212
213 \param Map pointer to Map_info struct
214
215 \return number of updated nodes
216 */
218{
219 return (Map->plus.uplist.n_upnodes);
220}
221
222/*!
223 \brief Get updated (modified) node by index
224
225 Note: Vect_set_updated() must be called to maintain list of updated
226 features
227
228 Negative id:
229 - if Node[id] is not NULL then the node was added
230 - if Node[id] is NULL then the node was deleted
231 Positive id:
232 - node was updated
233
234 \param Map pointer to Map_info struct
235 \param idx index
236
237 \return id of modified node
238 */
239int Vect_get_updated_node(struct Map_info *Map, int idx)
240{
241 return (Map->plus.uplist.upnodes[idx]);
242}
243
244/*!
245 \brief Get line type
246
247 \param Map pointer to Map_info struct
248 \param line line id
249
250 \return line type
251 */
252int Vect_get_line_type(struct Map_info *Map, int line)
253{
254 check_level(Map);
255
256 if (!Vect_line_alive(Map, line))
257 return 0;
258
259 return (Map->plus.Line[line]->type);
260}
261
262/*!
263 \brief Get node coordinates
264
265 \param Map pointer to Map_info struct
266 \param num node id (starts at 1)
267 \param[out] x,y,z coordinates values (for 2D coordinates z is NULL)
268
269 \return 0 on success
270 \return -1 on error
271 */
272int Vect_get_node_coor(struct Map_info *Map, int num, double *x, double *y,
273 double *z)
274{
275 struct P_node *Node;
276
277 if (num < 1 || num > Map->plus.n_nodes) {
278 G_warning(_("Invalid node id: %d"), num);
279 return -1;
280 }
281
282 Node = Map->plus.Node[num];
283 *x = Node->x;
284 *y = Node->y;
285
286 if (z != NULL)
287 *z = Node->z;
288
289 return 0;
290}
291
292/*!
293 \brief Get line nodes
294
295 \param Map pointer to Map_info struct
296 \param line line id
297 \param n1 (start node), ids of line nodes (or NULL)
298 \param n2 (end node) ids of line nodes (or NULL)
299
300 \return 1
301 */
302int Vect_get_line_nodes(struct Map_info *Map, int line, int *n1, int *n2)
303{
304 char type;
305
306 check_level(Map);
307
308 type = Vect_get_line_type(Map, line);
309
310 if (!(type & GV_LINES))
311 G_fatal_error(_("Nodes not available for line %d"), line);
312
313 if (type == GV_LINE) {
314 struct P_topo_l *topo = (struct P_topo_l *)Map->plus.Line[line]->topo;
315
316 if (n1 != NULL)
317 *n1 = topo->N1;
318 if (n2 != NULL)
319 *n2 = topo->N2;
320 }
321 else if (type == GV_BOUNDARY) {
322 struct P_topo_b *topo = (struct P_topo_b *)Map->plus.Line[line]->topo;
323
324 if (n1 != NULL)
325 *n1 = topo->N1;
326 if (n2 != NULL)
327 *n2 = topo->N2;
328 }
329
330 return 1;
331}
332
333/*!
334 \brief Get area id on the left and right side of the boundary
335
336 Negative area id indicates an isle.
337
338 \param Map pointer to Map_info struct
339 \param line line id
340 \param[out] left,right area id on the left and right side
341
342 \return 1 on success
343 \return -1 on failure (topology not available, line is not a boundary)
344 */
345int Vect_get_line_areas(struct Map_info *Map, int line, int *left, int *right)
346{
347 struct P_topo_b *topo;
348
349 check_level(Map);
350
351 if (!Map->plus.Line[line]->topo) {
352 G_warning(_("Areas not available for line %d"), line);
353 return -1;
354 }
355
356 if (Vect_get_line_type(Map, line) != GV_BOUNDARY) {
357 G_warning(_("Line %d is not a boundary"), line);
358 return -1;
359 }
360
361 topo = (struct P_topo_b *)Map->plus.Line[line]->topo;
362 if (left != NULL)
363 *left = topo->left;
364
365 if (right != NULL)
366 *right = topo->right;
367
368 return 1;
369}
370
371/*!
372 \brief Get number of lines for node
373
374 \param Map pointer to Map_info struct
375 \param node node id
376
377 \return numbers of lines
378 */
379int Vect_get_node_n_lines(struct Map_info *Map, int node)
380{
381 check_level(Map);
382
383 return (Map->plus.Node[node]->n_lines);
384}
385
386/*!
387 \brief Get line id for node line index
388
389 \param Map pointer to Map_info struct
390 \param node node id
391 \param line line index (range: 0 - Vect_get_node_n_lines())
392
393 \return line id
394 */
395int Vect_get_node_line(struct Map_info *Map, int node, int line)
396{
397 check_level(Map);
398
399 return (Map->plus.Node[node]->lines[line]);
400}
401
402/*!
403 \brief Angle of segment of the line connected to the node
404
405 \param Map pointer to Map_info struct
406 \param node node number
407 \param line line index (range: 0 - Vect_get_node_n_lines())
408
409 \return angle of segment of the line connected to the node
410 */
411float Vect_get_node_line_angle(struct Map_info *Map, int node, int line)
412{
413 check_level(Map);
414
415 return (Map->plus.Node[node]->angles[line]);
416}
417
418/*!
419 \brief Get area id the centroid is within
420
421 \param Map pointer to Map_info struct
422 \param centroid centroid id
423
424 \return area id the centroid is within
425 \return 0 for not in area
426 \return negative id if centroid is duplicated in the area
427 */
428int Vect_get_centroid_area(struct Map_info *Map, int centroid)
429{
430 struct P_topo_c *topo;
431
432 check_level(Map);
433
434 if (Map->plus.Line[centroid]->type != GV_CENTROID)
435 return 0;
436
437 topo = (struct P_topo_c *)Map->plus.Line[centroid]->topo;
438 if (!topo)
439 G_fatal_error(_("Topology info not available for feature %d"),
440 centroid);
441
442 return (topo->area);
443}
444
445/*!
446 \brief Enable/disable maintenance of list of updated lines/nodes
447
448 See Plus_head.uplist for details.
449
450 \param Map pointer to Map_info struct
451 \param enable TRUE/FALSE to enable/disable
452 */
454{
455 G_debug(1, "Vect_set_updated(): name = '%s' enabled = %d", Map->name,
456 enable);
457
458 check_level(Map);
459
460 Map->plus.uplist.do_uplist = enable != 0 ? TRUE : FALSE;
461}
462
463/*!
464 \brief Reset list of updated lines/nodes
465
466 \param Map pointer to Map_info struct
467 */
469{
470 struct Plus_head *Plus;
471
472 check_level(Map);
473
474 Plus = &(Map->plus);
477}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
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
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
#define GV_CENTROID
#define GV_LINE
#define GV_POINT
Feature types used in memory on run time (may change)
#define GV_LINES
#define GV_BOUNDARY
#define GV_FACE
#define GV_KERNEL
void dig_node_reset_updated(struct Plus_head *)
Reset number of updated nodes.
void dig_line_reset_updated(struct Plus_head *)
Reset number of updated lines.
int plus_t
plus_t size
Definition dig_structs.h:39
#define TRUE
Definition gis.h:75
#define FALSE
Definition gis.h:79
#define _(str)
Definition glocale.h:10
plus_t Vect_get_num_faces(struct Map_info *Map)
Get number of faces in vector map.
Definition level_two.c:109
plus_t Vect_get_num_primitives(struct Map_info *Map, int type)
Get number of primitives in vector map.
Definition level_two.c:45
void Vect_reset_updated(struct Map_info *Map)
Reset list of updated lines/nodes.
Definition level_two.c:468
void Vect_set_updated(struct Map_info *Map, int enable)
Enable/disable maintenance of list of updated lines/nodes.
Definition level_two.c:453
plus_t Vect_get_num_kernels(struct Map_info *Map)
Fetch number of kernels in vector map.
Definition level_two.c:97
int Vect_get_node_line(struct Map_info *Map, int node, int line)
Get line id for node line index.
Definition level_two.c:395
int Vect_get_node_n_lines(struct Map_info *Map, int node)
Get number of lines for node.
Definition level_two.c:379
plus_t Vect_get_num_nodes(struct Map_info *Map)
Get number of nodes in vector map.
Definition level_two.c:32
plus_t Vect_get_num_lines(struct Map_info *Map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition level_two.c:73
int Vect_get_num_updated_nodes(struct Map_info *Map)
Get number of updated nodes.
Definition level_two.c:217
int Vect_get_centroid_area(struct Map_info *Map, int centroid)
Get area id the centroid is within.
Definition level_two.c:428
int Vect_get_updated_node(struct Map_info *Map, int idx)
Get updated (modified) node by index.
Definition level_two.c:239
plus_t Vect_get_num_areas(struct Map_info *Map)
Get number of areas in vector map.
Definition level_two.c:85
plus_t Vect_get_num_volumes(struct Map_info *Map)
Fetch number of volumes in vector map.
Definition level_two.c:121
int Vect_get_updated_line(struct Map_info *Map, int idx)
Get updated line by index.
Definition level_two.c:189
off_t Vect_get_updated_line_offset(struct Map_info *Map, int idx)
Get updated line offset by index.
Definition level_two.c:205
plus_t Vect_get_num_holes(struct Map_info *Map)
Fetch number of holes in vector map.
Definition level_two.c:145
int Vect_get_line_nodes(struct Map_info *Map, int line, int *n1, int *n2)
Get line nodes.
Definition level_two.c:302
int Vect_get_line_type(struct Map_info *Map, int line)
Get line type.
Definition level_two.c:252
int Vect_get_num_updated_lines(struct Map_info *Map)
Get number of updated features.
Definition level_two.c:173
int Vect_get_num_dblinks(struct Map_info *Map)
Get number of defined dblinks.
Definition level_two.c:157
float Vect_get_node_line_angle(struct Map_info *Map, int node, int line)
Angle of segment of the line connected to the node.
Definition level_two.c:411
int Vect_get_node_coor(struct Map_info *Map, int num, double *x, double *y, double *z)
Get node coordinates.
Definition level_two.c:272
int Vect_get_line_areas(struct Map_info *Map, int line, int *left, int *right)
Get area id on the left and right side of the boundary.
Definition level_two.c:345
plus_t Vect_get_num_islands(struct Map_info *Map)
Get number of islands in vector map.
Definition level_two.c:133
Vector map info.
Topological feature - node.
double x
X coordinate.
double z
Z coordinate (used only for 3D data)
double y
Y coordinate.
Boundary topology.
plus_t left
Area number to the left, negative for isle.
plus_t N1
Start node.
plus_t N2
End node.
plus_t right
Area number to the right, negative for isle.
Centroid topology.
plus_t area
Area number, negative for duplicate centroid.
Line topology.
plus_t N1
Start node.
plus_t N2
End node.
Basic topology-related info.