main.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************
  2. *
  3. * MODULE: v.parallel
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Create parallel lines
  8. *
  9. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  10. *
  11. * This program is free software under the
  12. * GNU General Public License (>=v2).
  13. * Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. * TODO/BUG: The vector segments are not properly connected
  17. * and should be snapped.
  18. **************************************************************/
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/Vect.h>
  23. #include <grass/glocale.h>
  24. int main(int argc, char *argv[])
  25. {
  26. struct GModule *module;
  27. struct Option *in_opt, *out_opt, *distance_opt;
  28. struct Map_info In, Out;
  29. struct line_pnts *Points, *Points2;
  30. struct line_cats *Cats;
  31. int line, nlines;
  32. double distance, tolerance;
  33. G_gisinit(argv[0]);
  34. module = G_define_module();
  35. module->keywords = _("vector, geometry");
  36. module->description = _("Create parallel line to input lines");
  37. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  38. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  39. /* layer_opt = G_define_standard_option(G_OPT_V_FIELD); */
  40. distance_opt = G_define_option();
  41. distance_opt->key = "distance";
  42. distance_opt->type = TYPE_DOUBLE;
  43. distance_opt->required = YES;
  44. distance_opt->multiple = NO;
  45. distance_opt->description =
  46. _("Offset in map units, positive for right side, "
  47. "negative for left side.");
  48. if (G_parser(argc, argv))
  49. exit(EXIT_FAILURE);
  50. /* layer = atoi ( layer_opt->answer ); */
  51. distance = atof(distance_opt->answer);
  52. tolerance = distance / 10.;
  53. Vect_set_open_level(2);
  54. Vect_open_old(&In, in_opt->answer, "");
  55. Vect_open_new(&Out, out_opt->answer, 0);
  56. Vect_copy_head_data(&In, &Out);
  57. Vect_hist_copy(&In, &Out);
  58. Vect_hist_command(&Out);
  59. Points = Vect_new_line_struct();
  60. Points2 = Vect_new_line_struct();
  61. Cats = Vect_new_cats_struct();
  62. nlines = Vect_get_num_lines(&In);
  63. for (line = 1; line <= nlines; line++) {
  64. int ltype;
  65. G_percent(line, nlines, 1);
  66. ltype = Vect_read_line(&In, Points, Cats, line);
  67. if (ltype & GV_LINES) {
  68. Vect_line_parallel(Points, distance, tolerance, 1, Points2);
  69. Vect_write_line(&Out, ltype, Points2, Cats);
  70. }
  71. else {
  72. Vect_write_line(&Out, ltype, Points, Cats);
  73. }
  74. }
  75. Vect_close(&In);
  76. Vect_build(&Out);
  77. Vect_close(&Out);
  78. exit(EXIT_SUCCESS);
  79. }