cats.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include "raster3d_intern.h"
  8. /*---------------------------------------------------------------------------*/
  9. /*!
  10. * \brief
  11. *
  12. * Writes the
  13. * categories stored in the <em>cats</em> structure into the categories file for
  14. * map <em>name</em> in the current mapset. See <em>Rast_write_cats</em>
  15. * (Raster_Category_File) for details and return values.
  16. *
  17. * \param name
  18. * \param cats
  19. * \return int
  20. */
  21. int Rast3d_write_cats(const char *name, struct Categories *cats)
  22. /* adapted from Rast_write_cats */
  23. {
  24. FILE *fd;
  25. int i;
  26. const char *descr;
  27. DCELL val1, val2;
  28. char str1[100], str2[100];
  29. fd = G_fopen_new_misc(RASTER3D_DIRECTORY, RASTER3D_CATS_ELEMENT, name);
  30. if (!fd)
  31. return -1;
  32. /* write # cats - note # indicate 3.0 or later */
  33. fprintf(fd, "# %ld categories\n", (long)cats->num);
  34. /* title */
  35. fprintf(fd, "%s\n", cats->title != NULL ? cats->title : "");
  36. /* write format and coefficients */
  37. fprintf(fd, "%s\n", cats->fmt != NULL ? cats->fmt : "");
  38. fprintf(fd, "%.2f %.2f %.2f %.2f\n",
  39. cats->m1, cats->a1, cats->m2, cats->a2);
  40. /* write the cat numbers:label */
  41. for (i = 0; i < Rast_quant_nof_rules(&cats->q); i++) {
  42. descr = Rast_get_ith_d_cat(cats, i, &val1, &val2);
  43. if ((cats->fmt && cats->fmt[0]) || (descr && descr[0])) {
  44. if (val1 == val2) {
  45. sprintf(str1, "%.10f", val1);
  46. G_trim_decimal(str1);
  47. fprintf(fd, "%s:%s\n", str1, descr != NULL ? descr : "");
  48. }
  49. else {
  50. sprintf(str1, "%.10f", val1);
  51. G_trim_decimal(str1);
  52. sprintf(str2, "%.10f", val2);
  53. G_trim_decimal(str2);
  54. fprintf(fd, "%s:%s:%s\n", str1, str2,
  55. descr != NULL ? descr : "");
  56. }
  57. }
  58. }
  59. fclose(fd);
  60. return 1;
  61. }
  62. /*---------------------------------------------------------------------------*/
  63. static int
  64. read_cats(const char *name, const char *mapset, struct Categories *pcats)
  65. /* adapted from G__read_cats */
  66. {
  67. FILE *fd;
  68. char buff[1024];
  69. CELL cat;
  70. DCELL val1, val2;
  71. int old;
  72. long num = -1;
  73. fd = G_fopen_old_misc(RASTER3D_DIRECTORY, RASTER3D_CATS_ELEMENT, name, mapset);
  74. if (!fd)
  75. return -2;
  76. /* Read the number of categories */
  77. if (G_getl(buff, sizeof(buff), fd) == 0)
  78. goto error;
  79. if (sscanf(buff, "# %ld", &num) == 1)
  80. old = 0;
  81. else if (sscanf(buff, "%ld", &num) == 1)
  82. old = 1;
  83. /* Read the title for the file */
  84. if (G_getl(buff, sizeof(buff), fd) == 0)
  85. goto error;
  86. G_strip(buff);
  87. Rast_init_cats(buff, pcats);
  88. if (num >= 0)
  89. pcats->num = num;
  90. if (!old) {
  91. char fmt[256];
  92. float m1, a1, m2, a2;
  93. if (G_getl(fmt, sizeof(fmt), fd) == 0)
  94. goto error;
  95. /* next line contains equation coefficients */
  96. if (G_getl(buff, sizeof(buff), fd) == 0)
  97. goto error;
  98. if (sscanf(buff, "%f %f %f %f", &m1, &a1, &m2, &a2) != 4)
  99. goto error;
  100. Rast_set_cats_fmt(fmt, m1, a1, m2, a2, pcats);
  101. }
  102. /* Read all category names */
  103. for (cat = 0;; cat++) {
  104. char label[1024];
  105. if (G_getl(buff, sizeof(buff), fd) == 0)
  106. break;
  107. if (old)
  108. Rast_set_c_cat(&cat, &cat, buff, pcats);
  109. else {
  110. *label = 0;
  111. if (sscanf(buff, "%1s", label) != 1)
  112. continue;
  113. if (*label == '#')
  114. continue;
  115. *label = 0;
  116. /* try to read a range of data */
  117. if (sscanf(buff, "%lf:%lf:%[^\n]", &val1, &val2, label) == 3)
  118. Rast_set_cat(&val1, &val2, label, pcats, DCELL_TYPE);
  119. else if (sscanf(buff, "%d:%[^\n]", &cat, label) >= 1)
  120. Rast_set_cat(&cat, &cat, label, pcats, CELL_TYPE);
  121. else if (sscanf(buff, "%lf:%[^\n]", &val1, label) >= 1)
  122. Rast_set_cat(&val1, &val1, label, pcats, DCELL_TYPE);
  123. else
  124. goto error;
  125. }
  126. }
  127. fclose(fd);
  128. return 0;
  129. error:
  130. fclose(fd);
  131. return -1;
  132. }
  133. /*---------------------------------------------------------------------------*/
  134. /*!
  135. * \brief
  136. *
  137. * Reads the categories file for map <em>name</em> in <em>mapset</em> and
  138. * stores the categories in the <em>pcats</em> structure. See <em>Rast_read_cats</em>
  139. * (Raster_Category_File) for details and return values.
  140. *
  141. * \param name
  142. * \param mapset
  143. * \param pcats
  144. * \return int
  145. */
  146. int
  147. Rast3d_read_cats(const char *name, const char *mapset, struct Categories *pcats)
  148. /* adapted from Rast_read_cats */
  149. {
  150. const char *type;
  151. switch (read_cats(name, mapset, pcats)) {
  152. case -2:
  153. type = "missing";
  154. break;
  155. case -1:
  156. type = "invalid";
  157. break;
  158. default:
  159. return 0;
  160. }
  161. G_warning("category support for [%s] in mapset [%s] %s",
  162. name, mapset, type);
  163. return -1;
  164. }