reclass.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. static 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. int 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. return 0;
  150. }
  151. static int reclass_type(FILE * fd, char **rname, char **rmapset)
  152. {
  153. char buf[128];
  154. char label[128], arg[128];
  155. int i;
  156. int type;
  157. /* Check to see if this is a reclass file */
  158. if (fgets(buf, sizeof(buf), fd) == NULL)
  159. return 0;
  160. if (strncmp(buf, "reclas", 6))
  161. return 0;
  162. /* later may add other types of reclass */
  163. type = RECLASS_TABLE;
  164. /* Read the mapset and file name of the REAL cell file */
  165. if (*rname)
  166. **rname = '\0';
  167. if (*rmapset)
  168. **rmapset = '\0';
  169. for (i = 0; i < 2; i++) {
  170. if (fgets(buf, sizeof buf, fd) == NULL)
  171. return -1;
  172. if (sscanf(buf, "%[^:]:%s", label, arg) != 2)
  173. return -1;
  174. if (strncmp(label, "maps", 4) == 0) {
  175. if (*rmapset)
  176. strcpy(*rmapset, arg);
  177. else
  178. *rmapset = G_store(arg);
  179. }
  180. else if (strncmp(label, "name", 4) == 0) {
  181. if (*rname)
  182. strcpy(*rname, arg);
  183. else
  184. *rname = G_store(arg);
  185. }
  186. else
  187. return -1;
  188. }
  189. if (**rmapset && **rname)
  190. return type;
  191. else
  192. return -1;
  193. }
  194. static FILE *fopen_cellhd_old(const char *name, const char *mapset)
  195. {
  196. return G_fopen_old("cellhd", name, mapset);
  197. }
  198. int G_put_reclass(const char *name, const struct Reclass *reclass)
  199. {
  200. FILE *fd;
  201. long min, max;
  202. int i;
  203. char buf1[GPATH_MAX], buf2[GNAME_MAX], buf3[GNAME_MAX], *p;
  204. switch (reclass->type) {
  205. case RECLASS_TABLE:
  206. if (reclass->min > reclass->max || reclass->num <= 0) {
  207. G_fatal_error(_("Illegal reclass request"));
  208. return -1;
  209. }
  210. break;
  211. default:
  212. G_fatal_error(_("Illegal reclass type"));
  213. return -1;
  214. }
  215. fd = fopen_cellhd_new(name);
  216. if (fd == NULL) {
  217. G_warning(_("Unable to create header file for [%s in %s]"),
  218. name, G_mapset());
  219. return -1;
  220. }
  221. fprintf(fd, "reclass\n");
  222. fprintf(fd, "name: %s\n", reclass->name);
  223. fprintf(fd, "mapset: %s\n", reclass->mapset);
  224. /* find first non-null entry */
  225. for (min = 0; min < reclass->num; min++)
  226. if (!G_is_c_null_value(&reclass->table[min]))
  227. break;
  228. /* find last non-zero entry */
  229. for (max = reclass->num - 1; max >= 0; max--)
  230. if (!G_is_c_null_value(&reclass->table[max]))
  231. break;
  232. /*
  233. * if the resultant table is empty, write out a dummy table
  234. * else write out the table
  235. * first entry is #min
  236. * rest are translations for cat min+i
  237. */
  238. if (min > max)
  239. fprintf(fd, "0\n");
  240. else {
  241. fprintf(fd, "#%ld\n", (long)reclass->min + min);
  242. while (min <= max) {
  243. if (G_is_c_null_value(&reclass->table[min]))
  244. fprintf(fd, "%s\n", NULL_STRING);
  245. else
  246. fprintf(fd, "%ld\n", (long)reclass->table[min]);
  247. min++;
  248. }
  249. }
  250. fclose(fd);
  251. strcpy(buf2, reclass->name);
  252. if ((p = strchr(buf2, '@')))
  253. *p = 0;
  254. G__file_name_misc(buf1, "cell_misc", "reclassed_to", reclass->name,
  255. reclass->mapset);
  256. fd = fopen(buf1, "a+");
  257. if (fd == NULL) {
  258. #if 0
  259. G_warning(_("Unable to create dependency file in [%s in %s]"),
  260. buf2, reclass->mapset);
  261. #endif
  262. return 1;
  263. }
  264. fseek(fd, 0L, SEEK_SET);
  265. sprintf(buf2, "%s@%s\n", name, G_mapset());
  266. for (i = 0; !feof(fd) && fgets(buf3, 255, fd);) {
  267. if (!(strcmp(buf2, buf3))) {
  268. i = 1;
  269. break;
  270. }
  271. }
  272. if (!i) {
  273. fprintf(fd, "%s@%s\n", name, G_mapset());
  274. }
  275. fclose(fd);
  276. return 1;
  277. }
  278. static FILE *fopen_cellhd_new(const char *name)
  279. {
  280. return G_fopen_new("cellhd", name);
  281. }
  282. static int get_reclass_table(FILE * fd, struct Reclass *reclass)
  283. {
  284. char buf[128];
  285. int n;
  286. int first, null_str_size;
  287. CELL cat;
  288. long len;
  289. /*
  290. * allocate the table, expanding as each entry is read
  291. * note that G_realloc() will become G_malloc() if ptr in
  292. * NULL
  293. */
  294. reclass->min = 0;
  295. reclass->table = NULL;
  296. null_str_size = strlen(NULL_STRING);
  297. n = 0;
  298. first = 1;
  299. while (fgets(buf, sizeof buf, fd)) {
  300. if (first) {
  301. first = 0;
  302. if (sscanf(buf, "#%d", &cat) == 1) {
  303. reclass->min = cat;
  304. continue;
  305. }
  306. }
  307. if (strncmp(buf, NULL_STRING, null_str_size) == 0)
  308. G_set_c_null_value(&cat, 1);
  309. else {
  310. if (sscanf(buf, "%d", &cat) != 1)
  311. return -1;
  312. }
  313. n++;
  314. len = (long)n *sizeof(CELL);
  315. if (len != (int)len) { /* check for int overflow */
  316. if (reclass->table != NULL)
  317. G_free(reclass->table);
  318. return -2;
  319. }
  320. reclass->table = (CELL *) G_realloc((char *)reclass->table, (int)len);
  321. reclass->table[n - 1] = cat;
  322. }
  323. reclass->max = reclass->min + n - 1;
  324. reclass->num = n;
  325. return 1;
  326. }