main.c 7.9 KB

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