4
#include <grass/Vect.h>
5
#include <grass/glocale.h>
9
int asc_to_bin(FILE * ascii, struct Map_info *Map)
13
char east_str[256], north_str[256];
18
int i, n_points, n_coors, n_cats;
22
struct line_pnts *Points;
23
struct line_cats *Cats;
27
/* Must always use this to create an initialized line_pnts structure */
28
Points = Vect_new_line_struct();
29
Cats = Vect_new_cats_struct();
32
/*alloc_points = 1000 ; */
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));
39
while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
41
if (buff[0] == '\0') {
42
G_debug(3, "a2b: skipping blank line");
46
if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
47
n_coors < 0 || n_cats < 0) {
49
G_debug(2, "a2b: skipping commented line");
52
G_fatal_error(_("Error reading ASCII file: (bad type) [%s]"),
56
G_debug(2, "a2b: Skipping commented line");
87
type = 0; /* dead -> ignore */
90
G_fatal_error(_("Error reading ASCII file: (unknown type) [%s]"),
93
G_debug(5, "feature type = %d", type);
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"));
105
if (buff[0] == '\0') {
106
G_debug(3, "a2b: skipping blank line while reading vertices");
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]"),
117
if( ! G_scan_easting(east_str, x, G_projection()) )
118
G_fatal_error(_("Unparsable longitude value: [%s]"),
120
if( ! G_scan_northing(north_str, y, G_projection()) )
121
G_fatal_error(_("Unparsable latitude value: [%s]"),
126
G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
134
if (n_points >= alloc_points) {
135
alloc_points = n_points + 1000;
137
(double *)G_realloc((void *)xarray,
138
alloc_points * sizeof(double));
140
(double *)G_realloc((void *)yarray,
141
alloc_points * sizeof(double));
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;
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"));
156
if (buff[0] == '\0') {
158
"a2b: skipping blank line while reading category info");
163
if (sscanf(buff, "%u%u", &catn, &cat) != 2)
164
G_fatal_error(_("Error reading categories: [%s]"), buff);
166
Vect_cat_set(Cats, catn, cat);
169
/* Allocation is handled for line_pnts */
171
Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
172
G_fatal_error(_("Out of memory"));
175
Vect_write_line(Map, type, Points, Cats);
177
Vect_reset_cats(Cats);