GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
raster/band_reference.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/band_reference.c
3  *
4  * \brief Raster Library - Band reference managenent
5  *
6  * (C) 2019 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author Martin Landa (with financial support by mundialis, Bonn, for openEO EU H2020 grant 776242, https://openeo.org)
13  */
14 
15 #include <string.h>
16 
17 #include <grass/gis.h>
18 #include <grass/raster.h>
19 #include <grass/glocale.h>
20 
21 static const char *_band_file = "band_reference";
22 
23 /*!
24  \brief Check if band reference for raster map exists
25 
26  \param name map name
27  \param mapset mapset name
28 
29  \return 1 on success
30  \return 0 no band reference present
31 */
32 int Rast_has_band_reference(const char *name, const char *mapset)
33 {
34  if (!G_find_file2_misc("cell_misc", _band_file, name, mapset))
35  return 0;
36 
37  return 1;
38 }
39 
40 /*!
41  \brief Read raster map band reference identifier.
42 
43  Note that output arguments should be freed by the caller using G_free().
44 
45  \param name map name
46  \param mapset mapset name
47  \param[out] filename filename JSON reference
48  \param[out] band_reference band reference identifier
49 
50  \return 1 on success
51  \return 0 band reference not found
52  \return negative on error
53  */
54 int Rast_read_band_reference(const char *name, const char *mapset,
55  char **filename, char **band_reference)
56 {
57  int ret;
58  FILE *fd;
59  struct Key_Value *key_val;
60 
61  G_debug(1, "Reading band reference file for raster map <%s@%s>",
62  name, mapset);
63 
64  if (!Rast_has_band_reference(name, mapset))
65  return 0;
66 
67  fd = G_fopen_old_misc("cell_misc", _band_file, name, mapset);
68  if (!fd) {
69  G_debug(1, "Unable to read band identifier file for <%s@%s>",
70  name, mapset);
71  return -1;
72  }
73 
74  ret = G__read_band_reference(fd, &key_val);
75  *filename = G_store(G_find_key_value("file", key_val));
76  *band_reference = G_store(G_find_key_value("identifier", key_val));
77 
78  fclose(fd);
79  G_free_key_value(key_val);
80 
81  return ret;
82 }
83 
84 /*!
85  \brief Write raster map band reference identifier.
86 
87  \param name map name
88  \param filename filename JSON reference
89  \param band_reference band reference identifier
90 
91  \return 1 on success
92  \return negative on error
93  */
95  const char *filename, const char *band_reference)
96 {
97  int ret;
98  FILE *fd;
99 
100  fd = G_fopen_new_misc("cell_misc", _band_file, name);
101  if (!fd) {
102  G_fatal_error(_("Unable to create band file for <%s>"), name);
103  return -1;
104  }
105 
106  ret = G__write_band_reference(fd, filename, band_reference);
107  fclose(fd);
108 
109  return ret;
110 }
111 
112 /*!
113  \brief Remove band reference from raster map
114 
115  Only band reference files in current mapset can be removed.
116 
117  \param name map name
118 
119  \return 0 if no file
120  \return 1 on success
121  \return -1 on error
122 */
124 {
125  return G_remove_misc("cell_misc", _band_file, name);
126 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int G__read_band_reference(FILE *, struct Key_Value **)
Read band reference identifier from file (internal use only).
int Rast_has_band_reference(const char *name, const char *mapset)
Check if band reference for raster map exists.
int G__write_band_reference(FILE *, const char *, const char *)
Write band reference identifier to file (internal use only).
int Rast_read_band_reference(const char *name, const char *mapset, char **filename, char **band_reference)
Read raster map band reference identifier.
int G_remove_misc(const char *, const char *, const char *)
Remove a database misc file.
Definition: remove.c:65
int Rast_write_band_reference(const char *name, const char *filename, const char *band_reference)
Write raster map band reference identifier.
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database file for reading
Definition: open_misc.c:210
Definition: gis.h:501
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
#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
int Rast_remove_band_reference(const char *name)
Remove band reference from raster map.
int G_debug(int, const char *,...) __attribute__((format(printf
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
Definition: key_value1.c:103
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
Definition: key_value1.c:84