main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /****************************************************************************
  2. *
  3. * MODULE: v.patch
  4. * AUTHOR(S): Dave Gerdes, U.S.Army Construction Engineering Research Laboratory
  5. * (original contributor)
  6. * Radim Blazek <radim.blazek gmail.com> (update to GRASS 6)
  7. * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>,
  8. * Martin Landa <landa.martin gmail.com> (bbox)
  9. * PURPOSE:
  10. * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. /*
  18. ** v.patch input=file1,file2,.... output=composite
  19. **
  20. ** patch 2 or more vector maps together creating composite
  21. **
  22. **
  23. ** no checking is done for overlapping lines.
  24. ** header information will have to be editted afterwards.
  25. */
  26. /*
  27. ** Written by Dave Gerdes 8/1988, US Army Construction Engineering Research Lab
  28. ** Upgrade to 5.7 Radim Blazek
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <grass/gis.h>
  34. #include <grass/vector.h>
  35. #include <grass/dbmi.h>
  36. #include <grass/glocale.h>
  37. int patch(struct Map_info *, struct Map_info *, int, int *,
  38. struct Map_info *);
  39. int copy_records(dbDriver * driver_in, dbString * table_name_in,
  40. dbDriver * driver_out, dbString * table_name_out, int, int);
  41. int max_cat(struct Map_info *Map, int layer);
  42. int main(int argc, char *argv[])
  43. {
  44. int i, ret;
  45. char *in_name, *out_name, *bbox_name;
  46. struct GModule *module;
  47. struct Option *old, *new, *bbox;
  48. struct Flag *append, *table_flag, *no_topo;
  49. struct Map_info InMap, OutMap, BBoxMap;
  50. int n_files;
  51. int do_table;
  52. struct field_info *fi_in, *fi_out;
  53. dbString sql, table_name_in, table_name_out;
  54. dbDriver *driver_in, *driver_out;
  55. dbTable *table_in, *table_out;
  56. char *key = NULL;
  57. int keycol = -1;
  58. int maxcat = 0;
  59. int out_is_3d = WITHOUT_Z;
  60. G_gisinit(argv[0]);
  61. module = G_define_module();
  62. G_add_keyword(_("vector"));
  63. G_add_keyword(_("geometry"));
  64. module->description = _("Creates a new vector map "
  65. "by combining other vector maps.");
  66. old = G_define_standard_option(G_OPT_V_INPUTS);
  67. new = G_define_standard_option(G_OPT_V_OUTPUT);
  68. bbox = G_define_standard_option(G_OPT_V_OUTPUT);
  69. bbox->required = NO;
  70. bbox->key = "bbox";
  71. bbox->description =
  72. _("Name for output vector map where bounding boxes of input vector maps are written to");
  73. append = G_define_flag();
  74. append->key = 'a';
  75. append->description = _("Append files to existing file "
  76. "(overwriting existing files must be activated)");
  77. table_flag = G_define_flag();
  78. table_flag->key = 'e';
  79. table_flag->label = _("Copy also attribute table");
  80. table_flag->description =
  81. _("Only the table of layer 1 is currently supported");
  82. no_topo = G_define_standard_flag(G_FLG_V_TOPO);
  83. if (G_parser(argc, argv))
  84. exit(EXIT_FAILURE);
  85. out_name = new->answer;
  86. bbox_name = bbox->answer;
  87. do_table = table_flag->answer;
  88. db_init_string(&table_name_in);
  89. db_init_string(&table_name_out);
  90. db_init_string(&sql);
  91. i = 0;
  92. while (old->answers[i]) {
  93. in_name = old->answers[i++];
  94. Vect_check_input_output_name(in_name, new->answer, G_FATAL_EXIT);
  95. Vect_set_open_level(2);
  96. if (Vect_open_old_head(&InMap, in_name, "") < 0)
  97. G_fatal_error(_("Unable to open vector map <%s>"), in_name);
  98. if (out_is_3d != WITH_Z && Vect_is_3d(&InMap))
  99. out_is_3d = WITH_Z;
  100. Vect_close(&InMap);
  101. }
  102. table_out = NULL;
  103. fi_in = NULL;
  104. fi_out = NULL;
  105. /* Check input table structures */
  106. if (do_table) {
  107. if (append->answer) {
  108. Vect_set_open_level(1);
  109. if (Vect_open_old_head(&OutMap, out_name, G_mapset()) < 0)
  110. G_fatal_error(_("Unable to open vector map <%s>"), out_name);
  111. fi_out = Vect_get_field(&OutMap, 1);
  112. if (fi_out) {
  113. key = G_store(fi_out->key);
  114. driver_out =
  115. db_start_driver_open_database(fi_out->driver,
  116. fi_out->database);
  117. if (!driver_out) {
  118. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  119. fi_out->database, fi_out->driver);
  120. }
  121. db_set_error_handler_driver(driver_out);
  122. db_set_string(&table_name_out, fi_out->table);
  123. if (db_describe_table(driver_out, &table_name_out, &table_out)
  124. != DB_OK) {
  125. G_fatal_error(_("Unable to describe table <%s>"),
  126. fi_out->table);
  127. }
  128. db_close_database_shutdown_driver(driver_out);
  129. }
  130. Vect_close(&OutMap);
  131. }
  132. i = 0;
  133. while (old->answers[i]) {
  134. in_name = old->answers[i];
  135. Vect_set_open_level(1);
  136. if (Vect_open_old_head(&InMap, in_name, "") < 0)
  137. G_fatal_error(_("Unable to open vector map <%s>"), in_name);
  138. fi_in = Vect_get_field(&InMap, 1);
  139. table_in = NULL;
  140. if (fi_in) {
  141. dbTable **table;
  142. driver_in =
  143. db_start_driver_open_database(fi_in->driver,
  144. fi_in->database);
  145. if (!driver_in) {
  146. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  147. fi_in->database, fi_in->driver);
  148. }
  149. db_set_error_handler_driver(driver_in);
  150. if (!append->answer && i == 0) {
  151. table = &table_out;
  152. key = G_store(fi_in->key);
  153. }
  154. else {
  155. table = &table_in;
  156. }
  157. db_set_string(&table_name_in, fi_in->table);
  158. if (db_describe_table(driver_in, &table_name_in, table)
  159. != DB_OK) {
  160. G_fatal_error(_("Unable to describe table <%s>"),
  161. fi_in->table);
  162. }
  163. db_close_database_shutdown_driver(driver_in);
  164. }
  165. /* Check the structure */
  166. if (i > 0 || append->answer) {
  167. int ncols, col;
  168. if (!table_in ||
  169. (table_out && !table_in) || (!table_out && table_in)) {
  170. G_fatal_error(_("Missing table"));
  171. }
  172. if (G_strcasecmp(fi_in->key, key) != 0) {
  173. G_fatal_error(_("Key columns differ"));
  174. }
  175. ncols = db_get_table_number_of_columns(table_out);
  176. if (ncols != db_get_table_number_of_columns(table_in)) {
  177. G_fatal_error(_("Number of columns differ"));
  178. }
  179. for (col = 0; col < ncols; col++) {
  180. dbColumn *column_out, *column_in;
  181. int ctype_in, ctype_out;
  182. column_in = db_get_table_column(table_in, col);
  183. column_out = db_get_table_column(table_out, col);
  184. if (G_strcasecmp(db_get_column_name(column_in),
  185. db_get_column_name(column_out)) != 0) {
  186. G_fatal_error(_("Column names differ"));
  187. }
  188. ctype_in =
  189. db_sqltype_to_Ctype(db_get_column_sqltype(column_in));
  190. ctype_out =
  191. db_sqltype_to_Ctype(db_get_column_sqltype
  192. (column_out));
  193. if (ctype_in != ctype_out) {
  194. G_fatal_error(_("Column types differ"));
  195. }
  196. if (ctype_in == DB_C_TYPE_STRING &&
  197. db_get_column_length(column_in) !=
  198. db_get_column_length(column_out)) {
  199. G_fatal_error(_("Length of string columns differ"));
  200. }
  201. if (G_strcasecmp(key,
  202. db_get_column_name(column_out)) == 0) {
  203. keycol = col;
  204. }
  205. }
  206. }
  207. Vect_close(&InMap);
  208. i++;
  209. }
  210. if (keycol == -1) {
  211. G_fatal_error(_("Key column not found"));
  212. }
  213. }
  214. if (append->answer) {
  215. if (no_topo->answer)
  216. Vect_set_open_level(1);
  217. if (Vect_open_update(&OutMap, out_name, G_mapset()) < 0)
  218. G_fatal_error(_("Unable to open vector map <%s>"), out_name);
  219. if (out_is_3d == WITH_Z && !Vect_is_3d(&OutMap)) {
  220. G_warning(_("The output map is not 3D"));
  221. }
  222. maxcat = max_cat(&OutMap, 1);
  223. }
  224. else {
  225. if (Vect_open_new(&OutMap, out_name, out_is_3d) < 0)
  226. G_fatal_error(_("Unable to create vector map <%s>"), out_name);
  227. }
  228. if (bbox_name) {
  229. if (Vect_open_new(&BBoxMap, bbox_name, out_is_3d) < 0) /* TODO 3D */
  230. G_fatal_error(_("Unable to create vector map <%s>"), bbox_name);
  231. Vect_hist_command(&BBoxMap);
  232. }
  233. Vect_hist_command(&OutMap);
  234. driver_out = NULL;
  235. if (do_table) {
  236. if (append->answer) {
  237. fi_out = Vect_get_field(&OutMap, 1);
  238. }
  239. else {
  240. fi_out = Vect_default_field_info(&OutMap, 1, NULL, GV_1TABLE);
  241. fi_out->key = key;
  242. }
  243. if (fi_out) {
  244. driver_out =
  245. db_start_driver_open_database(fi_out->driver,
  246. Vect_subst_var(fi_out->database,
  247. &OutMap));
  248. if (!driver_out) {
  249. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  250. fi_out->database, fi_out->driver);
  251. }
  252. db_set_error_handler_driver(driver_out);
  253. db_begin_transaction(driver_out);
  254. }
  255. db_set_string(&table_name_out, fi_out->table);
  256. db_set_table_name(table_out, fi_out->table);
  257. if (!append->answer) {
  258. if (db_create_table(driver_out, table_out) != DB_OK) {
  259. G_fatal_error(_("Unable to create table <%s>"),
  260. fi_out->table);
  261. }
  262. /* do not allow duplicate keys */
  263. if (db_create_index2(driver_out, fi_out->table, fi_out->key) != DB_OK)
  264. G_warning(_("Unable to create index"));
  265. if (db_grant_on_table
  266. (driver_out, fi_out->table, DB_PRIV_SELECT,
  267. DB_GROUP | DB_PUBLIC) != DB_OK)
  268. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  269. fi_out->table);
  270. Vect_map_add_dblink(&OutMap, 1, NULL, fi_out->table,
  271. fi_in->key, fi_out->database, fi_out->driver);
  272. /* avoid Vect_subst_var() below */
  273. fi_out = Vect_get_field(&OutMap, 1);
  274. }
  275. }
  276. i = 0;
  277. while (old->answers[i]) {
  278. int add_cat;
  279. in_name = old->answers[i++];
  280. G_important_message(_("Patching vector map <%s>..."), in_name);
  281. if (bbox_name)
  282. Vect_set_open_level(2); /* needed for Vect_map_box() */
  283. else
  284. Vect_set_open_level(1);
  285. if (Vect_open_old(&InMap, in_name, "") < 0)
  286. G_fatal_error(_("Unable to open vector map <%s>"), in_name);
  287. /*first time around, copy first in head to out head */
  288. if (i == 1)
  289. Vect_copy_head_data(&InMap, &OutMap);
  290. if (do_table) {
  291. add_cat = maxcat + 1;
  292. }
  293. else {
  294. add_cat = 0;
  295. }
  296. G_debug(2, "maxcat = %d add_cat = %d", maxcat, add_cat);
  297. ret =
  298. patch(&InMap, &OutMap, add_cat, &maxcat,
  299. bbox_name ? &BBoxMap : NULL);
  300. if (ret < 0)
  301. G_warning(_("Error reading vector map <%s> - "
  302. "some data may not be correct"), in_name);
  303. if (do_table) {
  304. fi_in = Vect_get_field(&InMap, 1);
  305. if (fi_in) {
  306. /* SQLite does not like to have the same database opened twice */
  307. if (strcmp(fi_in->driver, fi_out->driver) == 0
  308. && strcmp(fi_in->database, fi_out->database) == 0) {
  309. G_debug(3, "Use the same driver");
  310. driver_in = driver_out;
  311. }
  312. else {
  313. driver_in =
  314. db_start_driver_open_database(fi_in->driver,
  315. fi_in->database);
  316. if (!driver_in) {
  317. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  318. fi_in->database, fi_in->driver);
  319. }
  320. db_set_error_handler_driver(driver_in);
  321. }
  322. db_set_string(&table_name_in, fi_in->table);
  323. copy_records(driver_in, &table_name_in,
  324. driver_out, &table_name_out, keycol, add_cat);
  325. if (driver_in != driver_out)
  326. db_close_database_shutdown_driver(driver_in);
  327. }
  328. }
  329. Vect_close(&InMap);
  330. }
  331. n_files = i;
  332. if (driver_out) {
  333. db_commit_transaction(driver_out);
  334. db_close_database_shutdown_driver(driver_out);
  335. }
  336. Vect_set_map_name(&OutMap, "Output from v.patch");
  337. Vect_set_person(&OutMap, G_whoami());
  338. if (!no_topo->answer)
  339. Vect_build(&OutMap);
  340. Vect_close(&OutMap);
  341. if (bbox_name) {
  342. Vect_set_map_name(&BBoxMap, "Output from v.patch (bounding boxes)");
  343. Vect_set_person(&BBoxMap, G_whoami());
  344. G_important_message(" ");
  345. G_important_message(_("Building topology for vector map <%s>..."),
  346. bbox_name);
  347. Vect_build(&BBoxMap);
  348. Vect_close(&BBoxMap);
  349. }
  350. G_message(_("Intersections at borders will have to be snapped"));
  351. G_message(_("Lines common between files will have to be edited"));
  352. G_message(_("The header information also may have to be edited"));
  353. G_done_msg(_("%d vector maps patched"), n_files);
  354. exit(EXIT_SUCCESS);
  355. }
  356. int copy_records(dbDriver * driver_in, dbString * table_name_in,
  357. dbDriver * driver_out, dbString * table_name_out,
  358. int keycol, int add_cat)
  359. {
  360. int ncols, col;
  361. dbCursor cursor;
  362. dbString value_str, sql;
  363. dbTable *table_in;
  364. db_init_string(&value_str);
  365. db_init_string(&sql);
  366. db_set_string(&sql, "select * from ");
  367. db_append_string(&sql, db_get_string(table_name_in));
  368. if (db_open_select_cursor(driver_in, &sql, &cursor, DB_SEQUENTIAL) !=
  369. DB_OK) {
  370. G_warning(_("Cannot open select cursor: '%s'"), db_get_string(&sql));
  371. return 0;
  372. }
  373. table_in = db_get_cursor_table(&cursor);
  374. ncols = db_get_table_number_of_columns(table_in);
  375. while (1) {
  376. int more;
  377. char buf[2000];
  378. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  379. db_close_cursor(&cursor);
  380. G_fatal_error(_("Cannot fetch row"));
  381. }
  382. if (!more)
  383. break;
  384. sprintf(buf, "insert into %s values ( ",
  385. db_get_string(table_name_out));
  386. db_set_string(&sql, buf);
  387. for (col = 0; col < ncols; col++) {
  388. int ctype, sqltype;
  389. dbColumn *column;
  390. dbValue *value;
  391. column = db_get_table_column(table_in, col);
  392. sqltype = db_get_column_sqltype(column);
  393. ctype = db_sqltype_to_Ctype(sqltype);
  394. value = db_get_column_value(column);
  395. if (col > 0)
  396. db_append_string(&sql, ", ");
  397. if (col == keycol) {
  398. db_set_value_int(value, db_get_value_int(value) + add_cat);
  399. }
  400. db_convert_value_to_string(value, sqltype, &value_str);
  401. switch (ctype) {
  402. case DB_C_TYPE_STRING:
  403. case DB_C_TYPE_DATETIME:
  404. if (db_test_value_isnull(value)) {
  405. db_append_string(&sql, "null");
  406. }
  407. else {
  408. db_double_quote_string(&value_str);
  409. sprintf(buf, "'%s'", db_get_string(&value_str));
  410. db_append_string(&sql, buf);
  411. }
  412. break;
  413. case DB_C_TYPE_INT:
  414. case DB_C_TYPE_DOUBLE:
  415. if (db_test_value_isnull(value)) {
  416. db_append_string(&sql, "null");
  417. }
  418. else {
  419. db_append_string(&sql, db_get_string(&value_str));
  420. }
  421. break;
  422. default:
  423. G_fatal_error(_("Unknown column type"));
  424. }
  425. }
  426. db_append_string(&sql, ")");
  427. G_debug(2, "SQL: %s", db_get_string(&sql));
  428. if (db_execute_immediate(driver_out, &sql) != DB_OK) {
  429. G_fatal_error(_("Cannot insert new record: '%s'"),
  430. db_get_string(&sql));
  431. }
  432. }
  433. db_close_cursor(&cursor);
  434. return 1;
  435. }
  436. int patch(struct Map_info *InMap, struct Map_info *OutMap, int add_cat,
  437. int *max_cat, struct Map_info *BBoxMap)
  438. {
  439. int type;
  440. struct line_pnts *Points;
  441. struct line_cats *Cats;
  442. *max_cat = add_cat;
  443. Points = Vect_new_line_struct();
  444. Cats = Vect_new_cats_struct();
  445. /* TODO:
  446. OutMap->head.orig_scale = GREATER (OutMap->head.orig_scale, InMap->head.orig_scale);
  447. OutMap->head.digit_thresh = 0;
  448. OutMap->head.map_thresh = GREATER (OutMap->head.map_thresh, InMap->head.map_thresh);
  449. */
  450. while ((type = Vect_read_next_line(InMap, Points, Cats)) > 0) {
  451. int i;
  452. for (i = 0; i < Cats->n_cats; i++) {
  453. if (Cats->field[i] == 1) {
  454. Cats->cat[i] += add_cat;
  455. if (Cats->cat[i] > *max_cat)
  456. *max_cat = Cats->cat[i];
  457. }
  458. }
  459. Vect_write_line(OutMap, type, Points, Cats);
  460. }
  461. if (BBoxMap) { /* inspired by v.in.region */
  462. struct bound_box box;
  463. double diff_long, mid_long;
  464. static int cat;
  465. Vect_get_map_box(InMap, &box);
  466. diff_long = box.E - box.W;
  467. mid_long = (box.W + box.E) / 2;
  468. /* rectangle */
  469. Vect_reset_cats(Cats);
  470. /* write each line, useful for snapping */
  471. Vect_reset_line(Points);
  472. Vect_append_point(Points, box.W, box.S, 0.0);
  473. if (Vect_get_proj(BBoxMap) == PROJECTION_LL && diff_long >= 179) {
  474. Vect_append_point(Points, mid_long, box.S, 0.0);
  475. }
  476. Vect_append_point(Points, box.E, box.S, 0.0);
  477. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  478. Vect_reset_line(Points);
  479. Vect_append_point(Points, box.E, box.S, 0.0);
  480. Vect_append_point(Points, box.E, box.N, 0.0);
  481. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  482. Vect_reset_line(Points);
  483. Vect_append_point(Points, box.E, box.N, 0.0);
  484. if (Vect_get_proj(BBoxMap) == PROJECTION_LL && diff_long >= 179) {
  485. Vect_append_point(Points, mid_long, box.N, 0.0);
  486. }
  487. Vect_append_point(Points, box.W, box.N, 0.0);
  488. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  489. Vect_reset_line(Points);
  490. Vect_append_point(Points, box.W, box.N, 0.0);
  491. Vect_append_point(Points, box.W, box.S, 0.0);
  492. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  493. /* centroid */
  494. Vect_reset_line(Points);
  495. Vect_cat_set(Cats, 1, ++cat); /* first layer */
  496. Vect_append_point(Points, (box.W + box.E) / 2, (box.S + box.N) / 2,
  497. 0.0);
  498. Vect_write_line(BBoxMap, GV_CENTROID, Points, Cats);
  499. }
  500. Vect_destroy_line_struct(Points);
  501. Vect_destroy_cats_struct(Cats);
  502. if (type != -2)
  503. return -1;
  504. return 0;
  505. }
  506. int max_cat(struct Map_info *Map, int layer)
  507. {
  508. struct line_cats *Cats;
  509. int max = 0;
  510. Cats = Vect_new_cats_struct();
  511. while (Vect_read_next_line(Map, NULL, Cats) > 0) {
  512. int i;
  513. for (i = 0; i < Cats->n_cats; i++) {
  514. if (Cats->field[i] == layer && Cats->cat[i] > max) {
  515. max = Cats->cat[i];
  516. }
  517. }
  518. }
  519. return max;
  520. }