main.c 7.2 KB

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