main.c 17 KB

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