check_reclass.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. int check_reclass(const char *name, const char *mapset, int force)
  6. {
  7. char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
  8. char **rmaps;
  9. int nrmaps;
  10. if (Rast_is_reclassed_to(name, mapset, &nrmaps, &rmaps) > 0) {
  11. for (; *rmaps; rmaps++) {
  12. /* force remove */
  13. if (force)
  14. G_warning(_("Raster map <%s@%s> is a base map for <%s>. Remove forced."),
  15. name, mapset, *rmaps);
  16. else
  17. G_warning(_("Raster map <%s@%s> is a base map. Remove reclassed map <%s> first."),
  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(_("Removing information about reclassed map from <%s@%s> failed"),
  34. rname, rmapset);
  35. }
  36. else {
  37. FILE *fp = fopen(path, "w");
  38. if (fp) {
  39. for (; *rmaps; rmaps++)
  40. if (G_strcasecmp(*rmaps, qname))
  41. fprintf(fp, "%s\n", *rmaps);
  42. fclose(fp);
  43. }
  44. else
  45. G_warning(_("Removing information about reclassed map from <%s@%s> failed"),
  46. rname, rmapset);
  47. }
  48. }
  49. return 0;
  50. }