[GRASS-SVN] r71037 - grass/branches/releasebranch_7_2/vector/v.out.ogr
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat May 6 02:11:40 PDT 2017
Author: martinl
Date: 2017-05-06 02:11:40 -0700 (Sat, 06 May 2017)
New Revision: 71037
Added:
grass/branches/releasebranch_7_2/vector/v.out.ogr/dsn.c
Modified:
grass/branches/releasebranch_7_2/vector/v.out.ogr/local_proto.h
grass/branches/releasebranch_7_2/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)
(merge r70948 from trunk)
Copied: grass/branches/releasebranch_7_2/vector/v.out.ogr/dsn.c (from rev 70948, grass/trunk/vector/v.out.ogr/dsn.c)
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.out.ogr/dsn.c (rev 0)
+++ grass/branches/releasebranch_7_2/vector/v.out.ogr/dsn.c 2017-05-06 09:11:40 UTC (rev 71037)
@@ -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/branches/releasebranch_7_2/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.out.ogr/local_proto.h 2017-05-06 09:09:28 UTC (rev 71036)
+++ grass/branches/releasebranch_7_2/vector/v.out.ogr/local_proto.h 2017-05-06 09:11:40 UTC (rev 71037)
@@ -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/branches/releasebranch_7_2/vector/v.out.ogr/main.c
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.out.ogr/main.c 2017-05-06 09:09:28 UTC (rev 71036)
+++ grass/branches/releasebranch_7_2/vector/v.out.ogr/main.c 2017-05-06 09:11:40 UTC (rev 71037)
@@ -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 */
@@ -280,6 +281,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;
@@ -288,7 +293,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')"),
@@ -464,22 +469,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