main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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,
  33. *units_opt, *vertices_opt;
  34. struct Flag *nosplit_flag;
  35. struct Map_info In, Out;
  36. struct line_pnts *Points, *Points2, *Points3;
  37. struct line_cats *Cats;
  38. int line, nlines, layer, nosplit;
  39. double length = -1;
  40. int vertices = 0;
  41. double (*line_length) ();
  42. int geodesic = 0;
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("vector"));
  46. G_add_keyword(_("geometry"));
  47. module->description = _("Splits vector lines to shorter segments.");
  48. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  49. layer_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  50. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  51. length_opt = G_define_option();
  52. length_opt->key = "length";
  53. length_opt->type = TYPE_DOUBLE;
  54. length_opt->required = NO;
  55. length_opt->multiple = NO;
  56. length_opt->description = _("Maximum segment length");
  57. units_opt = G_define_option();
  58. units_opt->key = "units";
  59. units_opt->type = TYPE_STRING;
  60. units_opt->required = NO;
  61. units_opt->multiple = NO;
  62. units_opt->options = "map,meters,kilometers,feet,surveyfeet,miles,nautmiles";
  63. units_opt->answer = "map";
  64. units_opt->description = _("Length units");
  65. vertices_opt = G_define_option();
  66. vertices_opt->key = "vertices";
  67. vertices_opt->type = TYPE_INTEGER;
  68. vertices_opt->required = NO;
  69. vertices_opt->multiple = NO;
  70. vertices_opt->description = _("Maximum number of vertices in segment");
  71. nosplit_flag = G_define_flag();
  72. nosplit_flag->key = 'n';
  73. nosplit_flag->label = _("Add new vertices, but do not split");
  74. nosplit_flag->description = _("Applies only to 'length' option");
  75. if (G_parser(argc, argv))
  76. exit(EXIT_FAILURE);
  77. if ((length_opt->answer && vertices_opt->answer) ||
  78. !(length_opt->answer || vertices_opt->answer))
  79. G_fatal_error(_("Use either length or vertices"));
  80. line_length = NULL;
  81. if (length_opt->answer) {
  82. length = atof(length_opt->answer);
  83. if (length <= 0)
  84. G_fatal_error(_("Length must be positive but is %g"), length);
  85. /* convert length to meters */
  86. if (strcmp(units_opt->answer, "meters") == 0)
  87. /* do nothing */ ;
  88. else if (strcmp(units_opt->answer, "kilometers") == 0)
  89. length *= FROM_KILOMETERS;
  90. else if (strcmp(units_opt->answer, "feet") == 0)
  91. length *= FROM_FEET;
  92. else if (strcmp(units_opt->answer, "surveyfeet") == 0)
  93. length *= FROM_SFEET;
  94. else if (strcmp(units_opt->answer, "miles") == 0)
  95. length *= FROM_MILES;
  96. else if (strcmp(units_opt->answer, "nautmiles") == 0)
  97. length *= FROM_NAUTMILES;
  98. else if (strcmp(units_opt->answer, "map") != 0)
  99. G_fatal_error(_("Unknown unit %s"), units_opt->answer);
  100. /* set line length function */
  101. if (G_projection() == PROJECTION_LL) {
  102. if (strcmp(units_opt->answer, "map") == 0)
  103. line_length = Vect_line_length;
  104. else {
  105. line_length = Vect_line_geodesic_length;
  106. geodesic = 1;
  107. }
  108. }
  109. else {
  110. double factor;
  111. line_length = Vect_line_length;
  112. /* convert length to map units */
  113. if ((factor = G_database_units_to_meters_factor()) == 0)
  114. G_fatal_error(_("Can not get projection units"));
  115. else if (strcmp(units_opt->answer, "map") != 0) {
  116. /* meters to units */
  117. length = length / factor;
  118. }
  119. }
  120. if (strcmp(units_opt->answer, "map") == 0)
  121. G_verbose_message(_("Length in map units: %g"), length);
  122. else
  123. G_verbose_message(_("Length in meters: %g"), length);
  124. }
  125. if (vertices_opt->answer) {
  126. vertices = atoi(vertices_opt->answer);
  127. if (vertices < 2)
  128. G_fatal_error(_("Number of vertices must be at least 2"));
  129. }
  130. nosplit = nosplit_flag->answer;
  131. Vect_set_open_level(2);
  132. if (Vect_open_old2(&In, in_opt->answer, "", layer_opt->answer) < 0)
  133. G_fatal_error(_("Unable to open vector map <%s>"), in_opt->answer);
  134. layer = Vect_get_field_number(&In, layer_opt->answer);
  135. if (Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In)) < 0)
  136. G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
  137. Vect_copy_head_data(&In, &Out);
  138. Vect_hist_copy(&In, &Out);
  139. Vect_hist_command(&Out);
  140. Vect_copy_tables(&In, &Out, layer);
  141. Points = Vect_new_line_struct();
  142. Points2 = Vect_new_line_struct();
  143. Points3 = Vect_new_line_struct();
  144. Cats = Vect_new_cats_struct();
  145. nlines = Vect_get_num_lines(&In);
  146. for (line = 1; line <= nlines; line++) {
  147. int ltype;
  148. G_percent(line, nlines, 1);
  149. if (!Vect_line_alive(&In, line))
  150. continue;
  151. ltype = Vect_read_line(&In, Points, Cats, line);
  152. if (layer != -1 && !Vect_cat_get(Cats, layer, NULL))
  153. continue;
  154. if (ltype & GV_LINES) {
  155. if (length > 0) {
  156. double l, from, to, step;
  157. l = line_length(Points);
  158. if (l <= length) {
  159. Vect_write_line(&Out, ltype, Points, Cats);
  160. }
  161. else {
  162. long n, i;
  163. G_debug(3, "l: %f, length: %f", l, length);
  164. n = ceil(l / length);
  165. if (geodesic)
  166. l = Vect_line_length(Points);
  167. step = l / n;
  168. from = 0.;
  169. G_debug(3, "n: %ld, step: %f", n, step);
  170. if (nosplit)
  171. Vect_reset_line(Points3);
  172. for (i = 0; i < n; i++) {
  173. int ret;
  174. double x, y, z;
  175. if (i == n - 1) {
  176. to = l; /* to be sure that it goes to end */
  177. }
  178. else {
  179. to = from + step;
  180. }
  181. ret = Vect_line_segment(Points, from, to, Points2);
  182. if (ret == 0) {
  183. G_warning(_("Unable to make line segment: %f - %f (line length = %f)"),
  184. from, to, l);
  185. continue;
  186. }
  187. /* To be sure that the coordinates are identical */
  188. if (i > 0) {
  189. Points2->x[0] = x;
  190. Points2->y[0] = y;
  191. Points2->z[0] = z;
  192. }
  193. if (i == n - 1) {
  194. Points2->x[Points2->n_points - 1] =
  195. Points->x[Points->n_points - 1];
  196. Points2->y[Points2->n_points - 1] =
  197. Points->y[Points->n_points - 1];
  198. Points2->z[Points2->n_points - 1] =
  199. Points->z[Points->n_points - 1];
  200. }
  201. if (nosplit) {
  202. if (Points3->n_points > 0)
  203. Points3->n_points--;
  204. Vect_append_points(Points3, Points2, GV_FORWARD);
  205. }
  206. else
  207. Vect_write_line(&Out, ltype, Points2, Cats);
  208. /* last point */
  209. x = Points2->x[Points2->n_points - 1];
  210. y = Points2->y[Points2->n_points - 1];
  211. z = Points2->z[Points2->n_points - 1];
  212. from += step;
  213. }
  214. if (nosplit)
  215. Vect_write_line(&Out, ltype, Points3, Cats);
  216. }
  217. }
  218. else {
  219. int start = 0; /* number of coordinates written */
  220. while (start < Points->n_points - 1) {
  221. int i, v = 0;
  222. Vect_reset_line(Points2);
  223. for (i = 0; i < vertices; i++) {
  224. v = start + i;
  225. if (v == Points->n_points)
  226. break;
  227. Vect_append_point(Points2, Points->x[v], Points->y[v],
  228. Points->z[v]);
  229. }
  230. Vect_write_line(&Out, ltype, Points2, Cats);
  231. start = v;
  232. }
  233. }
  234. }
  235. else {
  236. Vect_write_line(&Out, ltype, Points, Cats);
  237. }
  238. }
  239. Vect_close(&In);
  240. Vect_build(&Out);
  241. Vect_close(&Out);
  242. exit(EXIT_SUCCESS);
  243. }