23 #include <sys/types.h> 29 #include "local_proto.h" 32 #include "pg_local_proto.h" 44 #define MAX_OPEN_LEVEL 2 46 static int open_old_dummy()
51 static int open_new_dummy()
56 #if !defined HAVE_OGR || !defined HAVE_POSTGRES 59 G_fatal_error(
_(
"Requested format is not compiled in this version"));
64 static int Open_level = 0;
66 static int (*Open_old_array[][2]) () = {
73 open_old_dummy, V1_open_old_ogr}
76 open_old_dummy, format}
78 open_old_dummy, format}
85 open_old_dummy, format}
89 static int (*Open_new_array[][2]) () = {
96 open_new_dummy, V1_open_new_ogr}
99 open_new_dummy, format}
101 open_new_dummy, format}
108 open_old_dummy, format}
112 static int open_new(
struct Map_info *,
const char *,
int,
int);
113 static int map_format(
struct Map_info *);
138 G_warning(
_(
"Programmer requested unknown access level %d"),
163 const char *layer,
int update,
int head_only,
int is_tmp)
168 int level, level_request;
173 G_debug(1,
"Vect__open_old(): name = %s, mapset = %s, layer = %s, update = %d, " 174 "head_only = %d, is_tmp = %d", name, mapset, layer ? layer :
"NULL", update, head_only,
177 if (update && !is_tmp) {
178 is_tmp =
getenv(
"GRASS_VECTOR_TEMPORARY") ? TEMPORARY_MAP_ENV : TEMPORARY_MAP_DISABLED;
179 G_debug(1,
"Vect__open_old(): is_tmp = %d (check GRASS_VECTOR_TEMPORARY)", is_tmp);
187 level_request = Open_level;
201 if (strcasecmp(xmapset,
"ogr") == 0) {
203 G_debug(1,
"OGR mapset detected");
230 if (fmapset ==
NULL) {
231 if (mapset && strcmp(mapset,
G_mapset()) == 0)
244 if(strcmp(Map->
mapset,
"") == 0)
248 if (strcmp(Map->
mapset,
"") == 0)
252 G_warning(
_(
"Temporary vector maps can be accessed only in the current mapset"));
258 if (access(file_path, F_OK) != 0) {
266 if (access(file_path, F_OK) != 0)
276 if (update && !ogr_mapset && (0 != strcmp(Map->
mapset,
G_mapset()))) {
277 G_warning(
_(
"Vector map which is not in the current mapset cannot be opened for update"));
281 G_debug(1,
"Map: name = %s, mapset = %s, temporary = %d", Map->
name, Map->
mapset,
292 G_debug(1,
"Vector format: %d (native)", format);
299 G_debug(1,
"Vector format: %d (non-native)", format);
320 G_debug(1,
"Level request = %d", level_request);
331 if (level_request == 0 || level_request > 1) {
347 G_debug(1,
"topo file for vector '%s' not available.",
351 else if (ret == -1) {
352 G_fatal_error(
_(
"Unable to open topology file for vector map <%s>"),
361 G_debug(1,
"sidx file for vector '%s' not available.",
366 else if (ret == -1) {
367 G_fatal_error(
_(
"Unable to open spatial index file for vector map <%s>"),
373 G_warning(
"Vector map <%s>: topology is %s, but spatial index is %s",
385 "cidx file for vector '%s' not available.",
392 else if (ret == -1) {
393 G_fatal_error(
_(
"Unable to open category index file for vector map <%s>"),
416 if (level_request == 2 && level < 2) {
419 G_warning(
_(
"Unable to open vector map <%s> on level %d. " 420 "Try to rebuild vector topology with v.build."),
433 if (0 != (*Open_old_array[format][1]) (Map, update)) {
441 if (ogr_mapset && !head_only && level_request != 1) {
445 G_message(
_(
"Building topology for OGR layer <%s> from datasource '%s'..."),
452 if (level < level_request)
456 if (level < 2 && Map->head.with_z) {
463 else if (level > 1) {
488 G_debug(1,
"Vect__open_old(): vector opened on level %d", level);
504 if (update && !ogr_mapset) {
507 G_warning(
_(
"Unable to open history file for vector map <%s>"),
513 "---------------------------------------------------------------------------------\n");
532 if (update && !head_only) {
536 if (access(file_path, F_OK) == 0)
540 if (access(file_path, F_OK) == 0)
544 if (access(file_path, F_OK) == 0)
549 if (access(file_path, F_OK) == 0)
760 int open_new(
struct Map_info *Map,
const char *
name,
int with_z,
int is_tmp)
765 G_debug(1,
"Vect_open_new(): name = %s with_z = %d is_tmp = %d",
766 name, with_z, is_tmp);
776 if (strcmp(xmapset,
G_mapset()) != 0) {
777 G_warning(
_(
"Unable to create vector map: <%s> is not in the current mapset (%s)"),
786 G_fatal_error(
_(
"Unable to create vector map: <%s> is not SQL compliant"),
799 Map->
format = map_format(Map);
806 G_debug(2,
" using non-direct format");
818 env =
getenv(
"GRASS_VECTOR_TEMPORARY");
819 if (!Map->
temporary || (env && strcmp(env,
"move") == 0)) {
821 G_warning(
_(
"Vector map <%s> already exists and will be overwritten"),
846 G_warning(
_(
"Unable to open history file of vector map <%s>"),
857 if ((*Open_new_array[Map->
format][1]) (Map,
name, with_z) < 0) {
871 G_fatal_error(
_(
"Unable to open spatial index file for vector map <%s>"),
925 is_tmp =
getenv(
"GRASS_VECTOR_TEMPORARY") ? TEMPORARY_MAP_ENV : TEMPORARY_MAP_DISABLED;
926 G_debug(1,
"Vect_open_new(): is_tmp = %d", is_tmp);
928 return open_new(Map, name, with_z, is_tmp);
956 sprintf(tmp_name,
"tmp_%d", getpid());
959 sprintf(tmp_name,
"%s", name);
961 G_debug(1,
"Vect_open_tmp_new(): name = '%s' with_z = %d", name, with_z);
963 return open_new(Map, tmp_name, with_z, TEMPORARY_MAP);
978 struct stat stat_buf;
983 G_debug(1,
"get coor info: %s", file_path);
984 if (0 != stat(file_path, &stat_buf)) {
985 G_warning(
_(
"Unable to stat file <%s>"), file_path);
990 Info->
size = (off_t)stat_buf.st_size;
991 Info->
mtime = (
long)stat_buf.st_mtime;
1011 G_debug(1,
"Vect_coor_info(): Info->size = %lu, Info->mtime = %ld",
1012 (
unsigned long)Info->
size, Info->
mtime);
1039 sprintf(maptype,
"native");
1043 sprintf(maptype,
"OGR");
1046 sprintf(maptype,
"PostGIS");
1049 sprintf(maptype,
_(
"unknown %d (update Vect_maptype_info)"),
1074 finfo = &(Map->
fInfo);
1104 G_debug(1,
"Vect_open_topo(): name = %s mapset = %s", Map->
name,
1107 Plus = &(Map->
plus);
1111 if (access(file_path, F_OK) != 0) {
1119 G_debug(1,
"Cannot open topo file for vector '%s@%s'.",
1132 G_debug(1,
"Topo head: coor size = %lu, coor mtime = %ld",
1138 G_warning(
_(
"Size of 'coor' file differs from value saved in topology file"));
1149 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1163 return ret == 0 ? -1 : 0;
1182 G_debug(1,
"Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->
name,
1183 Map->
mapset, mode == 0 ?
"old" : (mode == 1 ?
"update" :
"new"));
1185 Plus = &(Map->
plus);
1188 G_debug(1,
"Spatial index already opened");
1199 if (access(file_path, F_OK) != 0)
1205 G_debug(1,
"Cannot open spatial index file for vector '%s@%s'.",
1231 G_debug(1,
"Sidx head: coor size = %lu, coor mtime = %ld",
1237 G_warning(
_(
"Size of 'coor' file differs from value saved in sidx file"));
1248 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1260 if (
getenv(
"GRASS_VECTOR_LOWMEM")) {
1267 G_debug(1,
"%s based spatial index",
1285 int map_format(
struct Map_info *Map)
1303 G_debug(2,
" using OGR format");
1304 if (
getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1339 def_file =
getenv(
"GRASS_VECTOR_PGFILE");
1343 G_warning(
_(
"OGR output also detected, using OGR"));
1352 G_debug(2,
" using PostGIS format");
1381 #ifdef HAVE_POSTGRES 1391 #ifdef HAVE_POSTGRES 1399 pg_info->
srid = atoi(p);
1406 pg_info->
srid = atoi(p);
1424 G_debug(1,
"PG: topology = yes, schema_name = %s",
1427 G_debug(1,
"PG: topology = no");
1429 if (
getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1443 G_debug(2,
"map_format = %d", format);
int V2_open_old_pg(struct Map_info *)
Open vector map - PostGIS feature table on topological level.
int V1_open_old_nat(struct Map_info *, int)
Open existing vector map (level 1)
int Spidx_new
Build new spatial index.
const char * G_find_file2(const char *, const char *, const char *)
Searches for a file from the mapset search list or in a specified mapset. (look but don't touch) ...
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
char * name
Map name (for 4.0)
void Vect__init_head(struct Map_info *)
Initialize Map_info head structure (dig_head)
int dig_read_frmt_ascii(FILE *, struct Format_info *)
Read external vector format file.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
off_t coor_size
Size of coor file.
int built
Highest level of topology currently available.
int G_zone(void)
Query cartographic zone.
int G_verbose(void)
Get current verbosity level.
int Vect_delete(const char *)
Delete vector map including attribute tables.
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
#define GV_DIRECTORY
Name of vector directory.
#define GV_PG_FID_COLUMN
GRASS-PostGIS data provider - default fid column.
#define GV_FRMT_ELEMENT
Format description, data location (OGR)
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)
off_t size
Total size (in bytes)
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
int spidx_with_z
2D/3D spatial index
const char * G_gisdbase(void)
Get name of top level database directory.
int V2_open_old_ogr(struct Map_info *)
Open existing OGR layer on topological level.
int with_z
2D/3D vector data
int dig_Rd_spidx_head(struct gvfile *, struct Plus_head *)
Read spatial index header from sidx file.
int G_projection(void)
Query cartographic projection.
int Vect_open_tmp_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing temporary vector map for reading.
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
#define GV_PG_GEOMETRY_COLUMN
GRASS-PostGIS data provider - default geometry column.
#define GV_FORMAT_OGR
OGR format.
FILE * G_fopen_modify(const char *, const char *)
Open a database file for update (r+ mode)
char * location
Location name.
#define GV_TOPO_ELEMENT
Native format, topology file.
#define GV_HEAD_ELEMENT
Native format, header information.
struct Format_info fInfo
Format info for non-native formats.
long mtime
Time of last modification.
char ** G_tokenize(const char *, const char *)
Tokenize string.
int do_uplist
Indicates if the list of updated features is maintained.
int dig_load_plus(struct Plus_head *, struct gvfile *, int)
Reads topo file to topo structure.
int V1_open_new_nat(struct Map_info *, const char *, int)
Create new vector map (level 1)
int Vect__delete(const char *map, int is_tmp)
Delete vector map (internal use only)
const char * G_database_epsg_code(void)
Get EPGS code for the current location.
void dig_free_plus(struct Plus_head *)
Free Plus structure.
int Spidx_built
Spatial index built?
off_t dig_ftell(struct gvfile *file)
Get struct gvfile position.
#define GV_BUILD_NONE
Topology levels - nothing to build.
#define LEVEL_1
Vector level - without topology.
#define GV_MODE_RW
Read-write vector map open mode.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
int Vect_get_zone(const struct Map_info *)
Get projection zone from map header.
void dig_spidx_free(struct Plus_head *)
Free spatial index (nodes, lines, areas, isles)
void G_message(const char *,...) __attribute__((format(printf
int Vect_read_dblinks(struct Map_info *)
Read dblinks to existing structure.
int Vect_set_proj(struct Map_info *, int)
Set projection in map header.
const char * Vect_maptype_info(const struct Map_info *Map)
Gets vector map format (as string)
int with_z
2D/3D vector data
void G__temp_element(char *, int)
Populates element with a path string (internal use only!)
struct gvfile spidx_fp
Spatial index file pointer.
const char * G_location(void)
Get current location name.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Basic topology-related info.
#define GV_CIDX_ELEMENT
Native format, category index.
int Vect_open_topo(struct Map_info *Map, int head_only)
Open topology file ('topo')
int Vect_set_open_level(int level)
Predetermine level at which a vector map will be opened for reading.
int Vect_open_sidx(struct Map_info *Map, int mode)
Open spatial index file ('sidx')
int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector map for reading/writing.
int Vect_build(struct Map_info *)
Build topology for vector map.
int Spidx_file
Build new spatial index in file.
#define GV_HIST_ELEMENT
Native format, history file.
int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
Reads only info about vector map (headers)
int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
int V1_open_old_pg(struct Map_info *, int)
Open vector map - PostGIS feature table on non-topological level.
struct Plus_head plus
Plus info (topology, version, ...)
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
void dig_file_init(struct gvfile *file)
Initialize gvfile strcuture.
int Vect_open_tmp_new(struct Map_info *Map, const char *name, int with_z)
Create new temporary vector map.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
int dig_Rd_Plus_head(struct gvfile *, struct Plus_head *)
Read Plus_head from file.
int Vect_legal_filename(const char *)
Check if output is legal vector name.
int Vect_rewind(struct Map_info *)
Rewind vector map to cause reads to start at beginning.
char * mapset
Mapset name.
#define WITHOUT_Z
2D/3D vector data
struct dig_head head
Header info.
int head_only
Open only header.
int Vect_open_update_head(struct Map_info *Map, const char *name, const char *mapset)
Open header file of existing vector map for updating (mostly for database link updates) ...
int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector map for reading.
int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
Reads only info about vector map (headers)
int support_updated
Support files were updated.
off_t size
Coor file size.
long coor_mtime
Time of last coor modification.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_COOR_HEAD_SIZE
Coordinates file head size.
int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
Open existing vector map for reading/writing.
struct dblinks * dblnk
Array of DB links.
int Vect_hist_write(struct Map_info *, const char *)
Write string to history file.
char * G_file_name_tmp(char *, const char *, const char *, const char *)
Builds full path names to GIS data files in temporary directory (for internal use only) ...
FILE * file
File descriptor.
const char * G_mapset(void)
Get current mapset name.
int temporary
Temporary map flag.
int V1_open_new_ogr(struct Map_info *, const char *, int)
Prepare OGR datasource for creating new OGR layer (level 1)
const char * Vect_get_full_name(const struct Map_info *)
Get fully qualified name of vector map.
int G_set_verbose(int)
Set verbosity level.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
int dig_spidx_init(struct Plus_head *)
Initit spatial index (nodes, lines, areas, isles)
void G_warning(const char *,...) __attribute__((format(printf
int Vect_maptype(const struct Map_info *Map)
Gets vector map format.
struct gvfile dig_fp
GV file pointer (native format only)
int format
Map format (native, ogr, postgis)
char * G_store(const char *)
Copy string to allocated memory.
#define GV_BUILD_ALL
Topology levels - build everything (currently same as GV_BUILD_CENTROIDS)
int Vect_open_tmp_update(struct Map_info *Map, const char *name, const char *mapset)
Open existing temporary vector map for reading/writing.
char * Vect__get_path(char *path, const struct Map_info *Map)
Get map directory name (internal use only)
struct Plus_head::@10 uplist
List of updated lines/nodes.
#define GV_FIDX_ELEMENT
External format (OGR), feature index.
int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
Create new vector map for reading/writing.
#define GV_SIDX_ELEMENT
Native format, spatial index.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int V1_open_new_pg(struct Map_info *, const char *, int)
Prepare PostGIS database for creating new feature table (level 1)
int Vect__write_head(const struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
#define GV_MODE_READ
Read-only vector map open mode.
int V1_open_old_ogr(struct Map_info *, int)
Open existing OGR layer on non-topological level.
int dig_Rd_spidx(struct gvfile *, struct Plus_head *)
Read spatial index from sidx file Only needed when old vector is opened in update mode...
#define GV_COOR_ELEMENT
Native format, coordinates.
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
void void G_verbose_message(const char *,...) __attribute__((format(printf
const char * Vect_get_name(const struct Map_info *)
Get name of vector map.
long head_size
Coor header size.
int G_debug(int, const char *,...) __attribute__((format(printf
#define VECT_OPEN_CODE
Vector map open code.
int Vect_set_zone(struct Map_info *, int)
Set projection zone in map header.
int dig_fseek(struct gvfile *file, off_t offset, int whence)
Set struct gvfile position.
int dig_init_plus(struct Plus_head *)
Initialize Plus_head structure.
char * gisdbase
GISDBASE path.
int Vect__open_topo_pg(struct Map_info *Map, int head_only, int update)
Read full-topology for PostGIS links.
int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
Open existing vector map for reading.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
FILE * hist_fp
History file.
int Vect__read_head(struct Map_info *)
Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only.
int Vect_cidx_open(struct Map_info *, int)
Read category index from cidx file if exists.