GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
plus.c
Go to the documentation of this file.
1 
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <grass/gis.h>
21 #include <grass/Vect.h>
22 #include <grass/glocale.h>
23 
31 int dig_init_plus(struct Plus_head *Plus)
32 {
33  G_debug(3, "dig_init_plus()");
34 
35  Plus->Version_Major = 0;
36  Plus->Version_Minor = 0;
37  Plus->Back_Major = 0;
38  Plus->Back_Minor = 0;
39  Plus->with_z = 0;
40 
41  Plus->box.N = 0;
42  Plus->box.S = 0;
43  Plus->box.E = 0;
44  Plus->box.W = 0;
45  Plus->box.T = 0;
46  Plus->box.B = 0;
47 
48  Plus->built = GV_BUILD_NONE;
49 
50  Plus->Node = NULL;
51  Plus->Line = NULL;
52  Plus->Area = NULL;
53  Plus->Isle = NULL;
54 
55  Plus->n_nodes = 0;
56  Plus->n_edges = 0;
57  Plus->n_lines = 0;
58  Plus->n_areas = 0;
59  Plus->n_isles = 0;
60  Plus->n_volumes = 0;
61  Plus->n_holes = 0;
62 
63  Plus->alloc_nodes = 0;
64  Plus->alloc_edges = 0;
65  Plus->alloc_lines = 0;
66  Plus->alloc_areas = 0;
67  Plus->alloc_isles = 0;
68  Plus->alloc_volumes = 0;
69  Plus->alloc_holes = 0;
70 
71  Plus->n_plines = 0;
72  Plus->n_llines = 0;
73  Plus->n_blines = 0;
74  Plus->n_clines = 0;
75  Plus->n_flines = 0;
76  Plus->n_klines = 0;
77 
78  Plus->Node_offset = 0L;
79  Plus->Edge_offset = 0L;
80  Plus->Line_offset = 0L;
81  Plus->Area_offset = 0L;
82  Plus->Isle_offset = 0L;
83  Plus->Volume_offset = 0L;
84  Plus->Hole_offset = 0L;
85 
86  Plus->Node_spidx_offset = 0L;
87  Plus->Edge_spidx_offset = 0L;
88  Plus->Line_spidx_offset = 0L;
89  Plus->Area_spidx_offset = 0L;
90  Plus->Isle_spidx_offset = 0L;
91  Plus->Volume_spidx_offset = 0L;
92  Plus->Hole_spidx_offset = 0L;
93 
94  dig_spidx_init(Plus);
95  dig_cidx_init(Plus);
96 
97  return 1;
98 }
99 
105 void dig_free_plus_nodes(struct Plus_head *Plus)
106 {
107  int i;
108  P_NODE *Node;
109 
110  G_debug(2, "dig_free_plus_nodes()");
111 
112  /* Nodes */
113  if (Plus->Node) { /* it may be that header only is loaded */
114  for (i = 1; i <= Plus->n_nodes; i++) {
115  Node = Plus->Node[i];
116  if (Node == NULL)
117  continue;
118 
119  if (Node->alloc_lines > 0) {
120  G_free(Node->lines);
121  G_free(Node->angles);
122  }
123  G_free(Node);
124  }
125  G_free(Plus->Node);
126  }
127  Plus->Node = NULL;
128  Plus->n_nodes = 0;
129  Plus->alloc_nodes = 0;
130 }
131 
137 void dig_free_plus_lines(struct Plus_head *Plus)
138 {
139  int i;
140  P_LINE *Line;
141 
142  G_debug(2, "dig_free_plus_lines()");
143 
144  /* Lines */
145  if (Plus->Line) { /* it may be that header only is loaded */
146  for (i = 1; i <= Plus->n_lines; i++) {
147  Line = Plus->Line[i];
148  if (Line == NULL)
149  continue;
150 
151  G_free(Line);
152  }
153  G_free(Plus->Line);
154  }
155 
156  Plus->Line = NULL;
157  Plus->n_lines = 0;
158  Plus->alloc_lines = 0;
159 
160  Plus->n_plines = 0;
161  Plus->n_llines = 0;
162  Plus->n_blines = 0;
163  Plus->n_clines = 0;
164  Plus->n_flines = 0;
165  Plus->n_klines = 0;
166 }
167 
173 void dig_free_plus_areas(struct Plus_head *Plus)
174 {
175  int i;
176  P_AREA *Area;
177 
178  G_debug(2, "dig_free_plus_areas()");
179 
180  /* Areas */
181  if (Plus->Area) { /* it may be that header only is loaded */
182  for (i = 1; i <= Plus->n_areas; i++) {
183  Area = Plus->Area[i];
184  if (Area == NULL)
185  continue;
186 
187  if (Area->alloc_lines > 0)
188  G_free(Area->lines);
189 
190  if (Area->alloc_isles > 0)
191  G_free(Area->isles);
192 
193  G_free(Area);
194  }
195  G_free(Plus->Area);
196  }
197  Plus->Area = NULL;
198  Plus->n_areas = 0;
199  Plus->alloc_areas = 0;
200 }
201 
207 void dig_free_plus_isles(struct Plus_head *Plus)
208 {
209  int i;
210  P_ISLE *Isle;
211 
212  G_debug(2, "dig_free_plus_isles()");
213 
214  /* Isles */
215  if (Plus->Isle) { /* it may be that header only is loaded */
216  for (i = 1; i <= Plus->n_isles; i++) {
217  Isle = Plus->Isle[i];
218  if (Isle == NULL)
219  continue;
220 
221  if (Isle->alloc_lines > 0)
222  G_free(Isle->lines);
223 
224  G_free(Isle);
225  }
226  G_free(Plus->Isle);
227  }
228 
229  Plus->Isle = NULL;
230  Plus->n_isles = 0;
231  Plus->alloc_isles = 0;
232 }
233 
241 void dig_free_plus(struct Plus_head *Plus)
242 {
243  G_debug(2, "dig_free_plus()");
244  dig_free_plus_nodes(Plus);
245  dig_free_plus_lines(Plus);
246  dig_free_plus_areas(Plus);
247  dig_free_plus_isles(Plus);
248 
249  dig_cidx_free(Plus);
250 }
251 
262 int dig_load_plus(struct Plus_head *Plus, GVFILE * plus, int head_only)
263 {
264  int i;
265 
266 
267  G_debug(1, "dig_load_plus()");
268  /* TODO
269  if (do_checks)
270  dig_do_file_checks (map, map->plus_file, map->digit_file);
271  */
272 
273  /* free and init old */
274  dig_init_plus(Plus);
275 
276  /* Now let's begin reading the Plus file nodes, lines, areas and isles */
277 
278  if (dig_Rd_Plus_head(plus, Plus) == -1)
279  return 0;
280 
281  if (head_only)
282  return 1;
283 
284  dig_set_cur_port(&(Plus->port));
285 
286  /* Nodes */
287  if (dig_fseek(plus, Plus->Node_offset, 0) == -1)
288  G_fatal_error(_("Unable read topology for nodes"));
289 
290  dig_alloc_nodes(Plus, Plus->n_nodes);
291  for (i = 1; i <= Plus->n_nodes; i++) {
292  if (dig_Rd_P_node(Plus, i, plus) == -1)
293  G_fatal_error(_("Unable to read topology for node %d"), i);
294  }
295 
296  /* Lines */
297  if (dig_fseek(plus, Plus->Line_offset, 0) == -1)
298  G_fatal_error(_("Unable read topology for lines"));
299 
300  dig_alloc_lines(Plus, Plus->n_lines);
301  for (i = 1; i <= Plus->n_lines; i++) {
302  if (dig_Rd_P_line(Plus, i, plus) == -1)
303  G_fatal_error(_("Unable to read topology for line %d"), i);
304  }
305 
306  /* Areas */
307  if (dig_fseek(plus, Plus->Area_offset, 0) == -1)
308  G_fatal_error(_("Unable to read topo for areas"));
309 
310  dig_alloc_areas(Plus, Plus->n_areas);
311  for (i = 1; i <= Plus->n_areas; i++) {
312  if (dig_Rd_P_area(Plus, i, plus) == -1)
313  G_fatal_error(_("Unable read topology for area %d"), i);
314  }
315 
316  /* Isles */
317  if (dig_fseek(plus, Plus->Isle_offset, 0) == -1)
318  G_fatal_error(_("Unable to read topology for isles"));
319 
320  dig_alloc_isles(Plus, Plus->n_isles);
321  for (i = 1; i <= Plus->n_isles; i++) {
322  if (dig_Rd_P_isle(Plus, i, plus) == -1)
323  G_fatal_error(_("Unable to read topology for isle %d"), i);
324  }
325 
326  return (1);
327 }
328 
338 int dig_write_plus_file(GVFILE * fp_plus, struct Plus_head *Plus)
339 {
340 
341  dig_set_cur_port(&(Plus->port));
342  dig_rewind(fp_plus);
343 
344  if (dig_Wr_Plus_head(fp_plus, Plus) < 0) {
345  G_warning(_("Unable to write head to plus file"));
346  return (-1);
347  }
348 
349  if (dig_write_nodes(fp_plus, Plus) < 0) {
350  G_warning(_("Unable to write nodes to plus file"));
351  return (-1);
352  }
353 
354  if (dig_write_lines(fp_plus, Plus) < 0) {
355  G_warning(_("Unable to write lines to plus file"));
356  return (-1);
357  }
358 
359  if (dig_write_areas(fp_plus, Plus) < 0) {
360  G_warning(_("Unable to write areas to plus file"));
361  return (-1);
362  }
363 
364  if (dig_write_isles(fp_plus, Plus) < 0) {
365  G_warning(_("Unable to write isles to plus file"));
366  return (-1);
367  }
368 
369  dig_rewind(fp_plus);
370  if (dig_Wr_Plus_head(fp_plus, Plus) < 0) {
371  G_warning(_("Unable to write head to plus file"));
372  return (-1);
373  }
374 
375  dig_fflush(fp_plus);
376  return (0);
377 } /* write_plus_file() */
378 
388 int dig_write_nodes(GVFILE * plus, struct Plus_head *Plus)
389 {
390  int i;
391 
392 
393  Plus->Node_offset = dig_ftell(plus);
394 
395  for (i = 1; i <= Plus->n_nodes; i++) {
396  if (dig_Wr_P_node(Plus, i, plus) < 0)
397  return (-1);
398  }
399 
400  return (0);
401 } /* write_nodes() */
402 
412 int dig_write_lines(GVFILE * plus, struct Plus_head *Plus)
413 {
414  int i;
415 
416 
417  Plus->Line_offset = dig_ftell(plus);
418 
419  for (i = 1; i <= Plus->n_lines; i++) {
420  if (dig_Wr_P_line(Plus, i, plus) < 0)
421  return (-1);
422  }
423 
424  return (0);
425 
426 } /* write_line() */
427 
437 int dig_write_areas(GVFILE * plus, struct Plus_head *Plus)
438 {
439  int i;
440 
441 
442  Plus->Area_offset = dig_ftell(plus);
443 
444  for (i = 1; i <= Plus->n_areas; i++) {
445  if (dig_Wr_P_area(Plus, i, plus) < 0)
446  return (-1);
447  }
448 
449  return (0);
450 
451 } /* write_areas() */
452 
462 int dig_write_isles(GVFILE * plus, struct Plus_head *Plus)
463 {
464  int i;
465 
466 
467  Plus->Isle_offset = dig_ftell(plus);
468 
469  for (i = 1; i <= Plus->n_isles; i++) {
470  if (dig_Wr_P_isle(Plus, i, plus) < 0)
471  return (-1);
472  }
473 
474  return (0);
475 
476 } /* write_isles() */
int dig_alloc_nodes(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:93
void dig_free_plus_lines(struct Plus_head *Plus)
Free Plus-&gt;Line structure.
Definition: plus.c:137
int dig_init_plus(struct Plus_head *Plus)
Init head structure.
Definition: plus.c:31
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int dig_Wr_Plus_head(GVFILE *fp, struct Plus_head *ptr)
Definition: plus_struct.c:698
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
int dig_spidx_init(struct Plus_head *Plus)
Initit spatial index (nodes, lines, areas, isles)
Definition: spindex.c:31
Definition: index.h:56
int dig_alloc_areas(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:149
int dig_write_plus_file(GVFILE *fp_plus, struct Plus_head *Plus)
Writes topo structure to topo file.
Definition: plus.c:338
int dig_Rd_P_area(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:336
int dig_Wr_P_line(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:251
int dig_Rd_P_node(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:50
void dig_free_plus_areas(struct Plus_head *Plus)
Free Plus-&gt;Area structure.
Definition: plus.c:173
int dig_write_areas(GVFILE *plus, struct Plus_head *Plus)
Writes topo structure (areas) to topo file.
Definition: plus.c:437
int dig_Wr_P_isle(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:517
int dig_Rd_Plus_head(GVFILE *fp, struct Plus_head *ptr)
Definition: plus_struct.c:567
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
Definition: file.c:60
int dig_Rd_P_isle(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:459
int dig_alloc_isles(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:171
void dig_cidx_free(struct Plus_head *Plus)
Definition: diglib/cindex.c:44
void dig_free_plus_isles(struct Plus_head *Plus)
Free Plus-&gt;Isle structure.
Definition: plus.c:207
void dig_free_plus(struct Plus_head *Plus)
Free Plus structure.
Definition: plus.c:241
int dig_write_nodes(GVFILE *plus, struct Plus_head *Plus)
Writes topo structure (nodes) to topo file.
Definition: plus.c:388
int dig_cidx_init(struct Plus_head *Plus)
Definition: diglib/cindex.c:29
return NULL
Definition: dbfopen.c:1394
int dig_fflush(GVFILE *file)
Flush GVFILE.
Definition: file.c:102
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int dig_write_isles(GVFILE *plus, struct Plus_head *Plus)
Writes topo structure (isles) to topo file.
Definition: plus.c:462
int dig_Rd_P_line(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:149
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void dig_rewind(GVFILE *file)
Rewind GVFILE position.
Definition: file.c:85
int dig_alloc_lines(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:127
int dig_write_lines(GVFILE *plus, struct Plus_head *Plus)
Writes topo structure (lines) to topo file.
Definition: plus.c:412
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_Wr_P_node(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:104
int dig_Wr_P_area(struct Plus_head *Plus, int n, GVFILE *fp)
Definition: plus_struct.c:405
void dig_free_plus_nodes(struct Plus_head *Plus)
Free Plus-&gt;Node structure.
Definition: plus.c:105
int dig_load_plus(struct Plus_head *Plus, GVFILE *plus, int head_only)
Reads topo file to topo structure.
Definition: plus.c:262