main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. *
  9. * PURPOSE: Generate segments or points from input map and segments read from stdin
  10. *
  11. * COPYRIGHT: (C) 2002-2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that
  15. * comes with GRASS for details.
  16. *
  17. **************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <time.h>
  22. #include <grass/gis.h>
  23. #include <grass/vector.h>
  24. #include <grass/dbmi.h>
  25. #include <grass/glocale.h>
  26. int find_line(struct Map_info *Map, int lfield, int cat);
  27. void offset_pt_90(double *, double *, double, double);
  28. int main(int argc, char **argv)
  29. {
  30. FILE *in_file;
  31. int ret, points_written, lines_written, points_read, lines_read;
  32. int lfield;
  33. int line;
  34. int id, lcat;
  35. double offset1, offset2, side_offset;
  36. double x, y, z, angle, len;
  37. char stype;
  38. struct Option *in_opt, *out_opt;
  39. struct Option *lfield_opt, *file_opt;
  40. struct GModule *module;
  41. char buf[2000];
  42. struct Map_info In, Out;
  43. struct line_cats *LCats, *SCats;
  44. struct line_pnts *LPoints, *SPoints, *PlPoints;
  45. G_gisinit(argv[0]);
  46. module = G_define_module();
  47. G_add_keyword(_("vector"));
  48. G_add_keyword(_("geometry"));
  49. module->description =
  50. _("Creates points/segments from input vector lines and positions.");
  51. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  52. in_opt->label = _("Name of input vector lines map");
  53. lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  54. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  55. file_opt = G_define_standard_option(G_OPT_F_INPUT);
  56. file_opt->key = "file";
  57. file_opt->label = _("Name of file containing segment rules");
  58. file_opt->description = _("'-' for standard input");
  59. if (G_parser(argc, argv))
  60. exit(EXIT_FAILURE);
  61. LCats = Vect_new_cats_struct();
  62. SCats = Vect_new_cats_struct();
  63. LPoints = Vect_new_line_struct();
  64. SPoints = Vect_new_line_struct();
  65. PlPoints = Vect_new_line_struct();
  66. Vect_check_input_output_name(in_opt->answer, out_opt->answer,
  67. G_FATAL_EXIT);
  68. if (strcmp(file_opt->answer, "-")) {
  69. /* open input file */
  70. if ((in_file = fopen(file_opt->answer, "r")) == NULL)
  71. G_fatal_error(_("Unable to open input file <%s>"),
  72. file_opt->answer);
  73. }
  74. else
  75. in_file = stdin;
  76. /* Open input lines */
  77. Vect_set_open_level(2);
  78. Vect_open_old2(&In, in_opt->answer, "", lfield_opt->answer);
  79. lfield = Vect_get_field_number(&In, lfield_opt->answer);
  80. /* Open output segments */
  81. Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In));
  82. Vect_hist_copy(&In, &Out);
  83. Vect_hist_command(&Out);
  84. points_read = 0;
  85. lines_read = 0;
  86. points_written = 0;
  87. lines_written = 0;
  88. while (1) {
  89. if (!file_opt->answer) {
  90. if (fgets(buf, sizeof(buf), stdin) == NULL)
  91. break;
  92. }
  93. else {
  94. if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
  95. break;
  96. }
  97. G_debug(2, "SEGMENT: %s", G_chop(buf));
  98. side_offset = 0;
  99. Vect_reset_line(SPoints);
  100. Vect_reset_cats(SCats);
  101. Vect_reset_line(PlPoints);
  102. switch (buf[0]) {
  103. case 'P':
  104. side_offset = 0;
  105. ret =
  106. sscanf(buf, "%c %d %d %lf %lf", &stype, &id, &lcat, &offset1,
  107. &side_offset);
  108. if (ret < 4) {
  109. G_warning(_("Unable to read input: %s"), buf);
  110. break;
  111. }
  112. points_read++;
  113. G_debug(2, "point: %d %d %f %f", id, lcat, offset1, side_offset);
  114. /* OK, write point */
  115. line = find_line(&In, lfield, lcat);
  116. if (line == 0) {
  117. G_warning(_("Unable to find line of cat %d"), lcat);
  118. break;
  119. }
  120. Vect_read_line(&In, LPoints, LCats, line);
  121. ret =
  122. Vect_point_on_line(LPoints, offset1, &x, &y, &z, &angle,
  123. NULL);
  124. if (ret == 0) {
  125. len = Vect_line_length(LPoints);
  126. G_warning(_("Unable to get point on line: cat = %d offset = %f "
  127. "(line length = %.15g)\n%s"), lcat, offset1, len,
  128. buf);
  129. break;
  130. }
  131. if (fabs(side_offset) > 0.0)
  132. offset_pt_90(&x, &y, angle, side_offset);
  133. Vect_append_point(SPoints, x, y, z);
  134. Vect_cat_set(SCats, 1, id);
  135. Vect_write_line(&Out, GV_POINT, SPoints, SCats);
  136. points_written++;
  137. break;
  138. case 'L':
  139. side_offset = 0;
  140. ret = sscanf(buf, "%c %d %d %lf %lf %lf", &stype, &id, &lcat,
  141. &offset1, &offset2, &side_offset);
  142. if (ret < 5) {
  143. G_warning(_("Unable to read input: %s"), buf);
  144. break;
  145. }
  146. lines_read++;
  147. G_debug(2, "line: %d %d %f %f %f", id, lcat, offset1, offset2,
  148. side_offset);
  149. line = find_line(&In, lfield, lcat);
  150. if (line == 0) {
  151. G_warning(_("Unable to find line of cat %d"), lcat);
  152. break;
  153. }
  154. Vect_read_line(&In, LPoints, LCats, line);
  155. len = Vect_line_length(LPoints);
  156. if (offset2 > len) {
  157. G_warning(_("End of segment > line length -> cut"));
  158. offset2 = len;
  159. }
  160. ret = Vect_line_segment(LPoints, offset1, offset2, SPoints);
  161. if (ret == 0) {
  162. G_warning(_("Unable to make line segment: "
  163. "cat = %d : %f - %f (line length = %.15g)\n%s"),
  164. lcat, offset1, offset2, len, buf);
  165. break;
  166. }
  167. Vect_cat_set(SCats, 1, id);
  168. if (fabs(side_offset) > 0.0) {
  169. Vect_line_parallel(SPoints, side_offset, side_offset / 10.,
  170. TRUE, PlPoints);
  171. Vect_write_line(&Out, GV_LINE, PlPoints, SCats);
  172. G_debug(3, " segment n_points = %d", PlPoints->n_points);
  173. }
  174. else {
  175. Vect_write_line(&Out, GV_LINE, SPoints, SCats);
  176. G_debug(3, " segment n_points = %d", SPoints->n_points);
  177. }
  178. lines_written++;
  179. break;
  180. default:
  181. G_warning(_("Incorrect segment type: %s"), buf);
  182. }
  183. }
  184. Vect_build(&Out);
  185. G_message(_("%d points read from input"), points_read);
  186. G_message(_("%d points written to output map (%d lost)"),
  187. points_written, points_read - points_written);
  188. G_message(_("%d lines read from input"), lines_read);
  189. G_message(_("%d lines written to output map (%d lost)"),
  190. lines_written, lines_read - lines_written);
  191. /* Free, close ... */
  192. Vect_close(&In);
  193. Vect_close(&Out);
  194. if (file_opt->answer)
  195. fclose(in_file);
  196. exit(EXIT_SUCCESS);
  197. }
  198. /* Find line by cat, returns 0 if not found */
  199. int find_line(struct Map_info *Map, int lfield, int lcat)
  200. {
  201. int i, nlines, type, cat;
  202. struct line_cats *Cats;
  203. G_debug(2, "find_line(): llayer = %d lcat = %d", lfield, lcat);
  204. Cats = Vect_new_cats_struct();
  205. nlines = Vect_get_num_lines(Map);
  206. for (i = 1; i <= nlines; i++) {
  207. type = Vect_read_line(Map, NULL, Cats, i);
  208. if (!(type & GV_LINE))
  209. continue;
  210. Vect_cat_get(Cats, lfield, &cat);
  211. if (cat == lcat)
  212. return i;
  213. }
  214. return 0;
  215. }
  216. /* calculate a point perpendicular to the current line angle, offset by a distance
  217. * works in the x,y plane.
  218. */
  219. void offset_pt_90(double *x, double *y, double angle, double distance)
  220. {
  221. *x -= distance * cos(M_PI_2 + angle);
  222. *y -= distance * sin(M_PI_2 + angle);
  223. }