22 #include <sys/types.h>
32 #include "local_proto.h"
79 static int copy_file(
const char *
src,
const char *
dst)
86 if ((fd =
open(
src, O_RDONLY)) < 0)
90 if ((f2 = fopen(
dst,
"w")) ==
NULL) {
98 while ((len = read(fd, buf, 4096)) > 0) {
99 while (len && (len2 = write(fd2, buf, len)) >= 0)
107 if (len == -1 || len2 == -1)
131 const char *files[] = {
134 const char *inmapset;
137 G_debug(2,
"Copy vector '%s' in '%s' to '%s'", in,
mapset, out);
156 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
160 G_warning(
_(
"Unable to delete vector map <%s>"), out);
170 sprintf(buf,
"%s/%s", in, files[i]);
172 sprintf(buf,
"%s/%s", out, files[i]);
175 if (access(old_path, F_OK) == 0) {
176 G_debug(2,
"copy %s to %s", old_path, new_path);
177 if (copy_file(old_path, new_path)) {
178 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"), old_path,
242 G_debug(2,
"Rename vector '%s' to '%s'", in, out);
249 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
263 G_warning(
_(
"Vector map <%s> not found"), in);
266 else if (ret == -1) {
267 G_warning(
_(
"Unable to copy vector map <%s> to <%s>"), in, out);
288 fields = (
int *)
G_malloc(n *
sizeof(
int));
290 for (i = 0; i < n; i++) {
295 for (i = 0; i < n; i++) {
296 G_debug(3,
"field[%d] = %d", i, fields[i]);
300 G_warning(
_(
"Database connection not defined for layer %d"),
307 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'", Fin->
driver,
339 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
344 G_warning(
_(
"Unable to create index for table <%s>, key <%s>"),
386 const char *tmp, *mapset, *env;
393 G_debug(3,
"Delete vector '%s' (is_tmp = %d)", map, is_tmp);
399 if (strcmp(mapset, xmapset) != 0)
400 G_warning(
_(
"Ignoring invalid mapset: %s"), xmapset);
404 if (map ==
NULL || strlen(map) == 0) {
405 G_warning(
_(
"Invalid vector map name <%s>"), map ? map :
"null");
415 G_warning(
_(
"Unable to open header file for vector map <%s>"), map);
423 if (access(
path, F_OK) == 0) {
430 for (i = 0; i < n; i++) {
433 G_warning(
_(
"Database connection not defined for layer %d"),
446 G_warning(
_(
"Unable to find table <%s> linked to vector "
468 G_warning(
_(
"Table <%s> linked to vector map <%s> does not "
486 while ((ent = readdir(dir))) {
487 G_debug(3,
"file = '%s'", ent->d_name);
488 if ((strcmp(ent->d_name,
".") == 0) || (strcmp(ent->d_name,
"..") == 0))
491 ret = snprintf(path_buf,
GPATH_MAX,
"%s/%s",
path, ent->d_name);
498 G_debug(3,
"delete file '%s'", path_buf);
499 ret = unlink(path_buf);
501 G_warning(
_(
"Unable to delete file '%s'"), path_buf);
508 env = getenv(
"GRASS_VECTOR_TMPDIR_MAPSET");
509 if (env && strcmp(env,
"0") == 0) {
519 ret = rename(
path, tmp);
521 G_warning(
_(
"Unable to rename directory '%s' to '%s'"),
path, tmp);
526 G_debug(3,
"remove directory '%s'", tmp);
530 G_warning(
_(
"Unable to remove directory '%s': %s"), tmp,
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
int db_delete_table(const char *, const char *, const char *)
Delete table.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int db_create_index2(dbDriver *, const char *, const char *)
Create unique index.
int db_table_exists(const char *, const char *, const char *)
Check if table exists.
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_make_mapset_dir_object(const char *, const char *)
Create directory for an object of a given type.
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
char * G_tempfile(void)
Returns a temporary file name.
const char * G_mapset(void)
Get current mapset name.
int G_debug(int, const char *,...) __attribute__((format(printf
int G_rename(const char *, const char *, const char *)
Rename a database file.
struct field_info * Vect_default_field_info(struct Map_info *, int, const char *, int)
Get default information about link to database for new dblink.
int Vect_map_del_dblink(struct Map_info *, int)
Delete db connection from Map_info structure.
int Vect_close(struct Map_info *)
Close vector map.
int Vect_open_update_head(struct Map_info *, const char *, const char *)
Open header file of existing vector map for updating (mostly for database link updates)
int Vect_legal_filename(const char *)
Check if output is legal vector name.
char * Vect_subst_var(const char *, struct Map_info *)
Substitute variable in string.
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
struct field_info * Vect_get_dblink(struct Map_info *, int)
Get information about link to database.
int Vect_map_add_dblink(struct Map_info *, int, const char *, const char *, const char *, const char *, const char *)
Add new db connection to Map_info structure.
int Vect_set_open_level(int)
Predetermine level at which a vector map will be opened for reading.
int Vect_open_old_head(struct Map_info *, const char *, const char *)
Reads only info about vector map (headers)
int Vect_copy_tables(struct Map_info *, struct Map_info *, int)
Copy attribute tables linked to vector map.
#define GV_FRMT_ELEMENT
Format description, data location (OGR)
#define GV_DIRECTORY
Name of vector directory.
#define GV_SIDX_ELEMENT
Native format, spatial index.
#define GV_1TABLE
One table linked to vector map.
#define GV_COOR_ELEMENT
Native format, coordinates.
#define GV_DBLN_ELEMENT
Native format, link to database.
#define GV_CIDX_ELEMENT
Native format, category index.
#define GV_TOPO_ELEMENT
Native format, topology file.
#define GV_MTABLE
More tables linked to vector map.
#define GV_HEAD_ELEMENT
Native format, header information.
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
#define GV_HIST_ELEMENT
Native format, history file.
const struct driver * driver
void Vect_set_category_index_update(struct Map_info *Map)
Set category index to be updated when vector is changed.
void Vect_destroy_map_struct(struct Map_info *p)
Frees all memory associated with a Map_info structure, including the structure itself.
int Vect__delete(const char *map, int is_tmp)
Delete vector map (internal use only)
int Vect_rename(const char *in, const char *out)
Rename existing vector map (in the current mapset).
int Vect_copy(const char *in, const char *mapset, const char *out)
Copy vector map including attribute tables.
void Vect_set_release_support(struct Map_info *Map)
Set spatial index to be released when vector is closed.
struct Map_info * Vect_new_map_struct(void)
Creates and initializes Map_info structure.
int Vect_delete(const char *map)
Delete vector map including attribute tables.
char * mapset
Mapset name.
int type
Feature type constraint.
int format
Map format (native, ogr, postgis)
struct dblinks * dblnk
Array of DB links.
struct Plus_head plus
Plus info (topology, version, ...)
int release_support
Release memory occupied by support structures (topo, spatial, category)
int update_cidx
Update category index if vector is modified.
struct field_info * field
Pointer to the first field_info structure.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * name
Layer name (optional)
char * key
Name of key column (usually 'cat')
int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, const char *layer, int update, int head_only, int is_tmp)
Open existing vector map for reading (internal use only)
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)