GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
build.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <grass/glocale.h>
24 #include <grass/gis.h>
25 #include <grass/Vect.h>
26 
27 
28 #ifndef HAVE_OGR
29 static int format()
30 {
31  G_fatal_error(_("Requested format is not compiled in this version"));
32  return 0;
33 }
34 #endif
35 
36 static int (*Build_array[]) () = {
38 #ifdef HAVE_OGR
40 #else
41  , format
42 #endif
43 };
44 
53 int Vect_build(struct Map_info *Map)
54 {
55  return Vect_build_partial(Map, GV_BUILD_ALL);
56 }
57 
58 
66 int Vect_get_built(struct Map_info *Map)
67 {
68  return (Map->plus.built);
69 }
70 
107 int Vect_build_partial(struct Map_info *Map, int build)
108 {
109  struct Plus_head *plus;
110  int ret;
111 
112  G_debug(3, "Vect_build(): build = %d", build);
113 
114  /* If topology is already build (map on level2), set level to 1 so that lines will
115  * be read by V1_read_ (all lines) */
116  Map->level = 1; /* may be not needed, because V1_read is used directly by Vect_build_ */
117  Map->support_updated = 1;
118 
119  Map->plus.Spidx_built = 1;
120 
121  plus = &(Map->plus);
122  if (build > GV_BUILD_NONE) {
123  G_message(_("Building topology for vector map <%s>..."),
124  Vect_get_name(Map));
125  }
126  plus->with_z = Map->head.with_z;
127  plus->spidx_with_z = Map->head.with_z;
128 
129  if (build == GV_BUILD_ALL) {
130  dig_cidx_free(plus); /* free old (if any) category index) */
131  dig_cidx_init(plus);
132  }
133 
134  ret = ((*Build_array[Map->format]) (Map, build));
135 
136  if (ret == 0) {
137  return 0;
138  }
139 
140  if (build > GV_BUILD_NONE) {
141  G_verbose_message(_("Topology was built"));
142  }
143 
144  Map->level = LEVEL_2;
145  plus->mode = GV_MODE_WRITE;
146 
147  if (build == GV_BUILD_ALL) {
148  plus->cidx_up_to_date = 1; /* category index was build */
149  dig_cidx_sort(plus);
150  }
151 
152  if (build > GV_BUILD_NONE) {
153  G_message(_("Number of nodes: %d"), plus->n_nodes);
154  G_message(_("Number of primitives: %d"), plus->n_lines);
155  G_message(_("Number of points: %d"), plus->n_plines);
156  G_message(_("Number of lines: %d"), plus->n_llines);
157  G_message(_("Number of boundaries: %d"), plus->n_blines);
158  G_message(_("Number of centroids: %d"), plus->n_clines);
159 
160  if (plus->n_flines > 0)
161  G_message(_("Number of faces: %d"), plus->n_flines);
162 
163  if (plus->n_klines > 0)
164  G_message(_("Number of kernels: %d"), plus->n_klines);
165  }
166 
167  if (plus->built >= GV_BUILD_AREAS) {
168  int line, nlines, area, nareas, err_boundaries, err_centr_out,
169  err_centr_dupl, err_nocentr;
170  P_LINE *Line;
171  struct Plus_head *Plus;
172 
173  /* Count errors (it does not take much time comparing to build process) */
174  Plus = &(Map->plus);
175  nlines = Vect_get_num_lines(Map);
176  err_boundaries = err_centr_out = err_centr_dupl = 0;
177  for (line = 1; line <= nlines; line++) {
178  Line = Plus->Line[line];
179  if (!Line)
180  continue;
181  if (Line->type == GV_BOUNDARY &&
182  (Line->left == 0 || Line->right == 0)) {
183  G_debug(3, "line = %d left = %d right = %d", line, Line->left,
184  Line->right);
185  err_boundaries++;
186  }
187  if (Line->type == GV_CENTROID) {
188  if (Line->left == 0)
189  err_centr_out++;
190  else if (Line->left < 0)
191  err_centr_dupl++;
192  }
193  }
194 
195  err_nocentr = 0;
196  nareas = Vect_get_num_areas(Map);
197  for (area = 1; area <= nareas; area++) {
198  if (!Vect_area_alive(Map, area))
199  continue;
200  line = Vect_get_area_centroid(Map, area);
201  if (line == 0)
202  err_nocentr++;
203  }
204 
205  G_message(_("Number of areas: %d"), plus->n_areas);
206  G_message(_("Number of isles: %d"), plus->n_isles);
207 
208  if (err_boundaries)
209  G_message(_("Number of incorrect boundaries: %d"),
210  err_boundaries);
211 
212  if (err_centr_out)
213  G_message(_("Number of centroids outside area: %d"),
214  err_centr_out);
215 
216  if (err_centr_dupl)
217  G_message(_("Number of duplicate centroids: %d"),
218  err_centr_dupl);
219 
220  if (err_nocentr)
221  G_message(_("Number of areas without centroid: %d"),
222  err_nocentr);
223 
224  }
225  else if (build > GV_BUILD_NONE) {
226  G_message(_("Number of areas: -"));
227  G_message(_("Number of isles: -"));
228  }
229  return 1;
230 }
231 
239 int Vect_save_topo(struct Map_info *Map)
240 {
241  struct Plus_head *plus;
242  char fname[GPATH_MAX], buf[GPATH_MAX];
243  GVFILE fp;
244 
245  G_debug(1, "Vect_save_topo()");
246 
247  plus = &(Map->plus);
248 
249  /* write out all the accumulated info to the plus file */
250  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
251  G__file_name(fname, buf, GV_TOPO_ELEMENT, Map->mapset);
252  G_debug(1, "Open topo: %s", fname);
253  dig_file_init(&fp);
254  fp.file = fopen(fname, "w");
255  if (fp.file == NULL) {
256  G_warning(_("Unable to open topo file for write <%s>"), fname);
257  return 0;
258  }
259 
260  /* set portable info */
261  dig_init_portable(&(plus->port), dig__byte_order_out());
262 
263  if (0 > dig_write_plus_file(&fp, plus)) {
264  G_warning(_("Error writing out topo file"));
265  return 0;
266  }
267 
268  fclose(fp.file);
269 
270  return 1;
271 }
272 
282 int Vect_topo_dump(struct Map_info *Map, FILE * out)
283 {
284  int i, j, line, isle;
285  P_NODE *Node;
286  P_LINE *Line;
287  P_AREA *Area;
288  P_ISLE *Isle;
289  BOUND_BOX box;
290  struct Plus_head *plus;
291 
292  plus = &(Map->plus);
293 
294  fprintf(out, "---------- TOPOLOGY DUMP ----------\n");
295 
296  /* box */
297  Vect_box_copy(&box, &(plus->box));
298  fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", box.N, box.S,
299  box.E, box.W, box.T, box.B);
300 
301  /* nodes */
302  fprintf(out, "Nodes (%d nodes, alive + dead ):\n", plus->n_nodes);
303  for (i = 1; i <= plus->n_nodes; i++) {
304  if (plus->Node[i] == NULL) {
305  continue;
306  }
307  Node = plus->Node[i];
308  fprintf(out, "node = %d, n_lines = %d, xyz = %f, %f, %f\n", i,
309  Node->n_lines, Node->x, Node->y, Node->z);
310  for (j = 0; j < Node->n_lines; j++) {
311  line = Node->lines[j];
312  Line = plus->Line[abs(line)];
313  fprintf(out, " line = %3d, type = %d, angle = %f\n", line,
314  Line->type, Node->angles[j]);
315  }
316  }
317 
318  /* lines */
319  fprintf(out, "Lines (%d lines, alive + dead ):\n", plus->n_lines);
320  for (i = 1; i <= plus->n_lines; i++) {
321  if (plus->Line[i] == NULL) {
322  continue;
323  }
324  Line = plus->Line[i];
325  fprintf(out, "line = %d, type = %d, offset = %ld n1 = %d, n2 = %d, "
326  "left/area = %d, right = %d\n",
327  i, Line->type, Line->offset, Line->N1, Line->N2,
328  Line->left, Line->right);
329  fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Line->N,
330  Line->S, Line->E, Line->W, Line->T, Line->B);
331  }
332 
333  /* areas */
334  fprintf(out, "Areas (%d areas, alive + dead ):\n", plus->n_areas);
335  for (i = 1; i <= plus->n_areas; i++) {
336  if (plus->Area[i] == NULL) {
337  continue;
338  }
339  Area = plus->Area[i];
340 
341  fprintf(out, "area = %d, n_lines = %d, n_isles = %d centroid = %d\n",
342  i, Area->n_lines, Area->n_isles, Area->centroid);
343 
344  fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Area->N,
345  Area->S, Area->E, Area->W, Area->T, Area->B);
346 
347  for (j = 0; j < Area->n_lines; j++) {
348  line = Area->lines[j];
349  Line = plus->Line[abs(line)];
350  fprintf(out, " line = %3d\n", line);
351  }
352  for (j = 0; j < Area->n_isles; j++) {
353  isle = Area->isles[j];
354  fprintf(out, " isle = %3d\n", isle);
355  }
356  }
357 
358  /* isles */
359  fprintf(out, "Islands (%d islands, alive + dead ):\n", plus->n_isles);
360  for (i = 1; i <= plus->n_isles; i++) {
361  if (plus->Isle[i] == NULL) {
362  continue;
363  }
364  Isle = plus->Isle[i];
365 
366  fprintf(out, "isle = %d, n_lines = %d area = %d\n", i, Isle->n_lines,
367  Isle->area);
368 
369  fprintf(out, "N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n", Isle->N,
370  Isle->S, Isle->E, Isle->W, Isle->T, Isle->B);
371 
372  for (j = 0; j < Isle->n_lines; j++) {
373  line = Isle->lines[j];
374  Line = plus->Line[abs(line)];
375  fprintf(out, " line = %3d\n", line);
376  }
377  }
378 
379  return 1;
380 }
381 
390 int Vect_save_spatial_index(struct Map_info *Map)
391 {
392  struct Plus_head *plus;
393  char fname[GPATH_MAX], buf[GPATH_MAX];
394  GVFILE fp;
395 
396  G_debug(1, "Vect_save_spatial_index()");
397 
398  plus = &(Map->plus);
399 
400  /* write out rtrees to the sidx file */
401  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
402  G__file_name(fname, buf, GV_SIDX_ELEMENT, Map->mapset);
403  G_debug(1, "Open sidx: %s", fname);
404  dig_file_init(&fp);
405  fp.file = fopen(fname, "w");
406  if (fp.file == NULL) {
407  G_warning(_("Unable open spatial index file for write <%s>"), fname);
408  return 0;
409  }
410 
411  /* set portable info */
412  dig_init_portable(&(plus->spidx_port), dig__byte_order_out());
413 
414  if (0 > dig_write_spidx(&fp, plus)) {
415  G_warning(_("Error writing out spatial index file"));
416  return 0;
417  }
418 
419  fclose(fp.file);
420 
421  return 1;
422 }
423 
433 int Vect_spatial_index_dump(struct Map_info *Map, FILE * out)
434 {
435  if (!(Map->plus.Spidx_built)) {
437  }
438 
439  fprintf(out, "---------- SPATIAL INDEX DUMP ----------\n");
440 
441  dig_dump_spidx(out, &(Map->plus));
442 
443  return 1;
444 }
int Vect_build_nat(struct Map_info *Map, int build)
Build topology.
Definition: build_nat.c:428
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int Vect_get_area_centroid(struct Map_info *Map, int area)
Returns centroid number of area.
int dig_dump_spidx(FILE *fp, struct Plus_head *Plus)
Definition: spindex_rw.c:386
const char * Vect_get_name(struct Map_info *Map)
Get map name.
int Vect_save_spatial_index(struct Map_info *Map)
Save spatial index file.
Definition: build.c:390
int Vect_build_sidx_from_topo(struct Map_info *Map)
Create spatial index from topo if necessary.
Definition: sindex.c:144
int Vect_build_ogr(struct Map_info *Map, int build)
Build topology.
Definition: build_ogr.c:325
Definition: index.h:56
int Vect_save_topo(struct Map_info *Map)
Save topology file for vector map.
Definition: build.c:239
int Vect_get_num_areas(struct Map_info *map)
Get number of areas in vector map.
Definition: level_two.c:81
int dig_write_plus_file(GVFILE *fp_plus, struct Plus_head *Plus)
Writes topo structure to topo file.
Definition: plus.c:338
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
tuple box
surface = wx.CheckBox(parent = panel, id = wx.ID_ANY, label = _(&quot;Follow source viewpoint&quot;)) pageSizer...
Definition: tools.py:1527
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition: lib/gis/error.c:95
int Vect_build(struct Map_info *Map)
Build topology for vector map.
Definition: build.c:53
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
int Vect_topo_dump(struct Map_info *Map, FILE *out)
Dump topology to file.
Definition: build.c:282
void dig_cidx_sort(struct Plus_head *Plus)
int Vect_box_copy(BOUND_BOX *A, BOUND_BOX *B)
Copy box B to box A.
Definition: Vlib/box.c:72
int
Definition: g3dcolor.c:48
void dig_cidx_free(struct Plus_head *Plus)
Definition: diglib/cindex.c:44
int dig_write_spidx(GVFILE *fp, struct Plus_head *Plus)
Definition: spindex_rw.c:333
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int dig_cidx_init(struct Plus_head *Plus)
Definition: diglib/cindex.c:29
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int Vect_get_num_lines(struct Map_info *map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
Definition: level_two.c:69
int Vect_get_built(struct Map_info *Map)
Return current highest built level (part)
Definition: build.c:66
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void dig_file_init(GVFILE *file)
Initialize GVFILE.
Definition: file.c:168
fclose(fd)
int Vect_spatial_index_dump(struct Map_info *Map, FILE *out)
Dump spatial index to file.
Definition: build.c:433
int dig__byte_order_out()
Definition: portable.c:646
int Vect_area_alive(struct Map_info *Map, int area)
Check if area is alive or dead.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_build_partial(struct Map_info *Map, int build)
Build partial topology for vector map.
Definition: build.c:107