main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  147. db_init_string(&table_name);
  148. db_set_string(&table_name, Fi->table);
  149. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  150. G_fatal_error(_("Unable to describe table <%s>"),
  151. qFi->table);
  152. fncols = db_get_table_number_of_columns(table);
  153. for (col = 0; col < ncols; col++) {
  154. int col_exists = 0;
  155. if (options.col[col] == NULL)
  156. G_fatal_error(_("Missing column name for input column number %d"), col + 1);
  157. for (icol = 0; icol < fncols; icol++) {
  158. column = db_get_table_column(table, icol);
  159. colname = db_get_column_name(column);
  160. if (colname == NULL)
  161. G_fatal_error(_("Missing column name for table column number %d"), col + 1);
  162. if (strcmp(options.col[col], colname) == 0) {
  163. int isqltype;
  164. col_exists = 1;
  165. isqltype = db_get_column_sqltype(column);
  166. if (isqltype != col_sqltype[col]) {
  167. int ctype1, ctype2;
  168. ctype1 = db_sqltype_to_Ctype(isqltype);
  169. ctype2 = db_sqltype_to_Ctype(col_sqltype[col]);
  170. if (ctype1 == ctype2) {
  171. G_warning(_("Existing column <%s> has a different but maybe compatible type"),
  172. options.col[col]);
  173. }
  174. else {
  175. G_fatal_error(_("Existing column <%s> has the wrong type"),
  176. options.col[col]);
  177. }
  178. }
  179. if (G_get_overwrite())
  180. G_warning(_("Values in column <%s> will be overwritten"),
  181. options.col[col]);
  182. else
  183. G_fatal_error(_("Column <%s> exists. To overwrite, use the --overwrite flag"),
  184. options.col[col]);
  185. break;
  186. }
  187. }
  188. if (!col_exists) {
  189. create_col[col] = 1;
  190. create_cols = 1;
  191. }
  192. }
  193. db_close_database_shutdown_driver(driver);
  194. driver = NULL;
  195. db_free_string(&table_name);
  196. /* create columns if not existing */
  197. if (create_cols) {
  198. char sqlbuf[4096];
  199. dbString stmt;
  200. db_init_string(&stmt);
  201. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  202. db_begin_transaction(driver);
  203. for (col = 0; col < ncols; col++) {
  204. if (!create_col[col])
  205. continue;
  206. if (col_sqltype[col] == DB_SQL_TYPE_INTEGER) {
  207. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s integer",
  208. Fi->table, options.col[col]);
  209. }
  210. else if (col_sqltype[col] == DB_SQL_TYPE_DOUBLE_PRECISION ||
  211. col_sqltype[col] == DB_SQL_TYPE_REAL) {
  212. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s double precision",
  213. Fi->table, options.col[col]);
  214. }
  215. else if (col_sqltype[col] == DB_SQL_TYPE_CHARACTER) {
  216. if (qlength > 0) {
  217. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s varchar(%d)",
  218. Fi->table, options.col[col], qlength);
  219. }
  220. else {
  221. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  222. Fi->table, options.col[col]);
  223. }
  224. }
  225. else if (col_sqltype[col] == DB_SQL_TYPE_TEXT) {
  226. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s text",
  227. Fi->table, options.col[col]);
  228. }
  229. else if (col_sqltype[col] == DB_SQL_TYPE_DATE) {
  230. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s date",
  231. Fi->table, options.col[col]);
  232. }
  233. else if (col_sqltype[col] == DB_SQL_TYPE_TIME) {
  234. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s time",
  235. Fi->table, options.col[col]);
  236. }
  237. else {
  238. sprintf(sqlbuf, "ALTER TABLE %s ADD COLUMN %s %s",
  239. Fi->table, options.col[col],
  240. db_sqltype_name(col_sqltype[col]));
  241. }
  242. db_set_string(&stmt, sqlbuf);
  243. if (db_execute_immediate(driver, &stmt) != DB_OK) {
  244. G_fatal_error(_("Unable to create column <%s>"),
  245. options.col[col]);
  246. }
  247. }
  248. db_commit_transaction(driver);
  249. db_close_database_shutdown_driver(driver);
  250. db_free_string(&stmt);
  251. }
  252. }
  253. /* allocate array for values */
  254. /* (+1 is for cat -1 (no category) reported at the end ) */
  255. findex = Vect_cidx_get_field_index(&Map, options.field);
  256. if (findex > -1) {
  257. n = Vect_cidx_get_num_unique_cats_by_index(&Map, findex);
  258. }
  259. else {
  260. n = 0;
  261. }
  262. G_debug(2, "%d unique cats", n);
  263. Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
  264. /* prepopulate Values */
  265. if (findex > -1)
  266. n = Vect_cidx_get_num_cats_by_index(&Map, findex);
  267. i = 0;
  268. Values[i].cat = -1; /* features without category */
  269. Values[i].used = 0;
  270. Values[i].count1 = 0;
  271. Values[i].count2 = 0;
  272. Values[i].i1 = -1;
  273. Values[i].i2 = -1;
  274. Values[i].d1 = 0.0;
  275. Values[i].d2 = 0.0;
  276. Values[i].d3 = 0.0;
  277. Values[i].d4 = 0.0;
  278. if (options.option == O_BBOX) {
  279. Values[i].d1 = -PORT_DOUBLE_MAX;
  280. Values[i].d2 = PORT_DOUBLE_MAX;
  281. Values[i].d3 = -PORT_DOUBLE_MAX;
  282. Values[i].d4 = PORT_DOUBLE_MAX;
  283. }
  284. Values[i].qcat = NULL;
  285. Values[i].nqcats = 0;
  286. Values[i].aqcats = 0;
  287. i = 1;
  288. lastcat = -1;
  289. /* category index must be sorted,
  290. * i.e. topology must have been built with GV_BUILD_ALL */
  291. for (j = 0; j < n; j++) {
  292. Vect_cidx_get_cat_by_index(&Map, findex, j, &cat, &type, &id);
  293. if (lastcat > cat) {
  294. Vect_close(&Map);
  295. G_fatal_error(_("Category index for vector map <%s> is not sorted"),
  296. options.name);
  297. }
  298. if (lastcat != cat) {
  299. Values[i].cat = cat;
  300. Values[i].used = 0;
  301. Values[i].count1 = 0;
  302. Values[i].count2 = 0;
  303. Values[i].i1 = -1;
  304. Values[i].i2 = -1;
  305. Values[i].d1 = 0.0;
  306. Values[i].d2 = 0.0;
  307. Values[i].d3 = 0.0;
  308. Values[i].d4 = 0.0;
  309. if (options.option == O_BBOX) {
  310. Values[i].d1 = -PORT_DOUBLE_MAX;
  311. Values[i].d2 = PORT_DOUBLE_MAX;
  312. Values[i].d3 = -PORT_DOUBLE_MAX;
  313. Values[i].d4 = PORT_DOUBLE_MAX;
  314. }
  315. Values[i].qcat = NULL;
  316. Values[i].nqcats = 0;
  317. Values[i].aqcats = 0;
  318. lastcat = cat;
  319. i++;
  320. }
  321. }
  322. vstat.rcat = i;
  323. /* Read values from map */
  324. if (options.option == O_QUERY) {
  325. query(&Map);
  326. }
  327. else if ((options.option == O_AREA) || (options.option == O_COMPACT) ||
  328. (options.option == O_PERIMETER) || (options.option == O_FD) ||
  329. (options.option == O_BBOX)) {
  330. read_areas(&Map);
  331. }
  332. else {
  333. read_lines(&Map);
  334. }
  335. /* prune unused values */
  336. n = vstat.rcat;
  337. j = 0;
  338. for (i = 0; i < n; i++) {
  339. if (Values[i].used) {
  340. Values[j] = Values[i];
  341. j++;
  342. }
  343. }
  344. vstat.rcat = j;
  345. conv_units();
  346. if (options.print || options.total) {
  347. report();
  348. }
  349. else {
  350. update(&Map);
  351. Vect_set_db_updated(&Map);
  352. }
  353. Vect_close(&Map);
  354. if (!(options.print || options.total)) {
  355. print_stat();
  356. }
  357. /* free list */
  358. G_free(Values);
  359. exit(EXIT_SUCCESS);
  360. }