main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /****************************************************************************
  2. *
  3. * MODULE: v.to.db
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Wolf Bergenheim <wolf+grass bergenheim net>,
  6. * Glynn Clements <glynn gclements.plus.com>,
  7. * Markus Neteler <neteler itc.it>
  8. * PURPOSE: load values from vector to database
  9. * COPYRIGHT: (C) 2000-2010 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <grass/dbmi.h>
  18. #include <grass/glocale.h>
  19. #include "global.h"
  20. struct value *Values;
  21. struct options options;
  22. struct vstat vstat;
  23. int main(int argc, char *argv[])
  24. {
  25. int n, i, j, cat, lastcat, type, id, findex;
  26. struct Map_info Map;
  27. struct GModule *module;
  28. struct field_info *Fi, *qFi;
  29. int ncols;
  30. G_gisinit(argv[0]);
  31. module = G_define_module();
  32. G_add_keyword(_("vector"));
  33. G_add_keyword(_("attribute table"));
  34. G_add_keyword(_("database"));
  35. G_add_keyword(_("area"));
  36. G_add_keyword(_("length"));
  37. G_add_keyword(_("perimeter"));
  38. G_add_keyword(_("coordinates"));
  39. G_add_keyword(_("bounding box"));
  40. G_add_keyword(_("category"));
  41. module->description = _("Populates attribute values from vector features.");
  42. parse_command_line(argc, argv);
  43. if (!options.print && !options.total) {
  44. const char *mapset;
  45. mapset = G_find_vector2(options.name, "");
  46. if (!mapset || (strcmp(mapset, G_mapset()) != 0))
  47. G_fatal_error(_("Vector map <%s> not found in the current mapset. "
  48. "Unable to modify vector maps from different mapsets."),
  49. options.name);
  50. }
  51. G_begin_distance_calculations();
  52. G_begin_polygon_area_calculations();
  53. /* open map */
  54. Vect_set_open_level(2);
  55. if (Vect_open_old(&Map, options.name, "") < 0)
  56. G_fatal_error(_("Unable to open vector map <%s>"), options.name);
  57. Vect_set_error_handler_io(&Map, NULL);
  58. Fi = Vect_get_field(&Map, options.field);
  59. if (!options.print && Fi == NULL) {
  60. G_fatal_error(_("Database connection not defined for layer %d. "
  61. "Use v.db.connect first."),
  62. options.field);
  63. }
  64. qFi = Vect_get_field(&Map, options.qfield);
  65. if (options.option == O_QUERY && qFi == NULL)
  66. G_fatal_error(_("Database connection not defined for layer %d. Use v.db.connect first."),
  67. options.qfield);
  68. if (!options.print) {
  69. dbDriver *driver = NULL;
  70. dbString table_name;
  71. dbTable *table;
  72. dbColumn *column;
  73. const char *colname;
  74. int col, fncols, icol;
  75. int col_sqltype[4];
  76. int create_col[4], create_cols;
  77. int qlength;
  78. /* get required column types */
  79. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = col_sqltype[3] = -1;
  80. ncols = 1;
  81. qlength = 0;
  82. switch (options.option) {
  83. case O_CAT:
  84. case O_COUNT:
  85. col_sqltype[0] = DB_SQL_TYPE_INTEGER;
  86. break;
  87. case O_LENGTH:
  88. case O_AREA:
  89. case O_PERIMETER:
  90. case O_SLOPE:
  91. case O_SINUOUS:
  92. case O_AZIMUTH:
  93. case O_COMPACT:
  94. case O_FD:
  95. col_sqltype[0] = DB_SQL_TYPE_DOUBLE_PRECISION;
  96. break;
  97. case O_BBOX:
  98. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = col_sqltype[3] = DB_SQL_TYPE_DOUBLE_PRECISION;
  99. ncols = 4;
  100. break;
  101. case O_COOR:
  102. case O_START:
  103. case O_END:
  104. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = DB_SQL_TYPE_DOUBLE_PRECISION;
  105. ncols = 2;
  106. if (options.col[2])
  107. ncols = 3;
  108. break;
  109. case O_SIDES:
  110. col_sqltype[0] = col_sqltype[1] = DB_SQL_TYPE_INTEGER;
  111. ncols = 2;
  112. break;
  113. case O_QUERY:
  114. driver = db_start_driver_open_database(qFi->driver, qFi->database);
  115. db_init_string(&table_name);
  116. db_set_string(&table_name, qFi->table);
  117. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  118. G_fatal_error(_("Unable to describe table <%s>"),
  119. qFi->table);
  120. fncols = db_get_table_number_of_columns(table);
  121. for (col = 0; col < fncols; col++) {
  122. column = db_get_table_column(table, col);
  123. colname = db_get_column_name(column);
  124. if (strcmp(options.qcol, colname) == 0) {
  125. col_sqltype[0] = db_get_column_sqltype(column);
  126. qlength = db_get_column_length(column);
  127. break;
  128. }
  129. }
  130. db_close_database_shutdown_driver(driver);
  131. driver = NULL;
  132. db_free_string(&table_name);
  133. break;
  134. }
  135. /* check if columns exist */
  136. create_col[0] = create_col[1] = create_col[2] = create_col[3] = 0;
  137. create_cols = 0;
  138. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  139. db_init_string(&table_name);
  140. db_set_string(&table_name, Fi->table);
  141. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  142. G_fatal_error(_("Unable to describe table <%s>"),
  143. qFi->table);
  144. fncols = db_get_table_number_of_columns(table);
  145. for (col = 0; col < ncols; col++) {
  146. int col_exists = 0;
  147. if (options.col[col] == NULL)
  148. G_fatal_error(_("Missing column name for input column number %d"), col + 1);
  149. for (icol = 0; icol < fncols; icol++) {
  150. column = db_get_table_column(table, icol);
  151. colname = db_get_column_name(column);
  152. if (colname == NULL)
  153. G_fatal_error(_("Missing column name for table column number %d"), col + 1);
  154. if (strcmp(options.col[col], colname) == 0) {
  155. int isqltype;
  156. col_exists = 1;
  157. isqltype = db_get_column_sqltype(column);
  158. if (isqltype != col_sqltype[col]) {
  159. int ctype1, ctype2;
  160. ctype1 = db_sqltype_to_Ctype(isqltype);
  161. ctype2 = db_sqltype_to_Ctype(col_sqltype[col]);
  162. if (ctype1 == ctype2) {
  163. G_warning(_("Existing column <%s> has a different but maybe compatible type"),
  164. options.col[col]);
  165. }
  166. else {
  167. G_fatal_error(_("Existing column <%s> has the wrong type"),
  168. options.col[col]);
  169. }
  170. }
  171. G_warning(_("Values in column <%s> will be overwritten"),
  172. options.col[col]);
  173. break;
  174. }
  175. }
  176. if (!col_exists) {
  177. create_col[col] = 1;
  178. create_cols = 1;
  179. }
  180. }
  181. db_close_database_shutdown_driver(driver);
  182. driver = NULL;
  183. db_free_string(&table_name);
  184. /* create columns if not existing */
  185. if (create_cols) {
  186. char sqlbuf[4096];
  187. dbString stmt;
  188. db_init_string(&stmt);
  189. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  190. db_begin_transaction(driver);
  191. for (col = 0; col < ncols; col++) {
  192. if (!create_col[col])
  193. continue;
  194. if (col_sqltype[col] == DB_SQL_TYPE_INTEGER) {
  195. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s integer",
  196. Fi->table, options.col[col]);
  197. }
  198. else if (col_sqltype[col] == DB_SQL_TYPE_DOUBLE_PRECISION ||
  199. col_sqltype[col] == DB_SQL_TYPE_REAL) {
  200. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s double precision",
  201. Fi->table, options.col[col]);
  202. }
  203. else if (col_sqltype[col] == DB_SQL_TYPE_CHARACTER) {
  204. if (qlength > 0) {
  205. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s varchar(%d)",
  206. Fi->table, options.col[col], qlength);
  207. }
  208. else {
  209. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  210. Fi->table, options.col[col]);
  211. }
  212. }
  213. else if (col_sqltype[col] == DB_SQL_TYPE_TEXT) {
  214. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  215. Fi->table, options.col[col]);
  216. }
  217. else if (col_sqltype[col] == DB_SQL_TYPE_DATE) {
  218. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s date",
  219. Fi->table, options.col[col]);
  220. }
  221. else if (col_sqltype[col] == DB_SQL_TYPE_TIME) {
  222. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s time",
  223. Fi->table, options.col[col]);
  224. }
  225. else {
  226. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s %s",
  227. Fi->table, options.col[col],
  228. db_sqltype_name(col_sqltype[col]));
  229. }
  230. db_set_string(&stmt, sqlbuf);
  231. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  232. G_fatal_error(_("Unable to create column <%s>"),
  233. options.col[col]);
  234. }
  235. }
  236. db_commit_transaction(driver);
  237. db_close_database_shutdown_driver(driver);
  238. db_free_string(&stmt);
  239. }
  240. }
  241. /* allocate array for values */
  242. /* (+1 is for cat -1 (no category) reported at the end ) */
  243. findex = Vect_cidx_get_field_index(&Map, options.field);
  244. if (findex > -1) {
  245. n = Vect_cidx_get_num_unique_cats_by_index(&Map, findex);
  246. }
  247. else {
  248. n = 0;
  249. }
  250. G_debug(2, "%d unique cats", n);
  251. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  252. /* prepopulate Values */
  253. if (findex > -1)
  254. n = Vect_cidx_get_num_cats_by_index(&Map, findex);
  255. i = 0;
  256. Values[i].cat = -1; /* features without category */
  257. Values[i].used = 0;
  258. Values[i].count1 = 0;
  259. Values[i].count2 = 0;
  260. Values[i].i1 = -1;
  261. Values[i].i2 = -1;
  262. Values[i].d1 = 0.0;
  263. Values[i].d2 = 0.0;
  264. Values[i].d3 = 0.0;
  265. Values[i].d4 = 0.0;
  266. if (options.option == O_BBOX) {
  267. Values[i].d1 = -PORT_DOUBLE_MAX;
  268. Values[i].d2 = PORT_DOUBLE_MAX;
  269. Values[i].d3 = -PORT_DOUBLE_MAX;
  270. Values[i].d4 = PORT_DOUBLE_MAX;
  271. }
  272. Values[i].qcat = NULL;
  273. Values[i].nqcats = 0;
  274. Values[i].aqcats = 0;
  275. i = 1;
  276. lastcat = -1;
  277. /* category index must be sorted,
  278. * i.e. topology must have been built with GV_BUILD_ALL */
  279. for (j = 0; j < n; j++) {
  280. Vect_cidx_get_cat_by_index(&Map, findex, j, &cat, &type, &id);
  281. if (lastcat > cat) {
  282. Vect_close(&Map);
  283. G_fatal_error(_("Category index for vector map <%s> is not sorted"),
  284. options.name);
  285. }
  286. if (lastcat != cat) {
  287. Values[i].cat = cat;
  288. Values[i].used = 0;
  289. Values[i].count1 = 0;
  290. Values[i].count2 = 0;
  291. Values[i].i1 = -1;
  292. Values[i].i2 = -1;
  293. Values[i].d1 = 0.0;
  294. Values[i].d2 = 0.0;
  295. Values[i].d3 = 0.0;
  296. Values[i].d4 = 0.0;
  297. if (options.option == O_BBOX) {
  298. Values[i].d1 = -PORT_DOUBLE_MAX;
  299. Values[i].d2 = PORT_DOUBLE_MAX;
  300. Values[i].d3 = -PORT_DOUBLE_MAX;
  301. Values[i].d4 = PORT_DOUBLE_MAX;
  302. }
  303. Values[i].qcat = NULL;
  304. Values[i].nqcats = 0;
  305. Values[i].aqcats = 0;
  306. lastcat = cat;
  307. i++;
  308. }
  309. }
  310. vstat.rcat = i;
  311. /* Read values from map */
  312. if (options.option == O_QUERY) {
  313. query(&Map);
  314. }
  315. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  316. (options.option == O_PERIMETER) || (options.option == O_FD) ||
  317. (options.option == O_BBOX)) {
  318. read_areas(&Map);
  319. }
  320. else {
  321. read_lines(&Map);
  322. }
  323. /* prune unused values */
  324. n = vstat.rcat;
  325. j = 0;
  326. for (i = 0; i < n; i++) {
  327. if (Values[i].used) {
  328. Values[j] = Values[i];
  329. j++;
  330. }
  331. }
  332. vstat.rcat = j;
  333. conv_units();
  334. if (options.print || options.total) {
  335. report();
  336. }
  337. else {
  338. update(&Map);
  339. Vect_set_db_updated(&Map);
  340. }
  341. Vect_close(&Map);
  342. if (!(options.print || options.total)) {
  343. print_stat();
  344. }
  345. /* free list */
  346. G_free(Values);
  347. exit(EXIT_SUCCESS);
  348. }