main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /***************************************************************
  2. *
  3. * MODULE: v.split
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * OGR support by Martin Landa <landa.martin gmail.com>
  7. * update for GRASS 7 Markus Metz
  8. *
  9. * PURPOSE: Split lines to segments
  10. *
  11. * COPYRIGHT: (C) 2001-2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that
  15. * comes with GRASS for details.
  16. *
  17. **************************************************************/
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <grass/gis.h>
  22. #include <grass/vector.h>
  23. #include <grass/glocale.h>
  24. #define FROM_KILOMETERS 1000.0
  25. #define FROM_FEET 0.3048
  26. #define FROM_SFEET 1200.0 / 3937.0
  27. #define FROM_MILES 1609.344
  28. #define FROM_NAUTMILES 1852.0
  29. int main(int argc, char *argv[])
  30. {
  31. struct GModule *module;
  32. struct Option *in_opt, *layer_opt, *out_opt, *length_opt, *units_opt, *vertices_opt;
  33. struct Map_info In, Out;
  34. struct line_pnts *Points, *Points2;
  35. struct line_cats *Cats;
  36. int line, nlines, layer;
  37. double length = -1;
  38. int vertices = 0;
  39. double (*line_length) ();
  40. int geodesic = 0;
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("vector"));
  44. G_add_keyword(_("geometry"));
  45. module->description = _("Splits vector lines to shorter segments.");
  46. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  47. layer_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  48. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  49. length_opt = G_define_option();
  50. length_opt->key = "length";
  51. length_opt->type = TYPE_DOUBLE;
  52. length_opt->required = NO;
  53. length_opt->multiple = NO;
  54. length_opt->description = _("Maximum segment length");
  55. units_opt = G_define_option();
  56. units_opt->key = "units";
  57. units_opt->type = TYPE_STRING;
  58. units_opt->required = NO;
  59. units_opt->multiple = NO;
  60. units_opt->options = "map,meters,kilometers,feet,surveyfeet,miles,nautmiles";
  61. units_opt->answer = "map";
  62. units_opt->description = _("Length units");
  63. vertices_opt = G_define_option();
  64. vertices_opt->key = "vertices";
  65. vertices_opt->type = TYPE_INTEGER;
  66. vertices_opt->required = NO;
  67. vertices_opt->multiple = NO;
  68. vertices_opt->description = _("Maximum number of vertices in segment");
  69. if (G_parser(argc, argv))
  70. exit(EXIT_FAILURE);
  71. if ((length_opt->answer && vertices_opt->answer) ||
  72. !(length_opt->answer || vertices_opt->answer))
  73. G_fatal_error(_("Use either length or vertices"));
  74. line_length = NULL;
  75. if (length_opt->answer) {
  76. length = atof(length_opt->answer);
  77. if (length <= 0)
  78. G_fatal_error(_("Length must be positive but is %g"), length);
  79. /* convert length to meters */
  80. if (strcmp(units_opt->answer, "meters") == 0)
  81. /* do nothing */ ;
  82. else if (strcmp(units_opt->answer, "kilometers") == 0)
  83. length *= FROM_KILOMETERS;
  84. else if (strcmp(units_opt->answer, "feet") == 0)
  85. length *= FROM_FEET;
  86. else if (strcmp(units_opt->answer, "surveyfeet") == 0)
  87. length *= FROM_SFEET;
  88. else if (strcmp(units_opt->answer, "miles") == 0)
  89. length *= FROM_MILES;
  90. else if (strcmp(units_opt->answer, "nautmiles") == 0)
  91. length *= FROM_NAUTMILES;
  92. else if (strcmp(units_opt->answer, "map") != 0)
  93. G_fatal_error(_("Unknown unit %s"), units_opt->answer);
  94. /* set line length function */
  95. if (G_projection() == PROJECTION_LL) {
  96. if (strcmp(units_opt->answer, "map") == 0)
  97. line_length = Vect_line_length;
  98. else {
  99. line_length = Vect_line_geodesic_length;
  100. geodesic = 1;
  101. }
  102. }
  103. else {
  104. double factor;
  105. line_length = Vect_line_length;
  106. /* convert length to map units */
  107. if ((factor = G_database_units_to_meters_factor()) == 0)
  108. G_fatal_error(_("Can not get projection units"));
  109. else if (strcmp(units_opt->answer, "map") != 0) {
  110. /* meters to units */
  111. length = length / factor;
  112. }
  113. }
  114. if (strcmp(units_opt->answer, "map") == 0)
  115. G_verbose_message(_("Length in map units: %g"), length);
  116. else
  117. G_verbose_message(_("Length in meters: %g"), length);
  118. }
  119. if (vertices_opt->answer) {
  120. vertices = atoi(vertices_opt->answer);
  121. if (vertices < 2)
  122. G_fatal_error(_("Number of vertices must be at least 2"));
  123. }
  124. Vect_set_open_level(2);
  125. Vect_open_old2(&In, in_opt->answer, "", layer_opt->answer);
  126. layer = Vect_get_field_number(&In, layer_opt->answer);
  127. Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In));
  128. Vect_copy_head_data(&In, &Out);
  129. Vect_hist_copy(&In, &Out);
  130. Vect_hist_command(&Out);
  131. Vect_copy_tables(&In, &Out, layer);
  132. Points = Vect_new_line_struct();
  133. Points2 = Vect_new_line_struct();
  134. Cats = Vect_new_cats_struct();
  135. nlines = Vect_get_num_lines(&In);
  136. for (line = 1; line <= nlines; line++) {
  137. int ltype;
  138. G_percent(line, nlines, 1);
  139. if (!Vect_line_alive(&In, line))
  140. continue;
  141. ltype = Vect_read_line(&In, Points, Cats, line);
  142. if (layer != -1 && !Vect_cat_get(Cats, layer, NULL))
  143. continue;
  144. if (ltype & GV_LINES) {
  145. if (length > 0) {
  146. double l, from, to, step;
  147. l = line_length(Points);
  148. if (l <= length) {
  149. Vect_write_line(&Out, ltype, Points, Cats);
  150. }
  151. else {
  152. long n, i;
  153. G_debug(3, "l: %f, length: %f", l, length);
  154. n = ceil(l / length);
  155. if (geodesic)
  156. l = Vect_line_length(Points);
  157. step = l / n;
  158. from = 0.;
  159. G_debug(3, "n: %ld, step: %f", n, step);
  160. for (i = 0; i < n; i++) {
  161. int ret;
  162. double x, y, z;
  163. if (i == n - 1) {
  164. to = l; /* to be sure that it goes to end */
  165. }
  166. else {
  167. to = from + step;
  168. }
  169. ret = Vect_line_segment(Points, from, to, Points2);
  170. if (ret == 0) {
  171. G_warning(_("Unable to make line segment: %f - %f (line length = %f)"),
  172. from, to, l);
  173. continue;
  174. }
  175. /* To be sure that the coordinates are identical */
  176. if (i > 0) {
  177. Points2->x[0] = x;
  178. Points2->y[0] = y;
  179. Points2->z[0] = z;
  180. }
  181. if (i == n - 1) {
  182. Points2->x[Points2->n_points - 1] =
  183. Points->x[Points->n_points - 1];
  184. Points2->y[Points2->n_points - 1] =
  185. Points->y[Points->n_points - 1];
  186. Points2->z[Points2->n_points - 1] =
  187. Points->z[Points->n_points - 1];
  188. }
  189. Vect_write_line(&Out, ltype, Points2, Cats);
  190. /* last point */
  191. x = Points2->x[Points2->n_points - 1];
  192. y = Points2->y[Points2->n_points - 1];
  193. z = Points2->z[Points2->n_points - 1];
  194. from += step;
  195. }
  196. }
  197. }
  198. else {
  199. int start = 0; /* number of coordinates written */
  200. while (start < Points->n_points - 1) {
  201. int i, v = 0;
  202. Vect_reset_line(Points2);
  203. for (i = 0; i < vertices; i++) {
  204. v = start + i;
  205. if (v == Points->n_points)
  206. break;
  207. Vect_append_point(Points2, Points->x[v], Points->y[v],
  208. Points->z[v]);
  209. }
  210. Vect_write_line(&Out, ltype, Points2, Cats);
  211. start = v;
  212. }
  213. }
  214. }
  215. else {
  216. Vect_write_line(&Out, ltype, Points, Cats);
  217. }
  218. }
  219. Vect_close(&In);
  220. Vect_build(&Out);
  221. Vect_close(&Out);
  222. exit(EXIT_SUCCESS);
  223. }