get_item.c 2.3 KB

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