main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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;
  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. if (G_parser(argc, argv))
  83. exit(EXIT_FAILURE);
  84. out_name = new->answer;
  85. bbox_name = bbox->answer;
  86. do_table = table_flag->answer;
  87. db_init_string(&table_name_in);
  88. db_init_string(&table_name_out);
  89. db_init_string(&sql);
  90. i = 0;
  91. while (old->answers[i]) {
  92. in_name = old->answers[i++];
  93. Vect_check_input_output_name(in_name, new->answer, GV_FATAL_EXIT);
  94. Vect_set_open_level(2);
  95. Vect_open_old_head(&InMap, in_name, "");
  96. if (out_is_3d != WITH_Z && Vect_is_3d(&InMap))
  97. out_is_3d = WITH_Z;
  98. Vect_close(&InMap);
  99. }
  100. table_out = NULL;
  101. /* Check input table structures */
  102. if (do_table) {
  103. if (append->answer) {
  104. Vect_set_open_level(1);
  105. Vect_open_old_head(&OutMap, out_name, G_mapset());
  106. fi_out = Vect_get_field(&OutMap, 1);
  107. if (fi_out) {
  108. key = G_store(fi_out->key);
  109. driver_out =
  110. db_start_driver_open_database(fi_out->driver,
  111. fi_out->database);
  112. if (!driver_out) {
  113. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  114. fi_out->database, fi_out->driver);
  115. }
  116. db_set_string(&table_name_out, fi_out->table);
  117. if (db_describe_table(driver_out, &table_name_out, &table_out)
  118. != DB_OK) {
  119. G_fatal_error(_("Unable to describe table <%s>"),
  120. fi_out->table);
  121. }
  122. db_close_database_shutdown_driver(driver_out);
  123. }
  124. Vect_close(&OutMap);
  125. }
  126. i = 0;
  127. while (old->answers[i]) {
  128. in_name = old->answers[i];
  129. Vect_set_open_level(1);
  130. Vect_open_old_head(&InMap, in_name, "");
  131. fi_in = Vect_get_field(&InMap, 1);
  132. table_in = NULL;
  133. if (fi_in) {
  134. dbTable **table;
  135. driver_in =
  136. db_start_driver_open_database(fi_in->driver,
  137. fi_in->database);
  138. if (!driver_in) {
  139. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  140. fi_in->database, fi_in->driver);
  141. }
  142. if (!append->answer && i == 0) {
  143. table = &table_out;
  144. key = G_store(fi_in->key);
  145. }
  146. else {
  147. table = &table_in;
  148. }
  149. db_set_string(&table_name_in, fi_in->table);
  150. if (db_describe_table(driver_in, &table_name_in, table)
  151. != DB_OK) {
  152. G_fatal_error(_("Unable to describe table <%s>"),
  153. fi_in->table);
  154. }
  155. db_close_database_shutdown_driver(driver_in);
  156. }
  157. /* Check the structure */
  158. if (i > 0 || append->answer) {
  159. int ncols, col;
  160. if (!table_in ||
  161. (table_out && !table_in) || (!table_out && table_in)) {
  162. G_fatal_error(_("Missing table"));
  163. }
  164. if (G_strcasecmp(fi_in->key, key) != 0) {
  165. G_fatal_error(_("Key columns differ"));
  166. }
  167. ncols = db_get_table_number_of_columns(table_out);
  168. if (ncols != db_get_table_number_of_columns(table_in)) {
  169. G_fatal_error(_("Number of columns differ"));
  170. }
  171. for (col = 0; col < ncols; col++) {
  172. dbColumn *column_out, *column_in;
  173. int ctype_in, ctype_out;
  174. column_in = db_get_table_column(table_in, col);
  175. column_out = db_get_table_column(table_out, col);
  176. if (G_strcasecmp(db_get_column_name(column_in),
  177. db_get_column_name(column_out)) != 0) {
  178. G_fatal_error(_("Column names differ"));
  179. }
  180. ctype_in =
  181. db_sqltype_to_Ctype(db_get_column_sqltype(column_in));
  182. ctype_out =
  183. db_sqltype_to_Ctype(db_get_column_sqltype
  184. (column_out));
  185. if (ctype_in != ctype_out) {
  186. G_fatal_error(_("Column types differ"));
  187. }
  188. if (ctype_in == DB_C_TYPE_STRING &&
  189. db_get_column_length(column_in) !=
  190. db_get_column_length(column_out)) {
  191. G_fatal_error(_("Length of string columns differ"));
  192. }
  193. if (G_strcasecmp(key,
  194. db_get_column_name(column_out)) == 0) {
  195. keycol = col;
  196. }
  197. }
  198. }
  199. Vect_close(&InMap);
  200. i++;
  201. }
  202. if (keycol == -1) {
  203. G_fatal_error(_("Key column not found"));
  204. }
  205. }
  206. if (append->answer) {
  207. Vect_open_update(&OutMap, out_name, G_mapset());
  208. if (out_is_3d == WITH_Z && !Vect_is_3d(&OutMap)) {
  209. G_warning(_("The output map is not 3D"));
  210. }
  211. maxcat = max_cat(&OutMap, 1);
  212. }
  213. else {
  214. Vect_open_new(&OutMap, out_name, out_is_3d);
  215. }
  216. if (bbox_name) {
  217. Vect_open_new(&BBoxMap, bbox_name, out_is_3d); /* TODO 3D */
  218. Vect_hist_command(&BBoxMap);
  219. }
  220. Vect_hist_command(&OutMap);
  221. driver_out = NULL;
  222. if (do_table) {
  223. if (append->answer) {
  224. fi_out = Vect_get_field(&OutMap, 1);
  225. }
  226. else {
  227. fi_out = Vect_default_field_info(&OutMap, 1, NULL, GV_1TABLE);
  228. fi_out->key = key;
  229. }
  230. if (fi_out) {
  231. driver_out =
  232. db_start_driver_open_database(fi_out->driver,
  233. fi_out->database);
  234. if (!driver_out) {
  235. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  236. fi_out->database, fi_out->driver);
  237. }
  238. db_begin_transaction(driver_out);
  239. }
  240. db_set_string(&table_name_out, fi_out->table);
  241. db_set_table_name(table_out, fi_out->table);
  242. if (!append->answer) {
  243. if (db_create_table(driver_out, table_out) != DB_OK) {
  244. G_fatal_error(_("Unable to create table <%s>"),
  245. fi_out->table);
  246. }
  247. Vect_map_add_dblink(&OutMap, 1, NULL, fi_out->table,
  248. fi_in->key, fi_out->database, fi_out->driver);
  249. }
  250. }
  251. i = 0;
  252. while (old->answers[i]) {
  253. int add_cat;
  254. in_name = old->answers[i++];
  255. G_important_message(_("Patching vector map <%s>..."), in_name);
  256. if (bbox_name)
  257. Vect_set_open_level(2); /* needed for Vect_map_box() */
  258. else
  259. Vect_set_open_level(1);
  260. Vect_open_old(&InMap, in_name, "");
  261. /*first time around, copy first in head to out head */
  262. if (i == 1)
  263. Vect_copy_head_data(&InMap, &OutMap);
  264. if (do_table) {
  265. add_cat = maxcat + 1;
  266. }
  267. else {
  268. add_cat = 0;
  269. }
  270. G_debug(2, "maxcat = %d add_cat = %d", maxcat, add_cat);
  271. ret =
  272. patch(&InMap, &OutMap, add_cat, &maxcat,
  273. bbox_name ? &BBoxMap : NULL);
  274. if (ret < 0)
  275. G_warning(_("Error reading vector map <%s> - "
  276. "some data may not be correct"), in_name);
  277. if (do_table) {
  278. fi_in = Vect_get_field(&InMap, 1);
  279. if (fi_in) {
  280. driver_in =
  281. db_start_driver_open_database(fi_in->driver,
  282. fi_in->database);
  283. if (!driver_in) {
  284. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  285. fi_in->database, fi_in->driver);
  286. }
  287. db_set_string(&table_name_in, fi_in->table);
  288. copy_records(driver_in, &table_name_in,
  289. driver_out, &table_name_out, keycol, add_cat);
  290. db_close_database_shutdown_driver(driver_in);
  291. }
  292. }
  293. Vect_close(&InMap);
  294. }
  295. n_files = i;
  296. if (driver_out) {
  297. db_commit_transaction(driver_out);
  298. db_close_database_shutdown_driver(driver_out);
  299. }
  300. Vect_set_map_name(&OutMap, "Output from v.patch");
  301. Vect_set_person(&OutMap, G_whoami());
  302. Vect_build(&OutMap);
  303. Vect_close(&OutMap);
  304. if (bbox_name) {
  305. Vect_set_map_name(&BBoxMap, "Output from v.patch (bounding boxes)");
  306. Vect_set_person(&BBoxMap, G_whoami());
  307. G_important_message(" ");
  308. G_important_message(_("Building topology for vector map <%s>..."),
  309. bbox_name);
  310. Vect_build(&BBoxMap);
  311. Vect_close(&BBoxMap);
  312. }
  313. G_message(_("Intersections at borders will have to be snapped"));
  314. G_message(_("Lines common between files will have to be edited"));
  315. G_message(_("The header information also may have to be edited"));
  316. G_done_msg(_("%d vector maps patched"), n_files);
  317. exit(EXIT_SUCCESS);
  318. }
  319. int copy_records(dbDriver * driver_in, dbString * table_name_in,
  320. dbDriver * driver_out, dbString * table_name_out,
  321. int keycol, int add_cat)
  322. {
  323. int ncols, col;
  324. dbCursor cursor;
  325. dbString value_str, sql;
  326. dbTable *table_in;
  327. db_init_string(&value_str);
  328. db_init_string(&sql);
  329. db_set_string(&sql, "select * from ");
  330. db_append_string(&sql, db_get_string(table_name_in));
  331. if (db_open_select_cursor(driver_in, &sql, &cursor, DB_SEQUENTIAL) !=
  332. DB_OK) {
  333. G_warning(_("Cannot open select cursor: '%s'"), db_get_string(&sql));
  334. return 0;
  335. }
  336. table_in = db_get_cursor_table(&cursor);
  337. ncols = db_get_table_number_of_columns(table_in);
  338. while (1) {
  339. int more;
  340. char buf[2000];
  341. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  342. G_fatal_error(_("Cannot fetch row"));
  343. db_close_cursor(&cursor);
  344. }
  345. if (!more)
  346. break;
  347. sprintf(buf, "insert into %s values ( ",
  348. db_get_string(table_name_out));
  349. db_set_string(&sql, buf);
  350. for (col = 0; col < ncols; col++) {
  351. int ctype, sqltype;
  352. dbColumn *column;
  353. dbValue *value;
  354. column = db_get_table_column(table_in, col);
  355. sqltype = db_get_column_sqltype(column);
  356. ctype = db_sqltype_to_Ctype(sqltype);
  357. value = db_get_column_value(column);
  358. if (col > 0)
  359. db_append_string(&sql, ", ");
  360. if (col == keycol) {
  361. db_set_value_int(value, db_get_value_int(value) + add_cat);
  362. }
  363. db_convert_value_to_string(value, sqltype, &value_str);
  364. switch (ctype) {
  365. case DB_C_TYPE_STRING:
  366. case DB_C_TYPE_DATETIME:
  367. if (db_test_value_isnull(value)) {
  368. db_append_string(&sql, "null");
  369. }
  370. else {
  371. db_double_quote_string(&value_str);
  372. sprintf(buf, "'%s'", db_get_string(&value_str));
  373. db_append_string(&sql, buf);
  374. }
  375. break;
  376. case DB_C_TYPE_INT:
  377. case DB_C_TYPE_DOUBLE:
  378. if (db_test_value_isnull(value)) {
  379. db_append_string(&sql, "null");
  380. }
  381. else {
  382. db_append_string(&sql, db_get_string(&value_str));
  383. }
  384. break;
  385. default:
  386. G_fatal_error(_("Unknown column type"));
  387. }
  388. }
  389. db_append_string(&sql, ")");
  390. G_debug(2, "SQL: %s", db_get_string(&sql));
  391. if (db_execute_immediate(driver_out, &sql) != DB_OK) {
  392. G_fatal_error(_("Cannot insert new record: '%s'"),
  393. db_get_string(&sql));
  394. }
  395. }
  396. db_close_cursor(&cursor);
  397. return 1;
  398. }
  399. int patch(struct Map_info *InMap, struct Map_info *OutMap, int add_cat,
  400. int *max_cat, struct Map_info *BBoxMap)
  401. {
  402. int type;
  403. struct line_pnts *Points;
  404. struct line_cats *Cats;
  405. *max_cat = add_cat;
  406. Points = Vect_new_line_struct();
  407. Cats = Vect_new_cats_struct();
  408. /* TODO:
  409. OutMap->head.orig_scale = GREATER (OutMap->head.orig_scale, InMap->head.orig_scale);
  410. OutMap->head.digit_thresh = 0;
  411. OutMap->head.map_thresh = GREATER (OutMap->head.map_thresh, InMap->head.map_thresh);
  412. */
  413. while ((type = Vect_read_next_line(InMap, Points, Cats)) > 0) {
  414. int i;
  415. for (i = 0; i < Cats->n_cats; i++) {
  416. if (Cats->field[i] == 1) {
  417. Cats->cat[i] += add_cat;
  418. if (Cats->cat[i] > *max_cat)
  419. *max_cat = Cats->cat[i];
  420. }
  421. }
  422. Vect_write_line(OutMap, type, Points, Cats);
  423. }
  424. if (BBoxMap) { /* inspired by v.in.region */
  425. struct bound_box box;
  426. double diff_long, mid_long;
  427. static int cat;
  428. Vect_get_map_box(InMap, &box);
  429. diff_long = box.E - box.W;
  430. mid_long = (box.W + box.E) / 2;
  431. /* rectangle */
  432. Vect_reset_cats(Cats);
  433. /* write each line, useful for snapping */
  434. Vect_reset_line(Points);
  435. Vect_append_point(Points, box.W, box.S, 0.0);
  436. if (Vect_get_proj(BBoxMap) == PROJECTION_LL && diff_long >= 179) {
  437. Vect_append_point(Points, mid_long, box.S, 0.0);
  438. }
  439. Vect_append_point(Points, box.E, box.S, 0.0);
  440. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  441. Vect_reset_line(Points);
  442. Vect_append_point(Points, box.E, box.S, 0.0);
  443. Vect_append_point(Points, box.E, box.N, 0.0);
  444. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  445. Vect_reset_line(Points);
  446. Vect_append_point(Points, box.E, box.N, 0.0);
  447. if (Vect_get_proj(BBoxMap) == PROJECTION_LL && diff_long >= 179) {
  448. Vect_append_point(Points, mid_long, box.N, 0.0);
  449. }
  450. Vect_append_point(Points, box.W, box.N, 0.0);
  451. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  452. Vect_reset_line(Points);
  453. Vect_append_point(Points, box.W, box.N, 0.0);
  454. Vect_append_point(Points, box.W, box.S, 0.0);
  455. Vect_write_line(BBoxMap, GV_BOUNDARY, Points, Cats);
  456. /* centroid */
  457. Vect_reset_line(Points);
  458. Vect_cat_set(Cats, 1, ++cat); /* first layer */
  459. Vect_append_point(Points, (box.W + box.E) / 2, (box.S + box.N) / 2,
  460. 0.0);
  461. Vect_write_line(BBoxMap, GV_CENTROID, Points, Cats);
  462. }
  463. Vect_destroy_line_struct(Points);
  464. Vect_destroy_cats_struct(Cats);
  465. if (type != -2)
  466. return -1;
  467. return 0;
  468. }
  469. int max_cat(struct Map_info *Map, int layer)
  470. {
  471. struct line_cats *Cats;
  472. int max = 0;
  473. Cats = Vect_new_cats_struct();
  474. while (Vect_read_next_line(Map, NULL, Cats) > 0) {
  475. int i;
  476. for (i = 0; i < Cats->n_cats; i++) {
  477. if (Cats->field[i] == layer && Cats->cat[i] > max) {
  478. max = Cats->cat[i];
  479. }
  480. }
  481. }
  482. return max;
  483. }