main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 *mapset, 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. mapset = G_find_vector2(in_opt->answer, NULL);
  82. if (mapset == NULL)
  83. G_fatal_error(_("Vector map <%s> not found"), in_opt->answer);
  84. Vect_set_open_level(2);
  85. Vect_open_old(&In, in_opt->answer, mapset);
  86. /* Open output segments */
  87. Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In));
  88. Vect_hist_copy(&In, &Out);
  89. Vect_hist_command(&Out);
  90. points_read = 0;
  91. lines_read = 0;
  92. points_written = 0;
  93. lines_written = 0;
  94. while (1) {
  95. if (!file_opt->answer) {
  96. if (fgets(buf, sizeof(buf), stdin) == NULL)
  97. break;
  98. }
  99. else {
  100. if (G_getl2(buf, sizeof(buf) - 1, in_file) == 0)
  101. break;
  102. }
  103. G_debug(2, "SEGMENT: %s", G_chop(buf));
  104. side_offset = 0;
  105. Vect_reset_line(SPoints);
  106. Vect_reset_cats(SCats);
  107. Vect_reset_line(PlPoints);
  108. switch (buf[0]) {
  109. case 'P':
  110. side_offset = 0;
  111. ret =
  112. sscanf(buf, "%c %d %d %lf %lf", &stype, &id, &lcat, &offset1,
  113. &side_offset);
  114. if (ret < 4) {
  115. G_warning(_("Unable to read input: %s"), buf);
  116. break;
  117. }
  118. points_read++;
  119. G_debug(2, "point: %d %d %f %f", id, lcat, offset1, side_offset);
  120. /* OK, write point */
  121. line = find_line(&In, lfield, lcat);
  122. if (line == 0) {
  123. G_warning(_("Unable to find line of cat %d"), lcat);
  124. break;
  125. }
  126. Vect_read_line(&In, LPoints, LCats, line);
  127. ret =
  128. Vect_point_on_line(LPoints, offset1, &x, &y, &z, &angle,
  129. NULL);
  130. if (ret == 0) {
  131. len = Vect_line_length(LPoints);
  132. G_warning(_("Unable to get point on line: cat = %d offset = %f "
  133. "(line length = %.15g)\n%s"), lcat, offset1, len,
  134. buf);
  135. break;
  136. }
  137. if (fabs(side_offset) > 0.0)
  138. offset_pt_90(&x, &y, angle, side_offset);
  139. Vect_append_point(SPoints, x, y, z);
  140. Vect_cat_set(SCats, 1, id);
  141. Vect_write_line(&Out, GV_POINT, SPoints, SCats);
  142. points_written++;
  143. break;
  144. case 'L':
  145. side_offset = 0;
  146. ret = sscanf(buf, "%c %d %d %lf %lf %lf", &stype, &id, &lcat,
  147. &offset1, &offset2, &side_offset);
  148. if (ret < 5) {
  149. G_warning(_("Unable to read input: %s"), buf);
  150. break;
  151. }
  152. lines_read++;
  153. G_debug(2, "line: %d %d %f %f %f", id, lcat, offset1, offset2,
  154. side_offset);
  155. line = find_line(&In, lfield, lcat);
  156. if (line == 0) {
  157. G_warning(_("Unable to find line of cat %d"), lcat);
  158. break;
  159. }
  160. Vect_read_line(&In, LPoints, LCats, line);
  161. len = Vect_line_length(LPoints);
  162. if (offset2 > len) {
  163. G_warning(_("End of segment > line length -> cut"));
  164. offset2 = len;
  165. }
  166. ret = Vect_line_segment(LPoints, offset1, offset2, SPoints);
  167. if (ret == 0) {
  168. G_warning(_("Unable to make line segment: "
  169. "cat = %d : %f - %f (line length = %.15g)\n%s"),
  170. lcat, offset1, offset2, len, buf);
  171. break;
  172. }
  173. Vect_cat_set(SCats, 1, id);
  174. if (fabs(side_offset) > 0.0) {
  175. Vect_line_parallel(SPoints, side_offset, side_offset / 10.,
  176. TRUE, PlPoints);
  177. Vect_write_line(&Out, GV_LINE, PlPoints, SCats);
  178. G_debug(3, " segment n_points = %d", PlPoints->n_points);
  179. }
  180. else {
  181. Vect_write_line(&Out, GV_LINE, SPoints, SCats);
  182. G_debug(3, " segment n_points = %d", SPoints->n_points);
  183. }
  184. lines_written++;
  185. break;
  186. default:
  187. G_warning(_("Incorrect segment type: %s"), buf);
  188. }
  189. }
  190. G_message(_("%d points read from input"), points_read);
  191. G_message(_("%d points written to output map (%d lost)"),
  192. points_written, points_read - points_written);
  193. G_message(_("%d lines read from input"), lines_read);
  194. G_message(_("%d lines written to output map (%d lost)"),
  195. lines_written, lines_read - lines_written);
  196. if (G_verbose() > G_verbose_min()) {
  197. Vect_build(&Out, stderr);
  198. }
  199. else {
  200. Vect_build(&Out, NULL);
  201. }
  202. /* Free, close ... */
  203. Vect_close(&In);
  204. Vect_close(&Out);
  205. if (file_opt->answer)
  206. fclose(in_file);
  207. exit(EXIT_SUCCESS);
  208. }
  209. /* Find line by cat, returns 0 if not found */
  210. int find_line(struct Map_info *Map, int lfield, int lcat)
  211. {
  212. int i, nlines, type, cat;
  213. struct line_cats *Cats;
  214. G_debug(2, "find_line(): llayer = %d lcat = %d", lfield, lcat);
  215. Cats = Vect_new_cats_struct();
  216. nlines = Vect_get_num_lines(Map);
  217. for (i = 1; i <= nlines; i++) {
  218. type = Vect_read_line(Map, NULL, Cats, i);
  219. if (!(type & GV_LINE))
  220. continue;
  221. Vect_cat_get(Cats, lfield, &cat);
  222. if (cat == lcat)
  223. return i;
  224. }
  225. return 0;
  226. }
  227. /* calculate a point perpendicular to the current line angle, offset by a distance
  228. * works in the x,y plane.
  229. */
  230. void offset_pt_90(double *x, double *y, double angle, double distance)
  231. {
  232. *x -= distance * cos(M_PI_2 + angle);
  233. *y -= distance * sin(M_PI_2 + angle);
  234. }