main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 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. /*
  25. * local macros
  26. */
  27. #define GV_NODE 1
  28. #define GV_VERTEX 2
  29. static int point_cat;
  30. static struct line_cats *PCats;
  31. static struct line_pnts *PPoints;
  32. static dbString stmt;
  33. static dbDriver *driver;
  34. static struct field_info *Fi;
  35. void write_point(struct Map_info *Out, double x, double y, double z,
  36. int line_cat, double along, int table)
  37. {
  38. char buf[2000];
  39. G_debug(3, "write_point()");
  40. Vect_reset_line(PPoints);
  41. Vect_reset_cats(PCats);
  42. /* Write point */
  43. Vect_append_point(PPoints, x, y, z);
  44. if (line_cat > 0) {
  45. Vect_cat_set(PCats, 1, line_cat);
  46. Vect_cat_set(PCats, 2, point_cat);
  47. }
  48. else {
  49. Vect_cat_set(PCats, 2, point_cat);
  50. }
  51. Vect_write_line(Out, GV_POINT, PPoints, PCats);
  52. /* Attributes */
  53. if (!table) {
  54. db_zero_string(&stmt);
  55. if (line_cat > 0)
  56. sprintf(buf, "insert into %s values ( %d, %d, %.15g )", Fi->table,
  57. point_cat, line_cat, along);
  58. else
  59. sprintf(buf, "insert into %s values ( %d, %.15g )", Fi->table,
  60. point_cat, along);
  61. db_append_string(&stmt, buf);
  62. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  63. G_warning(_("Unable to insert new record: '%s'"),
  64. db_get_string(&stmt));
  65. }
  66. }
  67. point_cat++;
  68. }
  69. void write_line(struct Map_info *Out, struct line_pnts *LPoints, int cat,
  70. int vertex, int interpolate, double dmax, int table)
  71. {
  72. if (vertex == GV_VERTEX || vertex == GV_NODE) { /* use line vertices */
  73. double along;
  74. int vert;
  75. along = 0;
  76. for (vert = 0; vert < LPoints->n_points; vert++) {
  77. G_debug(3, "vert = %d", vert);
  78. if (vertex == GV_VERTEX ||
  79. (vertex == GV_NODE &&
  80. (vert == 0 || vert == LPoints->n_points - 1))) {
  81. write_point(Out, LPoints->x[vert], LPoints->y[vert],
  82. LPoints->z[vert], cat, along, table);
  83. }
  84. if (vert < LPoints->n_points - 1) {
  85. double dx, dy, dz, len;
  86. dx = LPoints->x[vert + 1] - LPoints->x[vert];
  87. dy = LPoints->y[vert + 1] - LPoints->y[vert];
  88. dz = LPoints->z[vert + 1] - LPoints->z[vert];
  89. len = hypot(hypot(dx, dy), dz);
  90. /* interpolate segment */
  91. if (interpolate && vert < (LPoints->n_points - 1)) {
  92. int i, n;
  93. double x, y, z, dlen;
  94. if (len > dmax) {
  95. n = len / dmax + 1; /* number of segments */
  96. dx /= n;
  97. dy /= n;
  98. dz /= n;
  99. dlen = len / n;
  100. for (i = 1; i < n; i++) {
  101. x = LPoints->x[vert] + i * dx;
  102. y = LPoints->y[vert] + i * dy;
  103. z = LPoints->z[vert] + i * dz;
  104. write_point(Out, x, y, z, cat, along + i * dlen,
  105. table);
  106. }
  107. }
  108. }
  109. along += len;
  110. }
  111. }
  112. }
  113. else { /* do not use vertices */
  114. int i, n;
  115. double len, dlen, along, x, y, z;
  116. len = Vect_line_length(LPoints);
  117. n = len / dmax + 1; /* number of segments */
  118. dlen = len / n; /* length of segment */
  119. G_debug(3, "n = %d len = %f dlen = %f", n, len, dlen);
  120. for (i = 0; i <= n; i++) {
  121. if (i > 0 && i < n) {
  122. along = i * dlen;
  123. Vect_point_on_line(LPoints, along, &x, &y, &z, NULL, NULL);
  124. }
  125. else { /* first and last vertex */
  126. if (i == 0) {
  127. along = 0;
  128. x = LPoints->x[0];
  129. y = LPoints->y[0];
  130. z = LPoints->z[0];
  131. }
  132. else { /* last */
  133. along = len;
  134. x = LPoints->x[LPoints->n_points - 1];
  135. y = LPoints->y[LPoints->n_points - 1];
  136. z = LPoints->z[LPoints->n_points - 1];
  137. }
  138. }
  139. G_debug(3, " i = %d along = %f", i, along);
  140. write_point(Out, x, y, z, cat, along, table);
  141. }
  142. }
  143. }
  144. int main(int argc, char **argv)
  145. {
  146. int field, type, vertex_type;
  147. double dmax;
  148. struct Option *in_opt, *out_opt, *type_opt, *dmax_opt, *lfield_opt;
  149. struct Flag *inter_flag, *vertex_flag, *table_flag, *node_flag;
  150. struct GModule *module;
  151. struct Map_info In, Out;
  152. struct line_cats *LCats;
  153. struct line_pnts *LPoints;
  154. char buf[2000];
  155. G_gisinit(argv[0]);
  156. module = G_define_module();
  157. G_add_keyword(_("vector"));
  158. G_add_keyword(_("geometry"));
  159. module->description =
  160. _("Creates points along input lines in new vector map with 2 layers.");
  161. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  162. in_opt->label = _("Name of vector map containing lines");
  163. lfield_opt = G_define_standard_option(G_OPT_V_FIELD);
  164. lfield_opt->key = "llayer";
  165. lfield_opt->answer = "1";
  166. lfield_opt->label = "Line layer number or name";
  167. lfield_opt->guisection = _("Selection");
  168. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  169. type_opt->answer = "point,line,boundary,centroid";
  170. type_opt->guisection = _("Selection");
  171. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  172. out_opt->description =
  173. _("Name for output vector map where points will be written");
  174. node_flag = G_define_flag();
  175. node_flag->key = 'n';
  176. node_flag->description = _("Write line nodes");
  177. vertex_flag = G_define_flag();
  178. vertex_flag->key = 'v';
  179. vertex_flag->description = _("Write line vertices");
  180. inter_flag = G_define_flag();
  181. inter_flag->key = 'i';
  182. inter_flag->description = _("Interpolate points between line vertices");
  183. dmax_opt = G_define_option();
  184. dmax_opt->key = "dmax";
  185. dmax_opt->type = TYPE_DOUBLE;
  186. dmax_opt->required = NO;
  187. dmax_opt->answer = "100";
  188. dmax_opt->description = _("Maximum distance between points in map units");
  189. table_flag = G_define_standard_flag(G_FLG_V_TABLE);
  190. if (G_parser(argc, argv))
  191. exit(EXIT_FAILURE);
  192. LCats = Vect_new_cats_struct();
  193. PCats = Vect_new_cats_struct();
  194. LPoints = Vect_new_line_struct();
  195. PPoints = Vect_new_line_struct();
  196. db_init_string(&stmt);
  197. type = Vect_option_to_types(type_opt);
  198. dmax = atof(dmax_opt->answer);
  199. if (node_flag->answer && vertex_flag->answer)
  200. G_fatal_error(_("Use either -n or -v flag, not both"));
  201. if (node_flag->answer)
  202. vertex_type = GV_NODE;
  203. else if (vertex_flag->answer)
  204. vertex_type = GV_VERTEX;
  205. else
  206. vertex_type = 0;
  207. Vect_check_input_output_name(in_opt->answer, out_opt->answer,
  208. G_FATAL_EXIT);
  209. /* Open input lines */
  210. Vect_set_open_level(2);
  211. Vect_open_old2(&In, in_opt->answer, "", lfield_opt->answer);
  212. field = Vect_get_field_number(&In, lfield_opt->answer);
  213. /* Open output segments */
  214. Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In));
  215. Vect_copy_head_data(&In, &Out);
  216. Vect_hist_copy(&In, &Out);
  217. Vect_hist_command(&Out);
  218. /* Table */
  219. if (!table_flag->answer) {
  220. struct field_info *Fin;
  221. /* copy input table */
  222. Fin = Vect_get_field(&In, field);
  223. if (Fin) { /* table defined */
  224. int ret;
  225. Fi = Vect_default_field_info(&Out, 1, NULL, GV_MTABLE);
  226. Vect_map_add_dblink(&Out, 1, NULL, Fi->table, Fin->key,
  227. Fi->database, Fi->driver);
  228. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  229. Fi->driver, Vect_subst_var(Fi->database,
  230. &Out), Fi->table);
  231. if (ret == DB_FAILED) {
  232. G_fatal_error(_("Unable to copy table <%s>"),
  233. Fin->table);
  234. }
  235. }
  236. Fi = Vect_default_field_info(&Out, 2, NULL, GV_MTABLE);
  237. Vect_map_add_dblink(&Out, 2, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  238. Fi->driver);
  239. /* Open driver */
  240. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  241. if (driver == NULL)
  242. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  243. Fi->database, Fi->driver);
  244. if (field == -1)
  245. sprintf(buf,
  246. "create table %s ( cat int, along double precision )",
  247. Fi->table);
  248. else
  249. sprintf(buf,
  250. "create table %s ( cat int, lcat int, along double precision )",
  251. Fi->table);
  252. db_append_string(&stmt, buf);
  253. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  254. db_close_database_shutdown_driver(driver);
  255. G_fatal_error(_("Unable to create table: '%s'"),
  256. db_get_string(&stmt));
  257. }
  258. if (db_create_index2(driver, Fi->table, GV_KEY_COLUMN) != DB_OK)
  259. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  260. Fi->table, GV_KEY_COLUMN);
  261. if (db_grant_on_table
  262. (driver, Fi->table, DB_PRIV_SELECT,
  263. DB_GROUP | DB_PUBLIC) != DB_OK)
  264. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  265. Fi->table);
  266. db_begin_transaction(driver);
  267. }
  268. point_cat = 1;
  269. if (type & (GV_POINTS | GV_LINES)) {
  270. int line, nlines;
  271. nlines = Vect_get_num_lines(&In);
  272. for (line = 1; line <= nlines; line++) {
  273. int ltype, cat;
  274. G_debug(3, "line = %d", line);
  275. ltype = Vect_read_line(&In, LPoints, LCats, line);
  276. if (!(ltype & type))
  277. continue;
  278. if (!Vect_cat_get(LCats, field, &cat) && field != -1)
  279. continue;
  280. /* Assign CAT for layer 0 objects (i.e. boundaries) */
  281. if (field == -1)
  282. cat = -1;
  283. if (LPoints->n_points <= 1) {
  284. write_point(&Out, LPoints->x[0], LPoints->y[0], LPoints->z[0],
  285. cat, 0.0, table_flag->answer);
  286. }
  287. else { /* lines */
  288. write_line(&Out, LPoints, cat, vertex_type,
  289. inter_flag->answer, dmax, table_flag->answer);
  290. }
  291. G_percent(line, nlines, 2);
  292. }
  293. }
  294. if (type == GV_AREA) {
  295. int area, nareas, centroid, cat;
  296. nareas = Vect_get_num_areas(&In);
  297. for (area = 1; area <= nareas; area++) {
  298. int i, isle, nisles;
  299. centroid = Vect_get_area_centroid(&In, area);
  300. cat = -1;
  301. if (centroid > 0) {
  302. Vect_read_line(&In, NULL, LCats, centroid);
  303. if (!Vect_cat_get(LCats, field, &cat))
  304. continue;
  305. }
  306. Vect_get_area_points(&In, area, LPoints);
  307. write_line(&Out, LPoints, cat, vertex_type, inter_flag->answer,
  308. dmax, table_flag->answer);
  309. nisles = Vect_get_area_num_isles(&In, area);
  310. for (i = 0; i < nisles; i++) {
  311. isle = Vect_get_area_isle(&In, area, i);
  312. Vect_get_isle_points(&In, isle, LPoints);
  313. write_line(&Out, LPoints, cat, vertex_type,
  314. inter_flag->answer, dmax, table_flag->answer);
  315. }
  316. G_percent(area, nareas, 2);
  317. }
  318. }
  319. if (!table_flag->answer) {
  320. db_commit_transaction(driver);
  321. db_close_database_shutdown_driver(driver);
  322. }
  323. Vect_build(&Out);
  324. /* Free, close ... */
  325. Vect_close(&In);
  326. Vect_close(&Out);
  327. G_done_msg(_("%d points written to output vector map."), point_cat - 1);
  328. exit(EXIT_SUCCESS);
  329. }