main.c 17 KB

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