ascii.c 18 KB

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