Переглянути джерело

v.in.ogr: use default DB settings when dsn is omitted (see the manual `dsn=./` - it was not working) - pg driver only
manual updated


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@59627 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 11 роки тому
батько
коміт
28c487256f
2 змінених файлів з 70 додано та 24 видалено
  1. 46 6
      vector/v.in.ogr/main.c
  2. 24 18
      vector/v.in.ogr/v.in.ogr.html

+ 46 - 6
vector/v.in.ogr/main.c

@@ -9,7 +9,7 @@
  *
  * PURPOSE:      Import OGR vectors
  *
- * COPYRIGHT:    (C) 2003, 2011-2013 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003, 2011-2014 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2).  Read the file COPYING that
@@ -100,6 +100,7 @@ int main(int argc, char *argv[])
 
     int OFTIntegerListlength;
 
+    char *dsn;
     char *output;
     char **layer_names;		/* names of layers to be imported */
     int *layers;		/* layer indexes */
@@ -318,7 +319,46 @@ int main(int argc, char *argv[])
 	exit(EXIT_SUCCESS);
     }
 
-    if (param.dsn->answer == NULL) {
+    /* dsn not specified, check default connection settings */
+    dsn = NULL;
+    if (strcmp(db_get_default_driver_name(), "pg") == 0 &&
+        param.dsn->answer == NULL) {
+        const char *dbname;
+        dbConnection conn;
+        
+        dbname = db_get_default_database_name();
+        if (!dbname)
+            G_fatal_error(_("Database not defined, please check default "
+                            " connection settings by db.connect"));
+
+        dsn = (char *) G_malloc(GPATH_MAX);
+        /* -> dbname */
+        sprintf(dsn, "PG:dbname=%s", dbname);
+        
+        /* -> user/passwd */
+        if (DB_OK == db_get_connection(&conn) &&
+            strcmp(conn.driverName, "pg") == 0 &&
+            strcmp(conn.databaseName, dbname) == 0) {
+            if (conn.user) {
+                strcat(dsn, " user=");
+                strcat(dsn, conn.user);
+            }
+            if (conn.password) {
+                strcat(dsn, " passwd=");
+                strcat(dsn, conn.password);
+            }
+            /* TODO: host/port... */
+        }
+        else {
+            G_debug(1, "unable to get connection");
+        }
+        G_debug(1, "Using dsn=%s", dsn);
+    }
+    else if (param.dsn->answer) {
+        dsn = G_store(param.dsn->answer);
+    }
+    
+    if (dsn == NULL) {
 	G_fatal_error(_("Required parameter <%s> not set"), param.dsn->key);
     }
 
@@ -358,10 +398,10 @@ int main(int argc, char *argv[])
 
     /* open OGR DSN */
     Ogr_ds = NULL;
-    if (strlen(param.dsn->answer) > 0)
-	Ogr_ds = OGROpen(param.dsn->answer, FALSE, NULL);
+    if (strlen(dsn) > 0)
+	Ogr_ds = OGROpen(dsn, FALSE, NULL);
     if (Ogr_ds == NULL)
-	G_fatal_error(_("Unable to open data source <%s>"), param.dsn->answer);
+	G_fatal_error(_("Unable to open data source <%s>"), dsn);
 
     /* check encoding for given driver */
     if (param.encoding->answer) {
@@ -380,7 +420,7 @@ int main(int argc, char *argv[])
 
     if (flag.list->answer)
 	G_message(_("Data source <%s> (format '%s') contains %d layers:"),
-		  param.dsn->answer,
+		  dsn,
 		  OGR_Dr_GetName(OGR_DS_GetDriver(Ogr_ds)), navailable_layers);
     for (i = 0; i < navailable_layers; i++) {
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);

+ 24 - 18
vector/v.in.ogr/v.in.ogr.html

@@ -217,49 +217,55 @@ type=boundary,centroid snap=-1
 
 <h3>PostGIS tables</h3>
 
-Area example:
+Import polygons as areas:
 
 <div class="code"><pre>
 v.in.ogr dsn="PG:host=localhost dbname=postgis user=postgres" layer=polymap \
 output=polygons type=boundary,centroid
 </pre></div>
 
-<h3>Oracle Spatial maps</h3>
+<h3>Default connection settings as datasource (PostgreSQL only)</h3>
 
-Note that you have to set the environment-variables <tt>ORACLE_BASE,
-ORACLE_SID, ORACLE_HOME</tt> and <tt>TNS_ADMIN</tt> accordingly.
-
-<div class="code"><pre>
-v.in.ogr dsn=OCI:username/password@database_instance output=grasslayer layer=roads_oci
-</pre></div>
-
-<h3>Support of database schema</h3>
+Datasource (<b>dsn</b>) can be omitted in the case that default DB
+driver is <a href="grass-pg.html">PostgreSQL</a> (<tt>pg</tt>), see
+examples below.
 
 <p>
 For schema support, first set a default schema with 
 <em><a href="db.connect.html">db.connect</a></em>. If schema support
-is used the schema name must be specified whenever a db.* module is
-called.
+is used the schema name must be specified whenever a <tt>db.*</tt>
+module is called. User and password for connection to the database can
+be specified by <em><a href="db.login.html">db.login</a></em>.
 
 <p>
-Example:
+Example (with schema):
 
 <div class="code"><pre>
-db.connect driver=pg database=test schema=user1 group=group1
-db.login driver=pg database=test user=user1 password=pwd1
-v.in.ogr dsn=./ layer=river output=river     # -> table user1.river
+db.connect driver=pg database=test schema=user1
+db.login user=user1 password=pwd1
+v.in.ogr layer=river output=river     # -> table user1.river
 db.select table=user1.river
 </pre></div>
 
+<p>
 The user can ignore schemas, if desired:
 
 <div class="code"><pre>
 db.connect driver=pg database=test
-db.login driver=pg database=test user=user1 password=pwd1
-v.in.ogr dsn=./ layer=river output=river     # -> table public.river
+db.login user=user1 password=pwd1
+v.in.ogr layer=river output=river     # -> table public.river
 db.select table=river
 </pre></div>
 
+<h3>Oracle Spatial</h3>
+
+Note that you have to set the environment-variables <tt>ORACLE_BASE,
+ORACLE_SID, ORACLE_HOME</tt> and <tt>TNS_ADMIN</tt> accordingly.
+
+<div class="code"><pre>
+v.in.ogr dsn=OCI:username/password@database_instance output=grasslayer layer=roads_oci
+</pre></div>
+
 <h2>WARNINGS</h2>
 
 If a message like &quot;WARNING: Area size 1.3e-06, area not