in.c 15 KB

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