main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /***************************************************************
  2. *
  3. * MODULE: v.split
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Split lines to segments
  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. **************************************************************/
  17. #include <stdlib.h>
  18. #include <math.h>
  19. #include <grass/gis.h>
  20. #include <grass/Vect.h>
  21. #include <grass/glocale.h>
  22. int main(int argc, char *argv[])
  23. {
  24. struct GModule *module;
  25. struct Option *in_opt, *out_opt, *length_opt, *vertices_opt;
  26. /* struct Option *layer_opt; */
  27. struct Map_info In, Out;
  28. struct line_pnts *Points, *Points2;
  29. struct line_cats *Cats;
  30. /* int layer; */
  31. int line, nlines;
  32. double length;
  33. int vertices;
  34. G_gisinit(argv[0]);
  35. module = G_define_module();
  36. module->keywords = _("vector, geometry");
  37. module->description = "Split lines to shorter segments.";
  38. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  39. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  40. /* layer_opt = G_define_standard_option(G_OPT_V_FIELD); */
  41. length_opt = G_define_option();
  42. length_opt->key = "length";
  43. length_opt->type = TYPE_DOUBLE;
  44. length_opt->required = NO;
  45. length_opt->multiple = NO;
  46. length_opt->description = "Maximum segment length.";
  47. vertices_opt = G_define_option();
  48. vertices_opt->key = "vertices";
  49. vertices_opt->type = TYPE_INTEGER;
  50. vertices_opt->required = NO;
  51. vertices_opt->multiple = NO;
  52. vertices_opt->description = "Maximum number of vertices in segment.";
  53. if (G_parser(argc, argv))
  54. exit(EXIT_FAILURE);
  55. /* layer = atoi ( layer_opt->answer ); */
  56. if ((length_opt->answer && vertices_opt->answer) ||
  57. !(length_opt->answer || vertices_opt->answer))
  58. G_fatal_error("Use either length or vertices");
  59. if (length_opt->answer)
  60. length = atof(length_opt->answer);
  61. if (vertices_opt->answer) {
  62. vertices = atoi(vertices_opt->answer);
  63. if (vertices < 2)
  64. G_fatal_error("Number of vertices must be at least 2");
  65. }
  66. Vect_set_open_level(2);
  67. Vect_open_old(&In, in_opt->answer, "");
  68. Vect_open_new(&Out, out_opt->answer, 0);
  69. Vect_copy_head_data(&In, &Out);
  70. Vect_hist_copy(&In, &Out);
  71. Vect_hist_command(&Out);
  72. Points = Vect_new_line_struct();
  73. Points2 = Vect_new_line_struct();
  74. Cats = Vect_new_cats_struct();
  75. nlines = Vect_get_num_lines(&In);
  76. for (line = 1; line <= nlines; line++) {
  77. int ltype;
  78. G_percent(line, nlines, 1);
  79. ltype = Vect_read_line(&In, Points, Cats, line);
  80. if (ltype & GV_LINES) {
  81. if (length_opt->answer) {
  82. double l, from, to, step;
  83. l = Vect_line_length(Points);
  84. if (l <= length) {
  85. Vect_write_line(&Out, ltype, Points, Cats);
  86. }
  87. else {
  88. int n, i;
  89. n = ceil(l / length);
  90. step = l / n;
  91. from = 0.;
  92. for (i = 0; i < n; i++) {
  93. int ret;
  94. double x, y;
  95. if (i == n - 1) {
  96. to = l; /* to be sure that it goes to end */
  97. }
  98. else {
  99. to = from + step;
  100. }
  101. ret = Vect_line_segment(Points, from, to, Points2);
  102. if (ret == 0) {
  103. G_warning
  104. ("Cannot make line segment: %f - %f (line length = %f)",
  105. from, to, l);
  106. continue;
  107. }
  108. /* To be sure that the coordinates are identical */
  109. if (i > 0) {
  110. Points2->x[0] = x;
  111. Points2->y[0] = y;
  112. }
  113. if (i == n - 1) {
  114. Points2->x[Points2->n_points - 1] =
  115. Points->x[Points->n_points - 1];
  116. Points2->y[Points2->n_points - 1] =
  117. Points->y[Points->n_points - 1];
  118. }
  119. Vect_write_line(&Out, ltype, Points2, Cats);
  120. /* last point */
  121. x = Points2->x[Points2->n_points - 1];
  122. y = Points2->y[Points2->n_points - 1];
  123. from += step;
  124. }
  125. }
  126. }
  127. else {
  128. int start = 0; /* number of coordinates written */
  129. while (start < Points->n_points - 1) {
  130. int i, v;
  131. Vect_reset_line(Points2);
  132. for (i = 0; i < vertices; i++) {
  133. v = start + i;
  134. if (v == Points->n_points)
  135. break;
  136. Vect_append_point(Points2, Points->x[v], Points->y[v],
  137. Points->z[v]);
  138. }
  139. Vect_write_line(&Out, ltype, Points2, Cats);
  140. start = v;
  141. }
  142. }
  143. }
  144. else {
  145. Vect_write_line(&Out, ltype, Points, Cats);
  146. }
  147. }
  148. Vect_close(&In);
  149. Vect_build(&Out);
  150. Vect_close(&Out);
  151. exit(0);
  152. }