GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 }