main.c 8.0 KB

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