2
\file lib/vector/Vlib/build_nat.c
4
4
\brief Vector library - Building topology for native format
6
(C) 2001-2008 by the GRASS Development Team
6
(C) 2001-2013 by the GRASS Development Team
8
This program is free software under the
9
GNU General Public License (>=v2).
10
Read the file COPYING that comes with GRASS
8
This program is free software under the GNU General Public License
9
(>=v2). Read the file COPYING that comes with GRASS for details.
13
11
\author Original author CERL, probably Dave Gerdes or Mike Higgins.
14
Update to GRASS 5.7 Radim Blazek and David D. Gray.
12
\author Update to GRASS 5.7 Radim Blazek and David D. Gray.
19
15
#include <string.h>
20
16
#include <stdlib.h>
18
#include <sys/types.h>
22
19
#include <grass/glocale.h>
23
#include <grass/gis.h>
24
#include <grass/Vect.h>
27
\brief Build area on given side of line (GV_LEFT or GV_RIGHT)
29
\param Map_info vector map
31
\param side side (GV_LEFT or GV_RIGHT)
33
\return > 0 number of area
34
\return < 0 number of isle
35
\return 0 not created (may also already exist)
37
int Vect_build_line_area(struct Map_info *Map, int iline, int side)
39
int j, area, isle, n_lines, line, type, direction;
42
struct Plus_head *plus;
44
static struct line_pnts *Points, *APoints;
50
G_debug(3, "Vect_build_line_area() line = %d, side = %d", iline, side);
53
Points = Vect_new_line_struct();
54
APoints = Vect_new_line_struct();
58
area = dig_line_get_area(plus, iline, side);
60
G_debug(3, " area/isle = %d -> skip", area);
64
n_lines = dig_build_area_with_line(plus, iline, side, &lines);
65
G_debug(3, " n_lines = %d", n_lines);
68
} /* area was not built */
70
/* Area or island ? */
71
Vect_reset_line(APoints);
72
for (j = 0; j < n_lines; j++) {
74
BLine = plus->Line[line];
75
offset = BLine->offset;
76
G_debug(3, " line[%d] = %d, offset = %ld", j, line, offset);
77
type = Vect_read_line(Map, Points, NULL, line);
79
direction = GV_FORWARD;
81
direction = GV_BACKWARD;
82
Vect_append_points(APoints, Points, direction);
85
dig_find_area_poly(APoints, &area_size);
86
G_debug(3, " area/isle size = %f", area_size);
88
if (area_size > 0) { /* area */
89
/* add area structure to plus */
90
area = dig_add_area(plus, n_lines, lines);
91
if (area == -1) { /* error */
93
G_fatal_error(_("Unable to add area (map closed, topo saved)"));
95
G_debug(3, " -> area %d", area);
98
else if (area_size < 0) { /* island */
99
isle = dig_add_isle(plus, n_lines, lines);
100
if (isle == -1) { /* error */
102
G_fatal_error(_("Unable to add isle (map closed, topo saved)"));
104
G_debug(3, " -> isle %d", isle);
108
/* TODO: What to do with such areas? Should be areas/isles of size 0 stored,
109
* so that may be found and cleaned by some utility
110
* Note: it would be useful for vertical closed polygons, but such would be added twice
112
G_warning(_("Area of size = 0.0 ignored"));
118
\brief Find area outside island
120
\param Map_info vector map
123
\return number of area(s)
124
\return 0 if not found
126
int Vect_isle_find_area(struct Map_info *Map, int isle)
128
int j, line, node, sel_area, first, area, poly;
129
static int first_call = 1;
130
struct Plus_head *plus;
135
double size, cur_size;
137
static struct ilist *List;
138
static struct line_pnts *APoints;
140
/* Note: We should check all isle points (at least) because if topology is not clean
141
* and two areas overlap, isle which is not completely within area may be attached,
142
* but it would take long time */
144
G_debug(3, "Vect_isle_find_area () island = %d", isle);
147
if (plus->Isle[isle] == NULL) {
148
G_warning(_("Request to find area outside nonexistent isle"));
153
List = Vect_new_list();
154
APoints = Vect_new_line_struct();
158
Isle = plus->Isle[isle];
159
line = abs(Isle->lines[0]);
160
Line = plus->Line[line];
162
Node = plus->Node[node];
164
/* select areas by box */
169
box.T = PORT_DOUBLE_MAX;
170
box.B = -PORT_DOUBLE_MAX;
171
Vect_select_areas_by_box(Map, &box, List);
172
G_debug(3, "%d areas overlap island boundary point", List->n_values);
177
Vect_get_isle_box(Map, isle, &box);
178
for (j = 0; j < List->n_values; j++) {
179
area = List->value[j];
180
G_debug(3, "area = %d", area);
182
Area = plus->Area[area];
184
/* Before other tests, simply exclude those areas inside isolated isles formed by one boundary */
185
if (abs(Isle->lines[0]) == abs(Area->lines[0])) {
186
G_debug(3, " area inside isolated isle");
191
/* Note: If build is run on large files of areas imported from nontopo format (shapefile)
192
* attaching of isles takes very long time because each area is also isle and select by
193
* box all overlapping areas selects all areas with box overlapping first node.
194
* Then reading coordinates for all those areas would take a long time -> check first
195
* if isle's box is completely within area box */
196
Vect_get_area_box(Map, area, &abox);
197
if (box.E > abox.E || box.W < abox.W || box.N > abox.N ||
199
G_debug(3, " isle not completely inside area box");
203
poly = Vect_point_in_area_outer_ring(Node->x, Node->y, Map, area);
204
G_debug(3, " poly = %d", poly);
206
if (poly == 1) { /* pint in area, but node is not part of area inside isle (would be poly == 2) */
207
/* In rare case island is inside more areas in that case we have to calculate area
208
* of outer ring and take the smaller */
209
if (sel_area == 0) { /* first */
212
else { /* is not first */
213
if (cur_size < 0) { /* second area */
214
/* This is slow, but should not be called often */
215
Vect_get_area_points(Map, sel_area, APoints);
216
G_begin_polygon_area_calculations();
218
G_area_of_polygon(APoints->x, APoints->y,
220
G_debug(3, " first area size = %f (n points = %d)",
221
cur_size, APoints->n_points);
225
Vect_get_area_points(Map, area, APoints);
227
G_area_of_polygon(APoints->x, APoints->y,
229
G_debug(3, " area size = %f (n points = %d)", cur_size,
232
if (size < cur_size) {
237
G_debug(3, "sel_area = %d cur_size = %f", sel_area, cur_size);
241
G_debug(3, "Island %d in area %d", isle, sel_area);
244
G_debug(3, "Island %d is not in area", isle);
251
\brief (Re)Attach isle to area
253
\param Map_info vector map
258
int Vect_attach_isle(struct Map_info *Map, int isle)
262
struct Plus_head *plus;
264
/* Note!: If topology is not clean and areas overlap, one island may fall to more areas
265
* (partially or fully). Before isle is attached to area it must be check if it is not attached yet */
266
G_debug(3, "Vect_attach_isle (): isle = %d", isle);
270
sel_area = Vect_isle_find_area(Map, isle);
271
G_debug(3, " isle = %d -> area outside = %d", isle, sel_area);
273
Isle = plus->Isle[isle];
274
if (Isle->area > 0) {
276
"Attempt to attach isle %d to more areas (=>topology is not clean)",
280
Isle->area = sel_area;
281
dig_area_add_isle(plus, sel_area, isle);
288
\brief (Re)Attach isles to areas in given box
290
\param Map_info vector map
291
\param box bounding box
295
int Vect_attach_isles(struct Map_info *Map, BOUND_BOX * box)
298
static int first = 1;
299
static struct ilist *List;
300
struct Plus_head *plus;
302
G_debug(3, "Vect_attach_isles ()");
307
List = Vect_new_list();
311
Vect_select_isles_by_box(Map, box, List);
312
G_debug(3, " number of isles to attach = %d", List->n_values);
314
for (i = 0; i < List->n_values; i++) {
315
isle = List->value[i];
316
Vect_attach_isle(Map, isle);
322
\brief (Re)Attach centroids to areas in given box
324
\param Map_info vector map
325
\param box bounding box
329
int Vect_attach_centroids(struct Map_info *Map, BOUND_BOX * box)
331
int i, sel_area, centr;
332
static int first = 1;
333
static struct ilist *List;
336
struct Plus_head *plus;
338
G_debug(3, "Vect_attach_centroids ()");
343
List = Vect_new_list();
347
/* Warning: If map is updated on level2, it may happen that previously correct island
348
* becomes incorrect. In that case, centroid of area forming the island is reattached
349
* to outer area, because island polygon is not excluded.
351
* +-----------+ +-----------+
353
* | +---+---+ | | +---+---+ |
354
* | | 2 | 3 | | | | 2 | |
355
* | | x | | | -> | | x | |
357
* | +---+---+ | | +---+---+ |
359
* +-----------+ +-----------+
360
* centroid is centroid is
361
* attached to 2 reattached to 1
363
* Because of this, when the centroid is reattached to another area, it is always necessary
364
* to check if original area exist, unregister centroid from previous area.
365
* To simplify code, this is implemented so that centroid is always firs unregistered
366
* and if new area is found, it is registered again.
368
* This problem can be avoided altogether if properly attached centroids
373
Vect_select_lines_by_box(Map, box, GV_CENTROID, List);
374
G_debug(3, " number of centroids to reattach = %d", List->n_values);
375
for (i = 0; i < List->n_values; i++) {
378
centr = List->value[i];
379
Line = plus->Line[centr];
381
/* only attach unregistered and duplicate centroids because
382
* 1) all properly attached centroids are properly attached, really! Don't touch.
383
* 2) Vect_find_area() below does not always return the correct area
389
orig_area = Line->left;
391
sel_area = Vect_find_area(Map, Line->E, Line->N);
392
G_debug(3, " centroid %d is in area %d", centr, sel_area);
394
Area = plus->Area[sel_area];
395
if (Area->centroid == 0) { /* first centroid */
396
G_debug(3, " first centroid -> attach to area");
397
Area->centroid = centr;
398
Line->left = sel_area;
400
if (sel_area != orig_area && plus->do_uplist)
401
dig_line_add_updated(plus, centr);
403
else if (Area->centroid != centr) { /* duplicate centroid */
404
/* Note: it cannot happen that Area->centroid == centr, because the centroid
405
* was not registered or a duplicate */
406
G_debug(3, " duplicate centroid -> do not attach to area");
407
Line->left = -sel_area;
409
if (-sel_area != orig_area && plus->do_uplist)
410
dig_line_add_updated(plus, centr);
414
if (sel_area != orig_area && plus->do_uplist)
415
dig_line_add_updated(plus, centr);
20
#include <grass/vector.h>
22
static struct line_pnts *Points;
422
25
\brief Build topology
424
\param Map_info vector map
425
28
\param build build level
427
30
\return 1 on success
449
49
return 1; /* Do nothing */
451
51
/* Check if upgrade or downgrade */
452
if (build < plus->built) { /* lower level request */
454
/* release old sources (this also initializes structures and numbers of elements) */
455
if (plus->built >= GV_BUILD_CENTROIDS && build < GV_BUILD_CENTROIDS) {
456
/* reset info about areas stored for centroids */
457
int nlines = Vect_get_num_lines(Map);
459
for (line = 1; line <= nlines; line++) {
460
Line = plus->Line[line];
461
if (Line && Line->type == GV_CENTROID)
464
dig_free_plus_areas(plus);
465
dig_spidx_free_areas(plus);
466
dig_free_plus_isles(plus);
467
dig_spidx_free_isles(plus);
471
if (plus->built >= GV_BUILD_AREAS && build < GV_BUILD_AREAS) {
472
/* reset info about areas stored for lines */
473
int nlines = Vect_get_num_lines(Map);
475
for (line = 1; line <= nlines; line++) {
476
Line = plus->Line[line];
477
if (Line && Line->type == GV_BOUNDARY) {
482
dig_free_plus_areas(plus);
483
dig_spidx_free_areas(plus);
484
dig_free_plus_isles(plus);
485
dig_spidx_free_isles(plus);
487
if (plus->built >= GV_BUILD_BASE && build < GV_BUILD_BASE) {
488
dig_free_plus_nodes(plus);
489
dig_spidx_free_nodes(plus);
490
dig_free_plus_lines(plus);
491
dig_spidx_free_lines(plus);
52
if (build < plus->built) {
54
Vect__build_downgrade(Map, build);
498
Points = Vect_new_line_struct();
499
APoints = Vect_new_line_struct();
60
Points = Vect_new_line_struct();
500
61
Cats = Vect_new_cats_struct();
501
List = Vect_new_list();
503
63
if (plus->built < GV_BUILD_BASE) {
506
format = G_info_format();
509
* We shall go through all primitives in coor file and
510
* add new node for each end point to nodes structure
511
* if the node with the same coordinates doesn't exist yet.
67
* We shall go through all primitives in coor file and add
68
* new node for each end point to nodes structure if the node
69
* with the same coordinates doesn't exist yet.
514
72
/* register lines, create nodes */
516
74
G_message(_("Registering primitives..."));
520
78
/* register line */
521
79
type = Vect_read_next_line(Map, Points, Cats);
523
/* Note: check for dead lines is not needed, because they are skipped by V1_read_next_line_nat() */
81
/* Note: check for dead lines is not needed, because they
82
are skipped by V1_read_next_line() */
525
84
G_warning(_("Unable to read vector map"));