GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
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 SPDX-FileCopyrightText: 2001-2015 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
13 */
14
15#include <stdlib.h>
16#include <unistd.h>
17#include <errno.h>
18
19#include <grass/vector.h>
20#include <grass/glocale.h>
21
22#include "local_proto.h"
23
24/*!
25 \brief Close vector map
26
27 \param Map vector map to be closed
28
29 \return 0 on success
30 \return non-zero on error
31 */
33{
34 struct Coor_info CInfo;
35
36 G_debug(1, "V1_close_nat(): name = %s mapset= %s", Map->name, Map->mapset);
37 if (!VECT_OPEN(Map))
38 return 1;
39
40 if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
42 Map->head.size = CInfo.size;
44
47 }
48
49 /* close coor file */
50 fclose(Map->dig_fp.file);
51 dig_file_free(&(Map->dig_fp));
52
53 /* delete temporary map ? */
54 if (Map->temporary) {
55 int delete;
56 char *env = getenv("GRASS_VECTOR_TEMPORARY");
57
58 delete = TRUE;
59 if (Map->temporary == TEMPORARY_MAP_ENV && env) {
60 if (G_strcasecmp(env, "move") == 0) {
61 /* copy temporary vector map to the current mapset */
63
64 G_debug(1,
65 "V1_close_nat(): temporary map <%s> TO BE MOVED TO"
66 " CURRENT MAPSET",
67 Map->name);
69
71 if (access(path_map, 0) != 0 && G_mkdir(path_map) != 0)
72 G_fatal_error(_("Unable to create '%s': %s"), path_map,
74
76
77 G_debug(1, "V1_close_nat(): %s -> %s", path_tmp, path_map);
79 G_fatal_error(_("Unable to copy '%s': %s"), path_tmp,
81
82#ifdef TEMPORARY_MAP_DB
83 int i, ndblinks;
84 int tmp;
85
86 struct field_info *fi;
88 struct dblinks *dblinks;
89
90 G_debug(1, "V1_close_nat(): copying attributes");
91 /* copy also attributes */
95 for (i = 0; i < ndblinks; i++) {
96 fi = Vect_get_dblink(Map, i);
97 if (DB_OK != db_copy_table(fi->driver, fi->database,
98 fi->table, connection.driverName,
99 connection.databaseName,
100 fi->table)) {
101 G_warning(_("Unable to copy table <%s>"), fi->table);
102 continue;
103 }
104
105 Vect_add_dblink(dblinks, fi->number, fi->name, fi->table,
106 fi->key, connection.databaseName,
107 connection.driverName);
108 G_free(fi);
109 }
110 G_free(Map->dblnk);
111 Map->dblnk = dblinks;
112 tmp = Map->temporary;
113 Map->temporary = TEMPORARY_MAP_DISABLED;
115 Map->temporary = tmp;
116#endif
117 }
118 else if (G_strcasecmp(env, "delete") == 0) {
119 /* delete temporary vector map */
120 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
121 Map->name);
122 }
123 else {
124 /* do not delete temporary vector map */
125 G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED",
126 Map->name);
127 delete = FALSE;
128 }
129 }
130 else if (Map->temporary == TEMPORARY_MAP) {
131 G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED",
132 Map->name);
133 delete = TRUE;
134 }
135
136 if (delete) {
137 char path_tmp[GPATH_MAX];
138
139 /* delete vector directory */
142
143#ifndef TEMPORARY_MAP_DB
144 if (G_strcasecmp(env, "move") != 0) {
145 int i, ndblinks;
146
148 dbString table_name;
149
150 struct field_info *fi;
151
152 db_init_string(&table_name);
153
154 /* drop also attribute table */
156 for (i = 0; i < ndblinks; i++) {
157 fi = Vect_get_dblink(Map, i);
158
159 driver =
161 if (driver == NULL) {
162 G_warning(
163 _("Unable to open database <%s> by driver <%s>"),
164 fi->database, fi->driver);
165 continue;
166 }
167
168 db_set_string(&table_name, fi->table);
169 if (DB_OK != db_drop_table(driver, &table_name)) {
170 G_warning(_("Unable to drop table <%s>"), fi->table);
171 continue;
172 }
173 }
174 }
175#endif
176 }
177 }
178
179 return 0;
180}
#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:32
#define DB_OK
Definition dbmi.h:69
int db_drop_table(dbDriver *, dbString *)
Drop table.
Definition c_drop_tab.c:25
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:39
void db_init_string(dbString *)
Initialize dbString.
Definition string.c:23
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition db.c:25
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
Definition copy_tab.c:440
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition copy_dir.c:67
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
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:59
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:45
int G_recursive_remove(const char *)
Recursively remove all files in given directory.
Definition remove.c:110
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:278
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:45
int Vect_write_dblinks(struct Map_info *)
Write dblinks to file.
Definition field.c:867
struct field_info * Vect_get_dblink(struct Map_info *, int)
Get information about link to database.
Definition field.c:468
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
Definition level_two.c:157
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:20
void dig_file_free(struct gvfile *file)
Free struct gvfile.
Definition file.c:266
#define GPATH_MAX
Definition gis.h:196
#define TRUE
Definition gis.h:75
#define FALSE
Definition gis.h:79
#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)