main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /****************************************************************************
  2. *
  3. * MODULE: v.in.ascii
  4. *
  5. * AUTHOR(S): Original authors Michael Higgins, James Westervelt (CERL)
  6. * Updated to GRASS 5.7 Radim Blazek, ITC-Irst, Trento, Italy
  7. * PURPOSE: Converts a vector map in ASCII format to a vector map
  8. * in binary format
  9. *
  10. * COPYRIGHT: (C) 2000-2009 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. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <grass/gis.h>
  22. #include <grass/dbmi.h>
  23. #include <grass/vector.h>
  24. #include <grass/glocale.h>
  25. #include "local_proto.h"
  26. #define A_DIR "dig_ascii"
  27. int main(int argc, char *argv[])
  28. {
  29. FILE *ascii;
  30. struct GModule *module;
  31. struct Option *old, *new, *delim_opt, *columns_opt, *xcol_opt,
  32. *ycol_opt, *zcol_opt, *catcol_opt, *format_opt, *skip_opt;
  33. int xcol, ycol, zcol, catcol, format, skip_lines;
  34. struct Flag *zcoorf, *t_flag, *e_flag, *noheader_flag, *notopol_flag,
  35. *region_flag;
  36. char *table;
  37. char *fs;
  38. char *desc;
  39. int zcoor = WITHOUT_Z, make_table;
  40. struct Map_info Map;
  41. G_gisinit(argv[0]);
  42. module = G_define_module();
  43. G_add_keyword(_("vector"));
  44. G_add_keyword(_("import"));
  45. G_add_keyword("ASCII");
  46. module->description =
  47. _("Creates a vector map from an ASCII points file or ASCII vector file.");
  48. /************************** Command Parser ************************************/
  49. old = G_define_standard_option(G_OPT_F_INPUT);
  50. old->label = _("Name of input file to be imported");
  51. old->description = ("'-' for standard input");
  52. new = G_define_standard_option(G_OPT_V_OUTPUT);
  53. format_opt = G_define_option();
  54. format_opt->key = "format";
  55. format_opt->type = TYPE_STRING;
  56. format_opt->required = NO;
  57. format_opt->multiple = NO;
  58. format_opt->options = "point,standard";
  59. desc = NULL;
  60. G_asprintf(&desc,
  61. "point;%s;standard;%s",
  62. _("simple x,y[,z] list"),
  63. _("GRASS vector ASCII format"));
  64. format_opt->descriptions = desc;
  65. format_opt->answer = "point";
  66. format_opt->description = _("Input file format");
  67. format_opt->guisection = _("Input format");
  68. delim_opt = G_define_standard_option(G_OPT_F_SEP);
  69. delim_opt->guisection = _("Input format");
  70. skip_opt = G_define_option();
  71. skip_opt->key = "skip";
  72. skip_opt->type = TYPE_INTEGER;
  73. skip_opt->required = NO;
  74. skip_opt->multiple = NO;
  75. skip_opt->answer = "0";
  76. skip_opt->description =
  77. _("Number of header lines to skip at top of input file (points mode)");
  78. skip_opt->guisection = _("Points");
  79. columns_opt = G_define_option();
  80. columns_opt->key = "columns";
  81. columns_opt->type = TYPE_STRING;
  82. columns_opt->required = NO;
  83. columns_opt->multiple = NO;
  84. columns_opt->guisection = _("Points");
  85. columns_opt->label = _("Column definition in SQL style (points mode)");
  86. columns_opt->description = _("For example: "
  87. "'x double precision, y double precision, cat int, "
  88. "name varchar(10)'");
  89. xcol_opt = G_define_option();
  90. xcol_opt->key = "x";
  91. xcol_opt->type = TYPE_INTEGER;
  92. xcol_opt->required = NO;
  93. xcol_opt->multiple = NO;
  94. xcol_opt->answer = "1";
  95. xcol_opt->guisection = _("Points");
  96. xcol_opt->label = ("Number of column used as x coordinate (points mode)");
  97. xcol_opt->description = _("First column is 1");
  98. ycol_opt = G_define_option();
  99. ycol_opt->key = "y";
  100. ycol_opt->type = TYPE_INTEGER;
  101. ycol_opt->required = NO;
  102. ycol_opt->multiple = NO;
  103. ycol_opt->answer = "2";
  104. ycol_opt->guisection = _("Points");
  105. ycol_opt->label = _("Number of column used as y coordinate (points mode)");
  106. ycol_opt->description = _("First column is 1");
  107. zcol_opt = G_define_option();
  108. zcol_opt->key = "z";
  109. zcol_opt->type = TYPE_INTEGER;
  110. zcol_opt->required = NO;
  111. zcol_opt->multiple = NO;
  112. zcol_opt->answer = "0";
  113. zcol_opt->guisection = _("Points");
  114. zcol_opt->label = _("Number of column used as z coordinate (points mode)");
  115. zcol_opt->description = _("First column is 1. If 0, z coordinate is not used");
  116. catcol_opt = G_define_option();
  117. catcol_opt->key = "cat";
  118. catcol_opt->type = TYPE_INTEGER;
  119. catcol_opt->required = NO;
  120. catcol_opt->multiple = NO;
  121. catcol_opt->answer = "0";
  122. catcol_opt->guisection = _("Points");
  123. catcol_opt->label =
  124. _("Number of column used as category (points mode)");
  125. catcol_opt->description =
  126. _("First column is 1. If 0, unique category is assigned to each row and written to new column 'cat'");
  127. zcoorf = G_define_flag();
  128. zcoorf->key = 'z';
  129. zcoorf->description = _("Create 3D vector map");
  130. e_flag = G_define_flag();
  131. e_flag->key = 'e';
  132. e_flag->description =
  133. _("Create a new empty vector map and exit. Nothing is read from input.");
  134. noheader_flag = G_define_flag();
  135. noheader_flag->key = 'n';
  136. noheader_flag->description =
  137. _("Do not expect a header when reading in standard format");
  138. noheader_flag->guisection = _("Input format");
  139. t_flag = G_define_flag();
  140. t_flag->key = 't';
  141. t_flag->description = _("Do not create table in points mode");
  142. t_flag->guisection = _("Points");
  143. notopol_flag = G_define_standard_flag(G_FLG_V_TOPO);
  144. notopol_flag->description = _("Do not build topology in points mode");
  145. notopol_flag->guisection = _("Points");
  146. region_flag = G_define_flag();
  147. region_flag->key = 'r';
  148. region_flag->description =
  149. _("Only import points falling within current region (points mode)");
  150. region_flag->guisection = _("Points");
  151. if (G_parser(argc, argv))
  152. exit(EXIT_FAILURE);
  153. if (format_opt->answer[0] == 'p')
  154. format = GV_ASCII_FORMAT_POINT;
  155. else
  156. format = GV_ASCII_FORMAT_STD;
  157. skip_lines = atoi(skip_opt->answer);
  158. if (skip_lines < 0)
  159. G_fatal_error(_("Please specify reasonable number of lines to skip"));
  160. if (zcoorf->answer && format == GV_ASCII_FORMAT_POINT && !zcol_opt->answer)
  161. G_fatal_error(_("Please specify z column"));
  162. xcol = atoi(xcol_opt->answer) - 1;
  163. ycol = atoi(ycol_opt->answer) - 1;
  164. zcol = atoi(zcol_opt->answer) - 1;
  165. /* specifying zcol= implies that a 3D map is needed */
  166. if (zcol >= 0 && !zcoorf->answer)
  167. zcoorf->answer = TRUE;
  168. if (zcoorf->answer && format == GV_ASCII_FORMAT_POINT && zcol < 0)
  169. G_fatal_error(_("Please specify reasonable z column"));
  170. catcol = atoi(catcol_opt->answer) - 1;
  171. if (xcol+1 < 1 || ycol+1 < 1 || zcol+1 < 0 || catcol+1 < 0)
  172. G_fatal_error(_("Column numbers must not be negative"));
  173. ascii = G_open_option_file(old);
  174. fs = G_option_to_separator(delim_opt);
  175. /* check dimension */
  176. if (zcoorf->answer) {
  177. zcoor = 1;
  178. }
  179. if (Vect_open_new(&Map, new->answer, zcoor) < 0)
  180. G_fatal_error(_("Unable to create vector map <%s>"), new->answer);
  181. Vect_set_error_handler_io(NULL, &Map);
  182. Vect_hist_command(&Map);
  183. if (e_flag->answer) {
  184. Vect_build(&Map);
  185. Vect_close(&Map);
  186. exit(EXIT_SUCCESS);
  187. }
  188. if (format == GV_ASCII_FORMAT_POINT) {
  189. int i, rowlen, ncols, minncols, *coltype, *coltype2, *collen, nrows;
  190. int n_int = 0, n_double = 0, n_string = 0;
  191. char buf[1000];
  192. struct field_info *Fi;
  193. char *tmp, *key;
  194. dbDriver *driver;
  195. dbString sql;
  196. FILE *tmpascii;
  197. /* Open temporary file */
  198. tmp = G_tempfile();
  199. if (NULL == (tmpascii = fopen(tmp, "w+"))) {
  200. G_fatal_error(_("Unable to open temporary file <%s>"), tmp);
  201. }
  202. unlink(tmp);
  203. points_analyse(ascii, tmpascii, fs, &rowlen, &ncols, &minncols,
  204. &nrows, &coltype, &collen, skip_lines, xcol, ycol,
  205. region_flag->answer);
  206. G_verbose_message(_("Maximum input row length: %d"), rowlen);
  207. if (ncols != minncols) {
  208. G_message(_("Maximum number of columns: %d"), ncols);
  209. G_message(_("Minimum number of columns: %d"), minncols);
  210. }
  211. else {
  212. G_message(_("Number of columns: %d"), ncols);
  213. }
  214. /* check column numbers */
  215. if (xcol >= minncols) {
  216. G_fatal_error(_("'%s' column number > minimum last column number "
  217. "(incorrect field separator or format?)"), "x");
  218. }
  219. if (ycol >= minncols) {
  220. G_fatal_error(_("'%s' column number > minimum last column number "
  221. "(incorrect field separator or format?)"), "y");
  222. }
  223. if (zcol >= minncols) {
  224. G_fatal_error(_("'%s' column number > minimum last column number "
  225. "(incorrect field separator or format?)"), "z");
  226. }
  227. if (catcol >= minncols) {
  228. G_fatal_error(_("'%s' column number > minimum last column number "
  229. "(incorrect field separator or format?)"), "cat");
  230. }
  231. if (coltype[xcol] == DB_C_TYPE_STRING) {
  232. G_fatal_error(_("'%s' column is not of number type"), "x");
  233. }
  234. if (coltype[ycol] == DB_C_TYPE_STRING) {
  235. G_fatal_error(_("'%s' column is not of number type"), "y");
  236. }
  237. if (zcol >= 0 && coltype[zcol] == DB_C_TYPE_STRING) {
  238. G_fatal_error(_("'%s' column is not of number type"), "z");
  239. }
  240. if (catcol >= 0 && coltype[catcol] == DB_C_TYPE_STRING) {
  241. G_fatal_error(_("'%s' column is not of number type"), "cat");
  242. }
  243. /* Create table */
  244. make_table = FALSE;
  245. for (i = 0; i < ncols; i++) {
  246. if (xcol != i && ycol != i && zcol != i && catcol != i) {
  247. make_table = TRUE;
  248. break;
  249. }
  250. }
  251. if (t_flag->answer) {
  252. make_table = FALSE;
  253. }
  254. if (make_table) {
  255. Fi = Vect_default_field_info(&Map, 1, NULL, GV_1TABLE);
  256. driver =
  257. db_start_driver_open_database(Fi->driver,
  258. Vect_subst_var(Fi->database,
  259. &Map));
  260. if (driver == NULL) {
  261. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  262. Vect_subst_var(Fi->database, &Map), Fi->driver);
  263. }
  264. db_begin_transaction(driver);
  265. db_init_string(&sql);
  266. sprintf(buf, "create table %s ( ", Fi->table);
  267. db_append_string(&sql, buf);
  268. if (catcol < 0) {
  269. db_append_string(&sql, "cat integer, ");
  270. }
  271. for (i = 0; i < ncols; i++) {
  272. if (i > 0 && !columns_opt->answer) {
  273. db_append_string(&sql, ", ");
  274. }
  275. if (catcol == i && coltype[i] != DB_C_TYPE_INT) {
  276. G_fatal_error(_("Category column is not of integer type"));
  277. }
  278. switch (coltype[i]) {
  279. case DB_C_TYPE_INT:
  280. G_verbose_message("Column: %d type: integer", i + 1);
  281. if (!columns_opt->answer) {
  282. sprintf(buf, "int_%d integer", n_int + 1);
  283. db_append_string(&sql, buf);
  284. if (catcol == i) {
  285. sprintf(buf, "int_%d", n_int + 1);
  286. key = G_store(buf);
  287. }
  288. }
  289. n_int++;
  290. break;
  291. case DB_C_TYPE_DOUBLE:
  292. G_verbose_message("Column: %d type: double", i + 1);
  293. if (!columns_opt->answer) {
  294. sprintf(buf, "dbl_%d double precision", n_double + 1);
  295. db_append_string(&sql, buf);
  296. }
  297. n_double++;
  298. break;
  299. case DB_C_TYPE_STRING:
  300. G_verbose_message("Column: %d type: string length: %d",
  301. i + 1, collen[i]);
  302. if (!columns_opt->answer) {
  303. sprintf(buf, "str_%d varchar(%d)", n_string + 1,
  304. collen[i]);
  305. db_append_string(&sql, buf);
  306. }
  307. n_string++;
  308. break;
  309. }
  310. }
  311. if (columns_opt->answer) {
  312. db_append_string(&sql, columns_opt->answer);
  313. }
  314. db_append_string(&sql, " )");
  315. /* this link is added with default 'cat' key, later deleted and replaced by true key name,
  316. * otherwise if module crashes when the table exists but link is not written it makes troubles */
  317. Vect_map_add_dblink(&Map, 1, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  318. Fi->driver);
  319. /* Create table */
  320. G_debug(3, db_get_string(&sql));
  321. if (db_execute_immediate(driver, &sql) != DB_OK) {
  322. G_fatal_error(_("Unable to create table: %s"),
  323. db_get_string(&sql));
  324. }
  325. /* Grant */
  326. if (db_grant_on_table
  327. (driver, Fi->table, DB_PRIV_SELECT,
  328. DB_GROUP | DB_PUBLIC) != DB_OK) {
  329. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  330. Fi->table);
  331. }
  332. /* Check column types */
  333. if (columns_opt->answer) {
  334. int nc;
  335. dbTable *table;
  336. dbColumn *column;
  337. db_set_string(&sql, Fi->table);
  338. if (db_describe_table(driver, &sql, &table) != DB_OK) {
  339. G_fatal_error(_("Unable to describe table <%s>"),
  340. Fi->table);
  341. }
  342. nc = db_get_table_number_of_columns(table);
  343. if ((catcol >= 0 && nc != ncols) ||
  344. (catcol < 0 && (nc - 1) != ncols)) {
  345. G_fatal_error(_("Number of columns defined (%d) does not match number "
  346. "of columns (%d) in input"),
  347. catcol < 0 ? nc - 1 : nc, ncols);
  348. }
  349. coltype2 = (int *)G_malloc(ncols * sizeof(int));
  350. for (i = 0; i < ncols; i++) {
  351. int dbcol, ctype, length;
  352. if (catcol < 0)
  353. dbcol = i + 1; /* first is category */
  354. else
  355. dbcol = i;
  356. column = db_get_table_column(table, dbcol);
  357. ctype =
  358. db_sqltype_to_Ctype(db_get_column_sqltype(column));
  359. length = db_get_column_length(column);
  360. coltype2[i] = ctype;
  361. if (catcol == i) { /* if catcol == -1 it cannot be tru */
  362. key = G_store(db_get_column_name(column));
  363. }
  364. switch (coltype[i]) {
  365. case DB_C_TYPE_INT:
  366. if (ctype == DB_C_TYPE_DOUBLE) {
  367. G_warning(_("Column number %d <%s> defined as double "
  368. "has only integer values"), i + 1,
  369. db_get_column_name(column));
  370. }
  371. else if (ctype == DB_C_TYPE_STRING) {
  372. G_warning(_("Column number %d <%s> defined as string "
  373. "has only integer values"), i + 1,
  374. db_get_column_name(column));
  375. }
  376. break;
  377. case DB_C_TYPE_DOUBLE:
  378. if (ctype == DB_C_TYPE_INT) {
  379. G_fatal_error(_("Column number %d <%s> defined as integer "
  380. "has double values"), i + 1,
  381. db_get_column_name(column));
  382. }
  383. else if (ctype == DB_C_TYPE_STRING) {
  384. G_warning(_("Column number %d <%s> defined as string "
  385. "has double values"), i + 1,
  386. db_get_column_name(column));
  387. }
  388. break;
  389. case DB_C_TYPE_STRING:
  390. if (ctype == DB_C_TYPE_INT) {
  391. G_fatal_error(_("Column number %d <%s> defined as integer "
  392. "has string values"), i + 1,
  393. db_get_column_name(column));
  394. }
  395. else if (ctype == DB_C_TYPE_DOUBLE) {
  396. G_fatal_error(_("Column number %d <%s> defined as double "
  397. "has string values"), i + 1,
  398. db_get_column_name(column));
  399. }
  400. if (length < collen[i]) {
  401. G_fatal_error(_("Length of column %d <%s> (%d) is less than "
  402. "maximum value " "length (%d)"),
  403. i + 1, db_get_column_name(column),
  404. length, collen[i]);
  405. }
  406. break;
  407. }
  408. }
  409. }
  410. else {
  411. coltype2 = coltype;
  412. }
  413. if (catcol < 0) {
  414. key = GV_KEY_COLUMN;
  415. }
  416. else if (!columns_opt->answer) {
  417. }
  418. if (db_create_index2(driver, Fi->table, key) != DB_OK)
  419. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  420. Fi->table, key);
  421. Vect_map_del_dblink(&Map, 1);
  422. Vect_map_add_dblink(&Map, 1, NULL, Fi->table, key, Fi->database,
  423. Fi->driver);
  424. table = Fi->table;
  425. }
  426. else {
  427. driver = NULL;
  428. table = NULL;
  429. }
  430. points_to_bin(tmpascii, rowlen, &Map, driver, table, fs, nrows, ncols,
  431. coltype2, xcol, ycol, zcol, catcol, skip_lines);
  432. if (driver) {
  433. G_message(_("Populating table..."));
  434. db_commit_transaction(driver);
  435. if(db_close_database_shutdown_driver(driver) == DB_FAILED)
  436. #ifdef __MINGW32__
  437. G_warning("FIXME: db_close_database_shutdown_driver() fails on WinGrass. Ignoring...");
  438. #else
  439. G_fatal_error(_("Could not close attribute table. The DBMI driver did not accept all attributes"));
  440. #endif
  441. }
  442. fclose(tmpascii);
  443. }
  444. else { /* FORMAT_ALL (standard mode) */
  445. if (!noheader_flag->answer)
  446. if (Vect_read_ascii_head(ascii, &Map) == -1)
  447. G_fatal_error(_("Import failed"));
  448. if (Vect_read_ascii(ascii, &Map) == -1)
  449. G_fatal_error(_("Import failed"));
  450. }
  451. G_close_option_file(ascii);
  452. if (notopol_flag->answer) {
  453. Vect_close(&Map);
  454. }
  455. else {
  456. Vect_build(&Map);
  457. Vect_close(&Map);
  458. }
  459. exit(EXIT_SUCCESS);
  460. }