GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71759
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/color_write.c
4  \brief Vector Library - write color table for vector map
6  (C) 2011 by the GRASS Development Team
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
11  \author Martin Landa <landa.martin>
12 */
14 #include <string.h>
16 #include <grass/gis.h>
17 #include <grass/vector.h>
18 #include <grass/raster.h>
19 #include <grass/glocale.h>
21 /*!
22  \brief Write color table for vector map
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.
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().
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>.
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.
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
55  \param name vector map name
56  \param mapset mapset name
57  \param colors pointer to structure Colors which holds color info
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;
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  }
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  }
90  if (!(fd = G_fopen_new(element, cname)))
91  G_fatal_error(_("Unable to create <%s> file for map <%s>"),
92  element, name);
94  Rast__write_colors(fd, colors);
95  fclose(fd);
96 }
Definition: gis.h:149
Name of vector directory.
Definition: dig_defines.h:8
void Vect_write_colors(const char *name, const char *mapset, struct Colors *colors)
Write color table for vector map.
Definition: d/range.c:69
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
Definition: lidar.h:89
#define GPATH_MAX
Definition: gis.h:151
Name of directory for alternative color tables.
Definition: dig_defines.h:30
Definition: gis.h:626
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:220
#define GNAME_MAX
Definition: gis.h:148
#define _(str)
Definition: glocale.h:13
void Rast__write_colors(FILE *fd, struct Colors *colors)
Write map layer color table.
Color table.
Definition: dig_defines.h:28
int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * name
Definition: named_colr.c:7