26 #include <sys/types.h>
29 #include <grass/glocale.h>
30 #include <grass/gis.h>
31 #include <grass/Vect.h>
32 #include <grass/dbmi.h>
33 #include <grass/glocale.h>
46 int i,
type, nlines, ret;
47 struct line_pnts *Points;
48 struct line_cats *Cats;
55 _(
"input vector map is not open"));
62 for (i = 1; i <= nlines; i++) {
68 G_warning(_(
"Unable to read vector map <%s>"),
84 G_warning(_(
"Unable to read vector map <%s>"),
89 else if (type == -2) {
113 static int copy_file(
const char *src,
const char *dst)
120 if ((fd = open(src, O_RDONLY)) < 0)
124 if ((f2 = fopen(dst,
"w")) ==
NULL) {
131 while ((len = read(fd, buf, 1024)) > 0) {
132 while (len && (len2 = write(fd2, buf, len)) >= 0)
140 if (len == -1 || len2 == -1)
159 Vect_copy(
const char *in,
const char *mapset,
const char *out)
162 struct Map_info In, Out;
163 struct field_info *Fi, *Fin;
164 char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX];
166 const char *files[] = { GRASS_VECT_FRMT_ELEMENT, GRASS_VECT_COOR_ELEMENT,
167 GRASS_VECT_HEAD_ELEMENT, GRASS_VECT_HIST_ELEMENT,
168 GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT,
175 G_debug(2,
"Copy vector '%s' in '%s' to '%s'", in, mapset, out);
182 G_warning(_(
"Unable to find vector map <%s> in <%s>"), in, mapset);
189 G_warning(_(
"Vector map <%s> already exists and will be overwritten"),
193 G_warning(_(
"Unable to delete vector map <%s>"), out);
200 sprintf(buf,
"%s/%s", GRASS_VECT_DIRECTORY, out);
205 sprintf(buf,
"%s/%s", in, files[i]);
206 G__file_name(old_path, GRASS_VECT_DIRECTORY, buf, mapset);
207 sprintf(buf,
"%s/%s", out, files[i]);
210 if (
stat(old_path, &info) == 0) {
211 G_debug(2,
"copy %s to %s", old_path, new_path);
212 if (copy_file(old_path, new_path)) {
213 G_warning(_(
"Unable to copy vector map <%s> to <%s>"),
220 G__file_name(old_path, GRASS_VECT_DIRECTORY, in, mapset);
227 if (In.format != GV_FORMAT_NATIVE) {
240 for (i = 0; i <
n; i++) {
243 G_warning(_(
"Database connection not defined for layer %d"),
244 In.dblnk->field[i].number);
250 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
251 Fi->driver, Fi->database, Fi->table, Fin->driver,
252 Fin->database, Fin->table);
254 Fin->database, Fin->driver);
259 if (ret == DB_FAILED) {
260 G_warning(_(
"Unable to copy table <%s>"), Fin->table);
270 if (driver ==
NULL) {
271 G_warning(_(
"Unable to open database <%s> by driver <%s>"),
272 Fin->database, Fin->driver);
276 G_warning(_(
"Unable to create index for table <%s>, key <%s>"),
307 struct field_info *Fin, *Fout;
311 G_debug(2,
"Rename vector '%s' to '%s'", in, out);
318 G_warning(_(
"Vector map <%s> already exists and will be overwritten"),
324 ret =
G_rename(GRASS_VECT_DIRECTORY, in, out);
327 G_warning(_(
"Vector map <%s> not found"), in);
330 else if (ret == -1) {
331 G_warning(_(
"Unable to copy vector map <%s> to <%s>"), in, out);
339 if (Map.format != GV_FORMAT_NATIVE) {
351 fields = (
int *)G_malloc(n *
sizeof(
int));
353 for (i = 0; i <
n; i++) {
356 fields[i] = Fin->number;
359 for (i = 0; i <
n; i++) {
360 G_debug(3,
"field[%d] = %d", i, fields[i]);
364 G_warning(_(
"Database connection not defined for layer %d"),
371 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
372 Fin->driver, Fin->database, Fin->table, Fout->driver,
373 Fout->database, Fout->table);
380 if (ret == DB_FAILED) {
381 G_warning(_(
"Unable to copy table <%s>"), Fin->table);
390 Fin->key, Fout->database, Fout->driver);
394 if (ret == DB_FAILED) {
395 G_warning(_(
"Unable to delete table <%s>"), Fin->table);
404 if (driver ==
NULL) {
405 G_warning(_(
"Unable to open database <%s> by driver <%s>"),
406 Fout->database, Fout->driver);
410 G_warning(_(
"Unable to create index for table <%s>, key <%s>"),
411 Fout->table, Fout->key);
435 struct field_info *Fi;
441 G_debug(3,
"Delete vector '%s'", map);
443 if (map ==
NULL || strlen(map) == 0) {
444 G_warning(_(
"Invalid vector map name <%s>"), map ? map :
"null");
449 G_mapset(), GRASS_VECT_DIRECTORY, map, GRASS_VECT_DBLN_ELEMENT);
451 G_debug(1,
"dbln file: %s", buf);
453 if (access(buf, F_OK) == 0) {
458 G_warning(_(
"Unable to open header file for vector map <%s>"),
464 if (Map.format == GV_FORMAT_NATIVE) {
467 for (i = 0; i <
n; i++) {
470 G_warning(_(
"Database connection not defined for layer %d"),
471 Map.dblnk->field[i].number);
475 G_debug(3,
"Delete drv:db:table '%s:%s:%s'", Fi->driver,
476 Fi->database, Fi->table);
480 G_warning(_(
"Unable to find table <%s> linked to vector map <%s>"),
489 if (ret == DB_FAILED) {
490 G_warning(_(
"Unable to delete table <%s>"),
497 G_warning(_(
"Table <%s> linked to vector map <%s> does not exist"),
508 G_debug(3,
"opendir '%s'", buf);
511 G_warning(_(
"Unable to open directory '%s'"), buf);
516 G_debug(3,
"file = '%s'", ent->d_name);
517 if ((strcmp(ent->d_name,
".") == 0) ||
518 (strcmp(ent->d_name,
"..") == 0))
522 G_debug(3,
"delete file '%s'", buf);
525 G_warning(_(
"Unable to delete file '%s'"), buf);
538 G_debug(3,
"rename '%s' to '%s'", buf, tmp);
539 ret = rename(buf, tmp);
542 G_warning(_(
"Unable to rename directory '%s' to '%s'"), buf, tmp);
546 G_debug(3,
"remove directory '%s'", tmp);
550 G_warning(_(
"Unable to remove directory '%s'"), tmp);
574 struct field_info *Fi, *Fin;
579 G_debug(2,
"Vect_copy_tables(): copying %d tables",n);
585 for (i = 0; i <
n; i++) {
588 G_warning(_(
"Database connection not defined for layer %d"),
589 In->dblnk->field[i].number);
592 if (field > 0 && Fi->number != field)
596 G_debug(2,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
597 Fi->driver, Fi->database, Fi->table, Fin->driver,
598 Fin->database, Fin->table);
602 Fi->key, Fin->database, Fin->driver);
604 G_warning(_(
"Unable to add database link for vector map <%s>"),
612 if (ret == DB_FAILED) {
613 G_warning(_(
"Unable to copy table <%s>"), Fin->table);
620 if (driver ==
NULL) {
621 G_warning(_(
"Unable to open database <%s> by driver <%s>"),
622 Fin->database, Fin->driver);
626 G_warning(_(
"Unable to create index for table <%s>, key <%s>"),
627 Fin->table, Fin->key);
651 int field_out,
const char *field_name,
int type)
674 int field_in,
int field_out,
const char *field_name,
675 int type,
int *cats,
int ncats)
678 struct field_info *Fi, *Fin;
679 const char *
name, *key;
681 G_debug(2,
"Vect_copy_table(): field_in = %d field_out = %d", field_in,
686 G_warning(_(
"Database connection not defined for layer %d"),
691 if (field_name !=
NULL)
697 G_debug(3,
"Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
698 Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database,
703 Fin->database, Fin->driver);
705 G_warning(_(
"Unable to add database link for vector map <%s>"),
719 if (ret == DB_FAILED) {
720 G_warning(_(
"Unable to copy table <%s>"), Fin->table);
738 Map->plus.release_support = 1;
753 Map->plus.update_cidx = 1;
dbDriver * db_start_driver_open_database(const char *drvname, const char *dbname)
Open driver/database connection.
char * G_mapset(void)
current mapset name
int Vect_rename(const char *in, const char *out)
Rename a map.
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
int db_copy_table(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname)
Copy a table.
int Vect_copy_table_by_cats(struct Map_info *In, struct Map_info *Out, int field_in, int field_out, const char *field_name, int type, int *cats, int ncats)
Copy table linked to vector map based on category numbers.
struct field_info * Vect_get_field(struct Map_info *Map, int field)
Get information about link to database.
char * Vect_subst_var(const char *in, struct Map_info *Map)
Substitute variable in string.
int Vect_read_next_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
Read next vector feature (level 1 and 2)
int db_close_database_shutdown_driver(dbDriver *driver)
Close driver/database connection.
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
void Vect_set_category_index_update(struct Map_info *Map)
By default, category index is not updated if vector is changed, this function sets category index upd...
int Vect_level(struct Map_info *Map)
Returns level that Map is opened at.
char * G_tempfile(void)
Returns a temporary file name.
int G_rename(const char *element, const char *oldname, const char *newname)
Rename a database file.
char * G_find_vector2(const char *name, const char *mapset)
find a vector map (look but don't touch)
int Vect_map_add_dblink(struct Map_info *Map, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
Add new db connection to Map_info structure.
char * G__file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
char * G_location_path(void)
Get current location directory.
int Vect_set_open_level(int level)
Predetermine level at which a map will be opened for reading.
int db_copy_table_by_ints(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname, const char *selcol, int *ivals, int nvals)
Copy a table (by keys)
int db_delete_table(const char *drvname, const char *dbname, const char *tblname)
Delete table.
int Vect_get_num_dblinks(struct Map_info *map)
Get number of defined dblinks.
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 Vect_copy_tables(struct Map_info *In, struct Map_info *Out, int field)
Copy tables linked to vector map.
int Vect_legal_filename(const char *s)
Check if output is legal vector name.
int Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_delete(const char *map)
Delete vector map including attribute tables.
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_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead.
int db_create_index2(dbDriver *driver, const char *table_name, const char *column_name)
Create unique index.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int Vect_close(struct Map_info *Map)
Close vector data file.
struct field_info * Vect_get_dblink(struct Map_info *Map, int link)
Get information about link to database.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int Vect_get_num_lines(struct Map_info *map)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
int Vect_copy(const char *in, const char *mapset, const char *out)
Copy a map including attribute tables.
int db_table_exists(const char *drvname, const char *dbname, const char *tabname)
Check if table exists.
int G_debug(int level, const char *msg,...)
Print debugging message.
int G__make_mapset_element(const char *p_element)
Create element in the current mapset.
struct field_info * Vect_default_field_info(struct Map_info *Map, int field, const char *field_name, int type)
Get default information about link to database for new dblink.
long Vect_write_line(struct Map_info *Map, int type, struct line_pnts *points, struct line_cats *cats)
Writes new feature to the end of file (table)
int Vect_map_del_dblink(struct Map_info *Map, int field)
Delete db connection from Map_info structure.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void Vect_set_release_support(struct Map_info *Map)
Set spatial index to be realease when vector is closed.
int Vect_copy_map_lines(struct Map_info *In, struct Map_info *Out)
Copy all alive elements of opened vector map to another opened vector map.
int Vect_copy_table(struct Map_info *In, struct Map_info *Out, int field_in, int field_out, const char *field_name, int type)
Copy table linked to vector map based on type.
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.
int Vect_rewind(struct Map_info *Map)
Rewind vector data file to cause reads to start at beginning.