main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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-2014 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;
  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. module->description =
  52. _("Creates points along input lines in new vector map with 2 layers.");
  53. opt.input = G_define_standard_option(G_OPT_V_INPUT);
  54. opt.lfield = G_define_standard_option(G_OPT_V_FIELD);
  55. opt.lfield->label = "Line layer number or name";
  56. opt.lfield->guisection = _("Selection");
  57. opt.type = G_define_standard_option(G_OPT_V3_TYPE);
  58. opt.type->answer = "point,line,boundary,centroid,face";
  59. opt.type->guisection = _("Selection");
  60. opt.output = G_define_standard_option(G_OPT_V_OUTPUT);
  61. opt.use = G_define_option();
  62. opt.use->key = "use";
  63. opt.use->type = TYPE_STRING;
  64. opt.use->required = NO;
  65. opt.use->description = _("Use line nodes or vertices only");
  66. opt.use->options = "node,vertex";
  67. opt.dmax = G_define_option();
  68. opt.dmax->key = "dmax";
  69. opt.dmax->type = TYPE_DOUBLE;
  70. opt.dmax->required = NO;
  71. opt.dmax->answer = "100";
  72. opt.dmax->description = _("Maximum distance between points in map units");
  73. flag.inter = G_define_flag();
  74. flag.inter->key = 'i';
  75. flag.inter->description = _("Interpolate points between line vertices (only for use=vertex)");
  76. flag.table = G_define_standard_flag(G_FLG_V_TABLE);
  77. if (G_parser(argc, argv))
  78. exit(EXIT_FAILURE);
  79. LCats = Vect_new_cats_struct();
  80. LPoints = Vect_new_line_struct();
  81. db_init_string(&stmt);
  82. type = Vect_option_to_types(opt.type);
  83. dmax = atof(opt.dmax->answer);
  84. vertex_type = 0;
  85. if (opt.use->answer) {
  86. if (opt.use->answer[0] == 'n')
  87. vertex_type = GV_NODE;
  88. else
  89. vertex_type = GV_VERTEX;
  90. }
  91. Vect_check_input_output_name(opt.input->answer, opt.output->answer,
  92. G_FATAL_EXIT);
  93. /* Open input lines */
  94. Vect_set_open_level(2);
  95. if (Vect_open_old2(&In, opt.input->answer, "", opt.lfield->answer) < 0)
  96. G_fatal_error(_("Unable to open vector map <%s>"), opt.input->answer);
  97. Vect_set_error_handler_io(&In, &Out);
  98. field = Vect_get_field_number(&In, opt.lfield->answer);
  99. /* Open output segments */
  100. if (Vect_open_new(&Out, opt.output->answer, Vect_is_3d(&In)) < 0)
  101. G_fatal_error(_("Unable to create vector map <%s>"),
  102. opt.output->answer);
  103. Vect_copy_head_data(&In, &Out);
  104. Vect_hist_copy(&In, &Out);
  105. Vect_hist_command(&Out);
  106. /* Table */
  107. Fi = NULL;
  108. if (!flag.table->answer) {
  109. struct field_info *Fin;
  110. /* copy input table */
  111. Fin = Vect_get_field(&In, field);
  112. if (Fin) { /* table defined */
  113. int ret;
  114. Fi = Vect_default_field_info(&Out, 1, NULL, GV_MTABLE);
  115. Vect_map_add_dblink(&Out, 1, NULL, Fi->table, Fin->key,
  116. Fi->database, Fi->driver);
  117. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  118. Fi->driver, Vect_subst_var(Fi->database,
  119. &Out), Fi->table);
  120. if (ret == DB_FAILED) {
  121. G_fatal_error(_("Unable to copy table <%s>"),
  122. Fin->table);
  123. }
  124. }
  125. Fi = Vect_default_field_info(&Out, 2, NULL, GV_MTABLE);
  126. Vect_map_add_dblink(&Out, 2, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  127. Fi->driver);
  128. /* Open driver */
  129. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  130. if (driver == NULL)
  131. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  132. Fi->database, Fi->driver);
  133. db_set_error_handler_driver(driver);
  134. if (field == -1)
  135. sprintf(buf,
  136. "create table %s ( cat int, along double precision )",
  137. Fi->table);
  138. else
  139. sprintf(buf,
  140. "create table %s ( cat int, lcat int, along double precision )",
  141. Fi->table);
  142. db_append_string(&stmt, buf);
  143. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  144. G_fatal_error(_("Unable to create table: '%s'"),
  145. db_get_string(&stmt));
  146. }
  147. if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK)
  148. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  149. Fi->table, GV_KEY_COLUMN);
  150. if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT,
  151. DB_GROUP | DB_PUBLIC) != DB_OK)
  152. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  153. Fi->table);
  154. db_begin_transaction(driver);
  155. }
  156. if (type & (GV_POINTS | GV_LINES | GV_FACE)) {
  157. int line, nlines, nskipped;
  158. nskipped = 0;
  159. nlines = Vect_get_num_lines(&In);
  160. for (line = 1; line <= nlines; line++) {
  161. int ltype, cat;
  162. G_debug(3, "line = %d", line);
  163. G_percent(line, nlines, 2);
  164. ltype = Vect_read_line(&In, LPoints, LCats, line);
  165. if (!(ltype & type))
  166. continue;
  167. if (!Vect_cat_get(LCats, field, &cat) && field != -1) {
  168. nskipped++;
  169. continue;
  170. }
  171. /* Assign CAT for layer 0 objects (i.e. boundaries) */
  172. if (field == -1)
  173. cat = -1;
  174. if (LPoints->n_points <= 1) {
  175. write_point(&Out, LPoints->x[0], LPoints->y[0], LPoints->z[0],
  176. cat, 0.0, driver, Fi);
  177. }
  178. else { /* lines */
  179. write_line(&Out, LPoints, cat, vertex_type,
  180. flag.inter->answer, dmax, driver, Fi);
  181. }
  182. }
  183. if (nskipped > 0)
  184. G_warning(_("%d features without category in layer <%d> skipped. "
  185. "Note that features without category (usually boundaries) are not "
  186. "skipped when '%s=-1' is given."),
  187. nskipped, field, opt.lfield->key);
  188. }
  189. if (type == GV_AREA) {
  190. int area, nareas, centroid, cat;
  191. nareas = Vect_get_num_areas(&In);
  192. for (area = 1; area <= nareas; area++) {
  193. int i, isle, nisles;
  194. G_percent(area, nareas, 2);
  195. centroid = Vect_get_area_centroid(&In, area);
  196. cat = -1;
  197. if (centroid > 0) {
  198. Vect_read_line(&In, NULL, LCats, centroid);
  199. if (!Vect_cat_get(LCats, field, &cat))
  200. continue;
  201. }
  202. Vect_get_area_points(&In, area, LPoints);
  203. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  204. dmax, driver, Fi);
  205. nisles = Vect_get_area_num_isles(&In, area);
  206. for (i = 0; i < nisles; i++) {
  207. isle = Vect_get_area_isle(&In, area, i);
  208. Vect_get_isle_points(&In, isle, LPoints);
  209. write_line(&Out, LPoints, cat, vertex_type,
  210. flag.inter->answer, dmax, driver, Fi);
  211. }
  212. }
  213. }
  214. if (!flag.table->answer) {
  215. db_commit_transaction(driver);
  216. db_close_database_shutdown_driver(driver);
  217. }
  218. Vect_build(&Out);
  219. /* Free, close ... */
  220. Vect_close(&In);
  221. G_done_msg(_("%d points written to output vector map."),
  222. Vect_get_num_primitives(&Out, GV_POINT));
  223. Vect_close(&Out);
  224. exit(EXIT_SUCCESS);
  225. }