main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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-2013 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. module->description =
  57. _("Creates points/segments from input vector lines and positions.");
  58. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  59. in_opt->label = _("Name of input vector lines map");
  60. lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  61. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  62. file_opt = G_define_standard_option(G_OPT_F_INPUT);
  63. file_opt->key = "rules";
  64. file_opt->required = NO;
  65. file_opt->label = _("Name of file containing segment rules");
  66. file_opt->description = _("'-' for standard input");
  67. if (G_parser(argc, argv))
  68. exit(EXIT_FAILURE);
  69. LCats = Vect_new_cats_struct();
  70. SCats = Vect_new_cats_struct();
  71. LPoints = Vect_new_line_struct();
  72. SPoints = Vect_new_line_struct();
  73. PlPoints = Vect_new_line_struct();
  74. Vect_check_input_output_name(in_opt->answer, out_opt->answer,
  75. G_FATAL_EXIT);
  76. if (file_opt->answer && strcmp(file_opt->answer, "-") != 0) {
  77. /* open input file */
  78. if ((in_file = fopen(file_opt->answer, "r")) == NULL)
  79. G_fatal_error(_("Unable to open input file <%s>"),
  80. file_opt->answer);
  81. }
  82. else
  83. in_file = stdin;
  84. /* Open input lines */
  85. Vect_set_open_level(2);
  86. if (Vect_open_old2(&In, in_opt->answer, "", lfield_opt->answer) < 0)
  87. G_fatal_error(_("Unable to open vector map <%s>"), in_opt->answer);
  88. lfield = Vect_get_field_number(&In, lfield_opt->answer);
  89. /* Open output segments */
  90. if (Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In)) < 0)
  91. G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
  92. Vect_hist_copy(&In, &Out);
  93. Vect_hist_command(&Out);
  94. points_read = 0;
  95. lines_read = 0;
  96. points_written = 0;
  97. lines_written = 0;
  98. while (1) {
  99. int rev1, rev2, pct1, pct2;
  100. if (!file_opt->answer) {
  101. if (fgets(buf, sizeof(buf), stdin) == NULL)
  102. break;
  103. }
  104. else {
  105. if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
  106. break;
  107. }
  108. G_debug(2, "SEGMENT: %s", G_chop(buf));
  109. side_offset = 0;
  110. Vect_reset_line(SPoints);
  111. Vect_reset_cats(SCats);
  112. Vect_reset_line(PlPoints);
  113. switch (buf[0]) {
  114. case 'P':
  115. if (!read_point_input(buf, &stype, &id, &lcat, &offset1,
  116. &side_offset, &rev1, &pct1)) {
  117. G_warning(_("Unable to read input: %s"), buf);
  118. break;
  119. }
  120. points_read++;
  121. G_debug(2, "point: %d %d %s%f%s %f", id, lcat, rev1 ? "-" : "",
  122. offset1, pct1 ? "%" : "", side_offset);
  123. /* OK, write point */
  124. line = find_line(&In, lfield, lcat);
  125. if (line == 0) {
  126. G_warning(_("Unable to find line of cat %d"), lcat);
  127. break;
  128. }
  129. Vect_read_line(&In, LPoints, LCats, line);
  130. len = Vect_line_length(LPoints);
  131. if (pct1)
  132. offset1 = len * offset1 / 100.0;
  133. if (rev1)
  134. offset1 = len - offset1;
  135. ret = Vect_point_on_line(LPoints, offset1, &x, &y, &z, &angle,
  136. NULL);
  137. if (ret == 0) {
  138. G_warning(_("Unable to get point on line: cat = %d offset = %f "
  139. "(line length = %.15g)\n%s"), lcat, offset1, len,
  140. buf);
  141. break;
  142. }
  143. if (fabs(side_offset) > 0.0)
  144. offset_pt_90(&x, &y, angle, side_offset);
  145. Vect_append_point(SPoints, x, y, z);
  146. Vect_cat_set(SCats, 1, id);
  147. Vect_write_line(&Out, GV_POINT, SPoints, SCats);
  148. points_written++;
  149. break;
  150. case 'L':
  151. if (!read_line_input(buf, &stype, &id, &lcat, &offset1, &offset2,
  152. &side_offset, &rev1, &pct1, &rev2, &pct2)) {
  153. G_warning(_("Unable to read input: %s"), buf);
  154. break;
  155. }
  156. lines_read++;
  157. G_debug(2, "line: %d %d %s%f%s %s%f%s %f", id, lcat,
  158. rev1 ? "-" : "", offset1, pct1 ? "%" : "",
  159. rev2 ? "-" : "", offset2, pct2 ? "%" : "",
  160. side_offset);
  161. line = find_line(&In, lfield, lcat);
  162. if (line == 0) {
  163. G_warning(_("Unable to find line of cat %d"), lcat);
  164. break;
  165. }
  166. Vect_read_line(&In, LPoints, LCats, line);
  167. len = Vect_line_length(LPoints);
  168. if (pct1)
  169. offset1 = len * offset1 / 100.0;
  170. if (rev1)
  171. offset1 = len - offset1;
  172. if (pct2)
  173. offset2 = len * offset2 / 100.0;
  174. if (rev2)
  175. offset2 = len - offset2;
  176. if (offset1 > offset2) {
  177. double tmp;
  178. tmp = offset1;
  179. offset1 = offset2;
  180. offset2 = tmp;
  181. }
  182. if (offset2 > len) {
  183. G_warning(_("End of segment > line length -> cut"));
  184. offset2 = len;
  185. }
  186. ret = Vect_line_segment(LPoints, offset1, offset2, SPoints);
  187. if (ret == 0) {
  188. G_warning(_("Unable to make line segment: "
  189. "cat = %d : %f - %f (line length = %.15g)\n%s"),
  190. lcat, offset1, offset2, len, buf);
  191. break;
  192. }
  193. Vect_cat_set(SCats, 1, id);
  194. if (fabs(side_offset) > 0.0) {
  195. Vect_line_parallel(SPoints, side_offset, side_offset / 10.,
  196. TRUE, PlPoints);
  197. Vect_write_line(&Out, GV_LINE, PlPoints, SCats);
  198. G_debug(3, " segment n_points = %d", PlPoints->n_points);
  199. }
  200. else {
  201. Vect_write_line(&Out, GV_LINE, SPoints, SCats);
  202. G_debug(3, " segment n_points = %d", SPoints->n_points);
  203. }
  204. lines_written++;
  205. break;
  206. default:
  207. G_warning(_("Incorrect segment type: %s"), buf);
  208. }
  209. }
  210. Vect_build(&Out);
  211. G_message(_n("%d point read from input",
  212. "%d points read from input",
  213. points_read), points_read);
  214. /* GTC Number of lost points */
  215. G_asprintf(&tmpstr1, _n("%d lost", "%d lost", points_read - points_written), points_read - points_written);
  216. /* GTC %s is replaced with message indicating number of lost points. */
  217. G_message(_n("%d point written to output map (%s)",
  218. "%d points written to output map (%s)",
  219. points_written),
  220. points_written, tmpstr1);
  221. G_free(tmpstr1);
  222. G_message(_n("%d line read from input",
  223. "%d lines read from input",
  224. lines_read), lines_read);
  225. /* GTC Number of lost lines */
  226. G_asprintf(&tmpstr1, _n("%d lost", "%d lost", lines_read - lines_written), lines_read - lines_written);
  227. /* GTC %s is replaced with message indicating number of lost lines. */
  228. G_message(_n("%d line written to output map (%s)",
  229. "%d lines written to output map (%s)",
  230. lines_written),
  231. lines_written, tmpstr1);
  232. G_free(tmpstr1);
  233. /* Free, close ... */
  234. Vect_close(&In);
  235. Vect_close(&Out);
  236. if (file_opt->answer)
  237. fclose(in_file);
  238. exit(EXIT_SUCCESS);
  239. }
  240. int read_point_input(char *buf, char *stype, int *id, int *lcat, double *offset,
  241. double *side_offset, int *rev, int *pct)
  242. {
  243. char offsetbuf[100];
  244. int ret;
  245. char *p;
  246. *side_offset = 0;
  247. *rev = 0;
  248. *pct = 0;
  249. ret = sscanf(buf, "%c %d %d %[.0-9%-] %lf", stype, id, lcat, offsetbuf,
  250. side_offset);
  251. if (ret < 4) {
  252. return 0;
  253. }
  254. p = offsetbuf;
  255. if (offsetbuf[0] == '-') {
  256. *rev = 1;
  257. p++;
  258. }
  259. if (offsetbuf[strlen(offsetbuf)-1] == '%') {
  260. *pct = 1;
  261. offsetbuf[strlen(offsetbuf)-1] = '\0';
  262. }
  263. if (sscanf(p, "%lf", offset) != 1) {
  264. return 0;
  265. }
  266. return 1;
  267. }
  268. int read_line_input(char *buf, char *stype, int *id, int *lcat,
  269. double *offset1, double *offset2, double *side_offset,
  270. int *rev1, int *pct1, int *rev2, int *pct2)
  271. {
  272. char offset1buf[100];
  273. char offset2buf[100];
  274. int ret;
  275. char *p;
  276. *side_offset = 0;
  277. *rev1 = 0;
  278. *pct1 = 0;
  279. *rev2 = 0;
  280. *pct2 = 0;
  281. ret = sscanf(buf, "%c %d %d %[.0-9%-] %[.0-9%-] %lf", stype, id, lcat,
  282. offset1buf, offset2buf, side_offset);
  283. if (ret < 5) {
  284. return 0;
  285. }
  286. p = offset1buf;
  287. if (offset1buf[0] == '-') {
  288. *rev1 = 1;
  289. p++;
  290. }
  291. if (offset1buf[strlen(offset1buf)-1] == '%') {
  292. *pct1 = 1;
  293. offset1buf[strlen(offset1buf)-1] = '\0';
  294. }
  295. if (sscanf(p, "%lf", offset1) != 1) {
  296. return 0;
  297. }
  298. p = offset2buf;
  299. if (offset2buf[0] == '-') {
  300. *rev2 = 1;
  301. p++;
  302. }
  303. if (offset2buf[strlen(offset2buf)-1] == '%') {
  304. *pct2 = 1;
  305. offset2buf[strlen(offset2buf)-1] = '\0';
  306. }
  307. if (sscanf(p, "%lf", offset2) != 1) {
  308. return 0;
  309. }
  310. return 1;
  311. }
  312. /* Find line by cat, returns 0 if not found */
  313. int find_line(struct Map_info *Map, int lfield, int lcat)
  314. {
  315. int i, nlines, type;
  316. struct line_cats *Cats;
  317. struct ilist *cats;
  318. G_debug(2, "find_line(): llayer = %d lcat = %d", lfield, lcat);
  319. Cats = Vect_new_cats_struct();
  320. cats = Vect_new_list();
  321. nlines = Vect_get_num_lines(Map);
  322. for (i = 1; i <= nlines; i++) {
  323. type = Vect_read_line(Map, NULL, Cats, i);
  324. if (!(type & GV_LINE))
  325. continue;
  326. Vect_field_cat_get(Cats, lfield, cats);
  327. if (Vect_val_in_list(cats, lcat)) {
  328. Vect_destroy_list(cats);
  329. return i;
  330. }
  331. }
  332. Vect_destroy_list(cats);
  333. return 0;
  334. }
  335. /* calculate a point perpendicular to the current line angle, offset by a distance
  336. * works in the x,y plane.
  337. */
  338. void offset_pt_90(double *x, double *y, double angle, double distance)
  339. {
  340. *x -= distance * cos(M_PI_2 + angle);
  341. *y -= distance * sin(M_PI_2 + angle);
  342. }