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