reclass.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. static const char NULL_STRING[] = "null";
  5. static int reclass_type(FILE *, char **, char **);
  6. static FILE *fopen_cellhd_old(const char *, const char *);
  7. static FILE *fopen_cellhd_new(const char *);
  8. static int get_reclass_table(FILE *, struct Reclass *);
  9. /*!
  10. * \brief reclass file?
  11. *
  12. * This function determines if the raster map
  13. * <b>name</b> in <b>mapset</b> is a reclass file.
  14. * If it is, then the name and mapset of the referenced
  15. * raster map are copied into the <b>r_name</b> and <b>r_mapset</b>
  16. * buffers.
  17. * Returns 1 if <b>name</b> is a reclass file, 0 if it is not, and -1 if
  18. * there was a problem reading the raster header for <b>name.</b>
  19. *
  20. * \param name
  21. * \param mapset
  22. * \param r_name
  23. * \param r_mapset
  24. * \return int
  25. */
  26. int G_is_reclass(const char *name, const char *mapset, char *rname,
  27. char *rmapset)
  28. {
  29. FILE *fd;
  30. int type;
  31. fd = fopen_cellhd_old(name, mapset);
  32. if (fd == NULL)
  33. return -1;
  34. type = reclass_type(fd, &rname, &rmapset);
  35. fclose(fd);
  36. if (type < 0)
  37. return -1;
  38. else
  39. return type != 0;
  40. }
  41. /*!
  42. * \brief get child reclass maps list
  43. *
  44. * This function generates a
  45. * child reclass maps list from the cell_misc/reclassed_to file which stores
  46. * this list. The cell_misc/reclassed_to file is written by
  47. * G_put_reclass().
  48. * G_is_reclassed_to() is used by g.rename, g.remove and r.reclass to
  49. * prevent accidentally deleting the parent map of a reclassed raster map.
  50. *
  51. * \param name
  52. * \param mapset
  53. * \param nrmaps
  54. * \param rmaps
  55. * \return int
  56. */
  57. int G_is_reclassed_to(const char *name, const char *mapset, int *nrmaps,
  58. char ***rmaps)
  59. {
  60. FILE *fd;
  61. int i, j, k, l;
  62. char buf2[256], buf3[256];
  63. fd = G_fopen_old_misc("cell_misc", "reclassed_to", name, mapset);
  64. if (fd == NULL) {
  65. return -1;
  66. }
  67. if (rmaps)
  68. *rmaps = NULL;
  69. for (i = 0; !feof(fd) && fgets(buf2, 255, fd);) {
  70. l = strlen(buf2);
  71. for (j = 0, k = 0; j < l; j++) {
  72. if (buf2[j] == '#' ||
  73. ((buf2[j] == ' ' || buf2[j] == '\t' || buf2[j] == '\n') && k))
  74. break;
  75. else if (buf2[j] != ' ' && buf2[j] != '\t')
  76. buf3[k++] = buf2[j];
  77. }
  78. if (k) {
  79. buf3[k] = 0;
  80. i++;
  81. if (rmaps) {
  82. *rmaps = (char **)G_realloc(*rmaps, i * sizeof(char *));
  83. (*rmaps)[i - 1] = (char *)G_malloc(k + 1);
  84. strncpy((*rmaps)[i - 1], buf3, k);
  85. (*rmaps)[i - 1][k] = 0;
  86. }
  87. }
  88. }
  89. if (nrmaps)
  90. *nrmaps = i;
  91. if (i && rmaps) {
  92. i++;
  93. *rmaps = (char **)G_realloc(*rmaps, i * sizeof(char *));
  94. (*rmaps)[i - 1] = NULL;
  95. }
  96. return i;
  97. }
  98. int G_get_reclass(const char *name, const char *mapset,
  99. struct Reclass *reclass)
  100. {
  101. FILE *fd;
  102. int stat;
  103. fd = fopen_cellhd_old(name, mapset);
  104. if (fd == NULL)
  105. return -1;
  106. reclass->name = NULL;
  107. reclass->mapset = NULL;
  108. reclass->type = reclass_type(fd, &reclass->name, &reclass->mapset);
  109. if (reclass->type <= 0) {
  110. fclose(fd);
  111. return reclass->type;
  112. }
  113. switch (reclass->type) {
  114. case RECLASS_TABLE:
  115. stat = get_reclass_table(fd, reclass);
  116. break;
  117. default:
  118. stat = -1;
  119. }
  120. fclose(fd);
  121. if (stat < 0) {
  122. if (stat == -2)
  123. G_warning(_("Too many reclass categories for [%s in %s]"),
  124. name, mapset);
  125. else
  126. G_warning(_("Illegal reclass format in header file for [%s in %s]"),
  127. name, mapset);
  128. stat = -1;
  129. }
  130. return stat;
  131. }
  132. void G_free_reclass(struct Reclass *reclass)
  133. {
  134. switch (reclass->type) {
  135. case RECLASS_TABLE:
  136. if (reclass->num > 0)
  137. G_free(reclass->table);
  138. reclass->num = 0;
  139. if (reclass->name)
  140. G_free(reclass->name);
  141. if (reclass->mapset)
  142. G_free(reclass->mapset);
  143. reclass->name = NULL;
  144. reclass->mapset = NULL;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. static int reclass_type(FILE * fd, char **rname, char **rmapset)
  151. {
  152. char buf[128];
  153. char label[128], arg[128];
  154. int i;
  155. int type;
  156. /* Check to see if this is a reclass file */
  157. if (fgets(buf, sizeof(buf), fd) == NULL)
  158. return 0;
  159. if (strncmp(buf, "reclas", 6))
  160. return 0;
  161. /* later may add other types of reclass */
  162. type = RECLASS_TABLE;
  163. /* Read the mapset and file name of the REAL cell file */
  164. if (*rname)
  165. **rname = '\0';
  166. if (*rmapset)
  167. **rmapset = '\0';
  168. for (i = 0; i < 2; i++) {
  169. if (fgets(buf, sizeof buf, fd) == NULL)
  170. return -1;
  171. if (sscanf(buf, "%[^:]:%s", label, arg) != 2)
  172. return -1;
  173. if (strncmp(label, "maps", 4) == 0) {
  174. if (*rmapset)
  175. strcpy(*rmapset, arg);
  176. else
  177. *rmapset = G_store(arg);
  178. }
  179. else if (strncmp(label, "name", 4) == 0) {
  180. if (*rname)
  181. strcpy(*rname, arg);
  182. else
  183. *rname = G_store(arg);
  184. }
  185. else
  186. return -1;
  187. }
  188. if (**rmapset && **rname)
  189. return type;
  190. else
  191. return -1;
  192. }
  193. static FILE *fopen_cellhd_old(const char *name, const char *mapset)
  194. {
  195. return G_fopen_old("cellhd", name, mapset);
  196. }
  197. int G_put_reclass(const char *name, const struct Reclass *reclass)
  198. {
  199. FILE *fd;
  200. long min, max;
  201. int i;
  202. char buf1[GPATH_MAX], buf2[GNAME_MAX], buf3[GNAME_MAX], *p;
  203. switch (reclass->type) {
  204. case RECLASS_TABLE:
  205. if (reclass->min > reclass->max || reclass->num <= 0) {
  206. G_fatal_error(_("Illegal reclass request"));
  207. return -1;
  208. }
  209. break;
  210. default:
  211. G_fatal_error(_("Illegal reclass type"));
  212. return -1;
  213. }
  214. fd = fopen_cellhd_new(name);
  215. if (fd == NULL) {
  216. G_warning(_("Unable to create header file for [%s in %s]"),
  217. name, G_mapset());
  218. return -1;
  219. }
  220. fprintf(fd, "reclass\n");
  221. fprintf(fd, "name: %s\n", reclass->name);
  222. fprintf(fd, "mapset: %s\n", reclass->mapset);
  223. /* find first non-null entry */
  224. for (min = 0; min < reclass->num; min++)
  225. if (!G_is_c_null_value(&reclass->table[min]))
  226. break;
  227. /* find last non-zero entry */
  228. for (max = reclass->num - 1; max >= 0; max--)
  229. if (!G_is_c_null_value(&reclass->table[max]))
  230. break;
  231. /*
  232. * if the resultant table is empty, write out a dummy table
  233. * else write out the table
  234. * first entry is #min
  235. * rest are translations for cat min+i
  236. */
  237. if (min > max)
  238. fprintf(fd, "0\n");
  239. else {
  240. fprintf(fd, "#%ld\n", (long)reclass->min + min);
  241. while (min <= max) {
  242. if (G_is_c_null_value(&reclass->table[min]))
  243. fprintf(fd, "%s\n", NULL_STRING);
  244. else
  245. fprintf(fd, "%ld\n", (long)reclass->table[min]);
  246. min++;
  247. }
  248. }
  249. fclose(fd);
  250. strcpy(buf2, reclass->name);
  251. if ((p = strchr(buf2, '@')))
  252. *p = 0;
  253. G__file_name_misc(buf1, "cell_misc", "reclassed_to", reclass->name,
  254. reclass->mapset);
  255. fd = fopen(buf1, "a+");
  256. if (fd == NULL) {
  257. #if 0
  258. G_warning(_("Unable to create dependency file in [%s in %s]"),
  259. buf2, reclass->mapset);
  260. #endif
  261. return 1;
  262. }
  263. fseek(fd, 0L, SEEK_SET);
  264. sprintf(buf2, "%s@%s\n", name, G_mapset());
  265. for (i = 0; !feof(fd) && fgets(buf3, 255, fd);) {
  266. if (!(strcmp(buf2, buf3))) {
  267. i = 1;
  268. break;
  269. }
  270. }
  271. if (!i) {
  272. fprintf(fd, "%s@%s\n", name, G_mapset());
  273. }
  274. fclose(fd);
  275. return 1;
  276. }
  277. static FILE *fopen_cellhd_new(const char *name)
  278. {
  279. return G_fopen_new("cellhd", name);
  280. }
  281. static int get_reclass_table(FILE * fd, struct Reclass *reclass)
  282. {
  283. char buf[128];
  284. int n;
  285. int first, null_str_size;
  286. CELL cat;
  287. long len;
  288. /*
  289. * allocate the table, expanding as each entry is read
  290. * note that G_realloc() will become G_malloc() if ptr in
  291. * NULL
  292. */
  293. reclass->min = 0;
  294. reclass->table = NULL;
  295. null_str_size = strlen(NULL_STRING);
  296. n = 0;
  297. first = 1;
  298. while (fgets(buf, sizeof buf, fd)) {
  299. if (first) {
  300. first = 0;
  301. if (sscanf(buf, "#%d", &cat) == 1) {
  302. reclass->min = cat;
  303. continue;
  304. }
  305. }
  306. if (strncmp(buf, NULL_STRING, null_str_size) == 0)
  307. G_set_c_null_value(&cat, 1);
  308. else {
  309. if (sscanf(buf, "%d", &cat) != 1)
  310. return -1;
  311. }
  312. n++;
  313. len = (long)n *sizeof(CELL);
  314. if (len != (int)len) { /* check for int overflow */
  315. if (reclass->table != NULL)
  316. G_free(reclass->table);
  317. return -2;
  318. }
  319. reclass->table = (CELL *) G_realloc((char *)reclass->table, (int)len);
  320. reclass->table[n - 1] = cat;
  321. }
  322. reclass->max = reclass->min + n - 1;
  323. reclass->num = n;
  324. return 1;
  325. }