main.c 15 KB

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