GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
vector/Vlib/color_write.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/color_write.c
3 
4  \brief Vector Library - write color table for vector map
5 
6  (C) 2011 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 Martin Landa <landa.martin gmail.com>
12  */
13 
14 #include <string.h>
15 
16 #include <grass/gis.h>
17 #include <grass/vector.h>
18 #include <grass/raster.h>
19 #include <grass/glocale.h>
20 
21 /*!
22  \brief Write color table for vector map
23 
24  The color table is written for the vector map <i>name</i> in the
25  specified <i>mapset</i> from the <i>colors</i> structure.
26 
27  The <i>colors</i> structure must be created properly, i.e.,
28  Rast_init_colors() to initialize the structure and
29  Rast_add_c_color_rule() to set the category colors. These routines
30  are called by higher level routines which read or create entire
31  color tables, such as Rast_read_colors() or Rast_make_ramp_colors().
32 
33  <b>Note:</b> The calling sequence for this function deserves
34  special attention. The <i>mapset</i> parameter seems to imply that
35  it is possible to overwrite the color table for a vector map which
36  is in another mapset. However, this is not what actually
37  happens. It is very useful for users to create their own color
38  tables for vector maps in other mapsets, but without overwriting
39  other users' color tables for the same raster map. If <i>mapset</i>
40  is the current mapset, then the color file for <i>name</i> will be
41  overwritten by the new color table. But if <i>mapset</i> is not the
42  current mapset, then the color table is actually written in the
43  current mapset under the <tt>colr2</tt> element as:
44  <tt>vector/name/colr2</tt>.
45 
46  The rules are written out using floating-point format, removing
47  trailing zeros (possibly producing integers). The flag marking the
48  colors as floating-point is <b>not</b> written.
49 
50  If the environment variable FORCE_GRASS3_COLORS is set (to anything
51  at all) then the output format is 3.0, even if the structure
52  contains 4.0 rules. This allows users to create 3.0 color files for
53  export to sites which don't yet have 4.0
54 
55  \param name vector map name
56  \param mapset mapset name
57  \param colors pointer to structure Colors which holds color info
58 
59  \return void
60  */
61 void Vect_write_colors(const char *name, const char *mapset,
62  struct Colors *colors)
63 {
64  char element[GPATH_MAX];
65  const char *cname;
66  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
67  FILE *fd;
68 
69  if (G_name_is_fully_qualified(name, xname, xmapset)) {
70  if (strcmp(xmapset, mapset) != 0)
71  G_fatal_error(_("Qualified name <%s> doesn't match mapset <%s>"),
72  name, mapset);
73  name = xname;
74  mapset = xmapset;
75  }
76 
77  /*
78  if mapset is current mapset, write original color table
79  else write secondary color table
80  */
81  if (strcmp(mapset, G_mapset()) == 0) {
82  cname = GV_COLR_ELEMENT;
83  sprintf(element, "%s/%s", GV_DIRECTORY, name);
84  }
85  else {
86  cname = name;
87  sprintf(element, "%s/%s", GV_COLR2_DIRECTORY, mapset);
88  }
89 
90  if (!(fd = G_fopen_new(element, cname)))
91  G_fatal_error(_("Unable to create <%s> file for map <%s>"), element,
92  name);
93 
94  Rast__write_colors(fd, colors);
95  fclose(fd);
96 }
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition: gis/open.c:219
void Rast__write_colors(FILE *, struct Colors *)
Write map layer color table.
#define GV_COLR2_DIRECTORY
Name of directory for alternative color tables.
Definition: dig_defines.h:30
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
#define GV_COLR_ELEMENT
Color table.
Definition: dig_defines.h:28
#define GMAPSET_MAX
Definition: gis.h:192
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
const char * name
Definition: named_colr.c:6
Definition: gis.h:683
Definition: lidar.h:85
void Vect_write_colors(const char *name, const char *mapset, struct Colors *colors)
Write color table for vector map.