get_item.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/glocale.h>
  4. #include "format.h"
  5. int get_item(FILE * fd, int format, int *type, int *cat_int, double *cat_double, double **x, double **y,
  6. int *count, struct Categories *labels)
  7. {
  8. static double *X = NULL;
  9. static double *Y = NULL;
  10. static int nalloc = 0;
  11. char buf[1024];
  12. char lbl[1024];
  13. char east[256], north[256];
  14. double e, n;
  15. long offset;
  16. FCELL cat_float_tmp;
  17. *cat_int = 0;
  18. *cat_double = 0;
  19. *count = 0;
  20. *type = 0;
  21. /* scan until we find the start of a new feature */
  22. while (G_getl2(buf, sizeof buf, fd)) {
  23. /* skip comments and blank lines */
  24. if ((*buf == '#') || (*buf == '\0'))
  25. continue;
  26. G_strip(buf);
  27. if (*buf == 'A' || *buf == 'a') {
  28. *type = 'A';
  29. break;
  30. }
  31. if (*buf == 'L' || *buf == 'l') {
  32. *type = 'L';
  33. break;
  34. }
  35. if (*buf == 'P' || *buf == 'p') {
  36. *type = 'P';
  37. break;
  38. }
  39. }
  40. if (*type == 0)
  41. return 0;
  42. /* read the feature's data */
  43. while (1) {
  44. offset = G_ftell(fd);
  45. if (!G_getl2(buf, (sizeof buf) - 1, fd))
  46. break;
  47. /* skip comments and blank lines */
  48. if ((*buf == '#') || (*buf == '\0'))
  49. continue;
  50. G_strip(buf);
  51. /* if we've found the next feature, rewind to the start of it and complete */
  52. if (*buf == 'A' || *buf == 'a' ||
  53. *buf == 'L' || *buf == 'l' || *buf == 'P' || *buf == 'p') {
  54. G_fseek(fd, offset, 0);
  55. break;
  56. }
  57. /* if we found a cat (and optionally a label), read them and continue to scan */
  58. if (*buf == '=') {
  59. if (format == USE_FCELL || format == USE_DCELL) {
  60. if (sscanf(buf + 1, "%lf", cat_double) != 1)
  61. continue;
  62. /* probably change this as G_getl2() doesn't store the new line (?) */
  63. if (sscanf(buf + 1, "%lf%[^\n]", cat_double, lbl) == 2) {
  64. G_strip(lbl);
  65. if (format == USE_FCELL) {
  66. cat_float_tmp = (FCELL) *cat_double;
  67. Rast_set_f_cat(&cat_float_tmp, &cat_float_tmp, lbl, labels);
  68. }
  69. else {
  70. Rast_set_d_cat((DCELL*) cat_double, (DCELL *) cat_double, lbl, labels);
  71. }
  72. }
  73. continue;
  74. }
  75. else {
  76. if (sscanf(buf + 1, "%d", cat_int) != 1)
  77. continue;
  78. /* probably change this as G_getl2() doesn't store the new line (?) */
  79. if (sscanf(buf + 1, "%d%[^\n]", cat_int, lbl) == 2) {
  80. G_strip(lbl);
  81. Rast_set_c_cat((CELL*) cat_int, (CELL *) cat_int, lbl, labels);
  82. }
  83. continue;
  84. }
  85. }
  86. if (sscanf(buf, "%s %s", east, north) != 2) {
  87. G_warning(_("Illegal coordinate <%s, %s>, skipping."), east, north);
  88. continue;
  89. }
  90. if (!G_scan_northing(north, &n, G_projection())) {
  91. G_warning(_("Illegal north coordinate <%s>, skipping."), north);
  92. continue;
  93. }
  94. if (!G_scan_easting(east, &e, G_projection())) {
  95. G_warning(_("Illegal east coordinate <%s>, skipping."), east);
  96. continue;
  97. }
  98. if (*count >= nalloc) {
  99. nalloc += 32;
  100. X = (double *)G_realloc(X, nalloc * sizeof(double));
  101. Y = (double *)G_realloc(Y, nalloc * sizeof(double));
  102. }
  103. X[*count] = e;
  104. Y[*count] = n;
  105. (*count)++;
  106. }
  107. *x = X;
  108. *y = Y;
  109. return 1;
  110. }