-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
main.c
416 lines (347 loc) · 10.9 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/***************************************************************
*
* MODULE: v.segment
*
* AUTHOR(S): Radim Blazek
* Hamish Bowman (offset bits)
* OGR support by Martin Landa <landa.martin gmail.com>
* Reversed & percent offsets by Huidae Cho <grass4u gmail.com>
*
* PURPOSE: Generate segments or points from input map and segments read from stdin
*
* COPYRIGHT: (C) 2002-2014 by the GRASS Development Team
*
* This program is free software under the GNU General
* Public License (>=v2). Read the file COPYING that
* comes with GRASS for details.
*
**************************************************************/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <grass/gis.h>
#include <grass/vector.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
int read_point_input(char *buf, char *stype, int *id, int *lcat, double *offset,
double *side_offset, int *rev, int *pct);
int read_line_input(char *buf, char *stype, int *id, int *lcat,
double *offset1, double *offset2, double *side_offset,
int *rev1, int *pct1, int *rev2, int *pct2);
int find_line(struct Map_info *Map, int lfield, int cat);
void offset_pt_90(double *, double *, double, double);
int main(int argc, char **argv)
{
FILE *in_file;
int ret, points_written, lines_written, points_read, lines_read;
int lfield;
int line;
int id, lcat;
double offset1, offset2, side_offset;
double x, y, z, angle, len;
char stype;
struct Option *in_opt, *out_opt;
struct Option *lfield_opt, *file_opt;
struct GModule *module;
char buf[2000];
struct Map_info In, Out;
struct line_cats *LCats, *SCats;
struct line_pnts *LPoints, *SPoints, *PlPoints;
char *tmpstr1;
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("vector"));
G_add_keyword(_("geometry"));
G_add_keyword(_("node"));
G_add_keyword(_("point"));
G_add_keyword(_("segment"));
G_add_keyword(_("vertex"));
module->description =
_("Creates points/segments from input vector lines and positions.");
in_opt = G_define_standard_option(G_OPT_V_INPUT);
in_opt->label = _("Name of input vector lines map");
lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
file_opt = G_define_standard_option(G_OPT_F_INPUT);
file_opt->key = "rules";
file_opt->required = NO;
file_opt->label = _("Name of file containing segment rules");
file_opt->description = _("'-' for standard input");
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
LCats = Vect_new_cats_struct();
SCats = Vect_new_cats_struct();
LPoints = Vect_new_line_struct();
SPoints = Vect_new_line_struct();
PlPoints = Vect_new_line_struct();
Vect_check_input_output_name(in_opt->answer, out_opt->answer,
G_FATAL_EXIT);
if (file_opt->answer && strcmp(file_opt->answer, "-") != 0) {
/* open input file */
if ((in_file = fopen(file_opt->answer, "r")) == NULL)
G_fatal_error(_("Unable to open input file <%s>"),
file_opt->answer);
}
else
in_file = stdin;
/* Open input lines */
Vect_set_open_level(2);
if (Vect_open_old2(&In, in_opt->answer, "", lfield_opt->answer) < 0)
G_fatal_error(_("Unable to open vector map <%s>"), in_opt->answer);
lfield = Vect_get_field_number(&In, lfield_opt->answer);
/* Open output segments */
if (Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In)) < 0)
G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
Vect_hist_copy(&In, &Out);
Vect_hist_command(&Out);
points_read = 0;
lines_read = 0;
points_written = 0;
lines_written = 0;
while (1) {
int rev1, rev2, pct1, pct2;
if (!file_opt->answer) {
if (fgets(buf, sizeof(buf), stdin) == NULL)
break;
}
else {
if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
break;
}
G_debug(2, "SEGMENT: %s", G_chop(buf));
side_offset = 0;
Vect_reset_line(SPoints);
Vect_reset_cats(SCats);
Vect_reset_line(PlPoints);
switch (buf[0]) {
case 'P':
if (!read_point_input(buf, &stype, &id, &lcat, &offset1,
&side_offset, &rev1, &pct1)) {
G_warning(_("Unable to read input: %s"), buf);
break;
}
points_read++;
G_debug(2, "point: %d %d %s%f%s %f", id, lcat, rev1 ? "-" : "",
offset1, pct1 ? "%" : "", side_offset);
/* OK, write point */
line = find_line(&In, lfield, lcat);
if (line == 0) {
G_warning(_("Unable to find line of cat %d"), lcat);
break;
}
Vect_read_line(&In, LPoints, LCats, line);
len = Vect_line_length(LPoints);
if (pct1)
offset1 = len * offset1 / 100.0;
if (rev1)
offset1 = len - offset1;
ret = Vect_point_on_line(LPoints, offset1, &x, &y, &z, &angle,
NULL);
if (ret == 0) {
G_warning(_("Unable to get point on line: cat = %d offset = %f "
"(line length = %.15g)\n%s"), lcat, offset1, len,
buf);
break;
}
if (fabs(side_offset) > 0.0)
offset_pt_90(&x, &y, angle, side_offset);
Vect_append_point(SPoints, x, y, z);
Vect_cat_set(SCats, 1, id);
Vect_write_line(&Out, GV_POINT, SPoints, SCats);
points_written++;
break;
case 'L':
if (!read_line_input(buf, &stype, &id, &lcat, &offset1, &offset2,
&side_offset, &rev1, &pct1, &rev2, &pct2)) {
G_warning(_("Unable to read input: %s"), buf);
break;
}
lines_read++;
G_debug(2, "line: %d %d %s%f%s %s%f%s %f", id, lcat,
rev1 ? "-" : "", offset1, pct1 ? "%" : "",
rev2 ? "-" : "", offset2, pct2 ? "%" : "",
side_offset);
line = find_line(&In, lfield, lcat);
if (line == 0) {
G_warning(_("Unable to find line of cat %d"), lcat);
break;
}
Vect_read_line(&In, LPoints, LCats, line);
len = Vect_line_length(LPoints);
if (pct1)
offset1 = len * offset1 / 100.0;
if (rev1)
offset1 = len - offset1;
if (pct2)
offset2 = len * offset2 / 100.0;
if (rev2)
offset2 = len - offset2;
if (offset1 > offset2) {
double tmp;
tmp = offset1;
offset1 = offset2;
offset2 = tmp;
}
if (offset2 > len) {
G_warning(_("End of segment > line length -> cut"));
offset2 = len;
}
ret = Vect_line_segment(LPoints, offset1, offset2, SPoints);
if (ret == 0) {
G_warning(_("Unable to make line segment: "
"cat = %d : %f - %f (line length = %.15g)\n%s"),
lcat, offset1, offset2, len, buf);
break;
}
Vect_cat_set(SCats, 1, id);
if (fabs(side_offset) > 0.0) {
Vect_line_parallel2(SPoints, side_offset, side_offset,
0.0, 1, FALSE, side_offset / 10.,
PlPoints);
Vect_write_line(&Out, GV_LINE, PlPoints, SCats);
G_debug(3, " segment n_points = %d", PlPoints->n_points);
}
else {
Vect_write_line(&Out, GV_LINE, SPoints, SCats);
G_debug(3, " segment n_points = %d", SPoints->n_points);
}
lines_written++;
break;
default:
G_warning(_("Incorrect segment type: %s"), buf);
}
}
Vect_build(&Out);
G_message(n_("%d point read from input",
"%d points read from input",
points_read), points_read);
/* GTC Number of lost points */
G_asprintf(&tmpstr1, n_("%d lost", "%d lost", points_read - points_written), points_read - points_written);
/* GTC %s is replaced with message indicating number of lost points. */
G_message(n_("%d point written to output map (%s)",
"%d points written to output map (%s)",
points_written),
points_written, tmpstr1);
G_free(tmpstr1);
G_message(n_("%d line read from input",
"%d lines read from input",
lines_read), lines_read);
/* GTC Number of lost lines */
G_asprintf(&tmpstr1, n_("%d lost", "%d lost", lines_read - lines_written), lines_read - lines_written);
/* GTC %s is replaced with message indicating number of lost lines. */
G_message(n_("%d line written to output map (%s)",
"%d lines written to output map (%s)",
lines_written),
lines_written, tmpstr1);
G_free(tmpstr1);
/* Free, close ... */
Vect_close(&In);
Vect_close(&Out);
if (file_opt->answer)
fclose(in_file);
exit(EXIT_SUCCESS);
}
int read_point_input(char *buf, char *stype, int *id, int *lcat, double *offset,
double *side_offset, int *rev, int *pct)
{
char offsetbuf[100];
int ret;
char *p;
*side_offset = 0;
*rev = 0;
*pct = 0;
ret = sscanf(buf, "%c %d %d %[.0-9%-] %lf", stype, id, lcat, offsetbuf,
side_offset);
if (ret < 4) {
return 0;
}
p = offsetbuf;
if (offsetbuf[0] == '-') {
*rev = 1;
p++;
}
if (offsetbuf[strlen(offsetbuf)-1] == '%') {
*pct = 1;
offsetbuf[strlen(offsetbuf)-1] = '\0';
}
if (sscanf(p, "%lf", offset) != 1) {
return 0;
}
return 1;
}
int read_line_input(char *buf, char *stype, int *id, int *lcat,
double *offset1, double *offset2, double *side_offset,
int *rev1, int *pct1, int *rev2, int *pct2)
{
char offset1buf[100];
char offset2buf[100];
int ret;
char *p;
*side_offset = 0;
*rev1 = 0;
*pct1 = 0;
*rev2 = 0;
*pct2 = 0;
ret = sscanf(buf, "%c %d %d %[.0-9%-] %[.0-9%-] %lf", stype, id, lcat,
offset1buf, offset2buf, side_offset);
if (ret < 5) {
return 0;
}
p = offset1buf;
if (offset1buf[0] == '-') {
*rev1 = 1;
p++;
}
if (offset1buf[strlen(offset1buf)-1] == '%') {
*pct1 = 1;
offset1buf[strlen(offset1buf)-1] = '\0';
}
if (sscanf(p, "%lf", offset1) != 1) {
return 0;
}
p = offset2buf;
if (offset2buf[0] == '-') {
*rev2 = 1;
p++;
}
if (offset2buf[strlen(offset2buf)-1] == '%') {
*pct2 = 1;
offset2buf[strlen(offset2buf)-1] = '\0';
}
if (sscanf(p, "%lf", offset2) != 1) {
return 0;
}
return 1;
}
/* Find line by cat, returns 0 if not found */
int find_line(struct Map_info *Map, int lfield, int lcat)
{
int i, nlines, type;
struct line_cats *Cats;
struct ilist *cats;
G_debug(2, "find_line(): llayer = %d lcat = %d", lfield, lcat);
Cats = Vect_new_cats_struct();
cats = Vect_new_list();
nlines = Vect_get_num_lines(Map);
for (i = 1; i <= nlines; i++) {
type = Vect_read_line(Map, NULL, Cats, i);
if (!(type & GV_LINE))
continue;
Vect_field_cat_get(Cats, lfield, cats);
if (Vect_val_in_list(cats, lcat)) {
Vect_destroy_list(cats);
return i;
}
}
Vect_destroy_list(cats);
return 0;
}
/* calculate a point perpendicular to the current line angle, offset by a distance
* works in the x,y plane.
*/
void offset_pt_90(double *x, double *y, double angle, double distance)
{
*x -= distance * cos(M_PI_2 + angle);
*y -= distance * sin(M_PI_2 + angle);
}