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 */
Rast_read_units(const char * name,const char * mapset)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 */
Rast_write_units(const char * name,const char * str)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 */
Rast_read_vdatum(const char * name,const char * mapset)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 */
Rast_write_vdatum(const char * name,const char * str)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 */
misc_read_line(const char * elem,const char * name,const char * mapset)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 */
misc_write_line(const char * elem,const char * name,const char * str)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
152