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