main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. G_fatal_error(_("'%s' column number > minimum last column number "
  230. "(incorrect field separator or format?)"), "x");
  231. }
  232. if (ycol >= minncols) {
  233. G_fatal_error(_("'%s' column number > minimum last column number "
  234. "(incorrect field separator or format?)"), "y");
  235. }
  236. if (zcol >= minncols) {
  237. G_fatal_error(_("'%s' column number > minimum last column number "
  238. "(incorrect field separator or format?)"), "z");
  239. }
  240. if (catcol >= minncols) {
  241. G_fatal_error(_("'%s' column number > minimum last column number "
  242. "(incorrect field separator or format?)"), "cat");
  243. }
  244. if (coltype[xcol] == DB_C_TYPE_STRING) {
  245. G_fatal_error(_("'%s' column is not of number type"), "x");
  246. }
  247. if (coltype[ycol] == DB_C_TYPE_STRING) {
  248. G_fatal_error(_("'%s' column is not of number type"), "y");
  249. }
  250. if (zcol >= 0 && coltype[zcol] == DB_C_TYPE_STRING) {
  251. G_fatal_error(_("'%s' column is not of number type"), "z");
  252. }
  253. if (catcol >= 0 && coltype[catcol] == DB_C_TYPE_STRING) {
  254. G_fatal_error(_("'%s' column is not of number type"), "cat");
  255. }
  256. /* Create table */
  257. make_table = FALSE;
  258. for (i = 0; i < ncols; i++) {
  259. if (xcol != i && ycol != i && zcol != i && catcol != i) {
  260. make_table = TRUE;
  261. break;
  262. }
  263. }
  264. if (t_flag->answer) {
  265. make_table = FALSE;
  266. }
  267. if (make_table) {
  268. Fi = Vect_default_field_info(&Map, 1, NULL, GV_1TABLE);
  269. driver =
  270. db_start_driver_open_database(Fi->driver,
  271. Vect_subst_var(Fi->database,
  272. &Map));
  273. if (driver == NULL) {
  274. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  275. Vect_subst_var(Fi->database, &Map), Fi->driver);
  276. }
  277. db_begin_transaction(driver);
  278. db_init_string(&sql);
  279. sprintf(buf, "create table %s ( ", Fi->table);
  280. db_append_string(&sql, buf);
  281. if (catcol < 0) {
  282. db_append_string(&sql, "cat integer, ");
  283. }
  284. for (i = 0; i < ncols; i++) {
  285. if (i > 0 && !columns_opt->answer) {
  286. db_append_string(&sql, ", ");
  287. }
  288. if (catcol == i && coltype[i] != DB_C_TYPE_INT) {
  289. G_fatal_error(_("Category column is not of integer type"));
  290. }
  291. switch (coltype[i]) {
  292. case DB_C_TYPE_INT:
  293. G_verbose_message("Column: %d type: integer", i + 1);
  294. if (!columns_opt->answer) {
  295. sprintf(buf, "int_%d integer", n_int + 1);
  296. db_append_string(&sql, buf);
  297. if (catcol == i) {
  298. sprintf(buf, "int_%d", n_int + 1);
  299. key = G_store(buf);
  300. }
  301. }
  302. n_int++;
  303. break;
  304. case DB_C_TYPE_DOUBLE:
  305. G_verbose_message("Column: %d type: double", i + 1);
  306. if (!columns_opt->answer) {
  307. sprintf(buf, "dbl_%d double precision", n_double + 1);
  308. db_append_string(&sql, buf);
  309. }
  310. n_double++;
  311. break;
  312. case DB_C_TYPE_STRING:
  313. G_verbose_message("Column: %d type: string length: %d",
  314. i + 1, collen[i]);
  315. if (!columns_opt->answer) {
  316. sprintf(buf, "str_%d varchar(%d)", n_string + 1,
  317. collen[i]);
  318. db_append_string(&sql, buf);
  319. }
  320. n_string++;
  321. break;
  322. }
  323. }
  324. if (columns_opt->answer) {
  325. db_append_string(&sql, columns_opt->answer);
  326. }
  327. db_append_string(&sql, " )");
  328. /* this link is added with default 'cat' key, later deleted and replaced by true key name,
  329. * otherwise if module crashes when the table exists but link is not written it makes troubles */
  330. Vect_map_add_dblink(&Map, 1, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  331. Fi->driver);
  332. /* Create table */
  333. G_debug(3, db_get_string(&sql));
  334. if (db_execute_immediate(driver, &sql) != DB_OK) {
  335. G_fatal_error(_("Unable to create table: %s"),
  336. db_get_string(&sql));
  337. }
  338. /* Grant */
  339. if (db_grant_on_table
  340. (driver, Fi->table, DB_PRIV_SELECT,
  341. DB_GROUP | DB_PUBLIC) != DB_OK) {
  342. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  343. Fi->table);
  344. }
  345. /* Check column types */
  346. if (columns_opt->answer) {
  347. int nc;
  348. dbTable *table;
  349. dbColumn *column;
  350. db_set_string(&sql, Fi->table);
  351. if (db_describe_table(driver, &sql, &table) != DB_OK) {
  352. G_fatal_error(_("Unable to describe table <%s>"),
  353. Fi->table);
  354. }
  355. nc = db_get_table_number_of_columns(table);
  356. if ((catcol >= 0 && nc != ncols) ||
  357. (catcol < 0 && (nc - 1) != ncols)) {
  358. G_fatal_error(_("Number of columns defined (%d) does not match number "
  359. "of columns (%d) in input"),
  360. catcol < 0 ? nc - 1 : nc, ncols);
  361. }
  362. coltype2 = (int *)G_malloc(ncols * sizeof(int));
  363. for (i = 0; i < ncols; i++) {
  364. int dbcol, ctype, length;
  365. if (catcol < 0)
  366. dbcol = i + 1; /* first is category */
  367. else
  368. dbcol = i;
  369. column = db_get_table_column(table, dbcol);
  370. ctype =
  371. db_sqltype_to_Ctype(db_get_column_sqltype(column));
  372. length = db_get_column_length(column);
  373. coltype2[i] = ctype;
  374. if (catcol == i) { /* if catcol == -1 it cannot be tru */
  375. key = G_store(db_get_column_name(column));
  376. }
  377. switch (coltype[i]) {
  378. case DB_C_TYPE_INT:
  379. if (ctype == DB_C_TYPE_DOUBLE) {
  380. G_warning(_("Column number %d <%s> defined as double "
  381. "has only integer values"), i + 1,
  382. db_get_column_name(column));
  383. }
  384. else if (ctype == DB_C_TYPE_STRING) {
  385. G_warning(_("Column number %d <%s> defined as string "
  386. "has only integer values"), i + 1,
  387. db_get_column_name(column));
  388. }
  389. break;
  390. case DB_C_TYPE_DOUBLE:
  391. if (ctype == DB_C_TYPE_INT) {
  392. G_fatal_error(_("Column number %d <%s> defined as integer "
  393. "has double 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 double values"), i + 1,
  399. db_get_column_name(column));
  400. }
  401. break;
  402. case DB_C_TYPE_STRING:
  403. if (ctype == DB_C_TYPE_INT) {
  404. G_fatal_error(_("Column number %d <%s> defined as integer "
  405. "has string values"), i + 1,
  406. db_get_column_name(column));
  407. }
  408. else if (ctype == DB_C_TYPE_DOUBLE) {
  409. G_fatal_error(_("Column number %d <%s> defined as double "
  410. "has string values"), i + 1,
  411. db_get_column_name(column));
  412. }
  413. if (length < collen[i]) {
  414. G_fatal_error(_("Length of column %d <%s> (%d) is less than "
  415. "maximum value " "length (%d)"),
  416. i + 1, db_get_column_name(column),
  417. length, collen[i]);
  418. }
  419. break;
  420. }
  421. }
  422. }
  423. else {
  424. coltype2 = coltype;
  425. }
  426. if (catcol < 0) {
  427. key = GV_KEY_COLUMN;
  428. }
  429. else if (!columns_opt->answer) {
  430. }
  431. if (db_create_index2(driver, Fi->table, key) != DB_OK)
  432. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  433. Fi->table, key);
  434. Vect_map_del_dblink(&Map, 1);
  435. Vect_map_add_dblink(&Map, 1, NULL, Fi->table, key, Fi->database,
  436. Fi->driver);
  437. table = Fi->table;
  438. }
  439. else {
  440. driver = NULL;
  441. table = NULL;
  442. }
  443. points_to_bin(tmpascii, rowlen, &Map, driver, table, fs, nrows, ncols,
  444. coltype2, xcol, ycol, zcol, catcol, skip_lines);
  445. if (driver) {
  446. G_message(_("Populating table..."));
  447. db_commit_transaction(driver);
  448. if(db_close_database_shutdown_driver(driver) == DB_FAILED)
  449. #ifdef __MINGW32__
  450. G_warning("FIXME: db_close_database_shutdown_driver() fails on WinGrass. Ignoring...");
  451. #else
  452. G_fatal_error(_("Could not close attribute table. The DBMI driver did not accept all attributes"));
  453. #endif
  454. }
  455. fclose(tmpascii);
  456. }
  457. else { /* FORMAT_ALL (standard mode) */
  458. if (!noheader_flag->answer)
  459. if (Vect_read_ascii_head(ascii, &Map) == -1)
  460. G_fatal_error(_("Import failed"));
  461. if (Vect_read_ascii(ascii, &Map) == -1)
  462. G_fatal_error(_("Import failed"));
  463. }
  464. if (ascii != stdin)
  465. fclose(ascii);
  466. if (notopol_flag->answer) {
  467. Vect_close(&Map);
  468. }
  469. else {
  470. Vect_build(&Map);
  471. Vect_close(&Map);
  472. }
  473. exit(EXIT_SUCCESS);
  474. }