30 #include "local_proto.h" 33 #include <gdal_version.h> 60 static int name2sql(
char *
name)
69 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z'))) {
70 G_warning(
_(
"Name <%s> is not SQL compliant. Must start with a letter."),
78 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z') ||
79 (*s >=
'0' && *s <=
'9') || *s ==
'_')) {
80 G_debug(2,
"Character '%c' not allowed.", *s);
114 const char *table,
const char *key,
const char *db,
120 G_warning(
_(
"Layer number must be 1 or greater"));
125 G_warning(
_(
"Unable to add attribute link, vector map is " 126 "not opened in WRITE mode"));
138 G_warning(
_(
"Unable to write attribute links"));
158 G_debug(4,
"Vect_map_del_dblink() field = %d", field);
162 for (i = 0; i < links->
n_fields; i++) {
163 if (field < 0 || links->field[i].number == field) {
164 for (j = i; j < links->
n_fields - 1; j++) {
183 G_warning(
_(
"Unable to write database links"));
204 for (i = 0; i < ndblinks; i++) {
207 G_warning(
_(
"Database connection not defined. Skipping."));
213 if (first_only && ndblinks > 1)
214 G_warning(
_(
"More DB links defined for input vector map. " 215 "Using only first DB link for output."));
248 G_debug(3,
"Vect_check_dblink: field %d, name %s", field, (name !=
NULL ? name :
"not given"));
278 const char *
table,
const char *
key,
const char *db,
283 G_debug(3,
"Field number <%d>, name <%s>", number, name);
290 G_warning(
_(
"Layer number %d or name <%s> already exists"), number,
352 int field,
const char *field_name,
int type)
360 G_debug(1,
"Vect_default_field_info(): map = %s field = %d", Map->
name,
380 "driver: %s\ndatabase: %s"), connection.
driverName,
398 if (field_name && *field_name) {
400 if (!name2sql(fi->
name)) {
408 sprintf(buf,
"%s", Map->
name);
412 sprintf(buf,
"%s_%s", Map->
name, fi->
name);
413 if (!name2sql(buf)) {
414 sprintf(buf,
"%s_%d", Map->
name, field);
418 sprintf(buf,
"%s_%d", Map->
name, field);
421 if (schema && strlen(schema) > 0) {
434 #ifdef TEMPORARY_MAP_DB 438 strcat(buf,
"/sqlite.db");
440 strcat(buf,
"/db.dbf");
471 G_debug(1,
"Vect_get_dblink(): link = %d", link);
474 G_warning(
_(
"Requested dblink %d, maximum link number %d"), link,
512 G_debug(1,
"Vect_get_field(): field = %d", field);
538 G_debug(1,
"Vect_get_field_by_name(): field = %s", field);
567 G_debug(1,
"Vect_get_field2(): field = %s", field);
570 ifield = atoi(field);
577 else if (ifield == -1) {
583 else if (ifield == 0)
603 G_debug(1,
"Vect_get_field_number(): field = %s", field);
605 if (strcmp(field,
"-1") == 0)
619 static int read_dblinks_nat(
struct Map_info *Map)
622 char file[1024], buf[2001];
623 char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
637 G_debug(1,
"Cannot open vector database definition file");
643 while (
G_getl2(buf, 2000, fd)) {
648 c = (
char *)strchr(buf,
'#');
652 if (strlen(buf) == 0)
655 #ifdef NOT_ABLE_TO_READ_GRASS_6 657 ndef = sscanf(buf,
"%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
659 if (ndef < 2 || (ndef < 5 && rule < 1)) {
660 G_warning(
_(
"Error in rule on row %d in <%s>"), row, file);
667 if (ntok < 2 || (ntok < 5 && rule < 1)) {
668 G_warning(
_(
"Error in rule on row %d in <%s>"), row, file);
672 strcpy(fldstr, tokens[0]);
673 strcpy(tab, tokens[1]);
675 strcpy(col, tokens[2]);
677 strcpy(db, tokens[3]);
679 for (i=4; i < ntok-1; i++) {
681 strcat(db, tokens[i]);
684 strcpy(drv, tokens[ntok-1]);
691 fldname = strchr(fldstr,
'/');
692 if (fldname !=
NULL) {
701 "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
702 fld, fldname, tab, col, db, drv);
714 static int read_dblinks_ogr(
struct Map_info *Map)
719 G_debug(3,
"Searching for FID column in OGR DB");
721 G_warning(
_(
"GRASS is not compiled with OGR support"));
723 #if GDAL_VERSION_NUM > 1320 && HAVE_OGR 727 G_debug(3,
"GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
736 G_warning(
_(
"Unable to open OGR data source '%s'"),
743 nLayers = OGR_DS_GetLayerCount(Map->
fInfo.
ogr.
ds);
745 G_debug(3,
"%d layers (maps) found in data source", nLayers);
762 G_debug(3,
"Using FID column <%s> in OGR DB", ogr_fid_col);
770 int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
772 G_debug(3,
"GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
779 if (driver ==
NULL) {
780 G_warning(
_(
"Unable to open OGR DBMI driver"));
786 sprintf(buf,
"select FID from %s where FID > 0",
793 G_debug(3,
"Failed. Now searching for ogc_fid column in OGR DB");
794 sprintf(buf,
"select ogc_fid from %s where ogc_fid > 0",
802 "Failed. Now searching for ogr_fid column in OGR DB");
803 sprintf(buf,
"select ogr_fid from %s where ogr_fid > 0",
811 "Failed. Now searching for gid column in OGR DB");
812 sprintf(buf,
"select gid from %s where gid > 0",
819 G_warning(
_(
"All FID tests failed. Neither 'FID' nor 'ogc_fid' " 820 "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
836 G_debug(3,
"FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
844 G_debug(3,
"Using FID column in OGR DB");
850 G_debug(3,
"Using ogc_fid column in OGR DB");
856 G_debug(3,
"Using ogr_fid column in OGR DB");
862 G_debug(3,
"Using gid column in OGR DB");
875 static int read_dblinks_pg(
struct Map_info *Map)
886 G_warning(
_(
"Feature table <%s> has no primary key defined. " 887 "Unable to define DB links."), pg_info->
table_name);
906 G_warning(
_(
"GRASS not compiled with PostgreSQL support"));
923 G_debug(1,
"Vect_read_dblinks(): map = %s, mapset = %s", Map->
name,
929 return read_dblinks_nat(Map);
932 return read_dblinks_ogr(Map);
935 return read_dblinks_pg(Map);
963 G_debug(1,
"Vect_write_dblinks(): map = %s, mapset = %s", Map->
name,
971 G_warning(
_(
"Unable to create database definition file for vector map <%s>"),
976 for (i = 0; i < dbl->
n_fields; i++) {
982 fprintf(fd,
"%s|%s|%s|%s|%s\n", buf, dbl->
field[i].
table,
1007 char buf[1000], str[1000];
1009 G_debug(3,
"Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
1022 c = (
char *)strstr(buf,
"$GISDBASE");
1025 sprintf(str,
"%s%s%s", buf, Map->
gisdbase, c + 9);
1029 c = (
char *)strstr(buf,
"$LOCATION_NAME");
1032 sprintf(str,
"%s%s%s", buf, Map->
location, c + 14);
1036 c = (
char *)strstr(buf,
"$MAPSET");
1039 sprintf(str,
"%s%s%s", buf, Map->
mapset, c + 7);
1043 c = (
char *)strstr(buf,
"$MAP");
1046 sprintf(str,
"%s%s%s", buf, Map->
name, c + 4);
1066 G_fatal_error(
_(
"Bug: attempt to update map which is not in current mapset"));
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
#define GV_KEY_COLUMN
Name of default key column.
char * name
Map name (for 4.0)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void void void G_important_message(const char *,...) __attribute__((format(printf
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int Vect_map_check_dblink(const struct Map_info *Map, int field, const char *name)
Check if DB connection exists in dblinks structure.
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
int n_fields
Number of available layers (old: fields)
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
#define GV_DBLN_ELEMENT
Native format, link to database.
void db_init_string(dbString *)
Initialize dbString.
char * Vect_subst_var(const char *in, const struct Map_info *Map)
Substitute variable in string.
struct field_info * field
Pointer to the first field_info structure.
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
#define DB_DEFAULT_DRIVER
char * table
Name of DB table.
#define GV_FORMAT_OGR
OGR format.
char * location
Location name.
void G_free(void *)
Free allocated memory.
struct Format_info fInfo
Format info for non-native formats.
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
char ** G_tokenize(const char *, const char *)
Tokenize string.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int Vect_get_num_dblinks(const struct Map_info *)
Get number of defined dblinks.
#define GV_MODE_RW
Read-write vector map open mode.
Layer (old: field) information.
char * name
Layer name (optional)
void db_auto_print_errors(int)
Toggles printing of DBMI error messages.
int Vect_add_dblink(struct dblinks *p, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
Add new DB connection to dblinks structure.
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.
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.
char * G_str_replace(const char *, const char *, const char *)
Replace all occurrences of old_str in buffer with new_str.
struct field_info * Vect_get_field_by_name(const struct Map_info *Map, const char *field)
Get information about link to database (by layer name)
int G_number_of_tokens(char **)
Return number of tokens.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
const struct driver * driver
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
#define GV_1TABLE
One table linked to vector map.
char * mapset
Mapset name.
int Vect_write_dblinks(struct Map_info *Map)
Write dblinks to file.
#define GV_FORMAT_POSTGIS
PostGIS format.
struct field_info * Vect_get_dblink(const struct Map_info *Map, int link)
Get information about link to database.
char * G_chop(char *)
Chop leading and trailing white spaces.
struct dblinks * dblnk
Array of DB links.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
int Vect_check_dblink(const struct dblinks *p, int field, const char *name)
Check if DB connection exists in dblinks structure.
int alloc_fields
Number of allocated slots.
void Vect_copy_map_dblinks(const struct Map_info *In, struct Map_info *Out, int first_only)
Copy DB links from input vector map to output vector map.
const char * G_mapset(void)
Get current mapset name.
int temporary
Temporary map flag.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free_tokens(char **)
Free memory allocated to tokens.
void G_warning(const char *,...) __attribute__((format(printf
int format
Map format (native, ogr, postgis)
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.
char * G_store(const char *)
Copy string to allocated memory.
#define GV_MODE_WRITE
Write vector map open mode.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
char * Vect__get_path(char *path, const struct Map_info *Map)
Get map directory name (internal use only)
int Vect_map_del_dblink(struct Map_info *Map, int field)
Delete db connection from Map_info structure.
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int Vect_read_dblinks(struct Map_info *Map)
Read dblinks to existing structure.
int Vect_get_field_number(const struct Map_info *Map, const char *field)
Get field number of given field.
struct field_info * Vect_get_field(const struct Map_info *Map, int field)
Get information about link to database (by layer number)
void Vect_reset_dblinks(struct dblinks *p)
Reset dblinks structure (number of fields)
const char * Vect_get_name(const struct Map_info *)
Get name of vector map.
int G_debug(int, const char *,...) __attribute__((format(printf
struct field_info * Vect_get_field2(const struct Map_info *Map, const char *field)
Get information about link to database (by layer number or layer name)
int db_close_cursor(dbCursor *)
Close cursor.
char * gisdbase
GISDBASE path.
void Vect_set_db_updated(struct Map_info *Map)
Rewrite 'dbln' file.
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
char * key
Name of key column (usually 'cat')