GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Gp3.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 
21 #include <grass/gis.h>
22 #include <grass/site.h>
23 #include <grass/Vect.h>
24 #include <grass/glocale.h>
25 #include <grass/gstypes.h>
26 
41 int Gp_set_color(const char *grassname, geopoint * gp)
42 {
43  const char *col_map;
44  struct Colors sc;
45  CELL cat;
46  geopoint *tp;
47  int r, g, b, color;
48 
49  /* TODO: handle error messages */
50 
51  if (grassname) {
52  col_map = G_find_cell2(grassname, "");
53  if (!col_map) {
54  G_warning(_("Raster map <%s> not found"), grassname);
55  return 0;
56  }
57 
58  G_read_colors(grassname, col_map, &sc);
59 
60  for (tp = gp; tp; tp = tp->next) {
61  cat = (int)tp->fattr;
62  color = NULL_COLOR;
63 
64  if (G_get_color(cat, &r, &g, &b, &sc)) {
65  color = (r & 0xff) | ((g & 0xff) << 8) | ((b & 0xff) << 16);
66  }
67 
68  tp->iattr = color;
69  }
70 
71  return (1);
72  }
73 
74  return (0);
75 }
76 
90 geopoint *Gp_load_sites(const char *grassname, int *nsites, int *has_z,
91  int *has_att)
92 {
93  struct Map_info map;
94  static struct line_pnts *Points = NULL;
95  static struct line_cats *Cats = NULL;
96  geopoint *top, *gpt, *prev;
97  int np, ltype, eof;
98  struct Cell_head wind;
99  RASTER_MAP_TYPE rtype;
100  int ndim;
101  const char *mapset;
102 
103  np = 0;
104  eof = 0;
105  *has_z = *has_att = 0;
106 
107  mapset = G_find_vector2(grassname, "");
108  if (!mapset) {
109  G_warning(_("Vector map <%s> not found"), grassname);
110  return NULL;
111  }
112 
114  if (Vect_open_old(&map, grassname, "") == -1) {
115  G_fatal_error(_("Unable to open vector map <%s>"),
116  G_fully_qualified_name(grassname, mapset));
117  }
118 
119  Points = Vect_new_line_struct();
120  Cats = Vect_new_cats_struct();
121 
122  top = gpt = (geopoint *) G_malloc(sizeof(geopoint));
123  if (!top) {
124  return (NULL);
125  }
126 
127  G_get_set_window(&wind);
128  Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
129  wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
130 
131  /* get ndim */
132  ndim = 2;
133  if (Vect_is_3d(&map)) {
134  ndim = 3;
135  }
136 
137  /* set rtype */
138  rtype = CELL_TYPE;
139 
140  while (eof == 0) {
141  ltype = Vect_read_next_line(&map, Points, Cats);
142  switch (ltype) {
143  case -1:
144  {
145  G_warning(_("Unable to read vector map <%s>"),
146  G_fully_qualified_name(grassname, mapset));
147  return (NULL);
148  }
149  case -2: /* EOF */
150  {
151  eof = 1;
152  continue;
153  }
154  }
155  if ((ltype & GV_POINTS)) {
156  np++;
157  gpt->p3[X] = Points->x[0];
158  gpt->p3[Y] = Points->y[0];
159 
160  if (ndim > 2) {
161  *has_z = 1;
162  gpt->dims = 3;
163  gpt->p3[Z] = Points->z[0];
164  }
165  else {
166  gpt->dims = 2;
167  *has_z = 0;
168  }
169 
170  if (Cats->n_cats > 0) {
171  *has_att = 1;
172  gpt->fattr = Cats->field[0]; /* Is this correct? */
173  /* gpt->cat = ; ??** */
174  gpt->highlight_color = gpt->highlight_size =
175  gpt->highlight_marker = FALSE;
176  }
177  else {
178  gpt->fattr = 0;
179  *has_att = 0;
180  }
181 
182  gpt->iattr = gpt->fattr;
183  gpt->cattr = NULL;
184 
185  G_debug(3, "loading vector point %d %f %f -- %d",
186  np, Points->x[0], Points->y[0], Cats->n_cats);
187 
188  gpt->next = (geopoint *) G_malloc(sizeof(geopoint)); /* G_fatal_error */
189  if (!gpt->next) {
190  return (NULL);
191  }
192 
193  prev = gpt;
194  gpt = gpt->next;
195  }
196 
197  }
198  if (np > 0) {
199  prev->next = NULL;
200  G_free(gpt);
201  }
202 
203  Vect_close(&map);
204 
205  if (!np) {
206  G_warning(_("No points from vector map <%s> fall within current region"),
207  G_fully_qualified_name(grassname, mapset));
208  return (NULL);
209  }
210  else {
211  G_message(_("Vector map <%s> loaded (%d points)"),
212  G_fully_qualified_name(grassname, mapset), np);
213  }
214 
215  *nsites = np;
216 
217  return (top);
218 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
float b
Definition: named_colr.c:8
int G_get_set_window(struct Cell_head *window)
Get the current working window.
Definition: set_window.c:30
#define FALSE
Definition: dbfopen.c:117
int Vect_read_next_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Read next vector feature (level 1 and 2)
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
float r
Definition: named_colr.c:8
#define Y(x)
Definition: display/draw.c:246
#define X(y)
Definition: display/draw.c:248
char * G_find_vector2(const char *name, const char *mapset)
find a vector map (look but don&#39;t touch)
Definition: find_vect.c:75
int G_read_colors(const char *name, const char *mapset, struct Colors *colors)
read map layer color table
Definition: color_read.c:62
int Gp_set_color(const char *grassname, geopoint *gp)
Set color for point set.
Definition: Gp3.c:41
int Vect_is_3d(struct Map_info *Map)
Check if vector map is 3D (with z)
int Vect_set_open_level(int level)
Predetermine level at which a map will be opened for reading.
char * G_find_cell2(const char *name, const char *mapset)
find a raster map (look but don&#39;t touch)
Definition: find_cell.c:83
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
tuple color
Definition: tools.py:1703
int G_get_color(CELL n, int *red, int *grn, int *blu, struct Colors *colors)
Get a category color.
Definition: color_get.c:36
float g
Definition: named_colr.c:8
geopoint * Gp_load_sites(const char *grassname, int *nsites, int *has_z, int *has_att)
Load to points to memory.
Definition: Gp3.c:90
int
Definition: g3dcolor.c:48
if(!YY_CURRENT_BUFFER)
Definition: lex.yy.c:799
int Vect_set_constraint_region(struct Map_info *Map, double n, double s, double e, double w, double t, double b)
Set constraint region.
Definition: constraint.c:52
int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector for reading.
int Vect_close(struct Map_info *Map)
Close vector data file.
Definition: close.c:64
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
char * G_fully_qualified_name(const char *name, const char *mapset)
fully qualified file name
Definition: nme_in_mps.c:118
CELL cat
Definition: g3dcats.c:90
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.