get_cellhd.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*!
  2. \file gis/get_cellhd.c
  3. \brief GIS library - Read raster map header
  4. (C) 2001-2008 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/glocale.h>
  13. /*!
  14. \brief Read the raster header
  15. The raster header for the raster map <i>name</i> in the specified
  16. <i>mapset</i> is read into the <i>cellhd</i> structure. If there is
  17. an error reading the raster header file, a diagnostic message is
  18. printed and -1 is returned. Otherwise, 0 is returned.
  19. <b>Note</b>:a warning message for errors encountered.
  20. Cell header files may contain either grid cell header
  21. information or reclass information. If it is a reclass
  22. file, it will specify the map and mapset names of the actual
  23. grid cell file being reclassed. G_get_cellhd(), upon
  24. reading reclass information will go read the cell header
  25. information for the referenced file. Only one reference is
  26. allowed.
  27. \param name name of map
  28. \param mapset mapset that map belongs to
  29. \param cellhd structure to hold cell header info
  30. \return 0 on success
  31. \return -1 on error
  32. */
  33. int G_get_cellhd(const char *name, const char *mapset,
  34. struct Cell_head *cellhd)
  35. {
  36. FILE *fd;
  37. int is_reclass;
  38. char real_name[GNAME_MAX], real_mapset[GMAPSET_MAX];
  39. char buf[1024];
  40. char *tail;
  41. char *err;
  42. /*
  43. is_reclass = G_is_reclass (name, mapset, real_name, real_mapset);
  44. if (is_reclass < 0)
  45. {
  46. sprintf (buf,"Can't read header file for [%s in %s]\n", name, mapset);
  47. tail = buf + strlen(buf);
  48. strcpy (tail, "It is a reclass file, but with an invalid format");
  49. G_warning(buf);
  50. return -1;
  51. }
  52. */
  53. is_reclass = (G_is_reclass(name, mapset, real_name, real_mapset) > 0);
  54. if (is_reclass) {
  55. fd = G_fopen_old("cellhd", real_name, real_mapset);
  56. if (fd == NULL) {
  57. sprintf(buf,
  58. _("Unable to read header file for raster map <%s@%s>."),
  59. name, mapset);
  60. tail = buf + strlen(buf);
  61. sprintf(tail, _(" It is a reclass of raster map <%s@%s> "),
  62. real_name, real_mapset);
  63. tail = buf + strlen(buf);
  64. if (!G_find_cell(real_name, real_mapset))
  65. sprintf(tail, _("which is missing."));
  66. else
  67. sprintf(tail, _("whose header file can't be opened."));
  68. G_warning(buf);
  69. return -1;
  70. }
  71. }
  72. else {
  73. fd = G_fopen_old("cellhd", name, mapset);
  74. if (fd == NULL) {
  75. G_warning(_("Unable to open header file for raster map <%s@%s>"),
  76. name, mapset);
  77. return -1;
  78. }
  79. }
  80. err = G__read_Cell_head(fd, cellhd, 1);
  81. fclose(fd);
  82. if (err == NULL)
  83. return 0;
  84. sprintf(buf, _("Unable to read header file for raster map <%s@%s>."),
  85. name, mapset);
  86. tail = buf + strlen(buf);
  87. if (is_reclass) {
  88. sprintf(tail,
  89. _(" It is a reclass of raster map <%s@%s> whose header file is invalid."),
  90. real_name, real_mapset);
  91. }
  92. else
  93. sprintf(tail, _(" Invalid format."));
  94. tail = buf + strlen(buf);
  95. strcpy(tail, err);
  96. G_free(err);
  97. G_warning(buf);
  98. return -1;
  99. }