GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Gv3.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 
21 #include <grass/gis.h>
22 #include <grass/Vect.h>
23 #include <grass/glocale.h>
24 #include <grass/gstypes.h>
25 
26 /*
27  #define TRAK_MEM
28  */
29 
30 #ifdef TRAK_MEM
31 static int Tot_mem = 0;
32 #endif
33 
45 geoline *Gv_load_vect(const char *grassname, int *nlines)
46 {
47  struct Map_info map;
48  struct line_pnts *points;
49  geoline *top, *gln, *prev;
50  int np, i, n, nareas, nl = 0, area, type, is3d;
51  struct Cell_head wind;
52  float vect[2][3];
53  const char *mapset;
54 
55  mapset = G_find_vector2(grassname, "");
56  if (!mapset) {
57  G_warning(_("Vector map <%s> not found"), grassname);
58  return NULL;
59  }
60 
62  if (Vect_open_old(&map, grassname, "") == -1) {
63  G_warning(_("Unable to open vector map <%s>"),
64  G_fully_qualified_name(grassname, mapset));
65  return NULL;
66  }
67 
68  top = gln = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
69  if (!top) {
70  return NULL;
71  }
72 
73  prev = top;
74 
75 #ifdef TRAK_MEM
76  Tot_mem += sizeof(geoline);
77 #endif
78 
79  points = Vect_new_line_struct();
80 
81  G_get_set_window(&wind);
82  Vect_set_constraint_region(&map, wind.north, wind.south, wind.east,
83  wind.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);
84 
85  is3d = Vect_is_3d(&map);
86 
87  /* Read areas */
88  n = Vect_get_num_areas(&map);
89  nareas = 0;
90  G_debug(3, "Reading vector areas (nareas = %d)", n);
91  for (area = 1; area <= n; area++) {
92  G_debug(3, " area %d", area);
93 
94  Vect_get_area_points(&map, area, points);
95  if (points->n_points < 3)
96  continue;
97  gln->type = OGSF_POLYGON;
98  gln->npts = np = points->n_points;
99  G_debug(3, " np = %d", np);
100 
101  if (is3d) {
102  gln->dims = 3;
103  gln->p3 = (Point3 *) G_calloc(np, sizeof(Point3)); /* G_fatal_error */
104  if (!gln->p3) {
105  return (NULL);
106  }
107 #ifdef TRAK_MEM
108  Tot_mem += (np * sizeof(Point3));
109 #endif
110  }
111  else {
112  gln->dims = 2;
113  gln->p2 = (Point2 *) G_calloc(np, sizeof(Point2)); /* G_fatal_error */
114  if (!gln->p2) {
115  return (NULL);
116  }
117 #ifdef TRAK_MEM
118  Tot_mem += (np * sizeof(Point2));
119 #endif
120  }
121 
122  for (i = 0; i < np; i++) {
123  if (is3d) {
124  gln->p3[i][X] = points->x[i];
125  gln->p3[i][Y] = points->y[i];
126  gln->p3[i][Z] = points->z[i];
127  }
128  else {
129  gln->p2[i][X] = points->x[i];
130  gln->p2[i][Y] = points->y[i];
131  }
132  }
133  /* Calc normal (should be average) */
134  if (is3d) {
135  vect[0][X] = (float)(gln->p3[0][X] - gln->p3[1][X]);
136  vect[0][Y] = (float)(gln->p3[0][Y] - gln->p3[1][Y]);
137  vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
138  vect[1][X] = (float)(gln->p3[2][X] - gln->p3[1][X]);
139  vect[1][Y] = (float)(gln->p3[2][Y] - gln->p3[1][Y]);
140  vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
141  GS_v3cross(vect[1], vect[0], gln->norm);
142 
143  }
144 
145  gln->next = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
146  if (!gln->next) {
147  return (NULL);
148  }
149 
150 #ifdef TRAK_MEM
151  Tot_mem += sizeof(geoline);
152 #endif
153 
154  prev = gln;
155  gln = gln->next;
156  nareas++;
157  }
158  G_debug(3, "%d areas loaded", nareas);
159 
160  /* Read all lines */
161  G_debug(3, "Reading vector lines ...");
162  while (-1 < (type = Vect_read_next_line(&map, points, NULL))) {
163  G_debug(3, "line type = %d", type);
164 
165  if (type & (GV_LINES | GV_FACE)) {
166  if (type & (GV_LINES)) {
167  gln->type = OGSF_LINE;
168  }
169  else {
170  gln->type = OGSF_POLYGON;
171  /* Vect_append_point ( points, points->x[0], points->y[0], points->z[0] ); */
172  }
173 
174  gln->npts = np = points->n_points;
175  G_debug(3, " np = %d", np);
176 
177  if (is3d) {
178  gln->dims = 3;
179  gln->p3 = (Point3 *) G_calloc(np, sizeof(Point3)); /* G_fatal_error */
180  if (!gln->p3) {
181  return (NULL);
182  }
183 #ifdef TRAK_MEM
184  Tot_mem += (np * sizeof(Point3));
185 #endif
186  }
187  else {
188  gln->dims = 2;
189  gln->p2 = (Point2 *) G_calloc(np, sizeof(Point2)); /* G_fatal_error */
190  if (!gln->p2) {
191  return (NULL);
192  }
193 #ifdef TRAK_MEM
194  Tot_mem += (np * sizeof(Point2));
195 #endif
196  }
197 
198  for (i = 0; i < np; i++) {
199  if (is3d) {
200  gln->p3[i][X] = points->x[i];
201  gln->p3[i][Y] = points->y[i];
202  gln->p3[i][Z] = points->z[i];
203  }
204  else {
205  gln->p2[i][X] = points->x[i];
206  gln->p2[i][Y] = points->y[i];
207  }
208  }
209  /* Calc normal (should be average) */
210  if (is3d && gln->type == OGSF_POLYGON) {
211  vect[0][X] = (float)(gln->p3[0][X] - gln->p3[1][X]);
212  vect[0][Y] = (float)(gln->p3[0][Y] - gln->p3[1][Y]);
213  vect[0][Z] = (float)(gln->p3[0][Z] - gln->p3[1][Z]);
214  vect[1][X] = (float)(gln->p3[2][X] - gln->p3[1][X]);
215  vect[1][Y] = (float)(gln->p3[2][Y] - gln->p3[1][Y]);
216  vect[1][Z] = (float)(gln->p3[2][Z] - gln->p3[1][Z]);
217  GS_v3cross(vect[1], vect[0], gln->norm);
218  G_debug(3, "norm %f %f %f", gln->norm[0], gln->norm[1],
219  gln->norm[2]);
220  }
221 
222  gln->next = (geoline *) G_malloc(sizeof(geoline)); /* G_fatal_error */
223  if (!gln->next) {
224  return (NULL);
225  }
226 #ifdef TRAK_MEM
227  Tot_mem += sizeof(geoline);
228 #endif
229 
230  prev = gln;
231  gln = gln->next;
232  nl++;
233  }
234  }
235  G_debug(3, "%d lines loaded", nl);
236 
237  nl += nareas;
238 
239  prev->next = NULL;
240  G_free(gln);
241 
242 #ifdef TRAK_MEM
243  Tot_mem -= sizeof(geoline);
244 #endif
245 
246  Vect_close(&map);
247 
248  if (!nl) {
249  G_warning(_("No features from vector map <%s> fall within current region"),
250  G_fully_qualified_name(grassname, mapset));
251  return (NULL);
252  }
253  else {
254  G_message(_("Vector map <%s> loaded (%d features)"),
255  G_fully_qualified_name(grassname, mapset), nl);
256  }
257 
258  *nlines = nl;
259 
260 #ifdef TRAK_MEM
261  G_debug(3, "Total vect memory = %d Kbytes", Tot_mem / 1000);
262 #endif
263 
264  return (top);
265 }
266 
272 void sub_Vectmem(int minus)
273 {
274 #ifdef TRAK_MEM
275  {
276  Tot_mem -= minus;
277  }
278 #endif
279 
280  return;
281 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_get_set_window(struct Cell_head *window)
Get the current working window.
Definition: set_window.c:30
geoline * Gv_load_vect(const char *grassname, int *nlines)
Load vector map to memory.
Definition: Gv3.c:45
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
#define Y(x)
Definition: display/draw.c:246
int Vect_get_num_areas(struct Map_info *map)
Get number of areas in vector map.
Definition: level_two.c:81
#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
void GS_v3cross(float *v1, float *v2, float *v3)
Get the cross product v3 = v1 cross v2.
Definition: GS_util.c:406
int Vect_get_area_points(struct Map_info *Map, int area, struct line_pnts *BPoints)
Returns the polygon array of points in BPoints.
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.
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
int GV_LINES
Definition: vdigit/main.py:24
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
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
int n
Definition: dataquad.c:291
void sub_Vectmem(int minus)
Tracking memory.
Definition: Gv3.c:272