check_reclass.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <string.h>
  2. #include <grass/raster.h>
  3. #include "global.h"
  4. int check_reclass(const char *name, const char *mapset, int force)
  5. {
  6. char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
  7. char **rmaps;
  8. int nrmaps;
  9. if (Rast_is_reclassed_to(name, mapset, &nrmaps, &rmaps) > 0) {
  10. for (; *rmaps; rmaps++) {
  11. /* force remove */
  12. if (force)
  13. G_warning(_("[%s@%s] is a base map for [%s]. Remove forced."),
  14. name, mapset, *rmaps);
  15. else
  16. G_warning(_
  17. ("[%s@%s] is a base map. Remove reclassed map first: %s"),
  18. name, mapset, *rmaps);
  19. }
  20. if (!force)
  21. return 1;
  22. }
  23. if (Rast_is_reclass(name, mapset, rname, rmapset) > 0 &&
  24. Rast_is_reclassed_to(rname, rmapset, &nrmaps, &rmaps) > 0) {
  25. char path[GPATH_MAX];
  26. char *p = strchr(rname, '@');
  27. char *qname = G_fully_qualified_name(name, mapset);
  28. if (p)
  29. *p = '\0';
  30. G__file_name_misc(path, "cell_misc", "reclassed_to", rname, rmapset);
  31. if (nrmaps == 1 && !G_strcasecmp(rmaps[0], qname)) {
  32. if (remove(path) < 0)
  33. G_warning(_
  34. ("Removing information about reclassed map from [%s@%s] failed"),
  35. rname, rmapset);
  36. }
  37. else {
  38. FILE *fp = fopen(path, "w");
  39. if (fp) {
  40. for (; *rmaps; rmaps++)
  41. if (G_strcasecmp(*rmaps, qname))
  42. fprintf(fp, "%s\n", *rmaps);
  43. fclose(fp);
  44. }
  45. else
  46. G_warning(_
  47. ("Removing information about reclassed map from [%s@%s] failed"),
  48. rname, rmapset);
  49. }
  50. }
  51. return 0;
  52. }