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