ascii.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*!
  2. \file lib/vector/Vlib/ascii.c
  3. \brief Vector library - GRASS ASCII vector format
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2011-2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL
  9. \author Updated for GRASS 7 (SF support) by Martin Landa <landa.martin gmail.com>
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <grass/vector.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/glocale.h>
  17. #define BUFFSIZE 128
  18. static int srch(const void *, const void *);
  19. static int check_cat(const struct line_cats *, const struct cat_list *,
  20. const int *, int);
  21. static void free_col_arrays(int *, char *, char**);
  22. /*!
  23. \brief Read data in GRASS ASCII vector format
  24. \param ascii pointer to the input ASCII file
  25. \param[out] Map pointer to the output Map_info structure
  26. \return number of read features
  27. \return -1 on error
  28. */
  29. int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
  30. {
  31. char ctype;
  32. char buff[BUFFSIZE];
  33. char east_str[256], north_str[256];
  34. double *xarray;
  35. double *yarray;
  36. double *zarray;
  37. double *x, *y, *z;
  38. int i, n_points, n_coors, n_cats, n_lines;
  39. int type;
  40. int alloc_points;
  41. struct line_pnts *Points;
  42. struct line_cats *Cats;
  43. int catn, cat;
  44. /* Must always use this to create an initialized line_pnts structure */
  45. Points = Vect_new_line_struct();
  46. Cats = Vect_new_cats_struct();
  47. /*alloc_points = 1000 ; */
  48. alloc_points = 1;
  49. xarray = (double *)G_calloc(alloc_points, sizeof(double));
  50. yarray = (double *)G_calloc(alloc_points, sizeof(double));
  51. zarray = (double *)G_calloc(alloc_points, sizeof(double));
  52. n_lines = 0;
  53. while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
  54. n_cats = 0;
  55. if (buff[0] == '\0') {
  56. G_debug(3, "a2b: skipping blank line");
  57. continue;
  58. }
  59. if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
  60. n_coors < 0 || n_cats < 0) {
  61. if (ctype == '#') {
  62. G_debug(2, "a2b: skipping commented line");
  63. continue;
  64. }
  65. G_fatal_error(_("Error reading ASCII file: (bad type) [%s]"),
  66. buff);
  67. }
  68. if (ctype == '#') {
  69. G_debug(2, "a2b: Skipping commented line");
  70. continue;
  71. }
  72. switch (ctype) {
  73. case 'A':
  74. type = GV_BOUNDARY;
  75. break;
  76. case 'B':
  77. type = GV_BOUNDARY;
  78. break;
  79. case 'C':
  80. type = GV_CENTROID;
  81. break;
  82. case 'L':
  83. type = GV_LINE;
  84. break;
  85. case 'P':
  86. type = GV_POINT;
  87. break;
  88. case 'F':
  89. type = GV_FACE;
  90. break;
  91. case 'K':
  92. type = GV_KERNEL;
  93. break;
  94. case 'a':
  95. case 'b':
  96. case 'c':
  97. case 'l':
  98. case 'p':
  99. type = 0; /* dead -> ignore */
  100. break;
  101. default:
  102. G_fatal_error(_("Error reading ASCII file: (unknown type) [%s]"),
  103. buff);
  104. }
  105. G_debug(5, "feature type = %d", type);
  106. n_points = 0;
  107. x = xarray;
  108. y = yarray;
  109. z = zarray;
  110. /* Collect the points */
  111. for (i = 0; i < n_coors; i++) {
  112. if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
  113. G_fatal_error(_("End of ASCII file reached before end of coordinates"));
  114. if (buff[0] == '\0') {
  115. G_debug(3, "a2b: skipping blank line while reading vertices");
  116. i--;
  117. continue;
  118. }
  119. *z = 0;
  120. if (sscanf(buff, "%lf%lf%lf", x, y, z) < 2) {
  121. if (sscanf(buff, " %s %s %lf", east_str, north_str, z) < 2) {
  122. G_fatal_error(_("Error reading ASCII file: (bad point) [%s]"),
  123. buff);
  124. } else {
  125. if( ! G_scan_easting(east_str, x, G_projection()) )
  126. G_fatal_error(_("Unparsable longitude value: [%s]"),
  127. east_str);
  128. if( ! G_scan_northing(north_str, y, G_projection()) )
  129. G_fatal_error(_("Unparsable latitude value: [%s]"),
  130. north_str);
  131. }
  132. }
  133. G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
  134. *x, *y, *z);
  135. n_points++;
  136. x++;
  137. y++;
  138. z++;
  139. if (n_points >= alloc_points) {
  140. alloc_points = n_points + 1000;
  141. xarray =
  142. (double *)G_realloc((void *)xarray,
  143. alloc_points * sizeof(double));
  144. yarray =
  145. (double *)G_realloc((void *)yarray,
  146. alloc_points * sizeof(double));
  147. zarray =
  148. (double *)G_realloc((void *)zarray,
  149. alloc_points * sizeof(double));
  150. x = xarray + n_points;
  151. y = yarray + n_points;
  152. z = zarray + n_points;
  153. }
  154. }
  155. /* Collect the cats */
  156. for (i = 0; i < n_cats; i++) {
  157. if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
  158. G_fatal_error(_("End of ASCII file reached before end of categories"));
  159. if (buff[0] == '\0') {
  160. G_debug(3,
  161. "a2b: skipping blank line while reading category info");
  162. i--;
  163. continue;
  164. }
  165. if (sscanf(buff, "%u%u", &catn, &cat) != 2)
  166. G_fatal_error(_("Error reading categories: [%s]"), buff);
  167. Vect_cat_set(Cats, catn, cat);
  168. }
  169. /* Allocation is handled for line_pnts */
  170. if (0 >
  171. Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
  172. G_fatal_error(_("Out of memory"));
  173. if (type > 0) {
  174. if (-1 == Vect_write_line(Map, type, Points, Cats)) {
  175. G_warning(_("Unable to write new feature"));
  176. return -1;
  177. }
  178. n_lines++;
  179. }
  180. Vect_reset_cats(Cats);
  181. }
  182. Vect_destroy_line_struct(Points);
  183. Vect_destroy_cats_struct(Cats);
  184. return n_lines;
  185. }
  186. /*!
  187. \brief Read header of GRASS ASCII vector format
  188. \param dascii pointer to the ASCII file
  189. \param Map pointer to Map_info structure
  190. \return 0 on success
  191. \return -1 on error
  192. */
  193. int Vect_read_ascii_head(FILE *dascii, struct Map_info *Map)
  194. {
  195. char buff[1024];
  196. char *ptr;
  197. for (;;) {
  198. if (0 == G_getl2(buff, sizeof(buff) - 1, dascii))
  199. return (0);
  200. /* Last line of header */
  201. if (strncmp(buff, "VERTI:", 6) == 0)
  202. return (0);
  203. if (!(ptr = strchr(buff, ':')))
  204. G_fatal_error(_("Unexpected data in vector head:\n[%s]"), buff);
  205. ptr++; /* Search for the start of text */
  206. while (*ptr == ' ')
  207. ptr++;
  208. if (strncmp(buff, "ORGANIZATION:", 12) == 0)
  209. Vect_set_organization(Map, ptr);
  210. else if (strncmp(buff, "DIGIT DATE:", 11) == 0)
  211. Vect_set_date(Map, ptr);
  212. else if (strncmp(buff, "DIGIT NAME:", 11) == 0)
  213. Vect_set_person(Map, ptr);
  214. else if (strncmp(buff, "MAP NAME:", 9) == 0)
  215. Vect_set_map_name(Map, ptr);
  216. else if (strncmp(buff, "MAP DATE:", 9) == 0)
  217. Vect_set_map_date(Map, ptr);
  218. else if (strncmp(buff, "MAP SCALE:", 10) == 0)
  219. Vect_set_scale(Map, atoi(ptr));
  220. else if (strncmp(buff, "OTHER INFO:", 11) == 0)
  221. Vect_set_comment(Map, ptr);
  222. else if (strncmp(buff, "ZONE:", 5) == 0 ||
  223. strncmp(buff, "UTM ZONE:", 9) == 0)
  224. Vect_set_zone(Map, atoi(ptr));
  225. else if (strncmp(buff, "WEST EDGE:", 10) == 0) {
  226. }
  227. else if (strncmp(buff, "EAST EDGE:", 10) == 0) {
  228. }
  229. else if (strncmp(buff, "SOUTH EDGE:", 11) == 0) {
  230. }
  231. else if (strncmp(buff, "NORTH EDGE:", 11) == 0) {
  232. }
  233. else if (strncmp(buff, "MAP THRESH:", 11) == 0)
  234. Vect_set_thresh(Map, atof(ptr));
  235. else {
  236. G_warning(_("Unknown keyword <%s> in vector head"), buff);
  237. }
  238. }
  239. /* NOTREACHED */
  240. }
  241. /*!
  242. \brief Write data to GRASS ASCII vector format
  243. \param[out] ascii pointer to the output ASCII file
  244. \param[out] att att file (< version 5 only)
  245. \param Map pointer to Map_info structure
  246. \param ver version number 4 or 5
  247. \param format format GV_ASCII_FORMAT_POINT or GV_ASCII_FORMAT_STD
  248. \param dp number of significant digits
  249. \param fs field separator
  250. \param region_flag check region
  251. \param type feature type filter
  252. \param field field number
  253. \param Clist list of categories to filter features or NULL
  254. \param where SQL select where statement to filter features or NULL
  255. \param column_names array of columns to be included to the output or NULL
  256. "*" as the first item in the array indicates all columns
  257. \param header TRUE to print also header
  258. \return number of written features
  259. \return -1 on error
  260. */
  261. int Vect_write_ascii(FILE *ascii,
  262. FILE *att, struct Map_info *Map, int ver,
  263. int format, int dp, char *fs, int region_flag, int type,
  264. int field, const struct cat_list *Clist, const char* where,
  265. const char **column_names, int header)
  266. {
  267. int ltype, ctype, i, cat, n_lines, line, left, right, found;
  268. double *xptr, *yptr, *zptr, x, y;
  269. static struct line_pnts *Points;
  270. struct line_cats *Cats, *ACats;
  271. char *xstring, *ystring, *zstring;
  272. size_t xsize, ysize, zsize;
  273. struct Cell_head window;
  274. struct ilist *fcats;
  275. int count;
  276. /* where || columns */
  277. struct field_info *Fi;
  278. dbDriver *driver;
  279. dbValue value;
  280. dbHandle handle;
  281. int *cats, ncats, more;
  282. dbTable *Table;
  283. dbString dbstring;
  284. dbColumn *Column;
  285. dbValue *Value;
  286. char buf[2000];
  287. dbCursor cursor;
  288. /* columns */
  289. char **columns;
  290. int *coltypes;
  291. char *all_columns;
  292. Fi = NULL;
  293. driver = NULL;
  294. columns = NULL;
  295. coltypes = NULL;
  296. all_columns = NULL;
  297. G_zero(&value, sizeof(dbValue));
  298. db_init_string(&dbstring);
  299. /* TODO: free memory allocated by G_asprintf(),
  300. * this is a bad memory leak */
  301. xstring = NULL;
  302. ystring = NULL;
  303. zstring = NULL;
  304. xsize = 0;
  305. ysize = 0;
  306. zsize = 0;
  307. /* get the region */
  308. G_get_window(&window);
  309. count = n_lines = ncats = 0;
  310. xstring = ystring = zstring = NULL;
  311. cats = NULL;
  312. if (where || column_names) {
  313. Fi = Vect_get_field(Map, field);
  314. if (!Fi) {
  315. G_fatal_error(_("Database connection not defined for layer %d"),
  316. field);
  317. }
  318. driver = db_start_driver(Fi->driver);
  319. if (!driver)
  320. G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
  321. db_init_handle(&handle);
  322. db_set_handle(&handle, Fi->database, NULL);
  323. if (db_open_database(driver, &handle) != DB_OK)
  324. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  325. Fi->database, Fi->driver);
  326. /* select cats (sorted array) */
  327. ncats = db_select_int(driver, Fi->table, Fi->key, where, &cats);
  328. G_debug(3, "%d categories selected from table <%s>", ncats, Fi->table);
  329. if (!column_names) {
  330. db_close_database(driver);
  331. db_shutdown_driver(driver);
  332. }
  333. else {
  334. int len_all = 0;
  335. if (column_names[0] && strcmp(column_names[0], "*") == 0) {
  336. int icol, ncols;
  337. const char *col_name;
  338. /* all columns */
  339. db_set_string(&dbstring, Fi->table);
  340. if (db_describe_table(driver, &dbstring, &Table) != DB_OK) {
  341. G_warning(_("Unable to describe table <%s>"), Fi->table);
  342. return -1;
  343. }
  344. ncols = db_get_table_number_of_columns(Table);
  345. /* key column skipped */
  346. columns = (char **) G_malloc(ncols * sizeof(char *));
  347. icol = i = 0;
  348. for (i = 0; i < ncols; i++) {
  349. col_name = db_get_column_name(db_get_table_column(Table, i));
  350. if (strcmp(Fi->key, col_name) == 0)
  351. continue;
  352. columns[icol++] = G_store(col_name);
  353. }
  354. columns[ncols-1] = NULL;
  355. db_zero_string(&dbstring);
  356. db_free_table(Table);
  357. Table = NULL;
  358. }
  359. else {
  360. columns = (char **)column_names;
  361. }
  362. /* selected columns only */
  363. i = 0;
  364. while (columns[i])
  365. len_all += strlen(columns[i++]);
  366. coltypes = G_malloc(i * sizeof(int));
  367. all_columns = G_malloc(len_all + i + 2);
  368. i = 0;
  369. strcpy(all_columns, columns[0]);
  370. while (columns[i]) {
  371. /* get column types */
  372. coltypes[i] = db_column_Ctype(driver, Fi->table, columns[i]);
  373. if (coltypes[i] < 0) {
  374. db_close_database(driver);
  375. db_shutdown_driver(driver);
  376. G_warning(_("Unknown type of column <%s>, export cancelled"),
  377. columns[i]);
  378. return -1;
  379. }
  380. if (i > 0) {
  381. strcat(all_columns, ",");
  382. strcat(all_columns, columns[i]);
  383. }
  384. i++;
  385. }
  386. }
  387. }
  388. Points = Vect_new_line_struct();
  389. Cats = Vect_new_cats_struct();
  390. ACats = Vect_new_cats_struct();
  391. fcats = Vect_new_list();
  392. /* by default, read_next_line will NOT read Dead lines */
  393. /* but we can override that (in Level I only) by specifying */
  394. /* the type -1, which means match all line types */
  395. Vect_rewind(Map);
  396. line = 0;
  397. while (TRUE) {
  398. ltype = Vect_read_next_line(Map, Points, Cats);
  399. if (ltype == -1 ) { /* failure */
  400. if (columns) {
  401. db_close_database(driver);
  402. db_shutdown_driver(driver);
  403. free_col_arrays(coltypes, all_columns,
  404. column_names && strcmp(column_names[0], "*") == 0 ? columns : NULL);
  405. }
  406. return -1;
  407. }
  408. if (ltype == -2) { /* EOF */
  409. if (columns) {
  410. db_close_database(driver);
  411. db_shutdown_driver(driver);
  412. free_col_arrays(coltypes, all_columns,
  413. column_names && strcmp(column_names[0], "*") == 0 ? columns : NULL);
  414. }
  415. break;
  416. }
  417. line++;
  418. if (!(ltype & type))
  419. continue;
  420. if (format == GV_ASCII_FORMAT_POINT && !(ltype & GV_POINTS))
  421. continue;
  422. found = check_cat(Cats, Clist, cats, ncats);
  423. if (!found && ltype == GV_BOUNDARY &&
  424. type & GV_AREA && Vect_level(Map) > 1) {
  425. Vect_get_line_areas(Map, line, &left, &right);
  426. if (left < 0)
  427. left = Vect_get_isle_area(Map, abs(left));
  428. if (left > 0) {
  429. Vect_get_area_cats(Map, left, ACats);
  430. found = check_cat(ACats, Clist, cats, ncats);
  431. }
  432. if (right < 0)
  433. right = Vect_get_isle_area(Map, abs(right));
  434. if (!found && right > 0) {
  435. Vect_get_area_cats(Map, right, ACats);
  436. found = check_cat(ACats, Clist, cats, ncats);
  437. }
  438. }
  439. if (!found)
  440. continue;
  441. if (ver < 5) {
  442. Vect_cat_get(Cats, 1, &cat);
  443. }
  444. switch (ltype) {
  445. case GV_BOUNDARY:
  446. if (ver == 5)
  447. ctype = 'B';
  448. else
  449. ctype = 'A';
  450. break;
  451. case GV_CENTROID:
  452. if (ver < 5) {
  453. if (att != NULL) {
  454. if (cat > 0) {
  455. G_rasprintf(&xstring, &xsize, "%.*f", dp, Points->x[0]);
  456. G_trim_decimal(xstring);
  457. G_rasprintf(&ystring, &ysize, "%.*f", dp, Points->y[0]);
  458. G_trim_decimal(ystring);
  459. fprintf(att, "A %s %s %d\n", xstring, ystring, cat);
  460. }
  461. }
  462. continue;
  463. }
  464. ctype = 'C';
  465. break;
  466. case GV_LINE:
  467. ctype = 'L';
  468. break;
  469. case GV_POINT:
  470. ctype = 'P';
  471. break;
  472. case GV_FACE:
  473. ctype = 'F';
  474. break;
  475. case GV_KERNEL:
  476. ctype = 'K';
  477. break;
  478. default:
  479. ctype = 'X';
  480. G_warning(_("Unknown feature type %d"), (int)ltype);
  481. break;
  482. }
  483. if (format == GV_ASCII_FORMAT_POINT) {
  484. if (region_flag) {
  485. if ((window.east < Points->x[0]) ||
  486. (window.west > Points->x[0]))
  487. continue;
  488. }
  489. G_rasprintf(&xstring, &xsize, "%.*f", dp, Points->x[0]);
  490. G_trim_decimal(xstring);
  491. if (region_flag) {
  492. if ((window.north < Points->y[0]) ||
  493. (window.south > Points->y[0]))
  494. continue;
  495. }
  496. G_rasprintf(&ystring, &ysize, "%.*f", dp, Points->y[0]);
  497. G_trim_decimal(ystring);
  498. Vect_field_cat_get(Cats, field, fcats);
  499. /* print header */
  500. if (header && count < 1) {
  501. count++;
  502. if (Map->head.with_z)
  503. fprintf(ascii, "east%snorth%sheight%scat", fs, fs, fs);
  504. else
  505. fprintf(ascii, "east%snorth%scat", fs, fs);
  506. if (columns) {
  507. for (i = 0; columns[i]; i++) {
  508. if (db_select_value
  509. (driver, Fi->table, Fi->key, fcats->value[0],
  510. columns[i], &value) < 0)
  511. G_fatal_error(_("Unable to select record from table <%s> (key %s, column %s)"),
  512. Fi->table, Fi->key, columns[i]);
  513. if (columns[i])
  514. fprintf(ascii, "%s%s", fs, columns[i]);
  515. else
  516. fprintf(ascii, "%s", columns[i]); /* can not happen */
  517. }
  518. }
  519. fprintf(ascii, "\n");
  520. }
  521. if (Map->head.with_z && ver == 5) {
  522. if (region_flag) {
  523. if ((window.top < Points->z[0]) ||
  524. (window.bottom > Points->z[0]))
  525. continue;
  526. }
  527. G_rasprintf(&zstring, &zsize, "%.*f", dp, Points->z[0]);
  528. G_trim_decimal(zstring);
  529. fprintf(ascii, "%s%s%s%s%s", xstring, fs, ystring, fs,
  530. zstring);
  531. }
  532. else {
  533. fprintf(ascii, "%s%s%s", xstring, fs, ystring);
  534. }
  535. if (fcats->n_values > 0) {
  536. if (fcats->n_values > 1) {
  537. G_warning(_("Feature has more categories. Only first category (%d) "
  538. "is exported."), fcats->value[0]);
  539. }
  540. fprintf(ascii, "%s%d", fs, fcats->value[0]);
  541. /* print attributes */
  542. if (columns) {
  543. sprintf(buf, "SELECT %s FROM %s WHERE %s = %d",
  544. all_columns, Fi->table, Fi->key, fcats->value[0]);
  545. G_debug(2, "SQL: %s", buf);
  546. db_set_string(&dbstring, buf);
  547. if (db_open_select_cursor
  548. (driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
  549. db_close_database(driver);
  550. db_shutdown_driver(driver);
  551. G_fatal_error(_("Cannot select attributes for cat = %d"),
  552. fcats->value[0]);
  553. }
  554. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  555. db_close_database(driver);
  556. db_shutdown_driver(driver);
  557. G_fatal_error(_("Unable to fetch data from table"));
  558. }
  559. Table = db_get_cursor_table(&cursor);
  560. for(i = 0; columns[i]; i++) {
  561. Column = db_get_table_column(Table, i);
  562. Value = db_get_column_value(Column);
  563. if (db_test_value_isnull(Value)) {
  564. fprintf(ascii, "%s", fs);
  565. }
  566. else {
  567. switch(coltypes[i])
  568. {
  569. case DB_C_TYPE_INT: {
  570. fprintf(ascii, "%s%d", fs, db_get_value_int(Value));
  571. break;
  572. }
  573. case DB_C_TYPE_DOUBLE: {
  574. fprintf(ascii, "%s%.*f", fs, dp, db_get_value_double(Value));
  575. break;
  576. }
  577. case DB_C_TYPE_STRING: {
  578. fprintf(ascii, "%s%s", fs, db_get_value_string(Value));
  579. break;
  580. }
  581. case DB_C_TYPE_DATETIME: {
  582. break;
  583. }
  584. case -1:
  585. G_fatal_error(_("Column <%s> not found in table <%s>"),
  586. columns[i], Fi->table);
  587. default: G_fatal_error(_("Column <%s>: unsupported data type"),
  588. columns[i]);
  589. }
  590. }
  591. }
  592. db_close_cursor(&cursor);
  593. }
  594. }
  595. fprintf(ascii, "\n");
  596. }
  597. else if (format == GV_ASCII_FORMAT_STD) {
  598. /* FORMAT_STANDARD */
  599. if (ver == 5 && Cats->n_cats > 0)
  600. fprintf(ascii, "%c %d %d\n", ctype, Points->n_points,
  601. Cats->n_cats);
  602. else
  603. fprintf(ascii, "%c %d\n", ctype, Points->n_points);
  604. xptr = Points->x;
  605. yptr = Points->y;
  606. zptr = Points->z;
  607. while (Points->n_points--) {
  608. G_rasprintf(&xstring, &xsize, "%.*f", dp, *xptr++);
  609. G_trim_decimal(xstring);
  610. G_rasprintf(&ystring, &ysize, "%.*f", dp, *yptr++);
  611. G_trim_decimal(ystring);
  612. if (ver == 5) {
  613. if (Map->head.with_z) {
  614. G_rasprintf(&zstring, &zsize, "%.*f", dp, *zptr++);
  615. G_trim_decimal(zstring);
  616. fprintf(ascii, " %-12s %-12s %-12s\n", xstring,
  617. ystring, zstring);
  618. }
  619. else {
  620. fprintf(ascii, " %-12s %-12s\n", xstring, ystring);
  621. }
  622. } /*Version 4 */
  623. else {
  624. fprintf(ascii, " %-12s %-12s\n", ystring, xstring);
  625. }
  626. }
  627. if (ver == 5) {
  628. for (i = 0; i < Cats->n_cats; i++) {
  629. fprintf(ascii, " %-5d %-10d\n", Cats->field[i],
  630. Cats->cat[i]);
  631. }
  632. }
  633. else {
  634. if (cat > 0) {
  635. if (ltype == GV_POINT) {
  636. G_rasprintf(&xstring, &xsize, "%.*f", dp, Points->x[0]);
  637. G_trim_decimal(xstring);
  638. G_rasprintf(&ystring, &ysize, "%.*f", dp, Points->y[0]);
  639. G_trim_decimal(ystring);
  640. fprintf(att, "P %s %s %d\n", xstring, ystring, cat);
  641. }
  642. else {
  643. x = (Points->x[1] + Points->x[0]) / 2;
  644. y = (Points->y[1] + Points->y[0]) / 2;
  645. G_rasprintf(&xstring, &xsize, "%.*f", dp, x);
  646. G_trim_decimal(xstring);
  647. G_rasprintf(&ystring, &ysize, "%.*f", dp, y);
  648. G_trim_decimal(ystring);
  649. fprintf(att, "L %s %s %d\n", xstring, ystring, cat);
  650. }
  651. }
  652. }
  653. }
  654. else if (format == GV_ASCII_FORMAT_WKT) {
  655. if (ltype & (GV_BOUNDARY | GV_CENTROID | GV_FACE | GV_KERNEL))
  656. continue;
  657. /* Well-Known Text */
  658. Vect_sfa_line_astext(Points, ltype, Vect_is_3d(Map), dp, ascii);
  659. }
  660. else {
  661. G_fatal_error(_("Unknown format"));
  662. }
  663. n_lines++;
  664. }
  665. if (format == GV_ASCII_FORMAT_WKT) {
  666. /* process areas - topology required */
  667. int i, area, nareas, isle, nisles;
  668. if (Vect_level(Map) < 2) {
  669. G_warning(_("Topology not available, unable to process areas"));
  670. nareas = 0;
  671. }
  672. else {
  673. nareas = Vect_get_num_areas(Map);
  674. }
  675. for (area = 1; area <= nareas; area++) {
  676. if (!Vect_area_alive(Map, area)) /* skip dead areas */
  677. continue;
  678. if (Vect_get_area_cat(Map, area, field) < 0)
  679. continue;
  680. /* get boundary -> linearring */
  681. if (Vect_get_area_points(Map, area, Points) < 0) {
  682. G_warning(_("Unable to get boundary of area id %d"), area);
  683. continue;
  684. }
  685. fprintf(ascii, "POLYGON(");
  686. /* write outter ring */
  687. Vect_sfa_line_astext(Points, GV_BOUNDARY, 0, dp, ascii); /* boundary is always 2D */
  688. /* get isles (holes) -> inner rings */
  689. nisles = Vect_get_area_num_isles(Map, area);
  690. for (i = 0; i < nisles; i++) {
  691. /* get isle boundary -> linearring */
  692. isle = Vect_get_area_isle(Map, area, i);
  693. if (Vect_get_isle_points(Map, isle, Points) < 0) {
  694. G_warning(_("Unable to get boundary of isle id %d (area id %d)"), isle, area);
  695. continue;
  696. }
  697. fprintf(ascii, ", ");
  698. /* write inner ring */
  699. Vect_sfa_line_astext(Points, GV_BOUNDARY, 0, dp, ascii); /* boundary is always 2D */
  700. }
  701. fprintf(ascii, ")\n");
  702. }
  703. }
  704. Vect_destroy_line_struct(Points);
  705. Vect_destroy_cats_struct(Cats);
  706. Vect_destroy_cats_struct(ACats);
  707. return n_lines;
  708. }
  709. int srch(const void *pa, const void *pb)
  710. {
  711. int *p1 = (int *)pa;
  712. int *p2 = (int *)pb;
  713. if (*p1 < *p2)
  714. return -1;
  715. if (*p1 > *p2)
  716. return 1;
  717. return 0;
  718. }
  719. /*!
  720. \brief Write data to GRASS ASCII vector format
  721. \param[out] dascii pointer to the output ASCII file
  722. \param Map pointer to Map_info structure
  723. */
  724. void Vect_write_ascii_head(FILE *dascii, struct Map_info *Map)
  725. {
  726. fprintf(dascii, "ORGANIZATION: %s\n", Vect_get_organization(Map));
  727. fprintf(dascii, "DIGIT DATE: %s\n", Vect_get_date(Map));
  728. fprintf(dascii, "DIGIT NAME: %s\n", Vect_get_person(Map));
  729. fprintf(dascii, "MAP NAME: %s\n", Vect_get_map_name(Map));
  730. fprintf(dascii, "MAP DATE: %s\n", Vect_get_map_date(Map));
  731. fprintf(dascii, "MAP SCALE: %d\n", Vect_get_scale(Map));
  732. fprintf(dascii, "OTHER INFO: %s\n", Vect_get_comment(Map));
  733. fprintf(dascii, "ZONE: %d\n", Vect_get_zone(Map));
  734. fprintf(dascii, "MAP THRESH: %f\n", Vect_get_thresh(Map));
  735. }
  736. /* check category */
  737. int check_cat(const struct line_cats *Cats, const struct cat_list *Clist,
  738. const int *cats, int ncats)
  739. {
  740. int i, cat;
  741. if (Clist) {
  742. Vect_cat_get(Cats, Clist->field, &cat);
  743. if (!Vect_cat_in_cat_list(cat, Clist))
  744. return FALSE;
  745. }
  746. if (cats) {
  747. for (i = 0; i < Cats->n_cats; i++) {
  748. if ((int *)bsearch((void *) &(Cats->cat[i]), cats, ncats, sizeof(int),
  749. srch)) {
  750. /* found */
  751. break;
  752. }
  753. }
  754. if (i == Cats->n_cats)
  755. return FALSE;
  756. }
  757. return TRUE;
  758. }
  759. /* free column arrays, see Vect_write_ascii() */
  760. void free_col_arrays(int *coltypes, char *all_columns, char**columns)
  761. {
  762. G_free(coltypes);
  763. G_free(all_columns);
  764. if (columns) {
  765. int i;
  766. i = 0;
  767. while(columns[i])
  768. G_free(columns[i++]);
  769. G_free(columns);
  770. }
  771. }