GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
raster_metadata.c
Go to the documentation of this file.
1 /*!
2  \file lib/raster/raster_metadata.c
3 
4  \brief Raster library - Functions to read and write raster "units"
5  and "vertical datum" meta-data info
6 
7  (C) 2007-2009 by Hamish Bowman, and the GRASS Development Team
8 
9  This program is free software under the GNU General Public License
10  (>=v2). Read the file COPYING that comes with GRASS for details.
11 
12  \author Hamish Bowman
13  */
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <grass/gis.h>
19 #include <grass/raster.h>
20 #include <grass/glocale.h>
21 
22 static char *misc_read_line(const char *, const char *, const char *);
23 static void misc_write_line(const char *, const char *, const char *);
24 
25 /*!
26  * \brief Get a raster map's units metadata string
27  *
28  * Read the raster's units metadata file and put string in str
29  *
30  * \param name raster map name
31  * \param mapset mapset name
32  *
33  * \return string representing units on success
34  * \return NULL on error
35  */
36 char *Rast_read_units(const char *name, const char *mapset)
37 {
38  return misc_read_line("units", name, mapset);
39 }
40 
41 /*!
42  * \brief Write a string to a raster map's units metadata file
43  *
44  * Raster map must exist in the current mapset.
45  *
46  * \param name raster map name
47  * \param str string containing data to be written
48  */
49 void Rast_write_units(const char *name, const char *str)
50 {
51  misc_write_line("units", name, str);
52 }
53 
54 /*!
55  * \brief Get a raster map's vertical datum metadata string
56  *
57  * Read the raster's vertical datum metadata file and put string in str
58  *
59  * \param name raster map name
60  * \param mapset mapset name
61  *
62  * \return string representing vertical datum on success
63  * \return NULL on error
64  */
65 char *Rast_read_vdatum(const char *name, const char *mapset)
66 {
67  return misc_read_line("vertical_datum", name, mapset);
68 }
69 
70 
71 /*!
72  * \brief Write a string into a raster's vertical datum metadata file
73  *
74  * Raster map must exist in the current mapset.
75  *
76  * \param name raster map name
77  * \param str string containing data to be written
78  */
79 void Rast_write_vdatum(const char *name, const char *str)
80 {
81  misc_write_line("vertical_datum", name, str);
82 }
83 
84 
85 /*!
86  * \brief Read the first line of a file in cell_misc/
87  *
88  * Read the first line of data from a cell_misc/ meta-data file.
89  *
90  * \param element metadata component filename
91  * \param name
92  * \param mapset
93  * \return dynamically-allocated string on success
94  * \return NULL on error
95  */
96 static char *misc_read_line(const char *elem,
97  const char *name, const char *mapset)
98 {
99  char buff[GNAME_MAX];
100  FILE *fp;
101 
102  buff[0] = '\0';
103 
104  if (G_find_file2_misc("cell_misc", elem, name, mapset) == NULL)
105  return NULL;
106 
107  fp = G_fopen_old_misc("cell_misc", elem, name, mapset);
108  if (!fp) {
109  G_warning(_("Unable to read <%s> for raster map <%s@%s>"),
110  elem, name, mapset);
111  return NULL;
112  }
113  if (G_getl2(buff, sizeof(buff) - 1, fp) == 0) {
114  /* file is empty */
115  *buff = '\0';
116  }
117 
118  if (fclose(fp) != 0)
119  G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
120  elem, name, mapset);
121 
122  return *buff ? G_store(buff) : NULL;
123 }
124 
125 
126 /*!
127  * \brief Write a line to a raster map metadata file
128  *
129  * Write (including overwrite) a string into a raster map's metadata file
130  * found in in cell_misc/ in the current mapset.
131  *
132  * \param element metadata component filename
133  * \param name
134  * \param *str string containing data to be written
135  */
136 static void misc_write_line(const char *elem, const char *name, const char *str)
137 {
138  FILE *fp;
139 
140  fp = G_fopen_new_misc("cell_misc", elem, name);
141  if (!fp)
142  G_fatal_error(_("Unable to create <%s> metadata file for raster map <%s@%s>"),
143  elem, name, G_mapset());
144 
145  fprintf(fp, "%s\n", str);
146 
147  if (fclose(fp) != 0)
148  G_fatal_error(_("Error closing <%s> metadata file for raster map <%s@%s>"),
149  elem, name, G_mapset());
150 }
151 
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition: getl.c:64
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
char * Rast_read_vdatum(const char *name, const char *mapset)
Get a raster map&#39;s vertical datum metadata string.
#define NULL
Definition: ccmath.h:32
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database file for reading
Definition: open_misc.c:210
const char * G_find_file2_misc(const char *, const char *, const char *, const char *)
Searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) ...
Definition: find_file.c:267
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database file
Definition: open_misc.c:182
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
#define GNAME_MAX
Definition: gis.h:167
void G_warning(const char *,...) __attribute__((format(printf
void Rast_write_units(const char *name, const char *str)
Write a string to a raster map&#39;s units metadata file.
#define _(str)
Definition: glocale.h:10
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
const char * name
Definition: named_colr.c:7
void Rast_write_vdatum(const char *name, const char *str)
Write a string into a raster&#39;s vertical datum metadata file.
char * Rast_read_units(const char *name, const char *mapset)
Get a raster map&#39;s units metadata string.