30#include "local_proto.h"
33#include "pg_local_proto.h"
45#define MAX_OPEN_LEVEL 2
58#if !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"));
73static int Open_level = 0;
102static int open_new(
struct Map_info *,
const char *,
int,
int);
103static int map_format(
struct Map_info *);
128 G_warning(
_(
"Programmer requested unknown access level %d"),
165 "Vect__open_old(): name = %s, mapset = %s, layer = %s, update = %d, "
166 "head_only = %d, is_tmp = %d",
173 "Vect__open_old(): is_tmp = %d (check GRASS_VECTOR_TEMPORARY)",
198 G_debug(1,
"OGR mapset detected");
202 Map->fInfo.ogr.layer_name =
224 if (!
Map->temporary) {
229 _(
"Vector map <%s> not found in current mapset"),
250 G_warning(
_(
"Temporary vector maps can be accessed only in "
251 "the current mapset"));
275 G_warning(
_(
"Vector map which is not in the current mapset cannot be "
276 "opened for update"));
280 G_debug(1,
"Map: name = %s, mapset = %s, temporary = %d",
Map->
name,
281 Map->mapset,
Map->temporary);
346 G_debug(1,
"topo file for vector '%s' not available.",
350 else if (
ret == -1) {
352 _(
"Unable to open topology file for vector map <%s>"),
361 G_debug(1,
"sidx file for vector '%s' not available.",
367 else if (
ret == -1) {
369 _(
"Unable to open spatial index file for vector map <%s>"),
373 if ((
Map->plus.with_z != 0 &&
Map->plus.spidx_with_z == 0) ||
374 (
Map->plus.with_z == 0 &&
Map->plus.spidx_with_z != 0)) {
376 "Vector map <%s>: topology is %s, but spatial index is %s",
378 (
Map->plus.with_z != 0 ?
"3D" :
"2D"),
379 (
Map->plus.spidx_with_z != 0 ?
"3D" :
"2D"));
388 G_debug(1,
"cidx file for vector '%s' not available.",
396 else if (
ret == -1) {
398 _(
"Unable to open category index file for vector map <%s>"),
423 G_warning(
_(
"Unable to open vector map <%s> on level %d. "
424 "Try to rebuild vector topology with v.build."),
437 if (0 != (*Open_old_array[
format][1])(
Map, update)) {
450 G_message(
_(
"Building topology for OGR layer <%s> from datasource "
452 Map->fInfo.ogr.layer_name,
Map->fInfo.ogr.dsn);
466 Map->plus.with_z =
Map->head.with_z;
469 else if (
level > 1) {
471 Map->head.with_z =
Map->plus.with_z;
494 G_debug(1,
"Vect__open_old(): vector opened on level %d",
level);
514 G_warning(
_(
"Unable to open history file for vector map <%s>"),
520 "--------------------------------\n");
699 const char *
mapset,
const char *layer)
749 const char *
mapset,
const char *layer)
776 G_debug(1,
"Vect_open_new(): name = %s with_z = %d is_tmp = %d",
name,
788 G_warning(
_(
"Unable to create vector map: <%s> is not in the "
789 "current mapset (%s)"),
799 _(
"Unable to create vector map: <%s> is not SQL compliant"),
name);
811 Map->format = map_format(
Map);
814 getenv(
"GRASS_VECTOR_PGFILE") ==
819 G_debug(2,
" using non-direct format");
824 if (
Map->temporary) {
831 env =
getenv(
"GRASS_VECTOR_TEMPORARY");
832 if (!
Map->temporary || (env &&
strcmp(env,
"move") == 0)) {
835 _(
"Vector map <%s> already exists and will be overwritten"),
860 G_warning(
_(
"Unable to open history file of vector map <%s>"),
867 Map->plus.spidx_with_z =
Map->plus.with_z =
Map->head.with_z =
872 if ((*Open_new_array[
Map->format][1])(
Map,
name, with_z) < 0) {
873 if (
getenv(
"GRASS_VECTOR_PGFILE") ==
882 Map->plus.Spidx_file = 0;
888 _(
"Unable to open spatial index file for vector map <%s>"),
903 if (
Map->fInfo.ogr.driver_name) {
906 else if (
Map->fInfo.pg.conninfo) {
907 if (
Map->fInfo.pg.toposchema_name)
979 G_debug(1,
"Vect_open_tmp_new(): name = '%s' with_z = %d",
name, with_z);
998 switch (
Map->format) {
1029 G_debug(1,
"Vect_coor_info(): Info->size = %lu, Info->mtime = %ld",
1030 (
unsigned long)
Info->size,
Info->mtime);
1055 switch (
Map->format) {
1068 _(
"unknown %d (update Vect_maptype_info)"),
Map->format);
1089 if (
Map->temporary) {
1093 if (
finfo->ogr.driver_name) {
1096 if (
finfo->pg.conninfo) {
1137 G_debug(1,
"Cannot open topo file for vector '%s@%s'.",
Map->
name,
1150 G_debug(1,
"Topo head: coor size = %lu, coor mtime = %ld",
1151 (
unsigned long)
Plus->coor_size,
Plus->coor_mtime);
1157 _(
"Size of 'coor' file differs from value saved in topology file"));
1168 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1182 return ret == 0 ? -1 : 0;
1201 G_debug(1,
"Vect_open_sidx(): name = %s mapset= %s mode = %s",
Map->
name,
1202 Map->mapset,
mode == 0 ?
"old" : (
mode == 1 ?
"update" :
"new"));
1206 if (
Plus->Spidx_built) {
1207 G_debug(1,
"Spatial index already opened");
1224 G_debug(1,
"Cannot open spatial index file for vector '%s@%s'.",
1240 Plus->Spidx_file = 1;
1250 G_debug(1,
"Sidx head: coor size = %lu, coor mtime = %ld",
1251 (
unsigned long)
Plus->coor_size,
Plus->coor_mtime);
1257 _(
"Size of 'coor' file differs from value saved in sidx file"));
1268 G_warning(
_(
"Please rebuild topology for vector map <%s@%s>"),
1280 if (
getenv(
"GRASS_VECTOR_LOWMEM")) {
1284 Plus->Spidx_file = 1;
1287 G_debug(1,
"%s based spatial index",
1288 Plus->Spidx_file == 0 ?
"Memory" :
"File");
1312 if (
Map->temporary ||
getenv(
"GRASS_VECTOR_EXTERNAL_IGNORE"))
1323 G_debug(2,
" using OGR format");
1324 if (
getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1363 if (
Map->fInfo.ogr.driver_name) {
1364 G_warning(
_(
"OGR output also detected, using OGR"));
1373 G_debug(2,
" using PostGIS format");
1445 G_debug(1,
"PG: topology = yes, schema_name = %s",
1448 G_debug(1,
"PG: topology = no");
1450 if (
getenv(
"GRASS_VECTOR_EXTERNAL_IMMEDIATE")) {
1464 G_debug(2,
"map_format = %d", format);
1478 if (
Map->temporary) {
AMI_err name(char **stream_name)
FILE * G_fopen_modify(const char *, const char *)
Open a database file for update (r+ mode)
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
const char * G_database_epsg_code(void)
Get EPGS code for the current location.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
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
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
const char * G_gisdbase(void)
Get name of top level database directory.
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_tokenize(const char *, const char *)
Tokenize string.
int G_verbose(void)
Get current verbosity level.
const char * G_location(void)
Get current location name.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
int G_zone(void)
Query cartographic zone.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
char * G_store(const char *)
Copy string to allocated memory.
int G_set_verbose(int)
Set verbosity level.
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)
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_find_vector2(const char *, const char *)
Find a vector map (look but don't touch)
int G_projection(void)
Query cartographic projection.
const char * G_mapset(void)
Get current mapset name.
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)
Prepare PostGIS database for creating new feature table (level 1)
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
int Vect_read_dblinks(struct Map_info *)
Read dblinks to existing 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 *)
Open vector map - PostGIS feature table on topological level.
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
int Vect_get_zone(struct Map_info *)
Get projection zone from map header.
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.
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
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)
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)
Open vector map - PostGIS feature table on non-topological level.
#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, int head_only, int update)
Read full-topology for PostGIS links.
char * mapset
Mapset name.
struct dig_head head
Header info.
int head_only
Open only header.
int format
Map format (native, ogr, postgis)
Basic topology-related info.
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)
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.
const char * Vect_maptype_info(struct Map_info *Map)
Gets vector map format (as string)
int Vect_open_tmp_update(struct Map_info *Map, const char *name, const char *mapset)
Open existing temporary vector map for reading/writing.
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.
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_topo(struct Map_info *Map, int head_only)
Open topology file ('topo')
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)
#define MAX_OPEN_LEVEL
Number of levels.
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.