main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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-2020 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(_("azimuth"));
  37. G_add_keyword(_("bounding box"));
  38. G_add_keyword(_("category"));
  39. G_add_keyword(_("compactness"));
  40. G_add_keyword(_("coordinates"));
  41. G_add_keyword(_("fractal"));
  42. G_add_keyword(_("geometry"));
  43. G_add_keyword(_("length"));
  44. G_add_keyword(_("perimeter"));
  45. G_add_keyword(_("sides"));
  46. G_add_keyword(_("sinuous"));
  47. G_add_keyword(_("slope"));
  48. module->description = _("Populates attribute values from vector features.");
  49. module->overwrite = 1;
  50. parse_command_line(argc, argv);
  51. if (!options.print && !options.total) {
  52. const char *mapset;
  53. mapset = G_find_vector2(options.name, "");
  54. if (!mapset || (strcmp(mapset, G_mapset()) != 0))
  55. G_fatal_error(_("Vector map <%s> not found in the current mapset. "
  56. "Unable to modify vector maps from different mapsets."),
  57. options.name);
  58. }
  59. G_begin_distance_calculations();
  60. G_begin_polygon_area_calculations();
  61. /* open map */
  62. Vect_set_open_level(2);
  63. if (Vect_open_old(&Map, options.name, "") < 0)
  64. G_fatal_error(_("Unable to open vector map <%s>"), options.name);
  65. Vect_set_error_handler_io(&Map, NULL);
  66. Fi = Vect_get_field(&Map, options.field);
  67. if (!options.print && Fi == NULL) {
  68. G_fatal_error(_("Database connection not defined for layer %d. "
  69. "Use v.db.connect first."),
  70. options.field);
  71. }
  72. qFi = Vect_get_field(&Map, options.qfield);
  73. if (options.option == O_QUERY && qFi == NULL)
  74. G_fatal_error(_("Database connection not defined for layer %d. Use v.db.connect first."),
  75. options.qfield);
  76. if (!options.print) {
  77. dbDriver *driver = NULL;
  78. dbString table_name;
  79. dbTable *table;
  80. dbColumn *column;
  81. const char *colname;
  82. int col, fncols, icol;
  83. int col_sqltype[4];
  84. int create_col[4], create_cols;
  85. int qlength;
  86. /* get required column types */
  87. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = col_sqltype[3] = -1;
  88. ncols = 1;
  89. qlength = 0;
  90. switch (options.option) {
  91. case O_CAT:
  92. case O_COUNT:
  93. col_sqltype[0] = DB_SQL_TYPE_INTEGER;
  94. break;
  95. case O_LENGTH:
  96. case O_AREA:
  97. case O_PERIMETER:
  98. case O_SLOPE:
  99. case O_SINUOUS:
  100. case O_AZIMUTH:
  101. case O_COMPACT:
  102. case O_FD:
  103. col_sqltype[0] = DB_SQL_TYPE_DOUBLE_PRECISION;
  104. break;
  105. case O_BBOX:
  106. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = col_sqltype[3] = DB_SQL_TYPE_DOUBLE_PRECISION;
  107. ncols = 4;
  108. break;
  109. case O_COOR:
  110. case O_START:
  111. case O_END:
  112. col_sqltype[0] = col_sqltype[1] = col_sqltype[2] = DB_SQL_TYPE_DOUBLE_PRECISION;
  113. ncols = 2;
  114. if (options.col[2])
  115. ncols = 3;
  116. break;
  117. case O_SIDES:
  118. col_sqltype[0] = col_sqltype[1] = DB_SQL_TYPE_INTEGER;
  119. ncols = 2;
  120. break;
  121. case O_QUERY:
  122. driver = db_start_driver_open_database(qFi->driver, qFi->database);
  123. db_init_string(&table_name);
  124. db_set_string(&table_name, qFi->table);
  125. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  126. G_fatal_error(_("Unable to describe table <%s>"),
  127. qFi->table);
  128. fncols = db_get_table_number_of_columns(table);
  129. for (col = 0; col < fncols; col++) {
  130. column = db_get_table_column(table, col);
  131. colname = db_get_column_name(column);
  132. if (strcmp(options.qcol, colname) == 0) {
  133. col_sqltype[0] = db_get_column_sqltype(column);
  134. qlength = db_get_column_length(column);
  135. break;
  136. }
  137. }
  138. db_close_database_shutdown_driver(driver);
  139. driver = NULL;
  140. db_free_string(&table_name);
  141. break;
  142. }
  143. /* check if columns exist */
  144. create_col[0] = create_col[1] = create_col[2] = create_col[3] = 0;
  145. create_cols = 0;
  146. /* does not work if the query column is not a column, but
  147. * e.g. count(x) or avg(x) */
  148. if (options.option != O_QUERY) {
  149. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  150. db_init_string(&table_name);
  151. db_set_string(&table_name, Fi->table);
  152. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  153. G_fatal_error(_("Unable to describe table <%s>"),
  154. qFi->table);
  155. fncols = db_get_table_number_of_columns(table);
  156. for (col = 0; col < ncols; col++) {
  157. int col_exists = 0;
  158. if (options.col[col] == NULL)
  159. G_fatal_error(_("Missing column name for input column number %d"), col + 1);
  160. for (icol = 0; icol < fncols; icol++) {
  161. column = db_get_table_column(table, icol);
  162. colname = db_get_column_name(column);
  163. if (colname == NULL)
  164. G_fatal_error(_("Missing column name for table column number %d"), col + 1);
  165. if (strcmp(options.col[col], colname) == 0) {
  166. int isqltype;
  167. col_exists = 1;
  168. isqltype = db_get_column_sqltype(column);
  169. if (isqltype != col_sqltype[col]) {
  170. int ctype1, ctype2;
  171. ctype1 = db_sqltype_to_Ctype(isqltype);
  172. ctype2 = db_sqltype_to_Ctype(col_sqltype[col]);
  173. if (ctype1 == ctype2) {
  174. G_warning(_("Existing column <%s> has a different but maybe compatible type"),
  175. options.col[col]);
  176. }
  177. else {
  178. G_fatal_error(_("Existing column <%s> has the wrong type"),
  179. options.col[col]);
  180. }
  181. }
  182. if (G_get_overwrite())
  183. G_warning(_("Values in column <%s> will be overwritten"),
  184. options.col[col]);
  185. else
  186. G_fatal_error(_("Column <%s> exists. To overwrite, use the --overwrite flag"),
  187. options.col[col]);
  188. break;
  189. }
  190. }
  191. if (!col_exists) {
  192. create_col[col] = 1;
  193. create_cols = 1;
  194. }
  195. }
  196. db_close_database_shutdown_driver(driver);
  197. driver = NULL;
  198. db_free_string(&table_name);
  199. }
  200. /* create columns if not existing */
  201. if (create_cols) {
  202. char sqlbuf[4096];
  203. dbString stmt;
  204. db_init_string(&stmt);
  205. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  206. db_begin_transaction(driver);
  207. for (col = 0; col < ncols; col++) {
  208. if (!create_col[col])
  209. continue;
  210. if (col_sqltype[col] == DB_SQL_TYPE_INTEGER) {
  211. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s integer",
  212. Fi->table, options.col[col]);
  213. }
  214. else if (col_sqltype[col] == DB_SQL_TYPE_DOUBLE_PRECISION ||
  215. col_sqltype[col] == DB_SQL_TYPE_REAL) {
  216. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s double precision",
  217. Fi->table, options.col[col]);
  218. }
  219. else if (col_sqltype[col] == DB_SQL_TYPE_CHARACTER) {
  220. if (qlength > 0) {
  221. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s varchar(%d)",
  222. Fi->table, options.col[col], qlength);
  223. }
  224. else {
  225. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  226. Fi->table, options.col[col]);
  227. }
  228. }
  229. else if (col_sqltype[col] == DB_SQL_TYPE_TEXT) {
  230. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  231. Fi->table, options.col[col]);
  232. }
  233. else if (col_sqltype[col] == DB_SQL_TYPE_DATE) {
  234. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s date",
  235. Fi->table, options.col[col]);
  236. }
  237. else if (col_sqltype[col] == DB_SQL_TYPE_TIME) {
  238. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s time",
  239. Fi->table, options.col[col]);
  240. }
  241. else {
  242. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s %s",
  243. Fi->table, options.col[col],
  244. db_sqltype_name(col_sqltype[col]));
  245. }
  246. db_set_string(&stmt, sqlbuf);
  247. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  248. G_fatal_error(_("Unable to create column <%s>"),
  249. options.col[col]);
  250. }
  251. }
  252. db_commit_transaction(driver);
  253. db_close_database_shutdown_driver(driver);
  254. db_free_string(&stmt);
  255. }
  256. }
  257. /* allocate array for values */
  258. /* (+1 is for cat -1 (no category) reported at the end ) */
  259. findex = Vect_cidx_get_field_index(&Map, options.field);
  260. if (findex > -1) {
  261. n = Vect_cidx_get_num_unique_cats_by_index(&Map, findex);
  262. }
  263. else {
  264. n = 0;
  265. }
  266. G_debug(2, "%d unique cats", n);
  267. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  268. /* prepopulate Values */
  269. if (findex > -1)
  270. n = Vect_cidx_get_num_cats_by_index(&Map, findex);
  271. i = 0;
  272. Values[i].cat = -1; /* features without category */
  273. Values[i].used = 0;
  274. Values[i].count1 = 0;
  275. Values[i].count2 = 0;
  276. Values[i].i1 = -1;
  277. Values[i].i2 = -1;
  278. Values[i].d1 = 0.0;
  279. Values[i].d2 = 0.0;
  280. Values[i].d3 = 0.0;
  281. Values[i].d4 = 0.0;
  282. if (options.option == O_BBOX) {
  283. Values[i].d1 = -PORT_DOUBLE_MAX;
  284. Values[i].d2 = PORT_DOUBLE_MAX;
  285. Values[i].d3 = -PORT_DOUBLE_MAX;
  286. Values[i].d4 = PORT_DOUBLE_MAX;
  287. }
  288. Values[i].qcat = NULL;
  289. Values[i].nqcats = 0;
  290. Values[i].aqcats = 0;
  291. i = 1;
  292. lastcat = -1;
  293. /* category index must be sorted,
  294. * i.e. topology must have been built with GV_BUILD_ALL */
  295. for (j = 0; j < n; j++) {
  296. Vect_cidx_get_cat_by_index(&Map, findex, j, &cat, &type, &id);
  297. if (lastcat > cat) {
  298. Vect_close(&Map);
  299. G_fatal_error(_("Category index for vector map <%s> is not sorted"),
  300. options.name);
  301. }
  302. if (lastcat != cat) {
  303. Values[i].cat = cat;
  304. Values[i].used = 0;
  305. Values[i].count1 = 0;
  306. Values[i].count2 = 0;
  307. Values[i].i1 = -1;
  308. Values[i].i2 = -1;
  309. Values[i].d1 = 0.0;
  310. Values[i].d2 = 0.0;
  311. Values[i].d3 = 0.0;
  312. Values[i].d4 = 0.0;
  313. if (options.option == O_BBOX) {
  314. Values[i].d1 = -PORT_DOUBLE_MAX;
  315. Values[i].d2 = PORT_DOUBLE_MAX;
  316. Values[i].d3 = -PORT_DOUBLE_MAX;
  317. Values[i].d4 = PORT_DOUBLE_MAX;
  318. }
  319. Values[i].qcat = NULL;
  320. Values[i].nqcats = 0;
  321. Values[i].aqcats = 0;
  322. lastcat = cat;
  323. i++;
  324. }
  325. }
  326. vstat.rcat = i;
  327. /* Read values from map */
  328. if (options.option == O_QUERY) {
  329. query(&Map);
  330. }
  331. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  332. (options.option == O_PERIMETER) || (options.option == O_FD) ||
  333. (options.option == O_BBOX)) {
  334. read_areas(&Map);
  335. }
  336. else {
  337. read_lines(&Map);
  338. }
  339. /* prune unused values */
  340. n = vstat.rcat;
  341. j = 0;
  342. for (i = 0; i < n; i++) {
  343. if (Values[i].used) {
  344. Values[j] = Values[i];
  345. j++;
  346. }
  347. }
  348. vstat.rcat = j;
  349. conv_units();
  350. if (options.print || options.total) {
  351. report();
  352. }
  353. else {
  354. update(&Map);
  355. Vect_set_db_updated(&Map);
  356. }
  357. Vect_close(&Map);
  358. if (!(options.print || options.total)) {
  359. print_stat();
  360. if (Vect_open_update_head(&Map, options.name, "") < 0)
  361. G_warning(_("Unable to write history for vector map <%s>"),
  362. options.name);
  363. else {
  364. Vect_hist_command(&Map);
  365. Vect_close(&Map);
  366. }
  367. }
  368. /* free list */
  369. G_free(Values);
  370. exit(EXIT_SUCCESS);
  371. }