~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to vector/v.edit/a2b.c

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/****************************************************************
3
 
 *
4
 
 * MODULE:     v.edit
5
 
 *
6
 
 * PURPOSE:    Editing vector map.
7
 
 *
8
 
 * AUTHOR(S):  GRASS Development Team
9
 
 *             Wolf Bergenheim, Jachym Cepicky, Martin Landa
10
 
 *
11
 
 * COPYRIGHT:  (C) 2006-2008 by the GRASS Development Team
12
 
 *
13
 
 *             This program is free software under the
14
 
 *             GNU General Public License (>=v2).
15
 
 *             Read the file COPYING that comes with GRASS
16
 
 *             for details.
17
 
 *
18
 
 * TODO:       3D support
19
 
 ****************************************************************/
20
 
 
21
 
#include <string.h>
22
 
#include "global.h"
23
 
 
24
 
#define BUFFSIZE 128
25
 
 
26
 
/**
27
 
   \brief Add new vector features to the vector map
28
 
   
29
 
   Input format is GRASS ASCII vector, the code adopted from v.in.ascii
30
 
 
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)
34
 
 
35
 
   \return number of added features
36
 
*/
37
 
int asc_to_bin(FILE * ascii, struct Map_info *Map, struct ilist *List)
38
 
{
39
 
    char ctype;
40
 
    char buff[BUFFSIZE];
41
 
    double *xarray;
42
 
    double *yarray;
43
 
    double *zarray;
44
 
    double *x, *y, *z;
45
 
    int i, n_points, n_coors, n_cats;
46
 
    int type, newline;
47
 
    int alloc_points;
48
 
    int end_of_file;
49
 
    int catn;
50
 
    int cat;
51
 
    int nlines;
52
 
 
53
 
    struct line_pnts *Points;
54
 
    struct line_cats *Cats;
55
 
 
56
 
    nlines = 0;
57
 
 
58
 
    Points = Vect_new_line_struct();
59
 
    Cats = Vect_new_cats_struct();
60
 
 
61
 
    if (List) {
62
 
        Vect_reset_list(List);
63
 
    }
64
 
 
65
 
    end_of_file = 0;
66
 
    /*alloc_points     = 1000 ; */
67
 
    alloc_points = 1;
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));
71
 
 
72
 
    while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
73
 
        n_cats = 0;
74
 
        if (buff[0] == '\0') {
75
 
            G_debug(3, "a2b: skipping blank line");
76
 
            continue;
77
 
        }
78
 
 
79
 
        if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
80
 
            n_coors < 0 || n_cats < 0) {
81
 
            if (ctype == '#') {
82
 
                G_debug(2, "a2b: skipping commented line");
83
 
                continue;
84
 
            }
85
 
            G_warning(_("Error reading ASCII file: '%s'"), buff);
86
 
            return -1;
87
 
        }
88
 
        if (ctype == '#') {
89
 
            G_debug(2, "a2b: Skipping commented line");
90
 
            continue;
91
 
        }
92
 
 
93
 
        switch (ctype) {
94
 
        case 'A':
95
 
            type = GV_BOUNDARY;
96
 
            break;
97
 
        case 'B':
98
 
            type = GV_BOUNDARY;
99
 
            break;
100
 
        case 'C':
101
 
            type = GV_CENTROID;
102
 
            break;
103
 
        case 'L':
104
 
            type = GV_LINE;
105
 
            break;
106
 
        case 'P':
107
 
            type = GV_POINT;
108
 
            break;
109
 
        case 'F':
110
 
            type = GV_FACE;
111
 
            break;
112
 
        case 'K':
113
 
            type = GV_KERNEL;
114
 
            break;
115
 
        case 'a':
116
 
        case 'b':
117
 
        case 'c':
118
 
        case 'l':
119
 
        case 'p':
120
 
            type = 0;           /* dead -> ignore */
121
 
            break;
122
 
        default:
123
 
            G_warning(_("Error reading ASCII file: '%s'"), buff);
124
 
            return -1;
125
 
        }
126
 
        G_debug(5, "feature type = %d", type);
127
 
 
128
 
        n_points = 0;
129
 
        x = xarray;
130
 
        y = yarray;
131
 
        z = zarray;
132
 
 
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"));
137
 
                return -1;
138
 
            }
139
 
            if (buff[0] == '\0') {
140
 
                G_debug(3, "a2b: skipping blank line while reading vertices");
141
 
                i--;
142
 
                continue;
143
 
            }
144
 
 
145
 
            *z = 0;
146
 
            if (sscanf(buff, "%lf%lf%lf", x, y, z) < 2) {
147
 
                G_warning(_("Error reading ASCII file: '%s'"), buff);
148
 
                return -1;
149
 
            }
150
 
            G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
151
 
                    *x, *y, *z);
152
 
 
153
 
            n_points++;
154
 
            x++;
155
 
            y++;
156
 
            z++;
157
 
 
158
 
            if (n_points >= alloc_points) {
159
 
                alloc_points = n_points + 1000;
160
 
                xarray =
161
 
                    (double *)G_realloc((void *)xarray,
162
 
                                        alloc_points * sizeof(double));
163
 
                yarray =
164
 
                    (double *)G_realloc((void *)yarray,
165
 
                                        alloc_points * sizeof(double));
166
 
                zarray =
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;
172
 
            }
173
 
        }
174
 
 
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"));
179
 
                return -1;
180
 
            }
181
 
            if (buff[0] == '\0') {
182
 
                G_debug(3,
183
 
                        "a2b: skipping blank line while reading category info");
184
 
                i--;
185
 
                continue;
186
 
            }
187
 
 
188
 
            if (sscanf(buff, "%u%u", &catn, &cat) != 2) {
189
 
                G_warning(_("Error reading categories: '%s'"), buff);
190
 
                return -1;
191
 
            }
192
 
            Vect_cat_set(Cats, catn, cat);
193
 
        }
194
 
 
195
 
        /* Allocation is handled for line_pnts */
196
 
        if (0 >
197
 
            Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
198
 
            G_fatal_error(_("Out of memory"));
199
 
 
200
 
        if (type > 0) {
201
 
            newline = Vect_write_line(Map, type, Points, Cats);
202
 
            if (List) {
203
 
                Vect_list_append(List, newline);
204
 
            }
205
 
            nlines++;
206
 
 
207
 
            Vect_reset_cats(Cats);
208
 
        }
209
 
    }
210
 
 
211
 
    return nlines;
212
 
}
213
 
 
214
 
/**
215
 
   \brief Read header of input file
216
 
   
217
 
   \param[in] dascii file containing definition of vector features to be added
218
 
   \param[in] Map vector map
219
 
   
220
 
   \return 0
221
 
*/
222
 
int read_head(FILE * dascii, struct Map_info *Map)
223
 
{
224
 
    char buff[1024];
225
 
    char *ptr;
226
 
 
227
 
    for (;;) {
228
 
        if (0 == G_getl2(buff, sizeof(buff) - 1, dascii))
229
 
            return 0;
230
 
 
231
 
        /* Last line of header */
232
 
        if (strncmp(buff, "VERTI:", 6) == 0)
233
 
            return 0;
234
 
 
235
 
        if (!(ptr = G_index(buff, ':')))
236
 
            G_fatal_error(_("Unexpected data in vector head: '%s'"), buff);
237
 
 
238
 
        ptr++;                  /* Search for the start of text */
239
 
        while (*ptr == ' ')
240
 
            ptr++;
241
 
 
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) {
260
 
        }
261
 
        else if (strncmp(buff, "EAST EDGE:", 10) == 0) {
262
 
        }
263
 
        else if (strncmp(buff, "SOUTH EDGE:", 11) == 0) {
264
 
        }
265
 
        else if (strncmp(buff, "NORTH EDGE:", 11) == 0) {
266
 
        }
267
 
        else if (strncmp(buff, "MAP THRESH:", 11) == 0)
268
 
            Vect_set_thresh(Map, atof(ptr));
269
 
        else {
270
 
            G_warning(_("Unknown keyword '%s' in vector head"), buff);
271
 
        }
272
 
    }
273
 
    /* NOTREACHED */
274
 
}
275
 
 
276
 
/**
277
 
   \brief Close lines (boundaries)
278
 
   
279
 
   Using threshold distance (-1 for no limit)
280
 
   
281
 
   \param[in] Map vector map
282
 
   \param[in] ltype vector feature type (line | boundary)
283
 
   \param[in] thresh threshold distance
284
 
 
285
 
   \return number of modified features
286
 
*/
287
 
int close_lines(struct Map_info *Map, int ltype, double thresh)
288
 
{
289
 
    int nlines, line, type, nlines_modified, newline;
290
 
    int npoints;
291
 
    double *x, *y, *z;
292
 
    double dist;
293
 
 
294
 
    struct line_pnts *Points;
295
 
    struct line_cats *Cats;
296
 
 
297
 
    Points = Vect_new_line_struct();
298
 
    Cats = Vect_new_cats_struct();
299
 
 
300
 
    nlines_modified = 0;
301
 
 
302
 
    Vect_build_partial(Map, GV_BUILD_BASE);
303
 
    nlines = Vect_get_num_lines(Map);
304
 
 
305
 
    for (line = 1; line <= nlines; line++) {
306
 
        if (!Vect_line_alive(Map, line))
307
 
            continue;
308
 
 
309
 
        type = Vect_read_line(Map, Points, Cats, line);
310
 
 
311
 
        if (!(type & ltype))
312
 
            continue;
313
 
 
314
 
        npoints = Points->n_points - 1;
315
 
        x = Points->x;
316
 
        y = Points->y;
317
 
        z = Points->z;
318
 
 
319
 
        dist = Vect_points_distance(x[npoints], y[npoints], z[npoints],
320
 
                                    x[0], y[0], z[0], WITHOUT_Z);
321
 
 
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]);
325
 
 
326
 
            newline = Vect_rewrite_line(Map, line, type, Points, Cats);
327
 
            if (newline < 0) {
328
 
                G_warning(_("Unable to rewrite line %d"), line);
329
 
                return -1;
330
 
            }
331
 
            nlines_modified++;
332
 
        }
333
 
    }
334
 
 
335
 
    Vect_destroy_line_struct(Points);
336
 
    Vect_destroy_cats_struct(Cats);
337
 
 
338
 
    return nlines_modified;
339
 
}