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)
359 G_debug(1,
"Vect_default_field_info(): map = %s field = %d", Map->
name,
379 "driver: %s\ndatabase: %s"), connection.
driverName,
397 if (field_name && *field_name) {
399 if (!name2sql(fi->
name)) {
407 sprintf(buf,
"%s", Map->
name);
411 sprintf(buf,
"%s_%s", Map->
name, fi->
name);
412 if (!name2sql(buf)) {
413 sprintf(buf,
"%s_%d", Map->
name, field);
417 sprintf(buf,
"%s_%d", Map->
name, field);
420 if (schema && strlen(schema) > 0) {
421 sprintf(buf2,
"%s.%s", schema, buf);
433 #ifdef TEMPORARY_MAP_DB 437 strcat(buf,
"/sqlite.db");
439 strcat(buf,
"/db.dbf");
470 G_debug(1,
"Vect_get_dblink(): link = %d", link);
473 G_warning(
_(
"Requested dblink %d, maximum link number %d"), link,
511 G_debug(1,
"Vect_get_field(): field = %d", field);
537 G_debug(1,
"Vect_get_field_by_name(): field = %s", field);
566 G_debug(1,
"Vect_get_field2(): field = %s", field);
569 ifield = atoi(field);
576 else if (ifield == -1) {
582 else if (ifield == 0)
602 G_debug(1,
"Vect_get_field_number(): field = %s", field);
604 if (strcmp(field,
"-1") == 0)
618 static int read_dblinks_nat(
struct Map_info *Map)
621 char file[1024], buf[2001];
622 char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
636 G_debug(1,
"Cannot open vector database definition file");
642 while (
G_getl2(buf, 2000, fd)) {
647 c = (
char *)strchr(buf,
'#');
651 if (strlen(buf) == 0)
654 #ifdef NOT_ABLE_TO_READ_GRASS_6 656 ndef = sscanf(buf,
"%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
658 if (ndef < 2 || (ndef < 5 && rule < 1)) {
659 G_warning(
_(
"Error in rule on row %d in <%s>"), row, file);
666 if (ntok < 2 || (ntok < 5 && rule < 1)) {
667 G_warning(
_(
"Error in rule on row %d in <%s>"), row, file);
671 strcpy(fldstr, tokens[0]);
672 strcpy(tab, tokens[1]);
674 strcpy(col, tokens[2]);
676 strcpy(db, tokens[3]);
678 for (i=4; i < ntok-1; i++) {
680 strcat(db, tokens[i]);
683 strcpy(drv, tokens[ntok-1]);
690 fldname = strchr(fldstr,
'/');
691 if (fldname !=
NULL) {
700 "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
701 fld, fldname, tab, col, db, drv);
713 static int read_dblinks_ogr(
struct Map_info *Map)
718 G_debug(3,
"Searching for FID column in OGR DB");
720 G_warning(
_(
"GRASS is not compiled with OGR support"));
722 #if GDAL_VERSION_NUM > 1320 && HAVE_OGR 726 G_debug(3,
"GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
735 G_warning(
_(
"Unable to open OGR data source '%s'"),
742 nLayers = OGR_DS_GetLayerCount(Map->
fInfo.
ogr.
ds);
744 G_debug(3,
"%d layers (maps) found in data source", nLayers);
761 G_debug(3,
"Using FID column <%s> in OGR DB", ogr_fid_col);
769 int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
771 G_debug(3,
"GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
778 if (driver ==
NULL) {
779 G_warning(
_(
"Unable to open OGR DBMI driver"));
785 sprintf(buf,
"select FID from %s where FID > 0",
792 G_debug(3,
"Failed. Now searching for ogc_fid column in OGR DB");
793 sprintf(buf,
"select ogc_fid from %s where ogc_fid > 0",
801 "Failed. Now searching for ogr_fid column in OGR DB");
802 sprintf(buf,
"select ogr_fid from %s where ogr_fid > 0",
810 "Failed. Now searching for gid column in OGR DB");
811 sprintf(buf,
"select gid from %s where gid > 0",
818 G_warning(
_(
"All FID tests failed. Neither 'FID' nor 'ogc_fid' " 819 "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
835 G_debug(3,
"FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
843 G_debug(3,
"Using FID column in OGR DB");
849 G_debug(3,
"Using ogc_fid column in OGR DB");
855 G_debug(3,
"Using ogr_fid column in OGR DB");
861 G_debug(3,
"Using gid column in OGR DB");
874 static int read_dblinks_pg(
struct Map_info *Map)
885 G_warning(
_(
"Feature table <%s> has no primary key defined. " 886 "Unable to define DB links."), pg_info->
table_name);
905 G_warning(
_(
"GRASS not compiled with PostgreSQL support"));
922 G_debug(1,
"Vect_read_dblinks(): map = %s, mapset = %s", Map->
name,
928 return read_dblinks_nat(Map);
931 return read_dblinks_ogr(Map);
934 return read_dblinks_pg(Map);
962 G_debug(1,
"Vect_write_dblinks(): map = %s, mapset = %s", Map->
name,
970 G_warning(
_(
"Unable to create database definition file for vector map <%s>"),
975 for (i = 0; i < dbl->
n_fields; i++) {
981 fprintf(fd,
"%s|%s|%s|%s|%s\n", buf, dbl->
field[i].
table,
1006 char buf[1000], str[1000];
1008 G_debug(3,
"Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
1021 c = (
char *)strstr(buf,
"$GISDBASE");
1024 sprintf(str,
"%s%s%s", buf, Map->
gisdbase, c + 9);
1028 c = (
char *)strstr(buf,
"$LOCATION_NAME");
1031 sprintf(str,
"%s%s%s", buf, Map->
location, c + 14);
1035 c = (
char *)strstr(buf,
"$MAPSET");
1038 sprintf(str,
"%s%s%s", buf, Map->
mapset, c + 7);
1042 c = (
char *)strstr(buf,
"$MAP");
1045 sprintf(str,
"%s%s%s", buf, Map->
name, c + 4);
1065 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')