main.c 16 KB

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