main.c 10 KB

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