24 #include <sys/types.h>
26 #include <grass/glocale.h>
27 #include <grass/gis.h>
28 #include <grass/Vect.h>
30 #define MAX_OPEN_LEVEL 2
32 static int open_old_dummy()
40 G_fatal_error(_(
"Requested format is not compiled in this version"));
45 static int Open_level = 0;
47 static int (*Open_old_array[][2]) () = {
55 open_old_dummy, format}
59 static void fatal_error(
int ferror,
char *errmsg)
100 G_warning(_(
"Programmer requested unknown open level %d"),
126 int update,
int head_only)
128 char buf[GNAME_MAX + 10],
buf2[GMAPSET_MAX + 10],
xname[GNAME_MAX],
129 xmapset[GMAPSET_MAX], errmsg[2000];
131 int level, level_request, ferror;
135 G_debug(1,
"Vect_open_old(): name = %s mapset= %s update = %d", name,
143 level_request = Open_level;
149 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, xname);
150 sprintf(buf2,
"%s@%s", GRASS_VECT_COOR_ELEMENT, xmapset);
153 Map->mapset =
G_store(xmapset);
156 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, name);
157 sprintf(buf2,
"%s", GRASS_VECT_COOR_ELEMENT);
167 if (fmapset ==
NULL) {
168 sprintf(errmsg, _(
"Vector map <%s> not found"),
170 fatal_error(ferror, errmsg);
173 Map->mapset =
G_store(fmapset);
178 if (update && (0 != strcmp(Map->mapset,
G_mapset()))) {
179 G_warning(_(
"Vector map which is not in the current mapset cannot be opened for update"));
185 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
186 G_debug(1,
"open format file: '%s/%s/%s'", Map->mapset, buf,
187 GRASS_VECT_FRMT_ELEMENT);
188 fp =
G_fopen_old(buf, GRASS_VECT_FRMT_ELEMENT, Map->mapset);
190 G_debug(1,
"Vector format: %d (native)", format);
191 format = GV_FORMAT_NATIVE;
197 G_debug(1,
"Vector format: %d (non-native)", format);
199 sprintf(errmsg, _(
"Unable to open vector map <%s>"),
201 fatal_error(ferror, errmsg);
206 Map->format = format;
211 _(
"Unable to open vector map <%s> on level %d. "
212 "Try to rebuild vector topology by v.build."),
214 G_warning(_(
"Unable to read head file"));
217 G_debug(1,
"Level request = %d", level_request);
226 if (level_request == 0 || level_request == 2) {
231 G_debug(1,
"Topo file for vector '%s' not available.",
235 else if (ret == -1) {
236 G_fatal_error(_(
"Unable to open topology file for vector map <%s>"),
255 "Category index file for vector '%s' not available.",
261 else if (ret == -1) {
262 G_fatal_error(_(
"Unable to open category index file for vector map <%s>"),
268 if (level == 2 && Map->format == GV_FORMAT_OGR) {
277 if (level_request == 2 && level < 2) {
279 _(
"Unable to open vector map <%s> on level %d. "
280 "Try to rebuild vector topology by v.build."),
282 fatal_error(ferror, errmsg);
292 if (0 != (*Open_old_array[format][1]) (Map, update)) {
299 _(
"Unable to open vector map <%s> on level %d. "
300 "Try to rebuild vector topology by v.build."),
302 fatal_error(ferror, errmsg);
307 Map->head.with_z = Map->plus.with_z;
311 Map->open = VECT_OPEN_CODE;
313 Map->head_only = head_only;
314 Map->support_updated = 0;
316 Map->mode = GV_MODE_RW;
317 Map->plus.mode = GV_MODE_RW;
320 Map->mode = GV_MODE_READ;
321 Map->plus.mode = GV_MODE_READ;
330 Map->Constraint_region_flag = 0;
331 Map->Constraint_type_flag = 0;
332 G_debug(1,
"Vect_open_old(): vector opened on level %d", level);
335 Map->plus.built = GV_BUILD_NONE;
338 Map->plus.built = GV_BUILD_ALL;
341 Map->plus.do_uplist = 0;
347 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
351 if (Map->hist_fp ==
NULL) {
353 _(
"Unable to open history file for vector map <%s>"),
355 fatal_error(ferror, errmsg);
358 fseek(Map->hist_fp, (
long)0, SEEK_END);
360 "---------------------------------------------------------------------------------\n");
364 if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR) {
366 G_fopen_old(buf, GRASS_VECT_HIST_ELEMENT, Map->mapset);
379 if (update && !head_only) {
380 char file_path[2000];
383 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, name);
386 if (
stat(file_path, &info) == 0)
390 if (
stat(file_path, &info) == 0)
394 if (
stat(file_path, &info) == 0)
438 Map->plus.do_uplist = 1;
440 Map->plus.uplines =
NULL;
441 Map->plus.n_uplines = 0;
442 Map->plus.alloc_uplines = 0;
443 Map->plus.upnodes =
NULL;
444 Map->plus.n_upnodes = 0;
445 Map->plus.alloc_upnodes = 0;
494 Map->plus.do_uplist = 1;
496 Map->plus.uplines =
NULL;
497 Map->plus.n_uplines = 0;
498 Map->plus.alloc_uplines = 0;
499 Map->plus.upnodes =
NULL;
500 Map->plus.n_upnodes = 0;
501 Map->plus.alloc_upnodes = 0;
520 char errmsg[2000],
buf[200];
523 G_debug(2,
"Vect_open_new(): name = %s", name);
530 if (strcmp(xmapset,
G_mapset()) != 0) {
531 sprintf(errmsg, _(
"%s is not in the current mapset (%s)"), name,
533 fatal_error(ferror, errmsg);
540 sprintf(errmsg, _(
"Vector map name is not SQL compliant"));
541 fatal_error(ferror, errmsg);
547 G_warning(_(
"Vector map <%s> already exists and will be overwritten"),
552 sprintf(errmsg, _(
"Unable to delete vector map <%s>"), name);
553 fatal_error(ferror, errmsg);
563 Map->format = GV_FORMAT_NATIVE;
566 sprintf(errmsg, _(
"Unable to create vector map <%s>"),
568 fatal_error(ferror, errmsg);
573 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
574 Map->hist_fp =
G_fopen_new(buf, GRASS_VECT_HIST_ELEMENT);
575 if (Map->hist_fp ==
NULL) {
576 sprintf(errmsg, _(
"Unable to open history file for vector map <%s>"),
578 fatal_error(ferror, errmsg);
586 Map->open = VECT_OPEN_CODE;
589 Map->support_updated = 0;
590 Map->plus.built = GV_BUILD_NONE;
591 Map->mode = GV_MODE_RW;
592 Map->Constraint_region_flag = 0;
593 Map->Constraint_type_flag = 0;
594 Map->head.with_z = with_z;
595 Map->plus.do_uplist = 0;
613 char buf[2000], path[2000];
614 struct stat stat_buf;
616 switch (Map->format) {
617 case GV_FORMAT_NATIVE:
618 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
619 G__file_name(path, buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
620 G_debug(1,
"get coor info: %s", path);
621 if (0 !=
stat(path, &stat_buf)) {
622 G_warning(_(
"Unable to stat file <%s>"), path);
627 Info->size = (long)stat_buf.st_size;
628 Info->mtime = (
long)stat_buf.st_mtime;
633 if (Map->open == VECT_OPEN_CODE) {
645 G_debug(1,
"Info->size = %ld, Info->mtime = %ld", Info->size,
663 switch (Map->format) {
664 case GV_FORMAT_NATIVE:
671 sprintf(maptype,
"unknown %d (update Vect_maptype_info)",
692 char buf[500], file_path[2000];
694 struct Coor_info CInfo;
695 struct Plus_head *Plus;
698 G_debug(1,
"Vect_open_topo(): name = %s mapset= %s", Map->name,
703 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
704 G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
706 if (
stat(file_path, &info) != 0)
710 fp.file =
G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
712 if (fp.file ==
NULL) {
713 G_debug(1,
"Cannot open topo file for vector '%s@%s'.",
714 Map->name, Map->mapset);
725 G_debug(1,
"Topo head: coor size = %ld, coor mtime = %ld",
726 Plus->coor_size, Plus->coor_mtime);
730 if (CInfo.size != Plus->coor_size) {
731 G_warning(_(
"Size of 'coor' file differs from value saved in topology file"));
742 G_warning(_(
"Please rebuild topology for vector map <%s@%s>"),
743 Map->name, Map->mapset);
776 struct Plus_head *Plus;
778 G_debug(1,
"Vect_open_spatial_index(): name = %s mapset= %s", Map->name,
783 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, Map->name);
785 fp.file =
G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
787 if (fp.file ==
NULL) {
788 G_debug(1,
"Cannot open spatial index file for vector '%s@%s'.",
789 Map->name, Map->mapset);
char * G_mapset(void)
current mapset name
int dig_init_plus(struct Plus_head *Plus)
Init head structure.
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void dig_spidx_free(struct Plus_head *Plus)
Free spatial index (nodes, lines, areas, isles)
int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, int update, int head_only)
Open old vector for reading.
char * G_store(const char *s)
Copy string to allocated memory.
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
Open/Create new vector map.
int Vect_build_sidx_from_topo(struct Map_info *Map)
Create spatial index from topo if necessary.
int dig_spidx_init(struct Plus_head *Plus)
Initit spatial index (nodes, lines, areas, isles)
int V1_open_old_ogr(struct Map_info *Map, int update)
Open existing vector map.
FILE * G_fopen_modify(const char *element, const char *name)
Open a database file for update (r+ mode)
char * G_find_vector2(const char *name, const char *mapset)
find a vector map (look but don't touch)
const char * Vect_maptype_info(struct Map_info *Map)
Gets maptype (native, shape, postgis)
int Vect__init_head(struct Map_info *Map)
Initialize head structure.
int dig_Rd_Plus_head(GVFILE *fp, struct Plus_head *ptr)
int Vect_set_fatal_error(int err)
Set behaviour if fatal error occurs in some functions.
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
int Vect_get_fatal_error(void)
Get behaviour for fatal error.
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
int Vect_open_topo(struct Map_info *Map, int head_only)
Open topo file.
int Vect_set_open_level(int level)
Predetermine level at which a map will be opened for reading.
int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector for reading/writing.
char * G_gisdbase(void)
Get name of top level database directory.
int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
Reads only info about vector from headers of 'head', 'dbln', 'topo' and 'cidx' file.
int V2_open_old_ogr(struct Map_info *Map)
Open OGR specific level 2 files (feature index)
int Vect_legal_filename(const char *s)
Check if output is legal vector name.
int dig_read_spidx(GVFILE *fp, struct Plus_head *Plus)
int Vect_delete(const char *map)
Delete vector map including attribute tables.
void dig_cidx_free(struct Plus_head *Plus)
char * G_location(void)
Get current location name.
int Vect_open_update_head(struct Map_info *Map, const char *name, const char *mapset)
Open old vector head for updating (mostly for database link updates)
int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector for reading.
int Vect_coor_info(struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
void dig_free_plus(struct Plus_head *Plus)
Free Plus structure.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int Vect_open_spatial_index(struct Map_info *Map)
Open spatial index file.
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
char * G_find_file2(const char *element, const char *name, const char *mapset)
searches for a file from the mapset search list or in a specified mapset. (look but don't touch) retu...
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
void dig_file_init(GVFILE *file)
Initialize GVFILE.
int Vect_cidx_open(struct Map_info *Map, int head_only)
Read category index from file if exists.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks ctructure.
int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
Open new vector for reading/writing.
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int Vect_read_dblinks(struct Map_info *Map)
Read dblinks to existing structure.
int Vect_hist_write(struct Map_info *Map, const char *str)
Write string to history file.
long dig_ftell(GVFILE *file)
Get GVFILE position.
int V1_open_old_nat(struct Map_info *Map, int update)
Open existing vector map.
int dig_load_plus(struct Plus_head *Plus, GVFILE *plus, int head_only)
Reads topo file to topo structure.
int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
int Vect_rewind(struct Map_info *Map)
Rewind vector data file to cause reads to start at beginning.
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)