main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /***************************************************************
  2. *
  3. * MODULE: v.parallel
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * Upgraded by Rosen Matev (Google Summer of Code 2008)
  7. *
  8. * PURPOSE: Create parallel lines
  9. *
  10. * COPYRIGHT: (C) 2008 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 <math.h>
  20. #include <grass/gis.h>
  21. #include <grass/Vect.h>
  22. #include <grass/glocale.h>
  23. #include "local_proto.h"
  24. int main(int argc, char *argv[])
  25. {
  26. struct GModule *module;
  27. struct Option *in_opt, *out_opt, *dista_opt;
  28. struct Option *distb_opt, *angle_opt, *side_opt;
  29. struct Option *tol_opt;
  30. struct Flag *round_flag, *buf_flag;
  31. struct Map_info In, Out;
  32. struct line_pnts *Points, *Points2, *oPoints, **iPoints;
  33. struct line_cats *Cats;
  34. int line, nlines, inner_count, j;
  35. double da, db, dalpha, tolerance;
  36. int side;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. module->keywords = _("vector, geometry");
  40. module->description = _("Creates parallel line to input vector lines.");
  41. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  42. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  43. /* layer_opt = G_define_standard_option(G_OPT_V_FIELD); */
  44. dista_opt = G_define_option();
  45. dista_opt->key = "distance";
  46. dista_opt->type = TYPE_DOUBLE;
  47. dista_opt->required = YES;
  48. dista_opt->options = "0-100000000";
  49. dista_opt->multiple = NO;
  50. dista_opt->description = _("Offset along major axis in map units");
  51. distb_opt = G_define_option();
  52. distb_opt->key = "minordistance";
  53. distb_opt->type = TYPE_DOUBLE;
  54. distb_opt->required = NO;
  55. distb_opt->options = "0-100000000";
  56. distb_opt->multiple = NO;
  57. distb_opt->description = _("Offset along minor axis in map units");
  58. angle_opt = G_define_option();
  59. angle_opt->key = "angle";
  60. angle_opt->type = TYPE_DOUBLE;
  61. angle_opt->required = NO;
  62. angle_opt->answer = "0";
  63. angle_opt->multiple = NO;
  64. angle_opt->description = _("Angle of major axis in degrees");
  65. side_opt = G_define_option();
  66. side_opt->key = "side";
  67. side_opt->type = TYPE_STRING;
  68. side_opt->required = YES;
  69. side_opt->answer = "right";
  70. side_opt->multiple = NO;
  71. side_opt->options = "left,right,both";
  72. side_opt->description = _("Side");
  73. side_opt->descriptions =
  74. _("left;Parallel line is on the left;"
  75. "right;Parallel line is on the right;"
  76. "both;Parallel lines on both sides");
  77. tol_opt = G_define_option();
  78. tol_opt->key = "tolerance";
  79. tol_opt->type = TYPE_DOUBLE;
  80. tol_opt->required = NO;
  81. tol_opt->options = "0-100000000";
  82. tol_opt->multiple = NO;
  83. tol_opt->description = _("Tolerance of arc polylines in map units");
  84. round_flag = G_define_flag();
  85. round_flag->key = 'r';
  86. round_flag->description = _("Make outside corners round");
  87. buf_flag = G_define_flag();
  88. buf_flag->key = 'b';
  89. buf_flag->description = _("Create buffer-like parallel lines");
  90. if (G_parser(argc, argv))
  91. exit(EXIT_FAILURE);
  92. /* layer = atoi ( layer_opt->answer ); */
  93. da = atof(dista_opt->answer);
  94. if (distb_opt->answer)
  95. db = atof(distb_opt->answer);
  96. else
  97. db = da;
  98. if (angle_opt->answer)
  99. dalpha = atof(angle_opt->answer);
  100. else
  101. dalpha = 0;
  102. if (tol_opt->answer)
  103. tolerance = atof(tol_opt->answer);
  104. else
  105. tolerance = ((db < da) ? db : da) / 100.;
  106. if (strcmp(side_opt->answer, "right") == 0)
  107. side = 1;
  108. else if (strcmp(side_opt->answer, "left") == 0)
  109. side = -1;
  110. else
  111. side = 0;
  112. Vect_set_open_level(2);
  113. Vect_open_old(&In, in_opt->answer, "");
  114. Vect_open_new(&Out, out_opt->answer, 0);
  115. Vect_copy_head_data(&In, &Out);
  116. Vect_hist_copy(&In, &Out);
  117. Vect_hist_command(&Out);
  118. Points = Vect_new_line_struct();
  119. Points2 = Vect_new_line_struct();
  120. Cats = Vect_new_cats_struct();
  121. nlines = Vect_get_num_lines(&In);
  122. for (line = 1; line <= nlines; line++) {
  123. int ltype;
  124. G_percent(line, nlines, 1);
  125. ltype = Vect_read_line(&In, Points, Cats, line);
  126. if (ltype & GV_LINES) {
  127. if (!(buf_flag->answer)) {
  128. if (side != 0) {
  129. Vect_line_parallel2(Points, da, db, dalpha, side,
  130. round_flag->answer, tolerance,
  131. Points2);
  132. Vect_write_line(&Out, ltype, Points2, Cats);
  133. }
  134. else {
  135. Vect_line_parallel2(Points, da, db, dalpha, 1,
  136. round_flag->answer, tolerance,
  137. Points2);
  138. Vect_write_line(&Out, ltype, Points2, Cats);
  139. Vect_line_parallel2(Points, da, db, dalpha, -1,
  140. round_flag->answer, tolerance,
  141. Points2);
  142. Vect_write_line(&Out, ltype, Points2, Cats);
  143. }
  144. }
  145. else {
  146. Vect_line_buffer2(Points, da, db, dalpha, round_flag->answer,
  147. 1, tolerance, &oPoints, &iPoints,
  148. &inner_count);
  149. Vect_write_line(&Out, ltype, oPoints, Cats);
  150. for (j = 0; j < inner_count; j++) {
  151. Vect_write_line(&Out, ltype, iPoints[j], Cats);
  152. }
  153. }
  154. }
  155. else {
  156. Vect_write_line(&Out, ltype, Points, Cats);
  157. }
  158. }
  159. /* Vect_build_partial(&Out, GV_BUILD_BASE);
  160. Vect_snap_lines(&Out, GV_BOUNDARY, 1e-7, NULL);
  161. Vect_break_lines(&Out, GV_BOUNDARY, NULL);
  162. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  163. Vect_build_partial (&Out, GV_BUILD_NONE);
  164. */
  165. Vect_build(&Out);
  166. Vect_close(&In);
  167. Vect_close(&Out);
  168. exit(EXIT_SUCCESS);
  169. }