[GRASS-SVN] r51121 - grass/trunk/vector/v.external.out

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 20 17:14:17 EDT 2012


Author: martinl
Date: 2012-03-20 14:14:17 -0700 (Tue, 20 Mar 2012)
New Revision: 51121

Modified:
   grass/trunk/vector/v.external.out/link.c
   grass/trunk/vector/v.external.out/status.c
Log:
v.external.out: parse schema/fid/geometry_name for PG output format


Modified: grass/trunk/vector/v.external.out/link.c
===================================================================
--- grass/trunk/vector/v.external.out/link.c	2012-03-20 17:26:05 UTC (rev 51120)
+++ grass/trunk/vector/v.external.out/link.c	2012-03-20 21:14:17 UTC (rev 51121)
@@ -4,12 +4,14 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
+static char *get_option_pg(char **, const char *);
+
 void make_link(const char *dsn,
 	       const char *format,
 	       char *option_str, char **options)
 {
-    int i, use_ogr;
-    char *filename, *pg_schema;
+    int use_ogr;
+    char *filename, *pg_schema, *pg_fid, *pg_geom_name;
     FILE *fp;
     
     struct Key_Value *key_val;
@@ -28,31 +30,12 @@
 	G_remove("", "PG");
     }
     
-    /* parse options */
-    pg_schema = NULL;
+    /* parse options for PG data format */
     if (options && *options && !use_ogr) {
-	int   opt_len;
-	char *opt_schema;
-	
-	opt_schema = NULL;
-	for (i = 0; options[i]; i++) {
-	    if (G_strncasecmp("schema=", options[i], 6) == 0)
-		opt_schema = options[i];
-	    else
-		G_warning(_("Option '%s' ignored for 'PostGIS' format"),
-			  options[i]);
-	}
-	
-	if (opt_schema) {
-	    opt_len = strlen(opt_schema);
-	    pg_schema = G_malloc(opt_len - 6);
-	    for (i = 7; i < opt_len; i++) {
-		pg_schema[i - 7] = opt_schema[i];
-	    }
-	    pg_schema[opt_len - 7] = '\0';
-	}
+	pg_schema    = get_option_pg(options, "schema");
+	pg_fid       = get_option_pg(options, "fid");
+	pg_geom_name = get_option_pg(options, "geometry_name");
     }
-    
     /* add key/value items */
     if (dsn) {
 	if (use_ogr)
@@ -67,8 +50,14 @@
     if (use_ogr && option_str)
 	G_set_key_value("options", option_str, key_val);
     
-    if (!use_ogr && pg_schema)
-	G_set_key_value("schema", pg_schema, key_val);
+    if (!use_ogr) {
+	if (pg_schema)
+	    G_set_key_value("schema", pg_schema, key_val);
+	if (pg_fid)
+	    G_set_key_value("fid", pg_fid, key_val);
+	if (pg_geom_name)
+	    G_set_key_value("geometry_name", pg_geom_name, key_val);
+    }
     
     /* save file - OGR or PG */
     fp = G_fopen_new("", filename);
@@ -80,3 +69,30 @@
 
     fclose(fp);
 }
+
+char *get_option_pg(char **options, const char *key)
+{
+    int   i, opt_len, key_len;
+    char *opt, *value;
+    
+    key_len = strlen(key);
+    /* parse options for PG data provider*/
+    opt = value = NULL;
+    for (i = 0; options[i] && !opt; i++) {
+	if (G_strncasecmp(key, options[i], key_len) == 0)
+	    opt = options[i];
+    }
+	
+    if (!opt)
+	return NULL;
+    
+    opt_len = strlen(opt);
+    value = G_malloc(opt_len - key_len);
+    key_len++;
+    for (i = key_len; i < opt_len; i++) {
+	value[i - key_len] = opt[i];
+    }
+    value[opt_len - key_len] = '\0';
+
+    return value;
+}

Modified: grass/trunk/vector/v.external.out/status.c
===================================================================
--- grass/trunk/vector/v.external.out/status.c	2012-03-20 17:26:05 UTC (rev 51120)
+++ grass/trunk/vector/v.external.out/status.c	2012-03-20 21:14:17 UTC (rev 51121)
@@ -4,7 +4,8 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
-static int print_status_file(const char *, int shell);
+static int print_status_file(const char *, int);
+static void print_key_value(const char *, const char *, int);
 
 void print_status(int shell)
 {
@@ -21,6 +22,17 @@
 	fprintf(stdout, _("format: native\n"));
 }
 
+void print_key_value(const char *key, const char *value, int shell)
+{
+    if (!value)
+	return;
+    
+    if (shell)
+	fprintf(stdout, "%s=%s\n", key, value);
+    else 
+	fprintf(stdout, "%s: %s\n", key, value);
+}
+
 int print_status_file(const char *file, int shell)
 {
     FILE *fp;
@@ -36,43 +48,40 @@
     fclose(fp);
 
     if (strcmp(file, "OGR") == 0) {
+	/* dsn (required) */
 	p = G_find_key_value("dsn", key_val);
 	if (!p)
 	    G_fatal_error(_("OGR datasource (dsn) not defined"));
-	if (shell)
-	    fprintf(stdout, "dsn=%s\n", p);
-	else 
-	    fprintf(stdout, "dsn: %s\n", p);
+	print_key_value("dsn", p, shell);
     
+	/* format (required) */
 	p = G_find_key_value("format", key_val);
 	if (!p)
 	    G_fatal_error(_("OGR format not defined"));
-	if (shell)
-	    fprintf(stdout, "format=%s\n", p);
-	else
-	    fprintf(stdout, "format: %s\n", p);
+	print_key_value("format", p, shell);
 	
+	/* options */
 	p = G_find_key_value("options", key_val);
-	if (shell)
-	    fprintf(stdout, "options=%s\n", p ? p : "");
-	else
-	    fprintf(stdout, _("options: %s\n"), p ? p : _("<none>"));
+	print_key_value("options", p, shell);
     }
     else { /* PG */
+	/* conninfo (required) */
 	p = G_find_key_value("conninfo", key_val);
 	if (!p)
 	    G_fatal_error(_("PG connection info (conninfo) not defined"));
-	if (shell)
-	    fprintf(stdout, "conninfo=%s\n", p);
-	else 
-	    fprintf(stdout, "conninfo: %s\n", p);
+	print_key_value("conninfo", p, shell);
+
+	/* schema */
 	p = G_find_key_value("schema", key_val);
-	if (p) {
-	    if (shell)
-		fprintf(stdout, "schema=%s\n", p);
-	    else 
-		fprintf(stdout, "schema: %s\n", p);
-	}
+	print_key_value("schema", p, shell);
+
+	/* fid */
+	p = G_find_key_value("fid", key_val);
+	print_key_value("fid", p, shell);
+
+	/* geometry_name */
+	p = G_find_key_value("geometry_name", key_val);
+	print_key_value("geometry_name", p, shell);
     }
 
     G_free_key_value(key_val);



More information about the grass-commit mailing list