source: grass/trunk/lib/vector/Vlib/open.c@ 39545

Last change on this file since 39545 was 39537, checked in by martinl, 15 years ago

don't build topology for OGR layer on requested level 1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-csrc
File size: 25.3 KB
Line 
1/*!
2 * \file vector/Vlib/open.c
3 *
4 * \brief Vector library - Open vector map (native or OGR format)
5 *
6 * Higher level functions for reading/writing/manipulating vectors.
7 *
8 * (C) 2001-2009 by the GRASS Development Team
9 *
10 * This program is free software under the GNU General Public License
11 * (>=v2). Read the file COPYING that comes with GRASS for details.
12 *
13 * \author Original author CERL, probably Dave Gerdes or Mike
14 * Higgins.
15 * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
16 */
17
18#include <grass/config.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#include <grass/gis.h>
27#include <grass/vector.h>
28#include <grass/glocale.h>
29
30/*
31 \brief Number of available levels
32*/
33#define MAX_OPEN_LEVEL 2
34
35static int open_old_dummy()
36{
37 return 0;
38}
39
40#ifndef HAVE_OGR
41static int format()
42{
43 G_fatal_error(_("Requested format is not compiled in this version"));
44 return 0;
45}
46#endif
47
48static int Open_level = 0;
49
50static int (*Open_old_array[][2]) () = {
51 {
52 open_old_dummy, V1_open_old_nat}
53#ifdef HAVE_OGR
54 , {
55 open_old_dummy, V1_open_old_ogr}
56#else
57 , {
58 open_old_dummy, format}
59#endif
60};
61
62static void fatal_error(int ferror, char *errmsg)
63{
64 switch (ferror) {
65 case GV_FATAL_EXIT:
66 G_fatal_error(errmsg);
67 break;
68 case GV_FATAL_PRINT:
69 G_warning(errmsg);
70 break;
71 case GV_FATAL_RETURN:
72 break;
73 }
74}
75
76/*!
77 * \brief Predetermine level at which a vector map will be opened for
78 * reading.
79 *
80 * If it can't open that level, the open will fail. The specified
81 * level must be set before any call to open. The default is to try to
82 * open the highest level possible, and keep stepping down until
83 * success.
84 *
85 * NOTE: This should only be used to set when you wish to force a
86 * lower level open. If you require a higher level, then just check
87 * the return to verify the level instead of forcing it. This is
88 * because future releases will have higher levels which will be
89 * downward compatible and which your programs should support by
90 * default.
91 *
92 * \param level vector access level
93 *
94 * \return 0 on success
95 * \return 1 on error
96 */
97
98int Vect_set_open_level(int level)
99{
100 Open_level = level;
101 if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
102 G_warning(_("Programmer requested unknown access level %d"),
103 Open_level);
104 Open_level = 0;
105 return 1;
106 }
107
108 return 0;
109}
110
111/*!
112 * \brief Open existing vector map for reading (internal use only)
113 *
114 * In case of error, the functions respect fatal error settings.
115 *
116 * \param[out] Map pointer to Map_info structure
117 * \param name name of vector map to open
118 * \param mapset mapset name ("" for search path)
119 * \param update non-zero to open for update otherwise read-only mode
120 * \param head_only read only header info from 'head', 'dbln', 'topo',
121 * 'cidx' is not opened. The header may be opened on level 2 only.
122 *
123 * \return level of openness (1, 2)
124 * \return -1 in error
125 */
126int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, const char *layer,
127 int update, int head_only)
128{
129 char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
130 xmapset[GMAPSET_MAX], errmsg[2000];
131 FILE *fp;
132 int level, level_request, ferror;
133 int format, ret;
134 int ogr_mapset;
135 const char *fmapset;
136
137 G_debug(1, "Vect__open_old(): name = %s mapset= %s update = %d", name,
138 mapset, update);
139
140 /* zero Map_info structure */
141 G_zero(Map, sizeof(struct Map_info));
142
143 /* TODO: Open header for update ('dbln') */
144
145 ferror = Vect_get_fatal_error();
146 Vect_set_fatal_error(GV_FATAL_EXIT);
147
148 level_request = Open_level;
149 Open_level = 0;
150
151 /* initialize Map->head */
152 Vect__init_head(Map);
153 /* initialize support structures for 2D, update to 3D when reading support files */
154 Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = 0;
155 /* initialize Map->plus */
156 dig_init_plus(&(Map->plus));
157
158 /* check OGR mapset */
159 ogr_mapset = 0;
160 if (G_name_is_fully_qualified(name, xname, xmapset)) {
161 if (strcasecmp(xmapset, "ogr") == 0) {
162 /* unique OGR mapset detected */
163 G_debug(1, "OGR mapset detected");
164 ogr_mapset = 1;
165 Map->fInfo.ogr.dsn = G_store(xname);
166 if (layer) {
167 Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
168 }
169 }
170 else {
171 sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
172 sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
173 }
174 Map->name = G_store(xname);
175 Map->mapset = G_store(xmapset);
176 }
177 else {
178 sprintf(buf, "%s/%s", GV_DIRECTORY, name);
179 sprintf(buf2, "%s", GV_COOR_ELEMENT);
180 Map->name = G_store(name);
181
182 if (mapset)
183 Map->mapset = G_store(mapset);
184 else
185 Map->mapset = G_store("");
186 }
187
188 if (!ogr_mapset) {
189 /* try to find vector map (not for OGR mapset) */
190 fmapset = G_find_vector2(Map->name, Map->mapset);
191 if (fmapset == NULL) {
192 sprintf(errmsg, _("Vector map <%s> not found"),
193 Vect_get_full_name(Map));
194 fatal_error(ferror, errmsg);
195 return -1;
196 }
197 Map->mapset = G_store(fmapset);
198 }
199 else { /* OGR mapset */
200 if (update) {
201 sprintf(errmsg, _("OGR layer cannot be opened for update"));
202 fatal_error(ferror, errmsg);
203 return -1;
204 }
205 }
206 Map->location = G_store(G_location());
207 Map->gisdbase = G_store(G_gisdbase());
208
209 if (update && (0 != strcmp(Map->mapset, G_mapset()))) {
210 G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
211 return -1;
212 }
213
214 G_debug(1, "Map name: %s", Map->name);
215 G_debug(1, "Map mapset: %s", Map->mapset);
216
217 /* Read vector format information */
218 if (ogr_mapset) {
219 format = GV_FORMAT_OGR;
220 }
221 else {
222 format = 0;
223 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
224 G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
225 GV_FRMT_ELEMENT);
226 fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
227 if (fp == NULL) {
228 G_debug(1, "Vector format: %d (native)", format);
229 format = GV_FORMAT_NATIVE;
230 }
231 else {
232 format = dig_read_frmt_ascii(fp, &(Map->fInfo));
233 fclose(fp);
234
235 G_debug(1, "Vector format: %d (non-native)", format);
236 if (format < 0) {
237 sprintf(errmsg, _("Unable to open vector map <%s>"),
238 Vect_get_full_name(Map));
239 fatal_error(ferror, errmsg);
240 return -1;
241 }
242 }
243 }
244 Map->format = format;
245
246 /* projection info */
247 Vect_set_proj(Map, G_projection());
248 Vect_set_zone(Map, G_zone());
249
250 /* read vector head (ignored for OGR mapset) */
251 if (!ogr_mapset && Vect__read_head(Map) != 0) {
252 sprintf(errmsg,
253 _("Unable to open vector map <%s> on level %d. "
254 "Try to rebuild vector topology by v.build."),
255 Vect_get_full_name(Map), level_request);
256 G_warning(_("Unable to read header file of vector map <%s>"),
257 Vect_get_full_name(Map));
258 }
259
260 /* zone not set */
261 if (Vect_get_zone(Map) == -1)
262 Vect_set_zone(Map, G_zone());
263
264 G_debug(1, "Level request = %d", level_request);
265
266 /* There are only 2 possible open levels, 1 and 2. Try first to
267 * open 'support' files (topo,sidx,cidx), these files are the same
268 * for all formats. If it is not possible and requested level is
269 * 2, return error, otherwise call Open_old_array[format][1], to
270 * open remaining files/sources (level 1)
271 */
272
273 /* Try to open support files if level was not requested or
274 * requested level is 2 (format independent) */
275 if (level_request == 0 || level_request == 2) {
276 level = 2; /* we expect success */
277 /* open topo */
278 ret = Vect_open_topo(Map, head_only);
279 if (ret == 1) { /* topo file is not available */
280 G_debug(1, "topo file for vector '%s' not available.",
281 Vect_get_full_name(Map));
282 level = 1;
283 }
284 else if (ret == -1) {
285 G_fatal_error(_("Unable to open topology file for vector map <%s>"),
286 Vect_get_full_name(Map));
287 }
288 /* open spatial index */
289 if (level == 2) {
290 ret = Vect_open_sidx(Map, (update != 0));
291 if (ret == 1) { /* sidx file is not available */
292 G_debug(1, "sidx file for vector '%s' not available.",
293 Vect_get_full_name(Map));
294 level = 1;
295 }
296 else if (ret == -1) {
297 G_fatal_error(_("Unable to open spatial index file for vector map <%s>"),
298 Vect_get_full_name(Map));
299 }
300 /* check with_z consistency */
301 if ((Map->plus.with_z != 0 && Map->plus.spidx_with_z == 0) ||
302 (Map->plus.with_z == 0 && Map->plus.spidx_with_z != 0)) {
303 G_warning("Vector map <%s>: topology is %s, but spatial index is %s",
304 Vect_get_full_name(Map), (Map->plus.with_z != 0 ? "3D" : "2D"),
305 (Map->plus.spidx_with_z != 0 ? "3D" : "2D"));
306 level = 1;
307 }
308 }
309 /* open category index */
310 if (level == 2) {
311 ret = Vect_cidx_open(Map, head_only);
312 if (ret == 1) { /* category index is not available */
313 G_debug(1,
314 "cidx file for vector '%s' not available.",
315 Vect_get_full_name(Map));
316 dig_free_plus(&(Map->plus)); /* free topology */
317 dig_spidx_free(&(Map->plus)); /* free spatial index */
318 level = 1;
319 }
320 else if (ret == -1) { /* file exists, but cannot be opened */
321 G_fatal_error(_("Unable to open category index file for vector map <%s>"),
322 Vect_get_full_name(Map));
323 }
324 }
325#ifdef HAVE_OGR
326 /* Open OGR specific support files */
327 if (level == 2 && Map->format == GV_FORMAT_OGR) {
328 if (V2_open_old_ogr(Map) < 0) {
329 dig_free_plus(&(Map->plus));
330 dig_spidx_free(&(Map->plus));
331 dig_cidx_free(&(Map->plus));
332 level = 1;
333 }
334 }
335#endif
336 if (level_request == 2 && level < 2) {
337 if (!ogr_mapset) {
338 /* for direct OGR read access is built pseudo-topology on the fly */
339 sprintf(errmsg,
340 _("Unable to open vector map <%s> on level %d. "
341 "Try to rebuild vector topology by v.build."),
342 Vect_get_full_name(Map), level_request);
343 fatal_error(ferror, errmsg);
344 return -1;
345 }
346 }
347 }
348 else {
349 level = 1; /* i.e. requested level is 1 */
350 }
351
352 /* open level 1 files / sources (format specific) */
353 if (!head_only || ogr_mapset) { /* no need to open coordinates */
354 if (0 != (*Open_old_array[format][1]) (Map, update)) { /* cannot open */
355 if (level == 2) { /* support files opened */
356 dig_free_plus(&(Map->plus));
357 dig_spidx_free(&(Map->plus));
358 dig_cidx_free(&(Map->plus));
359 }
360 sprintf(errmsg,
361 _("Unable to open vector map <%s> on level %d. "
362 "Try to rebuild vector topology by v.build."),
363 Vect_get_full_name(Map), level_request);
364 fatal_error(ferror, errmsg);
365 return -1;
366 }
367 if (ogr_mapset && !head_only && level_request != 1) {
368 /* build pseudo-topology on the fly */
369 int verbose;
370 verbose = G_verbose();
371 G_message(_("Building topology for OGR layer <%s> from datasource '%s'..."),
372 Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
373 G_set_verbose(0);
374 Vect_build(Map);
375 level = 2;
376 G_set_verbose(verbose);
377 }
378 }
379 else {
380 Map->head.with_z = Map->plus.with_z; /* take dimension from topo */
381 }
382
383 /* set status */
384 Map->open = VECT_OPEN_CODE;
385 Map->level = level;
386 Map->head_only = head_only;
387 Map->support_updated = 0;
388 if (update) {
389 Map->mode = GV_MODE_RW;
390 Map->plus.mode = GV_MODE_RW;
391 }
392 else {
393 Map->mode = GV_MODE_READ;
394 Map->plus.mode = GV_MODE_READ;
395 }
396 if (head_only) {
397 Map->head_only = 1;
398 }
399 else {
400 Map->head_only = 0;
401 }
402
403 Map->Constraint_region_flag = 0;
404 Map->Constraint_type_flag = 0;
405 G_debug(1, "Vect_open_old(): vector opened on level %d", level);
406
407 if (level == 1) { /* without topology */
408 Map->plus.built = GV_BUILD_NONE;
409 }
410 else { /* level 2, with topology */
411 Map->plus.built = GV_BUILD_ALL; /* highest level of topology for level 2 */
412 }
413
414 Map->plus.do_uplist = 0;
415
416 /* read db links */
417 Map->dblnk = Vect_new_dblinks_struct();
418 Vect_read_dblinks(Map);
419
420 /* open history file */
421 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
422 if (update) { /* native only */
423 Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
424 if (Map->hist_fp == NULL) {
425 sprintf(errmsg,
426 _("Unable to open history file for vector map <%s>"),
427 Vect_get_full_name(Map));
428 fatal_error(ferror, errmsg);
429 return (-1);
430 }
431 G_fseek(Map->hist_fp, (off_t) 0, SEEK_END);
432 Vect_hist_write(Map,
433 "---------------------------------------------------------------------------------\n");
434
435 }
436 else {
437 if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR) {
438 Map->hist_fp =
439 G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
440 /* If NULL (does not exist) then Vect_hist_read() handle that */
441 }
442 else {
443 Map->hist_fp = NULL;
444 }
445 }
446
447 if (!head_only) { /* cannot rewind if not fully opened */
448 Vect_rewind(Map);
449 }
450
451 /* delete support files if native format was opened for update (not head_only) */
452 if (update && !head_only) {
453 char file_path[2000];
454 struct stat info;
455
456 sprintf(buf, "%s/%s", GV_DIRECTORY, name);
457
458 G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
459 if (stat(file_path, &info) == 0) /* file exists? */
460 unlink(file_path);
461
462 G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
463 if (stat(file_path, &info) == 0) /* file exists? */
464 unlink(file_path);
465
466 G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
467 if (stat(file_path, &info) == 0) /* file exists? */
468 unlink(file_path);
469 }
470
471 return (level);
472}
473
474/*!
475 * \brief Open existing vector for reading
476 *
477 * In case of error, the functions respect fatal error settings.
478 *
479 * \param[out] Map pointer to Map_info structure
480 * \param name name of vector map to open
481 * \param mapset mapset name
482 *
483 * \return level of openness [1, 2, (3)]
484 * \return -1 on error
485 */
486int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
487{
488 return (Vect__open_old(Map, name, mapset, NULL, 0, 0));
489}
490
491int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
492{
493 return (Vect__open_old(Map, name, mapset, layer, 0, 0));
494}
495
496/*!
497 * \brief Open existing vector for reading/writing
498 *
499 * In case of error, the functions respect fatal error settings.
500 *
501 * \param[out] Map pointer to Map_info structure
502 * \param name name of vector map to update
503 * \param mapset mapset name
504 *
505 * \return level of openness [1, 2, (3)]
506 * \return -1 on error
507 */
508int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
509{
510 int ret;
511
512 ret = Vect__open_old(Map, name, mapset, NULL, 1, 0);
513
514 if (ret > 0) {
515 Map->plus.do_uplist = 1;
516
517 Map->plus.uplines = NULL;
518 Map->plus.n_uplines = 0;
519 Map->plus.alloc_uplines = 0;
520 Map->plus.upnodes = NULL;
521 Map->plus.n_upnodes = 0;
522 Map->plus.alloc_upnodes = 0;
523
524 /* read spatial index */
525 /* Build spatial index from topo */
526 /* Vect_build_sidx_from_topo(Map); */
527 }
528
529 return ret;
530}
531
532/*!
533 * \brief Reads only info about vector map from headers of 'head',
534 * 'dbln', 'topo' and 'cidx' file.
535 *
536 * In case of error, the functions respect fatal error settings.
537 *
538 * \param[out] Map pointer to Map_info structure
539 * \param name name of vector map to read (dsn for OGR)
540 * \param mapset mapset name ("" for search path)
541 * \param layer layer name (only for OGR)
542 *
543 * \return level of openness [1, 2, (3)]
544 * \return -1 on error
545 */
546int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
547{
548 return (Vect__open_old(Map, name, mapset, NULL, 0, 1));
549}
550
551int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
552{
553 return (Vect__open_old(Map, name, mapset, layer, 0, 1));
554}
555
556/*!
557 * \brief Open header file of existing vector map for updating (mostly
558 * for database link updates)
559 *
560 * In case of error, the functions respect fatal error settings.
561 *
562 * \param[out] Map pointer to Map_info structure
563 * \param name name of vector map to update
564 * \param mapset mapset name
565 *
566 * \return level of openness [1, 2, (3)]
567 * \return -1 on error
568 */
569int Vect_open_update_head(struct Map_info *Map, const char *name,
570 const char *mapset)
571{
572 int ret;
573
574 ret = Vect__open_old(Map, name, mapset, NULL, 1, 1);
575
576 if (ret > 0) { /* Probably not important */
577 Map->plus.do_uplist = 1;
578
579 Map->plus.uplines = NULL;
580 Map->plus.n_uplines = 0;
581 Map->plus.alloc_uplines = 0;
582 Map->plus.upnodes = NULL;
583 Map->plus.n_upnodes = 0;
584 Map->plus.alloc_upnodes = 0;
585 }
586
587 return ret;
588}
589
590/*!
591 * \brief Create new vector map for reading/writing
592 *
593 * \param[in,out] Map pointer to Map_info structure
594 * \param name name of vector map
595 * \param with_z non-zero value for 3D vector data
596 *
597 * \return 1 on success
598 * \return -1 on error
599 */
600int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
601{
602 int ret, ferror;
603 char errmsg[2000], buf[500];
604 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
605
606 G_debug(2, "Vect_open_new(): name = %s", name);
607
608 /* init header */
609 Vect__init_head(Map);
610
611 /* error handling */
612 ferror = Vect_get_fatal_error();
613 Vect_set_fatal_error(GV_FATAL_EXIT);
614
615 if (G_name_is_fully_qualified(name, xname, xmapset)) {
616 if (strcmp(xmapset, G_mapset()) != 0) {
617 sprintf(errmsg, _("%s is not in the current mapset (%s)"), name,
618 G_mapset());
619 fatal_error(ferror, errmsg);
620 }
621 name = xname;
622 }
623
624 /* check for [A-Za-z][A-Za-z0-9_]* in name */
625 if (Vect_legal_filename(name) < 0) {
626 sprintf(errmsg, _("Vector map name is not SQL compliant"));
627 fatal_error(ferror, errmsg);
628 return (-1);
629 }
630
631 /* Check if map already exists */
632 if (G_find_vector2(name, G_mapset()) != NULL) {
633 G_warning(_("Vector map <%s> already exists and will be overwritten"),
634 name);
635
636 ret = Vect_delete(name);
637 if (ret == -1) {
638 sprintf(errmsg, _("Unable to delete vector map <%s>"), name);
639 fatal_error(ferror, errmsg);
640 return (-1);
641 }
642 }
643
644 Map->name = G_store(name);
645 Map->mapset = G_store(G_mapset());
646 Map->location = G_store(G_location());
647 Map->gisdbase = G_store(G_gisdbase());
648
649 Map->format = GV_FORMAT_NATIVE;
650
651 /* set 2D/3D */
652 Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
653
654 if (V1_open_new_nat(Map, name, with_z) < 0) {
655 sprintf(errmsg, _("Unable to create vector map <%s>"),
656 Vect_get_full_name(Map));
657 fatal_error(ferror, errmsg);
658 return (-1);
659 }
660
661 /* Open history file */
662 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
663 Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
664 if (Map->hist_fp == NULL) {
665 sprintf(errmsg, _("Unable to open history file of vector map <%s>"),
666 Vect_get_full_name(Map));
667 fatal_error(ferror, errmsg);
668 return (-1);
669 }
670
671 Open_level = 0;
672
673 /* initialize topo */
674 dig_init_plus(&(Map->plus));
675
676 /* open new spatial index */
677 Vect_open_sidx(Map, 2);
678
679 Map->open = VECT_OPEN_CODE;
680 Map->level = 1;
681 Map->head_only = 0;
682 Map->support_updated = 0;
683 Map->plus.built = GV_BUILD_NONE;
684 Map->mode = GV_MODE_RW;
685 Map->Constraint_region_flag = 0;
686 Map->Constraint_type_flag = 0;
687 Map->plus.do_uplist = 0;
688
689 Vect_set_proj(Map, G_projection());
690 Vect_set_zone(Map, G_zone());
691
692 Map->dblnk = Vect_new_dblinks_struct();
693
694 return 1;
695}
696
697/*!
698 * \brief Update Coor_info structure
699 *
700 * \param Map pointer to Map_info structure
701 * \param[out] Info pointer to Coor_info structure
702 *
703 * \return 1 on success
704 * \return 0 on error
705 */
706int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
707{
708 char buf[2000], path[2000];
709 struct stat stat_buf;
710
711 switch (Map->format) {
712 case GV_FORMAT_NATIVE:
713 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
714 G__file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
715 G_debug(1, "get coor info: %s", path);
716 if (0 != stat(path, &stat_buf)) {
717 G_warning(_("Unable to stat file <%s>"), path);
718 Info->size = -1L;
719 Info->mtime = -1L;
720 }
721 else {
722 Info->size = (off_t) stat_buf.st_size; /* file size */
723 Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
724 }
725 /* stat does not give correct size on MINGW
726 * if the file is opened */
727#ifdef __MINGW32__
728 if (Map->open == VECT_OPEN_CODE) {
729 dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
730 G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
731 Info->size = dig_ftell(&(Map->dig_fp));
732 }
733#endif
734 break;
735 case GV_FORMAT_OGR:
736 Info->size = 0L;
737 Info->mtime = 0L;
738 break;
739 }
740 G_debug(1, "Info->size = %lu, Info->mtime = %ld",
741 (unsigned long)Info->size, Info->mtime);
742
743 return 1;
744}
745
746/*!
747 * \brief Gets vector map format (as string)
748 *
749 * Note: string is allocated by G_store(). Free allocated memory with
750 * G_free().
751 *
752 * Currently are implemeted:
753 * - Native format (native)
754 * - OGR format (ogr)
755 *
756 * \param Map pointer to Map_info structure
757 *
758 * \return maptype string on success
759 * \return error message on error
760 */
761const char *Vect_maptype_info(const struct Map_info *Map)
762{
763 char maptype[1000];
764
765 switch (Map->format) {
766 case GV_FORMAT_NATIVE:
767 sprintf(maptype, "native");
768 break;
769 case GV_FORMAT_OGR:
770 sprintf(maptype, "ogr");
771 break;
772 default:
773 sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
774 Map->format);
775 }
776
777 return G_store(maptype);
778}
779
780/*!
781 \brief Gets vector map format
782
783 Currently are implemeted:
784 - Native format (GV_FORMAT_NATIVE)
785 - OGR format (GV_FORMAT_OGR)
786
787 \param Map pointer to Map_info structure
788
789 \return maptype code
790*/
791int Vect_maptype(const struct Map_info *Map)
792{
793 return Map->format;
794}
795
796/*!
797 * \brief Open topology file ('topo')
798 *
799 * \param[in,out] Map pointer to Map_info structure
800 * \param head_only open only head
801 *
802 * \return 0 on success
803 * \return 1 file does not exist
804 * \return -1 on error
805 */
806int Vect_open_topo(struct Map_info *Map, int head_only)
807{
808 int err, ret;
809 char buf[500], file_path[2000];
810 struct gvfile fp;
811 struct Coor_info CInfo;
812 struct Plus_head *Plus;
813 struct stat info;
814
815 G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
816 Map->mapset);
817
818 Plus = &(Map->plus);
819
820 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
821 G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
822
823 if (stat(file_path, &info) != 0) /* does not exist */
824 return 1;
825
826 dig_file_init(&fp);
827 fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
828
829 if (fp.file == NULL) { /* topo file is not available */
830 G_debug(1, "Cannot open topo file for vector '%s@%s'.",
831 Map->name, Map->mapset);
832 return -1;
833 }
834
835 /* get coor info */
836 Vect_coor_info(Map, &CInfo);
837
838 /* load head */
839 if (dig_Rd_Plus_head(&fp, Plus) == -1)
840 return -1;
841
842 G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
843 (unsigned long)Plus->coor_size, Plus->coor_mtime);
844
845 /* do checks */
846 err = 0;
847 if (CInfo.size != Plus->coor_size) {
848 G_warning(_("Size of 'coor' file differs from value saved in topology file"));
849 err = 1;
850 }
851 /* Do not check mtime because mtime is changed by copy */
852 /*
853 if ( CInfo.mtime != Plus->coor_mtime ) {
854 G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
855 err = 1;
856 }
857 */
858 if (err) {
859 G_warning(_("Please rebuild topology for vector map <%s@%s>"),
860 Map->name, Map->mapset);
861 return -1;
862 }
863
864 /* load file to the memory */
865 /* dig_file_load ( &fp); */
866
867 /* load topo to memory */
868 ret = dig_load_plus(Plus, &fp, head_only);
869
870 fclose(fp.file);
871 /* dig_file_free ( &fp); */
872
873 if (ret == 0)
874 return -1;
875
876 return 0;
877}
878
879/*!
880 * \brief Open spatial index file ('sidx')
881 *
882 * \param[in,out] Map pointer to Map_info
883 * \param mode 0 old, 1 update, 2 new
884 *
885 * \return 0 on success
886 * \return -1 on error
887 */
888int Vect_open_sidx(struct Map_info *Map, int mode)
889{
890 char buf[500], file_path[2000];
891 int err;
892 struct Coor_info CInfo;
893 struct Plus_head *Plus;
894 struct stat info;
895
896 G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
897 Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
898
899 if (Map->plus.Spidx_built == 1) {
900 G_warning("Spatial index already opened");
901 return 0;
902 }
903
904 Plus = &(Map->plus);
905
906 dig_file_init(&(Map->plus.spidx_fp));
907
908 if (mode < 2) {
909 sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
910 G__file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
911
912 if (stat(file_path, &info) != 0) /* does not exist */
913 return 1;
914
915 Map->plus.spidx_fp.file =
916 G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
917
918 if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
919 G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
920 Map->name, Map->mapset);
921 return -1;
922 }
923
924 /* get coor info */
925 Vect_coor_info(Map, &CInfo);
926
927 /* initialize spatial index */
928 Map->plus.Spidx_new = 0;
929
930 /* load head */
931 if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
932 fclose(Map->plus.spidx_fp.file);
933 return -1;
934 }
935
936 G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
937 (unsigned long)Plus->coor_size, Plus->coor_mtime);
938
939 /* do checks */
940 err = 0;
941 if (CInfo.size != Plus->coor_size) {
942 G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
943 err = 1;
944 }
945 /* Do not check mtime because mtime is changed by copy */
946 /*
947 if ( CInfo.mtime != Plus->coor_mtime ) {
948 G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
949 err = 1;
950 }
951 */
952 if (err) {
953 G_warning(_("Please rebuild topology for vector map <%s@%s>"),
954 Map->name, Map->mapset);
955 fclose(Map->plus.spidx_fp.file);
956 return -1;
957 }
958 }
959
960 if (mode) {
961 /* open new spatial index */
962 Map->plus.Spidx_new = 1;
963
964 if (mode == 1) {
965 /* load spatial index for update */
966 if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
967 fclose(Map->plus.spidx_fp.file);
968 return -1;
969 }
970 }
971 }
972
973 Map->plus.Spidx_built = 1;
974
975 return 0;
976}
Note: See TracBrowser for help on using the repository browser.