main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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-2010, 2013 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. Vect_open_old2(&In, opt.input->answer, "", opt.lfield->answer);
  97. Vect_set_error_handler_io(&In, &Out);
  98. field = Vect_get_field_number(&In, opt.lfield->answer);
  99. /* Open output segments */
  100. Vect_open_new(&Out, opt.output->answer, Vect_is_3d(&In));
  101. Vect_copy_head_data(&In, &Out);
  102. Vect_hist_copy(&In, &Out);
  103. Vect_hist_command(&Out);
  104. /* Table */
  105. Fi = NULL;
  106. if (!flag.table->answer) {
  107. struct field_info *Fin;
  108. /* copy input table */
  109. Fin = Vect_get_field(&In, field);
  110. if (Fin) { /* table defined */
  111. int ret;
  112. Fi = Vect_default_field_info(&Out, 1, NULL, GV_MTABLE);
  113. Vect_map_add_dblink(&Out, 1, NULL, Fi->table, Fin->key,
  114. Fi->database, Fi->driver);
  115. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  116. Fi->driver, Vect_subst_var(Fi->database,
  117. &Out), Fi->table);
  118. if (ret == DB_FAILED) {
  119. G_fatal_error(_("Unable to copy table <%s>"),
  120. Fin->table);
  121. }
  122. }
  123. Fi = Vect_default_field_info(&Out, 2, NULL, GV_MTABLE);
  124. Vect_map_add_dblink(&Out, 2, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  125. Fi->driver);
  126. /* Open driver */
  127. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  128. if (driver == NULL)
  129. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  130. Fi->database, Fi->driver);
  131. db_set_error_handler_driver(driver);
  132. if (field == -1)
  133. sprintf(buf,
  134. "create table %s ( cat int, along double precision )",
  135. Fi->table);
  136. else
  137. sprintf(buf,
  138. "create table %s ( cat int, lcat int, along double precision )",
  139. Fi->table);
  140. db_append_string(&stmt, buf);
  141. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  142. G_fatal_error(_("Unable to create table: '%s'"),
  143. db_get_string(&stmt));
  144. }
  145. if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK)
  146. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  147. Fi->table, GV_KEY_COLUMN);
  148. if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT,
  149. DB_GROUP | DB_PUBLIC) != DB_OK)
  150. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  151. Fi->table);
  152. db_begin_transaction(driver);
  153. }
  154. if (type & (GV_POINTS | GV_LINES | GV_FACE)) {
  155. int line, nlines;
  156. nlines = Vect_get_num_lines(&In);
  157. for (line = 1; line <= nlines; line++) {
  158. int ltype, cat;
  159. G_debug(3, "line = %d", line);
  160. G_percent(line, nlines, 2);
  161. ltype = Vect_read_line(&In, LPoints, LCats, line);
  162. if (!(ltype & type))
  163. continue;
  164. if (!Vect_cat_get(LCats, field, &cat) && field != -1)
  165. continue;
  166. /* Assign CAT for layer 0 objects (i.e. boundaries) */
  167. if (field == -1)
  168. cat = -1;
  169. if (LPoints->n_points <= 1) {
  170. write_point(&Out, LPoints->x[0], LPoints->y[0], LPoints->z[0],
  171. cat, 0.0, driver, Fi);
  172. }
  173. else { /* lines */
  174. write_line(&Out, LPoints, cat, vertex_type,
  175. flag.inter->answer, dmax, driver, Fi);
  176. }
  177. }
  178. }
  179. if (type == GV_AREA) {
  180. int area, nareas, centroid, cat;
  181. nareas = Vect_get_num_areas(&In);
  182. for (area = 1; area <= nareas; area++) {
  183. int i, isle, nisles;
  184. G_percent(area, nareas, 2);
  185. centroid = Vect_get_area_centroid(&In, area);
  186. cat = -1;
  187. if (centroid > 0) {
  188. Vect_read_line(&In, NULL, LCats, centroid);
  189. if (!Vect_cat_get(LCats, field, &cat))
  190. continue;
  191. }
  192. Vect_get_area_points(&In, area, LPoints);
  193. write_line(&Out, LPoints, cat, vertex_type, flag.inter->answer,
  194. dmax, driver, Fi);
  195. nisles = Vect_get_area_num_isles(&In, area);
  196. for (i = 0; i < nisles; i++) {
  197. isle = Vect_get_area_isle(&In, area, i);
  198. Vect_get_isle_points(&In, isle, LPoints);
  199. write_line(&Out, LPoints, cat, vertex_type,
  200. flag.inter->answer, dmax, driver, Fi);
  201. }
  202. }
  203. }
  204. if (!flag.table->answer) {
  205. db_commit_transaction(driver);
  206. db_close_database_shutdown_driver(driver);
  207. }
  208. Vect_build(&Out);
  209. /* Free, close ... */
  210. Vect_close(&In);
  211. G_done_msg(_("%d points written to output vector map."),
  212. Vect_get_num_primitives(&Out, GV_POINT));
  213. Vect_close(&Out);
  214. exit(EXIT_SUCCESS);
  215. }