main.c 11 KB

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