GRASS Programmer's Manual  6.4.2(2012)
 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 
129  /* get ndim */
130  ndim = 2;
131  if (Vect_is_3d(&map)) {
132  ndim = 3;
133  }
134 
135  /* set rtype */
136  rtype = CELL_TYPE;
137 
138  while (eof == 0) {
139  ltype = Vect_read_next_line(&map, Points, Cats);
140  switch (ltype) {
141  case -1:
142  {
143  G_warning(_("Unable to read vector map <%s>"),
144  G_fully_qualified_name(grassname, mapset));
145  return (NULL);
146  }
147  case -2: /* EOF */
148  {
149  eof = 1;
150  continue;
151  }
152  }
153  if ((ltype & GV_POINTS)) {
154  np++;
155  gpt->p3[X] = Points->x[0];
156  gpt->p3[Y] = Points->y[0];
157 
158  if (ndim > 2) {
159  *has_z = 1;
160  gpt->dims = 3;
161  gpt->p3[Z] = Points->z[0];
162  }
163  else {
164  gpt->dims = 2;
165  *has_z = 0;
166  }
167 
168  if (Cats->n_cats > 0) {
169  *has_att = 1;
170  gpt->fattr = Cats->field[0]; /* Is this correct? */
171  /* gpt->cat = ; ??** */
172  gpt->highlight_color = gpt->highlight_size =
173  gpt->highlight_marker = FALSE;
174  }
175  else {
176  gpt->fattr = 0;
177  *has_att = 0;
178  }
179 
180  gpt->iattr = gpt->fattr;
181  gpt->cattr = NULL;
182 
183  G_debug(3, "loading vector point %d %f %f -- %d",
184  np, Points->x[0], Points->y[0], Cats->n_cats);
185 
186  gpt->next = (geopoint *) G_malloc(sizeof(geopoint)); /* G_fatal_error */
187  if (!gpt->next) {
188  return (NULL);
189  }
190 
191  prev = gpt;
192  gpt = gpt->next;
193  }
194 
195  }
196  if (np > 0) {
197  prev->next = NULL;
198  G_free(gpt);
199  }
200 
201  Vect_close(&map);
202 
203  if (!np) {
204  G_warning(_("No points from vector map <%s> fall within current region"),
205  G_fully_qualified_name(grassname, mapset));
206  return (NULL);
207  }
208  else {
209  G_message(_("Vector map <%s> loaded (%d points)"),
210  G_fully_qualified_name(grassname, mapset), np);
211  }
212 
213  *nsites = np;
214 
215  return (top);
216 }