get_cellhd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*!
  2. \file lib/raster/get_cellhd.c
  3. \brief Raster library - Read raster map header
  4. (C) 2001-2020 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <grass/gis.h>
  12. #include <grass/raster.h>
  13. #include <grass/glocale.h>
  14. /*!
  15. \brief Read the raster header
  16. The raster header for the raster map <i>name</i> in the specified
  17. <i>mapset</i> is read into the <i>cellhd</i> structure. If there is
  18. an error reading the raster header file, G_fatal_error() is called.
  19. Cell header files may contain either grid cell header information or
  20. reclass information. If it is a reclass file, it will specify the
  21. map and mapset names of the actual grid cell file being
  22. reclassed. Rast_get_cellhd(), upon reading reclass information will go
  23. read the cell header information for the referenced file. Only one
  24. reference is allowed.
  25. \param name name of map
  26. \param mapset mapset that map belongs to
  27. \param[out] cellhd structure to hold cell header info
  28. \return void
  29. */
  30. void Rast_get_cellhd(const char *name, const char *mapset,
  31. struct Cell_head *cellhd)
  32. {
  33. FILE *fp;
  34. int is_reclass;
  35. char real_name[GNAME_MAX], real_mapset[GMAPSET_MAX];
  36. const char *detail;
  37. /*
  38. is_reclass = Rast_is_reclass (name, mapset, real_name, real_mapset);
  39. if (is_reclass < 0)
  40. {
  41. sprintf (buf,"Can't read header file for [%s in %s]\n", name, mapset);
  42. tail = buf + strlen(buf);
  43. strcpy (tail, "It is a reclass file, but with an invalid format");
  44. G_warning(buf);
  45. return -1;
  46. }
  47. */
  48. is_reclass = (Rast_is_reclass(name, mapset, real_name, real_mapset) > 0);
  49. if (is_reclass) {
  50. fp = G_fopen_old("cellhd", real_name, real_mapset);
  51. if (!fp) {
  52. detail = !G_find_raster(real_name, real_mapset)
  53. ? _("However, that raster map is missing."
  54. " Perhaps, it was deleted by mistake.")
  55. : _("However, header file of that raster map can't be"
  56. " opened. It seems that it was corrupted after"
  57. " creating the reclass raster map.");
  58. G_fatal_error(_("Unable to read header file for raster map <%s@%s>. "
  59. "It is a reclass of raster map <%s@%s>. %s"), name,
  60. mapset, real_name, real_mapset, detail);
  61. }
  62. }
  63. else {
  64. fp = G_fopen_old("cellhd", name, mapset);
  65. if (!fp)
  66. G_fatal_error(_("Unable to open header file for raster map <%s@%s>."
  67. " It seems that some previous step failed and"
  68. " created an incomplete raster map."), name,
  69. mapset);
  70. }
  71. G__read_Cell_head(fp, cellhd, 1);
  72. fclose(fp);
  73. }