2
/****************************************************************
6
* PURPOSE: Editing vector map.
8
* AUTHOR(S): GRASS Development Team
9
* Wolf Bergenheim, Jachym Cepicky, Martin Landa
11
* COPYRIGHT: (C) 2006-2008 by the GRASS Development Team
13
* This program is free software under the
14
* GNU General Public License (>=v2).
15
* Read the file COPYING that comes with GRASS
19
****************************************************************/
27
\brief Add new vector features to the vector map
29
Input format is GRASS ASCII vector, the code adopted from v.in.ascii
31
\param[in] ascii file containing definition of new vector features
32
\param[in] Map vector map
33
\param[out] List list of added features (if given)
35
\return number of added features
37
int asc_to_bin(FILE * ascii, struct Map_info *Map, struct ilist *List)
45
int i, n_points, n_coors, n_cats;
53
struct line_pnts *Points;
54
struct line_cats *Cats;
58
Points = Vect_new_line_struct();
59
Cats = Vect_new_cats_struct();
62
Vect_reset_list(List);
66
/*alloc_points = 1000 ; */
68
xarray = (double *)G_calloc(alloc_points, sizeof(double));
69
yarray = (double *)G_calloc(alloc_points, sizeof(double));
70
zarray = (double *)G_calloc(alloc_points, sizeof(double));
72
while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
74
if (buff[0] == '\0') {
75
G_debug(3, "a2b: skipping blank line");
79
if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
80
n_coors < 0 || n_cats < 0) {
82
G_debug(2, "a2b: skipping commented line");
85
G_warning(_("Error reading ASCII file: '%s'"), buff);
89
G_debug(2, "a2b: Skipping commented line");
120
type = 0; /* dead -> ignore */
123
G_warning(_("Error reading ASCII file: '%s'"), buff);
126
G_debug(5, "feature type = %d", type);
133
/* Collect the points */
134
for (i = 0; i < n_coors; i++) {
135
if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0) {
136
G_warning(_("End of ASCII file reached before end of coordinates"));
139
if (buff[0] == '\0') {
140
G_debug(3, "a2b: skipping blank line while reading vertices");
146
if (sscanf(buff, "%lf%lf%lf", x, y, z) < 2) {
147
G_warning(_("Error reading ASCII file: '%s'"), buff);
150
G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
158
if (n_points >= alloc_points) {
159
alloc_points = n_points + 1000;
161
(double *)G_realloc((void *)xarray,
162
alloc_points * sizeof(double));
164
(double *)G_realloc((void *)yarray,
165
alloc_points * sizeof(double));
167
(double *)G_realloc((void *)zarray,
168
alloc_points * sizeof(double));
169
x = xarray + n_points;
170
y = yarray + n_points;
171
z = zarray + n_points;
175
/* Collect the cats */
176
for (i = 0; i < n_cats; i++) {
177
if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0) {
178
G_warning(_("End of ascii file reached before end of categories"));
181
if (buff[0] == '\0') {
183
"a2b: skipping blank line while reading category info");
188
if (sscanf(buff, "%u%u", &catn, &cat) != 2) {
189
G_warning(_("Error reading categories: '%s'"), buff);
192
Vect_cat_set(Cats, catn, cat);
195
/* Allocation is handled for line_pnts */
197
Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
198
G_fatal_error(_("Out of memory"));
201
newline = Vect_write_line(Map, type, Points, Cats);
203
Vect_list_append(List, newline);
207
Vect_reset_cats(Cats);
215
\brief Read header of input file
217
\param[in] dascii file containing definition of vector features to be added
218
\param[in] Map vector map
222
int read_head(FILE * dascii, struct Map_info *Map)
228
if (0 == G_getl2(buff, sizeof(buff) - 1, dascii))
231
/* Last line of header */
232
if (strncmp(buff, "VERTI:", 6) == 0)
235
if (!(ptr = G_index(buff, ':')))
236
G_fatal_error(_("Unexpected data in vector head: '%s'"), buff);
238
ptr++; /* Search for the start of text */
242
if (strncmp(buff, "ORGANIZATION:", 12) == 0)
243
Vect_set_organization(Map, ptr);
244
else if (strncmp(buff, "DIGIT DATE:", 11) == 0)
245
Vect_set_date(Map, ptr);
246
else if (strncmp(buff, "DIGIT NAME:", 11) == 0)
247
Vect_set_person(Map, ptr);
248
else if (strncmp(buff, "MAP NAME:", 9) == 0)
249
Vect_set_map_name(Map, ptr);
250
else if (strncmp(buff, "MAP DATE:", 9) == 0)
251
Vect_set_map_date(Map, ptr);
252
else if (strncmp(buff, "MAP SCALE:", 10) == 0)
253
Vect_set_scale(Map, atoi(ptr));
254
else if (strncmp(buff, "OTHER INFO:", 11) == 0)
255
Vect_set_comment(Map, ptr);
256
else if (strncmp(buff, "ZONE:", 5) == 0 ||
257
strncmp(buff, "UTM ZONE:", 9) == 0)
258
Vect_set_zone(Map, atoi(ptr));
259
else if (strncmp(buff, "WEST EDGE:", 10) == 0) {
261
else if (strncmp(buff, "EAST EDGE:", 10) == 0) {
263
else if (strncmp(buff, "SOUTH EDGE:", 11) == 0) {
265
else if (strncmp(buff, "NORTH EDGE:", 11) == 0) {
267
else if (strncmp(buff, "MAP THRESH:", 11) == 0)
268
Vect_set_thresh(Map, atof(ptr));
270
G_warning(_("Unknown keyword '%s' in vector head"), buff);
277
\brief Close lines (boundaries)
279
Using threshold distance (-1 for no limit)
281
\param[in] Map vector map
282
\param[in] ltype vector feature type (line | boundary)
283
\param[in] thresh threshold distance
285
\return number of modified features
287
int close_lines(struct Map_info *Map, int ltype, double thresh)
289
int nlines, line, type, nlines_modified, newline;
294
struct line_pnts *Points;
295
struct line_cats *Cats;
297
Points = Vect_new_line_struct();
298
Cats = Vect_new_cats_struct();
302
Vect_build_partial(Map, GV_BUILD_BASE);
303
nlines = Vect_get_num_lines(Map);
305
for (line = 1; line <= nlines; line++) {
306
if (!Vect_line_alive(Map, line))
309
type = Vect_read_line(Map, Points, Cats, line);
314
npoints = Points->n_points - 1;
319
dist = Vect_points_distance(x[npoints], y[npoints], z[npoints],
320
x[0], y[0], z[0], WITHOUT_Z);
322
if (dist > 0 && (thresh < 0.0 || dist <= thresh)) {
323
Vect_line_delete_point(Points, npoints);
324
Vect_append_point(Points, x[0], y[0], z[0]);
326
newline = Vect_rewrite_line(Map, line, type, Points, Cats);
328
G_warning(_("Unable to rewrite line %d"), line);
335
Vect_destroy_line_struct(Points);
336
Vect_destroy_cats_struct(Cats);
338
return nlines_modified;