main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /***************************************************************
  2. *
  3. * MODULE: v.to.points
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * OGR support by Martin Landa <landa.martin gmail.com>
  7. *
  8. * PURPOSE: Create points along lines
  9. *
  10. * COPYRIGHT: (C) 2002-2017 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Public License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. *
  16. **************************************************************/
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/glocale.h>
  24. #include "local_proto.h"
  25. int main(int argc, char **argv)
  26. {
  27. int field, type, vertex_type;
  28. double dmax;
  29. char buf[DB_SQL_MAX];
  30. struct {
  31. struct Option *input, *output, *type, *dmax, *lfield, *use;
  32. } opt;
  33. struct {
  34. struct Flag *table, *inter, *percent, *reverse;
  35. } flag;
  36. struct GModule *module;
  37. struct Map_info In, Out;
  38. struct line_cats *LCats;
  39. struct line_pnts *LPoints;
  40. dbDriver *driver;
  41. struct field_info *Fi;
  42. dbString stmt;
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("vector"));
  46. G_add_keyword(_("geometry"));
  47. G_add_keyword("3D");
  48. G_add_keyword(_("line"));
  49. G_add_keyword(_("node"));
  50. G_add_keyword(_("vertex"));
  51. G_add_keyword(_("point"));
  52. module->description =
  53. _("Creates points along input lines in new vector map with 2 layers.");
  54. opt.input = G_define_standard_option(G_OPT_V_INPUT);
  55. opt.lfield = G_define_standard_option(G_OPT_V_FIELD);
  56. opt.lfield->label = "Line layer number or name";
  57. opt.lfield->guisection = _("Selection");
  58. opt.type = G_define_standard_option(G_OPT_V3_TYPE);
  59. opt.type->answer = "point,line,boundary,centroid,face";
  60. opt.type->guisection = _("Selection");
  61. opt.output = G_define_standard_option(G_OPT_V_OUTPUT);
  62. opt.use = G_define_option();
  63. opt.use->key = "use";
  64. opt.use->type = TYPE_STRING;
  65. opt.use->required = NO;
  66. opt.use->description = _("Use line nodes or vertices only");
  67. opt.use->options = "node,vertex";
  68. opt.dmax = G_define_option();
  69. opt.dmax->key = "dmax";
  70. opt.dmax->type = TYPE_DOUBLE;
  71. opt.dmax->required = NO;
  72. opt.dmax->answer = "100";
  73. opt.dmax->description = _("Maximum distance between points in map units or percentage with -p");
  74. flag.inter = G_define_flag();
  75. flag.inter->key = 'i';
  76. flag.inter->description = _("Interpolate points between line vertices (only for use=vertex)");
  77. flag.percent = G_define_flag();
  78. flag.percent->key = 'p';
  79. flag.percent->description = _("Use dmax as percentage of line length");
  80. flag.reverse = G_define_flag();
  81. flag.reverse->key = 'r';
  82. flag.reverse->description = _("Start from the end node");
  83. flag.table = G_define_standard_flag(G_FLG_V_TABLE);
  84. if (G_parser(argc, argv))
  85. exit(EXIT_FAILURE);
  86. LCats = Vect_new_cats_struct();
  87. LPoints = Vect_new_line_struct();
  88. db_init_string(&stmt);
  89. type = Vect_option_to_types(opt.type);
  90. dmax = atof(opt.dmax->answer);
  91. if (dmax <= 0)
  92. G_fatal_error(_("Option %s must be positive"), opt.dmax->key);
  93. vertex_type = 0;
  94. if (opt.use->answer) {
  95. if (opt.use->answer[0] == 'n')
  96. vertex_type = GV_NODE;
  97. else
  98. vertex_type = GV_VERTEX;
  99. }
  100. Vect_check_input_output_name(opt.input->answer, opt.output->answer,
  101. G_FATAL_EXIT);
  102. /* Open input lines */
  103. Vect_set_open_level(2);
  104. if (Vect_open_old2(&In, opt.input->answer, "", opt.lfield->answer) < 0)
  105. G_fatal_error(_("Unable to open vector map <%s>"), opt.input->answer);
  106. Vect_set_error_handler_io(&In, &Out);
  107. field = Vect_get_field_number(&In, opt.lfield->answer);
  108. /* Open output segments */
  109. if (Vect_open_new(&Out, opt.output->answer, Vect_is_3d(&In)) < 0)
  110. G_fatal_error(_("Unable to create vector map <%s>"),
  111. opt.output->answer);
  112. Vect_copy_head_data(&In, &Out);
  113. Vect_hist_copy(&In, &Out);
  114. Vect_hist_command(&Out);
  115. /* Table */
  116. driver = NULL;
  117. Fi = NULL;
  118. if (!flag.table->answer) {
  119. struct field_info *Fin;
  120. /* copy input table */
  121. Fin = Vect_get_field(&In, field);
  122. if (Fin) { /* table defined */
  123. int ret;
  124. Fi = Vect_default_field_info(&Out, 1, NULL, GV_MTABLE);
  125. Vect_map_add_dblink(&Out, 1, NULL, Fi->table, Fin->key,
  126. Fi->database, Fi->driver);
  127. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  128. Fi->driver, Vect_subst_var(Fi->database,
  129. &Out), Fi->table);
  130. if (ret == DB_FAILED) {
  131. G_fatal_error(_("Unable to copy table <%s>"),
  132. Fin->table);
  133. }
  134. }
  135. Fi = Vect_default_field_info(&Out, 2, NULL, GV_MTABLE);
  136. Vect_map_add_dblink(&Out, 2, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  137. Fi->driver);
  138. /* Open driver */
  139. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  140. if (driver == NULL)
  141. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  142. Fi->database, Fi->driver);
  143. db_set_error_handler_driver(driver);
  144. if (field == -1)
  145. sprintf(buf,
  146. "create table %s ( cat int, along double precision )",
  147. Fi->table);
  148. else
  149. sprintf(buf,
  150. "create table %s ( cat int, lcat int, along double precision )",
  151. Fi->table);
  152. db_append_string(&stmt, buf);
  153. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  154. G_fatal_error(_("Unable to create table: '%s'"),
  155. db_get_string(&stmt));
  156. }
  157. if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK)
  158. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  159. Fi->table, GV_KEY_COLUMN);
  160. if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT,
  161. DB_GROUP | DB_PUBLIC) != DB_OK)
  162. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  163. Fi->table);
  164. db_begin_transaction(driver);
  165. }
  166. if (type & (GV_POINTS | GV_LINES | GV_FACE)) {
  167. int line, nlines, nskipped;
  168. nskipped = 0;
  169. nlines = Vect_get_num_lines(&In);
  170. for (line = 1; line <= nlines; line++) {
  171. int ltype, cat;
  172. G_debug(3, "line = %d", line);
  173. G_percent(line, nlines, 2);
  174. ltype = Vect_read_line(&In, LPoints, LCats, line);
  175. if (!(ltype & type))
  176. continue;
  177. if (!Vect_cat_get(LCats, field, &cat) && field != -1) {
  178. nskipped++;
  179. continue;
  180. }
  181. /* Assign CAT for layer 0 objects (i.e. boundaries) */
  182. if (field == -1)
  183. cat = -1;
  184. if (LPoints->n_points <= 1) {
  185. write_point(&Out, LPoints->x[0], LPoints->y[0], LPoints->z[0],
  186. cat, 0.0, driver, Fi);
  187. }
  188. else if (flag.percent->answer) { /* lines */
  189. double dmaxlen = Vect_line_length(LPoints) * dmax / 100.0;
  190. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  191. flag.reverse->answer, dmaxlen, driver, Fi);
  192. } else {
  193. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  194. flag.reverse->answer, dmax, driver, Fi);
  195. }
  196. }
  197. if (nskipped > 0)
  198. G_warning(_("%d features without category in layer <%d> skipped. "
  199. "Note that features without category (usually boundaries) are not "
  200. "skipped when '%s=-1' is given."),
  201. nskipped, field, opt.lfield->key);
  202. }
  203. if (type == GV_AREA) {
  204. int area, nareas, centroid, cat;
  205. nareas = Vect_get_num_areas(&In);
  206. for (area = 1; area <= nareas; area++) {
  207. int i, isle, nisles;
  208. G_percent(area, nareas, 2);
  209. centroid = Vect_get_area_centroid(&In, area);
  210. cat = -1;
  211. if (centroid > 0) {
  212. Vect_read_line(&In, NULL, LCats, centroid);
  213. if (!Vect_cat_get(LCats, field, &cat))
  214. continue;
  215. }
  216. Vect_get_area_points(&In, area, LPoints);
  217. if (flag.percent->answer) {
  218. double dmaxlen = Vect_line_length(LPoints) * dmax / 100.0;
  219. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  220. flag.reverse->answer, dmaxlen, driver, Fi);
  221. } else {
  222. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  223. flag.reverse->answer, dmax, driver, Fi);
  224. }
  225. nisles = Vect_get_area_num_isles(&In, area);
  226. for (i = 0; i < nisles; i++) {
  227. isle = Vect_get_area_isle(&In, area, i);
  228. Vect_get_isle_points(&In, isle, LPoints);
  229. if (flag.percent->answer) {
  230. double dmaxlen = Vect_line_length(LPoints) * dmax / 100.0;
  231. write_line(&Out, LPoints, cat, vertex_type,
  232. flag.inter->answer, flag.reverse->answer,
  233. dmaxlen, driver, Fi);
  234. } else {
  235. write_line(&Out, LPoints, cat, vertex_type,
  236. flag.inter->answer, flag.reverse->answer, dmax,
  237. driver, Fi);
  238. }
  239. }
  240. }
  241. }
  242. if (!flag.table->answer) {
  243. db_commit_transaction(driver);
  244. db_close_database_shutdown_driver(driver);
  245. }
  246. Vect_build(&Out);
  247. /* Free, close ... */
  248. Vect_close(&In);
  249. G_done_msg(_("%d points written to output vector map."),
  250. Vect_get_num_primitives(&Out, GV_POINT));
  251. Vect_close(&Out);
  252. exit(EXIT_SUCCESS);
  253. }