24 #include <sys/types.h>
30 #include "local_proto.h"
33 #include "pg_local_proto.h"
45 #define MAX_OPEN_LEVEL 2
58 #if !defined HAVE_OGR || !defined HAVE_POSTGRES
61 G_fatal_error(
_(
"Requested format is not compiled in this version"));
68 G_fatal_error(
_(
"Requested format is not compiled in this version"));
73 static int Open_level = 0;
75 static int (*Open_old_array[][2])(
struct Map_info *,
83 {open_old_dummy, format_old},
84 {open_old_dummy, format_old}
91 {open_old_dummy, format_old}
95 static int (*Open_new_array[][2])(
struct Map_info *Map,
const char *
name,
104 {open_new_dummy, format_new},
105 {open_new_dummy, format_new}
112 {open_new_dummy, format_new}
116 static int open_new(
struct Map_info *,
const char *,
int,
int);
117 static int map_format(
struct Map_info *);
142 G_warning(
_(
"Programmer requested unknown access level %d"),
167 const char *layer,
int update,
int head_only,
int is_tmp)
172 int level, level_request;
179 "Vect__open_old(): name = %s, mapset = %s, layer = %s, update = %d, "
180 "head_only = %d, is_tmp = %d",
183 if (update && !is_tmp) {
184 is_tmp = getenv(
"GRASS_VECTOR_TEMPORARY") ? TEMPORARY_MAP_ENV
185 : TEMPORARY_MAP_DISABLED;
187 "Vect__open_old(): is_tmp = %d (check GRASS_VECTOR_TEMPORARY)",
196 level_request = Open_level;
210 if (strcasecmp(xmapset,
"ogr") == 0) {
212 G_debug(1,
"OGR mapset detected");
240 if (fmapset ==
NULL) {
243 _(
"Vector map <%s> not found in current mapset"),
256 if (strcmp(Map->
mapset,
"") == 0)
260 if (strcmp(Map->
mapset,
"") == 0)
264 G_warning(
_(
"Temporary vector maps can be accessed only in "
265 "the current mapset"));
271 if (access(file_path, F_OK) != 0) {
279 if (access(file_path, F_OK) != 0)
288 if (update && !ogr_mapset && (0 != strcmp(Map->
mapset,
G_mapset()))) {
289 G_warning(
_(
"Vector map which is not in the current mapset cannot be "
290 "opened for update"));
294 G_debug(1,
"Map: name = %s, mapset = %s, temporary = %d", Map->
name,
333 G_debug(1,
"Level request = %d", level_request);
344 if (level_request == 0 || level_request > 1) {
360 G_debug(1,
"topo file for vector '%s' not available.",
364 else if (ret == -1) {
366 _(
"Unable to open topology file for vector map <%s>"),
375 G_debug(1,
"sidx file for vector '%s' not available.",
381 else if (ret == -1) {
383 _(
"Unable to open spatial index file for vector map <%s>"),
390 "Vector map <%s>: topology is %s, but spatial index is %s",
402 G_debug(1,
"cidx file for vector '%s' not available.",
410 else if (ret == -1) {
412 _(
"Unable to open category index file for vector map <%s>"),
435 if (level_request == 2 &&
level < 2) {
439 G_warning(
_(
"Unable to open vector map <%s> on level %d. "
440 "Try to rebuild vector topology with v.build."),
453 if (0 != (*Open_old_array[
format][1])(Map, update)) {
461 if (ogr_mapset && !
head_only && level_request != 1) {
466 G_message(
_(
"Building topology for OGR layer <%s> from datasource "
474 if (
level < level_request)
485 else if (
level > 1) {
510 G_debug(1,
"Vect__open_old(): vector opened on level %d",
level);
527 if (update && !ogr_mapset) {
530 G_warning(
_(
"Unable to open history file for vector map <%s>"),
535 Vect_hist_write(Map,
"-------------------------------------------------"
536 "--------------------------------\n");
559 if (access(file_path, F_OK) == 0)
563 if (access(file_path, F_OK) == 0)
567 if (access(file_path, F_OK) == 0)
572 if (access(file_path, F_OK) == 0)
715 const char *
mapset,
const char *layer)
766 const char *
mapset,
const char *layer)
788 int open_new(
struct Map_info *Map,
const char *
name,
int with_z,
int is_tmp)
793 G_debug(1,
"Vect_open_new(): name = %s with_z = %d is_tmp = %d",
name,
804 if (strcmp(xmapset,
G_mapset()) != 0) {
805 G_warning(
_(
"Unable to create vector map: <%s> is not in the "
806 "current mapset (%s)"),
816 _(
"Unable to create vector map: <%s> is not SQL compliant"),
name);
828 Map->
format = map_format(Map);
831 getenv(
"GRASS_VECTOR_PGFILE") ==
836 G_debug(2,
" using non-direct format");
848 env = getenv(
"GRASS_VECTOR_TEMPORARY");
849 if (!Map->
temporary || (env && strcmp(env,
"move") == 0)) {
852 _(
"Vector map <%s> already exists and will be overwritten"),
877 G_warning(
_(
"Unable to open history file of vector map <%s>"),
889 if ((*Open_new_array[Map->
format][1])(Map,
name, with_z) < 0) {
890 if (getenv(
"GRASS_VECTOR_PGFILE") ==
905 _(
"Unable to open spatial index file for vector map <%s>"),
959 is_tmp = getenv(
"GRASS_VECTOR_TEMPORARY") ? TEMPORARY_MAP_ENV
960 : TEMPORARY_MAP_DISABLED;
961 G_debug(1,
"Vect_open_new(): is_tmp = %d", is_tmp);
963 return open_new(Map,
name, with_z, is_tmp);
991 sprintf(tmp_name,
"tmp_%d", getpid());
994 sprintf(tmp_name,
"%s",
name);
996 G_debug(1,
"Vect_open_tmp_new(): name = '%s' with_z = %d",
name, with_z);
998 return open_new(Map, tmp_name, with_z, TEMPORARY_MAP);
1013 struct stat stat_buf;
1018 G_debug(1,
"get coor info: %s", file_path);
1019 if (0 != stat(file_path, &stat_buf)) {
1020 G_warning(
_(
"Unable to stat file <%s>"), file_path);
1025 Info->
size = (off_t)stat_buf.st_size;
1026 Info->
mtime = (
long)stat_buf.st_mtime;
1046 G_debug(1,
"Vect_coor_info(): Info->size = %lu, Info->mtime = %ld",
1047 (
unsigned long)Info->
size, Info->
mtime);
1074 sprintf(maptype,
"native");
1078 sprintf(maptype,
"OGR");
1081 sprintf(maptype,
"PostGIS");
1084 sprintf(maptype,
_(
"unknown %d (update Vect_maptype_info)"),
1109 finfo = &(Map->
fInfo);
1139 G_debug(1,
"Vect_open_topo(): name = %s mapset = %s", Map->
name,
1142 Plus = &(Map->
plus);
1146 if (access(file_path, F_OK) != 0) {
1154 G_debug(1,
"Cannot open topo file for vector '%s@%s'.", Map->
name,
1167 G_debug(1,
"Topo head: coor size = %lu, coor mtime = %ld",
1174 _(
"Size of 'coor' file differs from value saved in topology file"));
1185 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1199 return ret == 0 ? -1 : 0;
1218 G_debug(1,
"Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->
name,
1219 Map->
mapset,
mode == 0 ?
"old" : (
mode == 1 ?
"update" :
"new"));
1221 Plus = &(Map->
plus);
1224 G_debug(1,
"Spatial index already opened");
1235 if (access(file_path, F_OK) != 0)
1241 G_debug(1,
"Cannot open spatial index file for vector '%s@%s'.",
1267 G_debug(1,
"Sidx head: coor size = %lu, coor mtime = %ld",
1274 _(
"Size of 'coor' file differs from value saved in sidx file"));
1285 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1297 if (getenv(
"GRASS_VECTOR_LOWMEM")) {
1304 G_debug(1,
"%s based spatial index",
1322 int map_format(
struct Map_info *Map)
1329 if (Map->
temporary || getenv(
"GRASS_VECTOR_EXTERNAL_IGNORE"))
1340 G_debug(2,
" using OGR format");
1341 if (getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1376 def_file = getenv(
"GRASS_VECTOR_PGFILE");
1381 G_warning(
_(
"OGR output also detected, using OGR"));
1390 G_debug(2,
" using PostGIS format");
1419 #ifdef HAVE_POSTGRES
1429 #ifdef HAVE_POSTGRES
1437 pg_info->
srid = atoi(p);
1444 pg_info->
srid = atoi(p);
1462 G_debug(1,
"PG: topology = yes, schema_name = %s",
1465 G_debug(1,
"PG: topology = no");
1467 if (getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1481 G_debug(2,
"map_format = %d", format);
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
const char * G_database_epsg_code(void)
Get EPGS code for the current location.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
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)
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.
const char * G_gisdbase(void)
Get name of top level database directory.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
void G__temp_element(char *, int)
Populates element with a path string (internal use only!)
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
void void G_verbose_message(const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
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 * G_fopen_modify(const char *, const char *)
Open a database file for update (r+ mode)
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
int G_verbose(void)
Get current verbosity level.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int G_zone(void)
Query cartographic zone.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
int G_set_verbose(int)
Set verbosity level.
const char * G_location(void)
Get current location name.
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
char ** G_tokenize(const char *, const char *)
Tokenize string.
int G_projection(void)
Query cartographic projection.
int V1_open_old_ogr(struct Map_info *, int)
Open existing OGR layer on non-topological level.
int Vect_cidx_open(struct Map_info *, int)
Read category index from cidx file if exists.
int Vect_set_proj(struct Map_info *, int)
Set projection in map header.
int V1_open_new_pg(struct Map_info *, const char *, int)
int Vect_read_dblinks(struct Map_info *)
Read dblinks to existing structure.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
int Vect_set_zone(struct Map_info *, int)
Set projection zone in map header.
int V1_open_new_nat(struct Map_info *, const char *, int)
Create new vector map (level 1)
int V1_open_old_nat(struct Map_info *, int)
Open existing vector map (level 1)
int V2_open_old_pg(struct Map_info *)
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
int Vect_get_zone(struct Map_info *)
int Vect_delete(const char *)
Delete vector map including attribute tables.
int Vect_legal_filename(const char *)
Check if output is legal vector name.
int Vect__read_head(struct Map_info *)
Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only.
int Vect_build(struct Map_info *)
Build topology for vector map.
void Vect__init_head(struct Map_info *)
Initialize Map_info head structure (dig_head)
int Vect_rewind(struct Map_info *)
Rewind vector map to cause reads to start at beginning.
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_name(struct Map_info *)
Get name of vector map.
int Vect__write_head(struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
int Vect_hist_write(struct Map_info *, const char *)
Write string to history file.
int V2_open_old_ogr(struct Map_info *)
Open existing OGR layer on topological level.
int V1_open_old_pg(struct Map_info *, int)
#define GV_FRMT_ELEMENT
Format description, data location (OGR)
#define GV_BUILD_NONE
Topology levels - nothing to build.
#define VECT_OPEN_CODE
Vector map open code.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_DIRECTORY
Name of vector directory.
#define GV_SIDX_ELEMENT
Native format, spatial index.
#define GV_MODE_READ
Read-only vector map open mode.
#define LEVEL_1
Vector level - without topology.
#define GV_BUILD_ALL
Topology levels - build everything (currently same as GV_BUILD_CENTROIDS)
#define GV_COOR_ELEMENT
Native format, coordinates.
#define GV_FIDX_ELEMENT
External format (OGR), feature index.
#define GV_COOR_HEAD_SIZE
Coordinates file head size.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
#define WITHOUT_Z
2D/3D vector data
#define GV_CIDX_ELEMENT
Native format, category index.
#define GV_TOPO_ELEMENT
Native format, topology file.
#define GV_PG_GEOMETRY_COLUMN
GRASS-PostGIS data provider - default geometry column.
#define GV_HEAD_ELEMENT
Native format, header information.
#define GV_FORMAT_OGR
OGR format.
#define GV_MODE_RW
Read-write vector map open mode.
#define GV_PG_FID_COLUMN
GRASS-PostGIS data provider - default fid column.
#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.
int dig_load_plus(struct Plus_head *, struct gvfile *, int)
Reads topo file to topo structure.
void dig_free_plus(struct Plus_head *)
Free Plus structure.
int dig_init_plus(struct Plus_head *)
Initialize Plus_head structure.
int dig_read_frmt_ascii(FILE *, struct Format_info *)
Read external vector format file.
off_t dig_ftell(struct gvfile *file)
Get struct gvfile position.
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.
int dig_Rd_Plus_head(struct gvfile *, struct Plus_head *)
Read Plus_head from file.
int dig_fseek(struct gvfile *file, off_t offset, int whence)
Set struct gvfile position.
int dig_Rd_spidx_head(struct gvfile *, struct Plus_head *)
Read spatial index header from sidx file.
void dig_spidx_free(struct Plus_head *)
Free spatial index (nodes, lines, areas, isles)
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
int dig_spidx_init(struct Plus_head *)
Initit spatial index (nodes, lines, areas, isles)
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
int Vect__delete(const char *map, int is_tmp)
Delete vector map (internal use only)
int Vect__open_topo_pg(struct Map_info *Map NOPG_UNUSED, int head_only NOPG_UNUSED, int update NOPG_UNUSED)
Read full-topology for PostGIS links.
off_t size
Total size (in bytes)
long mtime
Time of last modification.
char * mapset
Mapset name.
int temporary
Temporary map flag.
struct gvfile dig_fp
GV file pointer (native format only)
struct dig_head head
Header info.
int support_updated
Support files were updated.
char * gisdbase
GISDBASE path.
char * name
Map name (for 4.0)
FILE * hist_fp
History file.
char * location
Location name.
int head_only
Open only header.
int format
Map format (native, ogr, postgis)
struct dblinks * dblnk
Array of DB links.
struct Format_info fInfo
Format info for non-native formats.
struct Plus_head plus
Plus info (topology, version, ...)
Basic topology-related info.
int do_uplist
Indicates if the list of updated features is maintained.
struct gvfile spidx_fp
Spatial index file pointer.
int Spidx_built
Spatial index built?
int with_z
2D/3D vector data
off_t coor_size
Size of coor file.
int Spidx_new
Build new spatial index.
int Spidx_file
Build new spatial index in file.
int spidx_with_z
2D/3D spatial index
struct Plus_head::@10 uplist
List of updated lines/nodes.
long coor_mtime
Time of last coor modification.
int built
Highest level of topology currently available.
off_t size
Coor file size.
int with_z
2D/3D vector data
long head_size
Coor header size.
FILE * file
File descriptor.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
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)
const char * Vect_maptype_info(struct Map_info *Map)
Gets vector map format (as string)
int Vect_open_tmp_new(struct Map_info *Map, const char *name, int with_z)
Create new temporary vector map.
int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector map for reading.
int Vect_set_open_level(int level)
Predetermine level at which a vector map will be opened for reading.
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, struct Map_info *Map)
Get map directory name (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)
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_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
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 Vect_coor_info(struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
int Vect_open_topo(struct Map_info *Map, int head_only)
Open topology file ('topo')
int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
Create new vector map for reading/writing.
int Vect_open_sidx(struct Map_info *Map, int mode)
Open spatial index file ('sidx')
int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
Reads only info about vector map (headers)
int Vect_maptype(struct Map_info *Map)
Gets vector map format.
int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
Open existing vector map for reading/writing.
int Vect_open_tmp_old(struct Map_info *Map, const char *name, const char *mapset)
Open existing temporary vector map for reading.
int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
Open existing vector map for reading/writing.