~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to vector/v.in.ascii/a2b.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <string.h>
3
 
#include <grass/gis.h>
4
 
#include <grass/Vect.h>
5
 
#include <grass/glocale.h>
6
 
 
7
 
#define BUFFSIZE 128
8
 
 
9
 
int asc_to_bin(FILE * ascii, struct Map_info *Map)
10
 
{
11
 
    char ctype;
12
 
    char buff[BUFFSIZE];
13
 
    char east_str[256], north_str[256];
14
 
    double *xarray;
15
 
    double *yarray;
16
 
    double *zarray;
17
 
    double *x, *y, *z;
18
 
    int i, n_points, n_coors, n_cats;
19
 
    int type;
20
 
    int alloc_points;
21
 
    int end_of_file;
22
 
    struct line_pnts *Points;
23
 
    struct line_cats *Cats;
24
 
    int catn;
25
 
    int cat;
26
 
 
27
 
    /* Must always use this to create an initialized  line_pnts structure */
28
 
    Points = Vect_new_line_struct();
29
 
    Cats = Vect_new_cats_struct();
30
 
 
31
 
    end_of_file = 0;
32
 
    /*alloc_points     = 1000 ; */
33
 
    alloc_points = 1;
34
 
    xarray = (double *)G_calloc(alloc_points, sizeof(double));
35
 
    yarray = (double *)G_calloc(alloc_points, sizeof(double));
36
 
    zarray = (double *)G_calloc(alloc_points, sizeof(double));
37
 
 
38
 
 
39
 
    while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
40
 
        n_cats = 0;
41
 
        if (buff[0] == '\0') {
42
 
            G_debug(3, "a2b: skipping blank line");
43
 
            continue;
44
 
        }
45
 
 
46
 
        if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
47
 
            n_coors < 0 || n_cats < 0) {
48
 
            if (ctype == '#') {
49
 
                G_debug(2, "a2b: skipping commented line");
50
 
                continue;
51
 
            }
52
 
            G_fatal_error(_("Error reading ASCII file: (bad type) [%s]"),
53
 
                          buff);
54
 
        }
55
 
        if (ctype == '#') {
56
 
            G_debug(2, "a2b: Skipping commented line");
57
 
            continue;
58
 
        }
59
 
 
60
 
        switch (ctype) {
61
 
        case 'A':
62
 
            type = GV_BOUNDARY;
63
 
            break;
64
 
        case 'B':
65
 
            type = GV_BOUNDARY;
66
 
            break;
67
 
        case 'C':
68
 
            type = GV_CENTROID;
69
 
            break;
70
 
        case 'L':
71
 
            type = GV_LINE;
72
 
            break;
73
 
        case 'P':
74
 
            type = GV_POINT;
75
 
            break;
76
 
        case 'F':
77
 
            type = GV_FACE;
78
 
            break;
79
 
        case 'K':
80
 
            type = GV_KERNEL;
81
 
            break;
82
 
        case 'a':
83
 
        case 'b':
84
 
        case 'c':
85
 
        case 'l':
86
 
        case 'p':
87
 
            type = 0;           /* dead -> ignore */
88
 
            break;
89
 
        default:
90
 
            G_fatal_error(_("Error reading ASCII file: (unknown type) [%s]"),
91
 
                          buff);
92
 
        }
93
 
        G_debug(5, "feature type = %d", type);
94
 
 
95
 
        n_points = 0;
96
 
        x = xarray;
97
 
        y = yarray;
98
 
        z = zarray;
99
 
 
100
 
        /* Collect the points */
101
 
        for (i = 0; i < n_coors; i++) {
102
 
            if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
103
 
                G_fatal_error(_("End of ASCII file reached before end of coordinates"));
104
 
 
105
 
            if (buff[0] == '\0') {
106
 
                G_debug(3, "a2b: skipping blank line while reading vertices");
107
 
                i--;
108
 
                continue;
109
 
            }
110
 
 
111
 
            *z = 0;
112
 
            if (sscanf(buff, "%lf%lf%lf", x, y, z) < 2) {
113
 
                if (sscanf(buff, " %s %s %lf", east_str, north_str, z) < 2) {
114
 
                    G_fatal_error(_("Error reading ASCII file: (bad point) [%s]"),
115
 
                                  buff);
116
 
                } else {
117
 
                    if( ! G_scan_easting(east_str, x, G_projection()) )
118
 
                        G_fatal_error(_("Unparsable longitude value: [%s]"),
119
 
                                      east_str);
120
 
                    if( ! G_scan_northing(north_str, y, G_projection()) )
121
 
                        G_fatal_error(_("Unparsable latitude value: [%s]"),
122
 
                                      north_str);
123
 
                }
124
 
            }
125
 
 
126
 
            G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
127
 
                    *x, *y, *z);
128
 
 
129
 
            n_points++;
130
 
            x++;
131
 
            y++;
132
 
            z++;
133
 
 
134
 
            if (n_points >= alloc_points) {
135
 
                alloc_points = n_points + 1000;
136
 
                xarray =
137
 
                    (double *)G_realloc((void *)xarray,
138
 
                                        alloc_points * sizeof(double));
139
 
                yarray =
140
 
                    (double *)G_realloc((void *)yarray,
141
 
                                        alloc_points * sizeof(double));
142
 
                zarray =
143
 
                    (double *)G_realloc((void *)zarray,
144
 
                                        alloc_points * sizeof(double));
145
 
                x = xarray + n_points;
146
 
                y = yarray + n_points;
147
 
                z = zarray + n_points;
148
 
            }
149
 
        }
150
 
 
151
 
        /* Collect the cats */
152
 
        for (i = 0; i < n_cats; i++) {
153
 
            if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
154
 
                G_fatal_error(_("End of ASCII file reached before end of categories"));
155
 
 
156
 
            if (buff[0] == '\0') {
157
 
                G_debug(3,
158
 
                        "a2b: skipping blank line while reading category info");
159
 
                i--;
160
 
                continue;
161
 
            }
162
 
 
163
 
            if (sscanf(buff, "%u%u", &catn, &cat) != 2)
164
 
                G_fatal_error(_("Error reading categories: [%s]"), buff);
165
 
 
166
 
            Vect_cat_set(Cats, catn, cat);
167
 
        }
168
 
 
169
 
        /* Allocation is handled for line_pnts */
170
 
        if (0 >
171
 
            Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
172
 
            G_fatal_error(_("Out of memory"));
173
 
 
174
 
        if (type > 0)
175
 
            Vect_write_line(Map, type, Points, Cats);
176
 
 
177
 
        Vect_reset_cats(Cats);
178
 
    }
179
 
    return 0;
180
 
}