30 #include "local_proto.h"
33 #include <gdal_version.h>
43 #define NOPG_UNUSED UNUSED
66 static int name2sql(
char *
name)
75 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z'))) {
77 _(
"Name <%s> is not SQL compliant. Must start with a letter."),
85 if (!((*s >=
'A' && *s <=
'Z') || (*s >=
'a' && *s <=
'z') ||
86 (*s >=
'0' && *s <=
'9') || *s ==
'_')) {
87 G_debug(2,
"Character '%c' not allowed.", *s);
121 const char *table,
const char *key,
const char *db,
127 G_warning(
_(
"Layer number must be 1 or greater"));
132 G_warning(
_(
"Unable to add attribute link, vector map is "
133 "not opened in WRITE mode"));
145 G_warning(
_(
"Unable to write attribute links"));
169 for (i = 0; i < links->
n_fields; i++) {
170 if (field < 0 || links->
field[i].number ==
field) {
171 for (j = i; j < links->
n_fields - 1; j++) {
190 G_warning(
_(
"Unable to write database links"));
212 for (i = 0; i < ndblinks; i++) {
215 G_warning(
_(
"Database connection not defined. Skipping."));
221 if (first_only && ndblinks > 1)
222 G_warning(
_(
"More DB links defined for input vector map. "
223 "Using only first DB link for output."));
256 G_debug(3,
"Vect_check_dblink: field %d, name %s", field,
286 const char *
table,
const char *
key,
const char *db,
359 const char *field_name,
int type)
367 G_debug(1,
"Vect_default_field_info(): map = %s field = %d", Map->
name,
388 "driver: %s\ndatabase: %s"),
406 if (field_name && *field_name) {
408 if (!name2sql(fi->
name)) {
416 sprintf(buf,
"%s", Map->
name);
420 sprintf(buf,
"%s_%s", Map->
name, fi->
name);
421 if (!name2sql(buf)) {
422 sprintf(buf,
"%s_%d", Map->
name, field);
426 sprintf(buf,
"%s_%d", Map->
name, field);
429 if (schema && strlen(schema) > 0) {
442 #ifdef TEMPORARY_MAP_DB
446 strcat(buf,
"/sqlite.db");
448 strcat(buf,
"/db.dbf");
479 G_debug(1,
"Vect_get_dblink(): link = %d", link);
482 G_warning(
_(
"Requested dblink %d, maximum link number %d"), link,
520 G_debug(1,
"Vect_get_field(): field = %d", field);
547 G_debug(1,
"Vect_get_field_by_name(): field = %s", field);
576 G_debug(1,
"Vect_get_field2(): field = %s", field);
579 ifield = atoi(field);
586 else if (ifield == -1) {
592 else if (ifield == 0)
612 G_debug(1,
"Vect_get_field_number(): field = %s", field);
614 if (strcmp(field,
"-1") == 0)
647 static int read_dblinks_nat(
struct Map_info *Map)
650 char file[1024], buf[2001];
651 char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
665 G_debug(1,
"Cannot open vector database definition file");
671 while (
G_getl2(buf, 2000, fd)) {
676 c = (
char *)strchr(buf,
'#');
680 if (strlen(buf) == 0)
683 #ifdef NOT_ABLE_TO_READ_GRASS_6
686 ndef = sscanf(buf,
"%s|%s|%s|%s|%s", fldstr, tab, col, db, drv);
688 if (ndef < 2 || (ndef < 5 && rule < 1)) {
696 if (ntok < 2 || (ntok < 5 && rule < 1)) {
701 strcpy(fldstr, tokens[0]);
708 for (i = 4; i < ntok - 1; i++) {
710 strcat(db, tokens[i]);
713 strcpy(drv, tokens[ntok - 1]);
720 fldname = strchr(fldstr,
'/');
721 if (fldname !=
NULL) {
730 "field = %d name = %s, table = %s, key = %s, database = %s, "
732 fld, fldname, tab, col, db, drv);
744 static int read_dblinks_ogr(
struct Map_info *Map)
749 G_debug(3,
"Searching for FID column in OGR DB");
751 G_warning(
_(
"GRASS is not compiled with OGR support"));
756 G_debug(3,
"GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
765 G_warning(
_(
"Unable to open OGR data source '%s'"),
772 nLayers = OGR_DS_GetLayerCount(
775 G_debug(3,
"%d layers (maps) found in data source", nLayers);
793 G_debug(3,
"Using FID column <%s> in OGR DB", ogr_fid_col);
812 G_warning(
_(
"Feature table <%s> has no primary key defined. "
813 "Unable to define DB links."),
831 G_warning(
_(
"GRASS not compiled with PostgreSQL support"));
848 G_debug(1,
"Vect_read_dblinks(): map = %s, mapset = %s", Map->
name,
854 return read_dblinks_nat(Map);
858 return read_dblinks_ogr(Map);
861 return read_dblinks_pg(Map);
889 G_debug(1,
"Vect_write_dblinks(): map = %s, mapset = %s", Map->
name,
898 _(
"Unable to create database definition file for vector map <%s>"),
903 for (i = 0; i < dbl->
n_fields; i++) {
909 fprintf(fd,
"%s|%s|%s|%s|%s\n", buf, dbl->
field[i].
table,
934 char buf[1000], str[1000];
936 G_debug(3,
"Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
950 c = (
char *)strstr(buf,
"$GISDBASE");
953 sprintf(str,
"%s%s%s", buf, Map->
gisdbase, c + 9);
957 c = (
char *)strstr(buf,
"$LOCATION_NAME");
960 sprintf(str,
"%s%s%s", buf, Map->
location, c + 14);
964 c = (
char *)strstr(buf,
"$MAPSET");
967 sprintf(str,
"%s%s%s", buf, Map->
mapset, c + 7);
971 c = (
char *)strstr(buf,
"$MAP");
974 sprintf(str,
"%s%s%s", buf, Map->
name, c + 4);
995 _(
"Bug: attempt to update map which is not in current mapset"));
#define DB_DEFAULT_DRIVER
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
int db_set_default_connection(void)
Sets up database connection settings using GRASS default from dbmi.h.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
char * G_str_replace(const char *, const char *, const char *)
Replace all occurrences of old_str in buffer with new_str.
const char * G_mapset(void)
Get current mapset name.
void G_free_tokens(char **)
Free memory allocated to tokens.
void void void G_important_message(const char *,...) __attribute__((format(printf
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int G_number_of_tokens(char **)
Return number of tokens.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
char * G_chop(char *)
Chop leading and trailing white spaces.
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 Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_1TABLE
One table linked to vector map.
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_DBLN_ELEMENT
Native format, link to database.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
#define GV_FORMAT_OGR
OGR format.
#define GV_MODE_RW
Read-write vector map open mode.
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
struct field_info * Vect_get_field(struct Map_info *Map, int field)
Get information about link to database (by layer number)
struct field_info * Vect_get_dblink(struct Map_info *Map, int link)
Get information about link to database.
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
void Vect_set_db_updated(struct Map_info *Map)
Rewrite 'dbln' file.
int Vect_map_del_dblink(struct Map_info *Map, int field)
Delete db connection from Map_info structure.
int Vect_get_field_number(struct Map_info *Map, const char *field)
Get field number of given field.
void Vect_reset_dblinks(struct dblinks *p)
Reset dblinks structure (number of fields)
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.
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_check_dblink(const struct dblinks *p, int field, const char *name)
Check if DB connection exists in dblinks structure.
int Vect_read_dblinks(struct Map_info *Map)
Read dblinks to existing structure.
char * Vect_subst_var(const char *in, struct Map_info *Map)
Substitute variable in string.
struct field_info * Vect_get_field_by_name(struct Map_info *Map, const char *field)
Get information about link to database (by layer name)
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.
struct field_info * Vect_get_field2(struct Map_info *Map, const char *field)
Get information about link to database (by layer number or layer name)
int Vect_write_dblinks(struct Map_info *Map)
Write dblinks to file.
void Vect_destroy_field_info(struct field_info *fi)
Free a struct field_info and all memory associated with it.
int Vect_map_check_dblink(struct Map_info *Map, int field, const char *name)
Check if DB connection exists in dblinks structure.
void Vect_copy_map_dblinks(struct Map_info *In, struct Map_info *Out, int first_only)
Copy DB links from input vector map to output vector map.
#define GV_KEY_COLUMN
Name of default key column.
char * mapset
Mapset name.
int temporary
Temporary map flag.
char * gisdbase
GISDBASE path.
char * name
Map name (for 4.0)
char * location
Location name.
int format
Map format (native, ogr, postgis)
struct dblinks * dblnk
Array of DB links.
struct Format_info fInfo
Format info for non-native formats.
int n_fields
Number of available layers (old: fields)
int alloc_fields
Number of allocated slots.
struct field_info * field
Pointer to the first field_info structure.
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * name
Layer name (optional)
char * key
Name of key column (usually 'cat')
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)