main.c 7.2 KB

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