main.c 17 KB

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