main.c 10 KB

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