a2b.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/Vect.h>
  5. #include <grass/glocale.h>
  6. #define BUFFSIZE 128
  7. int asc_to_bin(FILE * ascii, struct Map_info *Map)
  8. {
  9. char ctype;
  10. char buff[BUFFSIZE];
  11. double *xarray;
  12. double *yarray;
  13. double *zarray;
  14. double *x, *y, *z;
  15. int i, n_points, n_coors, n_cats;
  16. int type;
  17. int alloc_points;
  18. int end_of_file;
  19. struct line_pnts *Points;
  20. struct line_cats *Cats;
  21. int catn;
  22. int cat;
  23. /* Must always use this to create an initialized line_pnts structure */
  24. Points = Vect_new_line_struct();
  25. Cats = Vect_new_cats_struct();
  26. end_of_file = 0;
  27. /*alloc_points = 1000 ; */
  28. alloc_points = 1;
  29. xarray = (double *)G_calloc(alloc_points, sizeof(double));
  30. yarray = (double *)G_calloc(alloc_points, sizeof(double));
  31. zarray = (double *)G_calloc(alloc_points, sizeof(double));
  32. while (G_getl2(buff, BUFFSIZE - 1, ascii) != 0) {
  33. n_cats = 0;
  34. if (buff[0] == '\0') {
  35. G_debug(3, "a2b: skipping blank line");
  36. continue;
  37. }
  38. if (sscanf(buff, "%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
  39. n_coors < 0 || n_cats < 0) {
  40. if (ctype == '#') {
  41. G_debug(2, "a2b: skipping commented line");
  42. continue;
  43. }
  44. G_fatal_error(_("Error reading ASCII file: (bad type) [%s]"),
  45. buff);
  46. }
  47. if (ctype == '#') {
  48. G_debug(2, "a2b: Skipping commented line");
  49. continue;
  50. }
  51. switch (ctype) {
  52. case 'A':
  53. type = GV_BOUNDARY;
  54. break;
  55. case 'B':
  56. type = GV_BOUNDARY;
  57. break;
  58. case 'C':
  59. type = GV_CENTROID;
  60. break;
  61. case 'L':
  62. type = GV_LINE;
  63. break;
  64. case 'P':
  65. type = GV_POINT;
  66. break;
  67. case 'F':
  68. type = GV_FACE;
  69. break;
  70. case 'K':
  71. type = GV_KERNEL;
  72. break;
  73. case 'a':
  74. case 'b':
  75. case 'c':
  76. case 'l':
  77. case 'p':
  78. type = 0; /* dead -> ignore */
  79. break;
  80. default:
  81. G_fatal_error(_("Error reading ASCII file: (unknown type) [%s]"),
  82. buff);
  83. }
  84. G_debug(5, "feature type = %d", type);
  85. n_points = 0;
  86. x = xarray;
  87. y = yarray;
  88. z = zarray;
  89. /* Collect the points */
  90. for (i = 0; i < n_coors; i++) {
  91. if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
  92. G_fatal_error(_("End of ASCII file reached before end of coordinates"));
  93. if (buff[0] == '\0') {
  94. G_debug(3, "a2b: skipping blank line while reading vertices");
  95. i--;
  96. continue;
  97. }
  98. *z = 0;
  99. if (sscanf(buff, "%lf%lf%lf", x, y, z) < 2)
  100. G_fatal_error(_("Error reading ASCII file: (bad point) [%s]"),
  101. buff);
  102. G_debug(5, "coor in: %s -> x = %f y = %f z = %f", G_chop(buff),
  103. *x, *y, *z);
  104. n_points++;
  105. x++;
  106. y++;
  107. z++;
  108. if (n_points >= alloc_points) {
  109. alloc_points = n_points + 1000;
  110. xarray =
  111. (double *)G_realloc((void *)xarray,
  112. alloc_points * sizeof(double));
  113. yarray =
  114. (double *)G_realloc((void *)yarray,
  115. alloc_points * sizeof(double));
  116. zarray =
  117. (double *)G_realloc((void *)zarray,
  118. alloc_points * sizeof(double));
  119. x = xarray + n_points;
  120. y = yarray + n_points;
  121. z = zarray + n_points;
  122. }
  123. }
  124. /* Collect the cats */
  125. for (i = 0; i < n_cats; i++) {
  126. if (G_getl2(buff, BUFFSIZE - 1, ascii) == 0)
  127. G_fatal_error(_("End of ASCII file reached before end of categories"));
  128. if (buff[0] == '\0') {
  129. G_debug(3,
  130. "a2b: skipping blank line while reading category info");
  131. i--;
  132. continue;
  133. }
  134. if (sscanf(buff, "%u%u", &catn, &cat) != 2)
  135. G_fatal_error(_("Error reading categories: [%s]"), buff);
  136. Vect_cat_set(Cats, catn, cat);
  137. }
  138. /* Allocation is handled for line_pnts */
  139. if (0 >
  140. Vect_copy_xyz_to_pnts(Points, xarray, yarray, zarray, n_points))
  141. G_fatal_error(_("Out of memory"));
  142. if (type > 0)
  143. Vect_write_line(Map, type, Points, Cats);
  144. Vect_reset_cats(Cats);
  145. }
  146. return 0;
  147. }