GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
read_nat.c
Go to the documentation of this file.
1 
26 #include <grass/gis.h>
27 #include <grass/Vect.h>
28 #include <grass/glocale.h>
29 
30 static int
31 Vect__Read_line_nat(struct Map_info *,
32  struct line_pnts *, struct line_cats *, long);
33 
47 int
48 V1_read_line_nat(struct Map_info *Map,
49  struct line_pnts *Points,
50  struct line_cats *Cats, long offset)
51 {
52  return Vect__Read_line_nat(Map, Points, Cats, offset);
53 }
54 
67 int
68 V1_read_next_line_nat(struct Map_info *Map,
69  struct line_pnts *line_p, struct line_cats *line_c)
70 {
71  int itype;
72  long offset;
73  BOUND_BOX lbox, mbox;
74 
75  G_debug(3, "V1_read_next_line_nat()");
76 
77  if (Map->Constraint_region_flag)
78  Vect_get_constraint_box(Map, &mbox);
79 
80  while (1) {
81  offset = dig_ftell(&(Map->dig_fp));
82  itype = Vect__Read_line_nat(Map, line_p, line_c, offset);
83  if (itype < 0)
84  return (itype);
85 
86  if (itype == 0) /* is it DEAD? */
87  continue;
88 
89  /* Constraint on Type of line
90  * Default is all of Point, Line, Area and whatever else comes along
91  */
92  if (Map->Constraint_type_flag) {
93  if (!(itype & Map->Constraint_type))
94  continue;
95  }
96 
97  /* Constraint on specified region */
98  if (Map->Constraint_region_flag) {
99  Vect_line_box(line_p, &lbox);
100 
101  if (!Vect_box_overlap(&lbox, &mbox))
102  continue;
103  }
104 
105  return (itype);
106  }
107  /* NOTREACHED */
108 }
109 
123 int
124 V2_read_line_nat(struct Map_info *Map,
125  struct line_pnts *line_p, struct line_cats *line_c, int line)
126 {
127  P_LINE *Line;
128 
129  G_debug(3, "V2_read_line_nat(): line = %d", line);
130 
131 
132  Line = Map->plus.Line[line];
133 
134  if (Line == NULL)
135  G_fatal_error("V2_read_line_nat(): %s %d",
136  _("Attempt to read dead line"), line);
137 
138  return Vect__Read_line_nat(Map, line_p, line_c, Line->offset);
139 }
140 
153 int
154 V2_read_next_line_nat(struct Map_info *Map,
155  struct line_pnts *line_p, struct line_cats *line_c)
156 {
157  register int line;
158  register P_LINE *Line;
159  BOUND_BOX lbox, mbox;
160 
161  G_debug(3, "V2_read_next_line_nat()");
162 
163  if (Map->Constraint_region_flag)
164  Vect_get_constraint_box(Map, &mbox);
165 
166  while (1) {
167  line = Map->next_line;
168 
169  if (line > Map->plus.n_lines)
170  return (-2);
171 
172  Line = Map->plus.Line[line];
173  if (Line == NULL) { /* Dead line */
174  Map->next_line++;
175  continue;
176  }
177 
178  if ((Map->Constraint_type_flag &&
179  !(Line->type & Map->Constraint_type))) {
180  Map->next_line++;
181  continue;
182  }
183 
184  if (Map->Constraint_region_flag) {
185  Vect_get_line_box(Map, line, &lbox);
186  if (!Vect_box_overlap(&lbox, &mbox)) {
187  Map->next_line++;
188  continue;
189  }
190  }
191 
192  return V2_read_line_nat(Map, line_p, line_c, Map->next_line++);
193  }
194 
195  /* NOTREACHED */ }
196 
197 
211 int
212 Vect__Read_line_nat(struct Map_info *Map,
213  struct line_pnts *p, struct line_cats *c, long offset)
214 {
215  int i, dead = 0;
216  int n_points;
217  long size;
218  int n_cats, do_cats;
219  int type;
220  char rhead, nc;
221  short field;
222 
223  G_debug(3, "Vect__Read_line_nat: offset = %ld", offset);
224 
225  Map->head.last_offset = offset;
226 
227  /* reads must set in_head, but writes use default */
228  dig_set_cur_port(&(Map->head.port));
229 
230  dig_fseek(&(Map->dig_fp), offset, 0);
231 
232  if (0 >= dig__fread_port_C(&rhead, 1, &(Map->dig_fp)))
233  return (-2);
234 
235  if (!(rhead & 0x01)) /* dead line */
236  dead = 1;
237 
238  if (rhead & 0x02) /* categories exists */
239  do_cats = 1; /* do not return here let file offset moves forward to next */
240  else /* line */
241  do_cats = 0;
242 
243  rhead >>= 2;
244  type = dig_type_from_store((int)rhead);
245 
246  G_debug(3, " type = %d, do_cats = %d dead = %d", type, do_cats, dead);
247 
248  if (c != NULL)
249  c->n_cats = 0;
250 
251  if (do_cats) {
252  if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
253  if (0 >= dig__fread_port_I(&n_cats, 1, &(Map->dig_fp)))
254  return (-2);
255  }
256  else { /* coor format 5.0 */
257  if (0 >= dig__fread_port_C(&nc, 1, &(Map->dig_fp)))
258  return (-2);
259  n_cats = (int)nc;
260  }
261  G_debug(3, " n_cats = %d", n_cats);
262 
263  if (c != NULL) {
264  c->n_cats = n_cats;
265  if (n_cats > 0) {
266  if (0 > dig_alloc_cats(c, (int)n_cats + 1))
267  return (-1);
268 
269  if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
270  if (0 >=
271  dig__fread_port_I(c->field, n_cats, &(Map->dig_fp)))
272  return (-2);
273  }
274  else { /* coor format 5.0 */
275  for (i = 0; i < n_cats; i++) {
276  if (0 >= dig__fread_port_S(&field, 1, &(Map->dig_fp)))
277  return (-2);
278  c->field[i] = (int)field;
279  }
280  }
281  if (0 >= dig__fread_port_I(c->cat, n_cats, &(Map->dig_fp)))
282  return (-2);
283 
284  }
285  }
286  else {
287  if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
288  size = (2 * PORT_INT) * n_cats;
289  }
290  else { /* coor format 5.0 */
291  size = (PORT_SHORT + PORT_INT) * n_cats;
292  }
293 
294  dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
295  }
296  }
297 
298  if (type & GV_POINTS) {
299  n_points = 1;
300  }
301  else {
302  if (0 >= dig__fread_port_I(&n_points, 1, &(Map->dig_fp)))
303  return (-2);
304  }
305 
306  G_debug(3, " n_points = %d", n_points);
307 
308  if (p != NULL) {
309  if (0 > dig_alloc_points(p, n_points + 1))
310  return (-1);
311 
312  p->n_points = n_points;
313  if (0 >= dig__fread_port_D(p->x, n_points, &(Map->dig_fp)))
314  return (-2);
315  if (0 >= dig__fread_port_D(p->y, n_points, &(Map->dig_fp)))
316  return (-2);
317 
318  if (Map->head.with_z) {
319  if (0 >= dig__fread_port_D(p->z, n_points, &(Map->dig_fp)))
320  return (-2);
321  }
322  else {
323  for (i = 0; i < n_points; i++)
324  p->z[i] = 0.0;
325  }
326  }
327  else {
328  if (Map->head.with_z)
329  size = n_points * 3 * PORT_DOUBLE;
330  else
331  size = n_points * 2 * PORT_DOUBLE;
332 
333  dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
334  }
335 
336  G_debug(3, " off = %ld", dig_ftell(&(Map->dig_fp)));
337 
338  if (dead)
339  return 0;
340 
341  return (type);
342 }
int V1_read_next_line_nat(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Read next line from coor file.
Definition: read_nat.c:68
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
int Vect_line_box(struct line_pnts *Points, BOUND_BOX *Box)
Get bounding box of line.
Definition: line.c:771
int Vect_get_constraint_box(struct Map_info *Map, BOUND_BOX *Box)
Get constraint box.
Definition: constraint.c:81
int dig__fread_port_S(short *buf, int cnt, GVFILE *fp)
Definition: portable.c:277
int V2_read_line_nat(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Reads any specified line, this is NOT affected by constraints.
Definition: read_nat.c:124
int Vect_box_overlap(BOUND_BOX *A, BOUND_BOX *B)
Tests for overlap of two boxes.
Definition: Vlib/box.c:53
int V2_read_next_line_nat(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Reads next unread line each time called. Use Vect_rewind to reset.
Definition: read_nat.c:154
int dig_alloc_cats(struct line_cats *cats, int num)
Definition: struct_alloc.c:272
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
Definition: file.c:60
int dig_alloc_points(struct line_pnts *points, int num)
Definition: struct_alloc.c:232
int dig_type_from_store(int stype)
Convert type from store type.
int
Definition: g3dcolor.c:48
int dig__fread_port_D(double *buf, int cnt, GVFILE *fp)
Definition: portable.c:75
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:347
return NULL
Definition: dbfopen.c:1394
int V1_read_line_nat(struct Map_info *Map, struct line_pnts *Points, struct line_cats *Cats, long offset)
Read line from coor file on given offset.
Definition: read_nat.c:48
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int Vect_get_line_box(struct Map_info *Map, int line, BOUND_BOX *Box)
Get boundary box of line.
Definition: Vlib/box.c:208
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
long dig_ftell(GVFILE *file)
Get GVFILE position.
Definition: file.c:36
int dig__fread_port_I(int *buf, int cnt, GVFILE *fp)
Definition: portable.c:207