[GRASS-SVN] r70948 - grass/trunk/vector/v.out.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Apr 25 07:56:27 PDT 2017


Author: martinl
Date: 2017-04-25 07:56:27 -0700 (Tue, 25 Apr 2017)
New Revision: 70948

Added:
   grass/trunk/vector/v.out.ogr/dsn.c
Modified:
   grass/trunk/vector/v.out.ogr/local_proto.h
   grass/trunk/vector/v.out.ogr/main.c
Log:
v.in.ogr: build dsn from db.login if available (code duplicated from v.in.ogr -> TODO: design new GDAL-GRASS lib)


Copied: grass/trunk/vector/v.out.ogr/dsn.c (from rev 70947, grass/trunk/vector/v.in.ogr/dsn.c)
===================================================================
--- grass/trunk/vector/v.out.ogr/dsn.c	                        (rev 0)
+++ grass/trunk/vector/v.out.ogr/dsn.c	2017-04-25 14:56:27 UTC (rev 70948)
@@ -0,0 +1,78 @@
+#include <string.h>
+
+#include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+char *get_datasource_name(const char *opt_dsn, int use_ogr)
+{
+    char *dsn;
+    
+    if (G_strncasecmp(opt_dsn, "PG:", 3) == 0) {
+        /* PostgreSQL/PostGIS */
+        size_t i;
+        char connect_str[DB_SQL_MAX], database[GNAME_MAX];
+        char *p, *pp;
+        const char *user, *passwd, *host, *port;
+
+        /* dbname is mandatory */
+        p = G_strcasestr(opt_dsn, "dbname");
+        if (!p) 
+            G_fatal_error(_("Invalid connection string (dbname missing)"));
+        
+        /* get dbname */
+        p += strlen("dbname=");
+        for (i = 0, pp = p; *pp != ' ' && *pp != '\0'; pp++, i++)
+            database[i] = *pp;
+        database[i] = '\0';
+        
+        /* build connection string */
+        sprintf(connect_str, "dbname=%s", database);
+        
+        /* add db.login settings (user, password, host, port) */
+        if (DB_OK == db_get_login2("pg", database, &user, &passwd, &host, &port)) {
+            if (user) {
+                if (!G_strcasestr(opt_dsn, "user=")) {
+                    strcat(connect_str, " user=");
+                    strcat(connect_str, user);
+                }
+                G_free((char *)user);
+            }
+            if (passwd) {
+                if (!G_strcasestr(opt_dsn, "password=")) {
+                    strcat(connect_str, " password=");
+                    strcat(connect_str, passwd);
+                }
+                G_free((char *)passwd);
+            }
+            if (host) {
+                if (!G_strcasestr(opt_dsn, "host=")) {
+                    strcat(connect_str, " host=");
+                    strcat(connect_str, host);
+                }
+                G_free((char *)host);
+            }
+            if (port) {
+                if (!G_strcasestr(opt_dsn, "port=")) {
+                    strcat(connect_str, " port=");
+                    strcat(connect_str, port);
+                }
+                G_free((char *)port);
+            }
+        }
+        
+        if (!use_ogr)
+            /* be friendly, ignored 'PG:' prefix for PostGIS links */
+            dsn = G_store(connect_str);
+        else
+            G_asprintf(&dsn, "PG:%s", connect_str);
+    }
+    else {
+        /* other datasources */
+        dsn = G_store(opt_dsn);
+    }
+
+    G_debug(1, "dsn: %s", dsn);
+
+    return dsn;
+}

Modified: grass/trunk/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ogr/local_proto.h	2017-04-25 08:40:47 UTC (rev 70947)
+++ grass/trunk/vector/v.out.ogr/local_proto.h	2017-04-25 14:56:27 UTC (rev 70948)
@@ -28,6 +28,9 @@
 	   int, int *, const char **, int, int,
 	   OGRFeatureH, int *);
 
+/* dsn.c */
+char *get_datasource_name(const char *, int);
+
 /* list.c */
 char *OGR_list_write_drivers();
 

Modified: grass/trunk/vector/v.out.ogr/main.c
===================================================================
--- grass/trunk/vector/v.out.ogr/main.c	2017-04-25 08:40:47 UTC (rev 70947)
+++ grass/trunk/vector/v.out.ogr/main.c	2017-04-25 14:56:27 UTC (rev 70948)
@@ -35,7 +35,7 @@
     int num_to_export;
     int field;
     int overwrite, found;
-
+    
     struct GModule *module;
     struct Options options;
     struct Flags flags;
@@ -73,7 +73,8 @@
     OGRSpatialReferenceH Ogr_projection;
     char **papszDSCO = NULL, **papszLCO = NULL;
     int num_types;
-
+    char *dsn;
+    
     G_gisinit(argv[0]);
 
     /* Module options */
@@ -276,6 +277,10 @@
 	    OSRMorphToESRI(Ogr_projection);
     }
 
+    dsn = NULL;
+    if (options.dsn->answer)
+        dsn = get_datasource_name(options.dsn->answer, TRUE);
+
     /* create new OGR layer in datasource */
     if (flags.new->answer) {
 	const char *name;
@@ -284,7 +289,7 @@
 	    options.layer->answer ? options.layer->answer : options.input->
 	    answer;
 
-	create_ogr_layer(options.dsn->answer, options.format->answer, name,
+	create_ogr_layer(dsn, options.format->answer, name,
 			 wkbtype, papszDSCO, papszLCO);
 
 	G_message(_("OGR layer <%s> created in datasource <%s> (format '%s')"),
@@ -460,22 +465,22 @@
     
     if (flags.append->answer) {
 	G_debug(1, "Append to OGR layer");
-	Ogr_ds = OGR_Dr_Open(Ogr_driver, options.dsn->answer, TRUE);
+	Ogr_ds = OGR_Dr_Open(Ogr_driver, dsn, TRUE);
 	
 	if (Ogr_ds == NULL) {
 	    G_debug(1, "Create OGR data source");
-	    Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, options.dsn->answer,
+	    Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, dsn,
 					     papszDSCO);
 	}
     }
     else {
 	if (flags.update->answer) {
 	    G_debug(1, "Update OGR data source");
-	    Ogr_ds = OGR_Dr_Open(Ogr_driver, options.dsn->answer, TRUE);
+	    Ogr_ds = OGR_Dr_Open(Ogr_driver, dsn, TRUE);
 	}
 	else {
 	    G_debug(1, "Create OGR data source");
-	    Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, options.dsn->answer,
+	    Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, dsn,
 					     papszDSCO);
 	}
     }



More information about the grass-commit mailing list