GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
close_nat.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/close_nat.c
3
4 \brief Vector library - Close map (native format)
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 (C) 2001-2015 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 Higgins.
14 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
15 */
16
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
20
21#include <grass/vector.h>
22#include <grass/glocale.h>
23
24#include "local_proto.h"
25
26/*!
27 \brief Close vector map
28
29 \param Map vector map to be closed
30
31 \return 0 on success
32 \return non-zero on error
33 */
35{
36 struct Coor_info CInfo;
37
38 G_debug(1, "V1_close_nat(): name = %s mapset= %s", Map->name, Map->mapset);
39 if (!VECT_OPEN(Map))
40 return 1;
41
42 if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
44 Map->head.size = CInfo.size;
46
49 }
50
51 /* close coor file */
52 fclose(Map->dig_fp.file);
53 dig_file_free(&(Map->dig_fp));
54
55 /* delete temporary map ? */
56 if (Map->temporary) {
57 int delete;
58 char *env = getenv("GRASS_VECTOR_TEMPORARY");
59
60 delete = TRUE;
61 if (Map->temporary == TEMPORARY_MAP_ENV && env) {
62 if (G_strcasecmp(env, "move") == 0) {
63 /* copy temporary vector map to the current mapset */
65
66 G_debug(1,
67 "V1_close_nat(): temporary map <%s> TO BE MOVED TO"
68 " CURRENT MAPSET",
69 Map->name);
71
73 if (access(path_map, 0) != 0 && G_mkdir(path_map) != 0)
74 G_fatal_error(_("Unable to create '%s': %s"), path_map,
76
78
79 G_debug(1, "V1_close_nat(): %s -> %s", path_tmp, path_map);
81 G_fatal_error(_("Unable to copy '%s': %s"), path_tmp,
83
84#ifdef TEMPORARY_MAP_DB
85 int i, ndblinks;
86 int tmp;
87
88 struct field_info *fi;
90 struct dblinks *dblinks;
91
92 G_debug(1, "V1_close_nat(): copying attributes");
93 /* copy also attributes */
97 for (i = 0; i < ndblinks; i++) {
98 fi = Vect_get_dblink(Map, i);
99 if (DB_OK != db_copy_table(fi->driver, fi->database,
100 fi->table, connection.driverName,
101 connection.databaseName,
102 fi->table)) {
103 G_warning(_("Unable to copy table <%s>"), fi->table);
104 continue;
105 }
106
107 Vect_add_dblink(dblinks, fi->number, fi->name, fi->table,
108 fi->key, connection.databaseName,
109 connection.driverName);
110 G_free(fi);
111 }
112 G_free(Map->dblnk);
113 Map->dblnk = dblinks;
114 tmp = Map->temporary;
115 Map->temporary = TEMPORARY_MAP_DISABLED;
117 Map->temporary = tmp;
118#endif
119 }
120 else if (G_strcasecmp(env, "delete") == 0) {
121 /* delete temporary vector map */
122 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
123 Map->name);
124 }
125 else {
126 /* do not delete temporary vector map */
127 G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED",
128 Map->name);
129 delete = FALSE;
130 }
131 }
132 else if (Map->temporary == TEMPORARY_MAP) {
133 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
134 Map->name);
135 delete = TRUE;
136 }
137
138 if (delete) {
139 char path_tmp[GPATH_MAX];
140
141 /* delete vector directory */
144
145#ifndef TEMPORARY_MAP_DB
146 if (G_strcasecmp(env, "move") != 0) {
147 int i, ndblinks;
148
150 dbString table_name;
151
152 struct field_info *fi;
153
154 db_init_string(&table_name);
155
156 /* drop also attribute table */
158 for (i = 0; i < ndblinks; i++) {
159 fi = Vect_get_dblink(Map, i);
160
161 driver =
163 if (driver == NULL) {
164 G_warning(
165 _("Unable to open database <%s> by driver <%s>"),
166 fi->database, fi->driver);
167 continue;
168 }
169
170 db_set_string(&table_name, fi->table);
171 if (DB_OK != db_drop_table(driver, &table_name)) {
172 G_warning(_("Unable to drop table <%s>"), fi->table);
173 continue;
174 }
175 }
176 }
177#endif
178 }
179 }
180
181 return 0;
182}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
int V1_close_nat(struct Map_info *Map)
Close vector map.
Definition close_nat.c:34
#define DB_OK
Definition dbmi.h:71
int db_drop_table(dbDriver *, dbString *)
Drop table.
Definition c_drop_tab.c:28
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition string.c:41
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:25
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition db.c:28
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
Definition copy_tab.c:443
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition copy_dir.c:69
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:147
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:61
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:47
int G_recursive_remove(const char *)
Recursively remove all files in given directory.
Definition remove.c:112
int G_mkdir(const char *)
Creates a new directory.
Definition paths.c:27
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_add_dblink(struct dblinks *, int, const char *, const char *, const char *, const char *, const char *)
Add new DB connection to dblinks structure.
Definition field.c:280
int Vect_coor_info(struct Map_info *, struct Coor_info *)
Update Coor_info structure.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
Definition field.c:47
int Vect_write_dblinks(struct Map_info *)
Write dblinks to file.
Definition field.c:869
struct field_info * Vect_get_dblink(struct Map_info *, int)
Get information about link to database.
Definition field.c:470
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition level_two.c:159
int Vect__write_head(struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
#define VECT_OPEN(Map)
Check if vector map is open.
#define GV_DIRECTORY
Name of vector directory.
Definition dig_defines.h:8
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_MODE_RW
Read-write vector map open mode.
int dig__write_head(struct Map_info *)
Definition head.c:23
void dig_file_free(struct gvfile *file)
Free struct gvfile.
Definition file.c:268
#define GPATH_MAX
Definition gis.h:199
#define TRUE
Definition gis.h:78
#define FALSE
Definition gis.h:82
#define _(str)
Definition glocale.h:10
Coor file info.
Vector map info.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * name
Layer name (optional)
char * database
char * key
Name of key column (usually 'cat')
int number
Layer number.
#define access
Definition unistd.h:7
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)