main.c 16 KB

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