1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
/*
****************************************************************************
*
* MODULE: Vector library
*
* AUTHOR(S): Radim Blazek
*
* PURPOSE: Higher level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT: (C) 2001 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "glocale.h"
#include "gis.h"
#include "Vect.h"
#include "dbmi.h"
/*!
\fn int Vect_copy_map_lines ( struct Map_info *In, struct Map_info *Out )
\brief copy all alive elements of opened vector map to another opened vector map
\return 0 on success, 1 on error
\param in Map_info structure, out Map_info structure
*/
int
Vect_copy_map_lines ( struct Map_info *In, struct Map_info *Out )
{
int i, type, nlines, ret;
struct line_pnts *Points;
struct line_cats *Cats;
Points = Vect_new_line_struct ();
Cats = Vect_new_cats_struct ();
if ( Vect_level ( In ) < 1 )
G_fatal_error ("Vect_copy_map_lines(): input vector is not open");
ret = 0;
/* Note: sometimes is important to copy on level 2 (pseudotopo centroids)
* and sometimes on level 1 if build take too long time */
if ( Vect_level ( In ) >= 2 ) {
nlines = Vect_get_num_lines ( In );
for ( i = 1; i <= nlines; i++ ) {
type = Vect_read_line (In, Points, Cats, i);
if ( type == -1 ) {
G_warning ("Cannot read vector file\n" );
ret = 1;
break;
}
if ( type == 0 ) continue; /* dead line */
Vect_write_line ( Out, type, Points, Cats );
}
} else { /* Level 1 */
Vect_rewind ( In );
while ( 1 ) {
type = Vect_read_next_line (In, Points, Cats);
if ( type == -1 ) {
G_warning ("Cannot read vector file\n" );
ret = 1;
break;
} else if ( type == -2 ) { /* EOF */
break;
} else if ( type == 0 ) { /* dead line */
continue;
}
Vect_write_line ( Out, type, Points, Cats );
}
}
Vect_destroy_line_struct (Points);
Vect_destroy_cats_struct (Cats);
return ret;
}
/*!
\fn int Vect_copy ( char *in, char *mapset, char *out, FILE *msgout )
\brief copy a map including attribute tables
Old vector is deleted
\return -1 error, 0 success
\param in input vector
\param out output vector
\param msgout output file for messages or NULL
*/
int
Vect_copy ( char *in, char *mapset, char *out, FILE *msgout )
{
int i, n, ret, type;
struct Map_info In, Out;
struct field_info *Fi, *Fin;
char old_path[1000], new_path[1000], cmd[2000];
struct stat info;
dbDriver *driver;
G_debug (3, "Copy vector '%s' in '%s' to '%s'", in, mapset, out );
/* Delete old vector if it exists */
if ( G_find_vector2(out, G_mapset()) ) {
G_warning (_("The vector '%s' already exists and will be overwritten."), out);
ret = Vect_delete ( out );
if ( ret != 0 ) {
G_warning ( "Cannot copy vector" );
return -1;
}
}
/* Copy the directory */
G__make_mapset_element ( GRASS_VECT_DIRECTORY );
G__file_name (old_path, GRASS_VECT_DIRECTORY, in, mapset );
G__file_name (new_path, GRASS_VECT_DIRECTORY, out, G_mapset() );
sprintf ( cmd, "cp -r '%s' '%s'", old_path, new_path );
G_debug (2, "system: %s", cmd );
ret = system ( cmd );
if (ret != 0 ) {
G_warning ( "Cannot copy vector" );
return -1;
}
/* remove dbln */
sprintf (old_path, "%s/%s", GRASS_VECT_DIRECTORY, out);
G__file_name ( new_path, old_path, GRASS_VECT_DBLN_ELEMENT, G_mapset ());
if (stat (new_path, &info) == 0) /* file exists? */
unlink (new_path);
/* Open input */
Vect_set_open_level (1);
Vect_open_old_head (&In, in, mapset);
if ( In.format != GV_FORMAT_NATIVE ) { /* Done */
Vect_close ( &In );
return 0;
}
/* Open output */
Vect_open_update_head ( &Out, out, G_mapset() );
/* Copy tables */
n = Vect_get_num_dblinks ( &In );
type = GV_1TABLE;
if ( n > 1 ) type = GV_MTABLE;
for ( i = 0; i < n; i++ ) {
Fi = Vect_get_dblink ( &In, i );
if ( Fi == NULL ) {
G_warning ( "Cannot get db link info" );
Vect_close ( &In );
Vect_close ( &Out );
return -1;
}
Fin = Vect_default_field_info ( &Out, Fi->number, Fi->name, type );
G_debug (3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database, Fin->table );
Vect_map_add_dblink ( &Out, Fi->number, Fi->name, Fin->table, Fi->key, Fin->database, Fin->driver);
ret = db_copy_table ( Fi->driver, Fi->database, Fi->table,
Fin->driver, Vect_subst_var(Fin->database,&Out), Fin->table );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot copy table" );
Vect_close ( &In );
Vect_close ( &Out );
return -1;
}
driver = db_start_driver_open_database ( Fin->driver, Vect_subst_var(Fin->database,&Out) );
if ( driver == NULL ) {
G_warning ( "Cannot open database -> create index" );
} else {
if ( db_create_index2(driver, Fin->table, Fi->key ) != DB_OK )
G_warning ( "Cannot create index" );
db_close_database_shutdown_driver ( driver );
}
}
Vect_close ( &In );
Vect_close ( &Out );
return 0;
}
/*!
\fn int Vect_rename ( char *in, char *out, FILE *msgout )
\brief rename a map, attribute tables are created in the same database where input tables were stored.
The original format (native/OGR) is used.
Old map ('out') is deleted!!!
\return -1 error, 0 success
\param in input vector
\param out output vector
\param msgout output file for messages or NULL
*/
int
Vect_rename ( char *in, char *out, FILE *msgout )
{
int i, n, ret, type;
struct Map_info Map;
struct field_info *Fin, *Fout;
int *fields;
dbDriver *driver;
G_debug (2, "Rename vector '%s' to '%s'", in, out );
/* Delete old vector if it exists */
if ( G_find_vector2(out, G_mapset()) ) {
G_warning (_("The vector '%s' already exists and will be overwritten."), out);
Vect_delete ( out );
}
/* Move the directory */
ret = G_rename ( GRASS_VECT_DIRECTORY, in, out );
if ( ret == 0 ) {
G_warning (_("Input vector '%s' not found"), in );
return -1;
} else if ( ret == -1 ) {
G_warning (_("Cannot copy vector '%s' to '%s'"), in, out );
return -1;
}
/* Rename all tables if the format is native */
Vect_set_open_level (1);
Vect_open_update_head ( &Map, out, G_mapset() );
if ( Map.format != GV_FORMAT_NATIVE ) { /* Done */
Vect_close ( &Map );
return 0;
}
/* Copy tables */
n = Vect_get_num_dblinks ( &Map );
type = GV_1TABLE;
if ( n > 1 ) type = GV_MTABLE;
/* Make the list of fields */
fields = (int *) G_malloc ( n * sizeof(int) );
for ( i = 0; i < n; i++ ) {
Fin = Vect_get_dblink ( &Map, i );
fields[i] = Fin->number;
}
for ( i = 0; i < n; i++ ) {
G_debug (3, "field[%d] = %d", i, fields[i] );
Fin = Vect_get_field ( &Map, fields[i] );
if ( Fin == NULL ) {
G_warning ( "Cannot get db link info" );
Vect_close ( &Map );
return -1;
}
Fout = Vect_default_field_info ( &Map, Fin->number, Fin->name, type );
G_debug (3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
Fin->driver, Fin->database, Fin->table, Fout->driver, Fout->database, Fout->table );
/* TODO: db_rename_table instead of db_copy_table */
ret = db_copy_table ( Fin->driver, Fin->database, Fin->table,
Fout->driver, Vect_subst_var(Fout->database,&Map), Fout->table );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot copy table" );
Vect_close ( &Map );
return -1;
}
/* Change the link */
Vect_map_del_dblink ( &Map, Fin->number );
Vect_map_add_dblink ( &Map, Fout->number, Fout->name, Fout->table, Fin->key,
Fout->database, Fout->driver);
/* Delete old table */
ret = db_delete_table ( Fin->driver, Fin->database, Fin->table );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot delete table" );
Vect_close ( &Map );
return -1;
}
driver = db_start_driver_open_database ( Fout->driver, Vect_subst_var(Fout->database, &Map) );
if ( driver == NULL ) {
G_warning ( "Cannot open database -> create index" );
} else {
if ( db_create_index2(driver, Fout->table, Fin->key ) != DB_OK )
G_warning ( "Cannot create index" );
db_close_database_shutdown_driver ( driver );
}
}
Vect_close ( &Map );
free ( fields );
return 0;
}
/*!
\fn int Vect_delete ( char *map )
\brief delete a map including attribute tables
\return -1 error, 0 success
\param map name
*/
int
Vect_delete ( char *map )
{
int i, n, ret;
struct Map_info Map;
struct field_info *Fi;
char buf[5000];
DIR *dir;
struct dirent *ent;
G_debug (3, "Delete vector '%s'", map );
G_chop ( map );
if ( map == NULL || strlen ( map ) == 0 ) {
G_warning ( "Wrong map name '%s'", map );
return -1;
}
/* Open input */
Vect_set_open_level (1); /* Topo not needed */
ret = Vect_open_old_head (&Map, map, G_mapset());
if ( ret < 1 ) {
G_warning ( "Cannot open vector %s", map );
return -1;
}
/* Delete all tables, NOT external (OGR) */
if ( Map.format == GV_FORMAT_NATIVE ) {
n = Vect_get_num_dblinks ( &Map );
for ( i = 0; i < n; i++ ) {
Fi = Vect_get_dblink ( &Map, i );
if ( Fi == NULL ) {
G_warning ( "Cannot get db link info" );
Vect_close ( &Map );
return -1;
}
G_debug (3, "Delete drv:db:table '%s:%s:%s'", Fi->driver, Fi->database, Fi->table);
ret = db_table_exists ( Fi->driver, Fi->database, Fi->table );
if ( ret == -1 ) {
G_warning ( "Cannot get info if table '%s' linked to vector exists.", Fi->table );
Vect_close ( &Map );
return -1;
}
if ( ret == 1 ) {
ret = db_delete_table ( Fi->driver, Fi->database, Fi->table );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot delete table" );
Vect_close ( &Map );
return -1;
}
} else {
G_warning ( "Table '%s' linked to vector did not exist.", Fi->table );
}
}
}
Vect_close ( &Map );
/* Delete all files from vector/name directory */
sprintf ( buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map );
G_debug (3, "opendir '%s'", buf );
dir = opendir( buf );
if (dir == NULL) {
G_warning ( "Cannot open directory '%s'", buf );
return -1;
}
while ( (ent = readdir (dir)) ) {
G_debug (3, "file = '%s'", ent->d_name );
if ( (strcmp (ent->d_name, ".") == 0) || (strcmp (ent->d_name, "..") == 0) ) continue;
sprintf ( buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map, ent->d_name );
G_debug (3, "delete file '%s'", buf );
ret = unlink ( buf );
if ( ret == -1 ) {
G_warning ( "Cannot delete file '%s'", buf );
closedir (dir);
return -1;
}
}
closedir (dir);
/* NFS can create . files for those deleted -> second time */
sprintf ( buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map );
G_debug (3, "opendir '%s'", buf );
dir = opendir( buf );
if (dir == NULL) {
G_warning ( "Cannot open directory '%s'", buf );
return -1;
}
while ( (ent = readdir (dir)) ) {
G_debug (3, "file = '%s'", ent->d_name );
if ( (strcmp (ent->d_name, ".") == 0) || (strcmp (ent->d_name, "..") == 0) ) continue;
sprintf ( buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map, ent->d_name );
G_debug (3, "delete file '%s'", buf );
ret = unlink ( buf );
if ( ret == -1 ) {
G_warning ( "Cannot delete file '%s'", buf );
closedir (dir);
return -1;
}
}
closedir (dir);
sprintf ( buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map );
G_debug (3, "delete directory '%s'", buf );
ret = rmdir ( buf );
if ( ret == -1 ) {
G_warning ( "Cannot delete directory '%s'", buf );
return -1;
}
return 0;
}
/*!
\fn int Vect_copy_tables ( struct Map_info *In, struct Map_info *Out, int field )
\brief Copy map tables. All if field = 0, or table defined by given field if field > 0
\return 0 on success, -1 on error
\param in Map_info structure, out Map_info structure, field number
*/
int
Vect_copy_tables ( struct Map_info *In, struct Map_info *Out, int field )
{
int i, n, ret, type;
struct field_info *Fi, *Fin;
dbDriver *driver;
G_debug (2, "Vect_copy_tables()");
n = Vect_get_num_dblinks ( In );
type = GV_1TABLE;
if ( n > 1 ) type = GV_MTABLE;
for ( i = 0; i < n; i++ ) {
Fi = Vect_get_dblink ( In, i );
if ( Fi == NULL ) {
G_warning ( "Cannot get db link info" );
return -1;
}
if ( field > 0 && Fi->number != field ) continue;
Fin = Vect_default_field_info ( Out, Fi->number, Fi->name, type );
G_debug (2, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database, Fin->table );
ret = Vect_map_add_dblink ( Out, Fi->number, Fi->name, Fin->table, Fi->key, Fin->database, Fin->driver);
if ( ret == -1 ) {
G_warning ( "Cannot add database link" );
return -1;
}
ret = db_copy_table ( Fi->driver, Fi->database, Fi->table,
Fin->driver, Vect_subst_var(Fin->database,Out), Fin->table );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot copy table" );
return -1;
}
driver = db_start_driver_open_database ( Fin->driver, Vect_subst_var(Fin->database,Out) );
if ( driver == NULL ) {
G_warning ( "Cannot open database -> create index" );
} else {
if ( db_create_index2(driver, Fin->table, Fi->key ) != DB_OK )
G_warning ( "Cannot create index" );
db_close_database_shutdown_driver ( driver );
}
}
return 0;
}
/*!
\fn int Vect_copy_table ( struct Map_info *In, struct Map_info *Out, int field_in,
int field_out, char *field_name, int type )
\brief Copy map table.
\return 0 on success, -1 on error
\param In
\param Out
\param field_in
\param field_out
\param field_name
\param type
*/
int
Vect_copy_table ( struct Map_info *In, struct Map_info *Out, int field_in,
int field_out, char *field_name, int type )
{
return Vect_copy_table_by_cats ( In, Out, field_in, field_out, field_name, type, NULL, 0);
}
/*!
\fn int Vect_copy_table_by_cats ( struct Map_info *In, struct Map_info *Out, int field_in,
int field_out, char *field_name, int type, int *cats, int ncats )
\brief Copy map table.
\return 0 on success, -1 on error
\param In
\param Out
\param field_in
\param field_out
\param field_name
\param type
\param cats pointer to array of cats or NULL
\param ncats number of cats in 'cats'
*/
int
Vect_copy_table_by_cats ( struct Map_info *In, struct Map_info *Out, int field_in,
int field_out, char *field_name, int type, int *cats, int ncats )
{
int ret;
struct field_info *Fi, *Fin;
char *name, *key;
G_debug (2, "Vect_copy_table(): field_in = %d field_out = %d", field_in, field_out);
Fi = Vect_get_field ( In, field_in );
if ( Fi == NULL ) {
G_warning ( "Cannot get db link info" );
return -1;
}
if ( field_name != NULL ) name = field_name;
else name = Fi->name;
Fin = Vect_default_field_info ( Out, field_out, name, type );
G_debug (3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database, Fin->table );
ret = Vect_map_add_dblink ( Out, Fin->number, Fin->name, Fin->table, Fi->key, Fin->database, Fin->driver);
if ( ret == -1 ) {
G_warning ( "Cannot add database link" );
return -1;
}
if ( cats )
key = Fi->key;
else
key = NULL;
ret = db_copy_table_by_ints ( Fi->driver, Fi->database, Fi->table,
Fin->driver, Vect_subst_var(Fin->database,Out), Fin->table, key, cats, ncats );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot copy table" );
return -1;
}
return 0;
}
/*!
\brief Set spatial index to be realease when vector is closed, by default, the memory
occupied by spatial index is not released.
\param Map pointer to map
*/
void
Vect_set_release_support ( struct Map_info * Map )
{
Map->plus.release_support = 1;
}
/*!
\brief By default, category index is not updated if vector is changed,
this function sets category index update.
WARNING: currently only category for elements is updated
not for areas
\param Map pointer to map
*/
void
Vect_set_category_index_update ( struct Map_info * Map )
{
Map->plus.update_cidx = 1;
}
|