ascii.c 21 KB

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