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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Mar 19 17:38:57 EDT 2012


Author: martinl
Date: 2012-03-19 14:38:57 -0700 (Mon, 19 Mar 2012)
New Revision: 51114

Modified:
   grass/trunk/vector/v.external.out/Makefile
   grass/trunk/vector/v.external.out/args.c
   grass/trunk/vector/v.external.out/format.c
   grass/trunk/vector/v.external.out/link.c
   grass/trunk/vector/v.external.out/list.c
   grass/trunk/vector/v.external.out/local_proto.h
   grass/trunk/vector/v.external.out/main.c
   grass/trunk/vector/v.external.out/status.c
Log:
v.external.out: major update for pg support


Modified: grass/trunk/vector/v.external.out/Makefile
===================================================================
--- grass/trunk/vector/v.external.out/Makefile	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/Makefile	2012-03-19 21:38:57 UTC (rev 51114)
@@ -10,6 +10,6 @@
 
 include $(MODULE_TOPDIR)/include/Make/Module.make
 
-ifneq ($(USE_OGR),)
+ifneq ($(filter 1,$(USE_OGR) $(USE_POSTGRES)),)
 default: cmd
 endif

Modified: grass/trunk/vector/v.external.out/args.c
===================================================================
--- grass/trunk/vector/v.external.out/args.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/args.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -10,20 +10,31 @@
 {
     options->dsn = G_define_option();
     options->dsn->key = "dsn";
-    options->dsn->description = _("Name for output OGR datasource");
+    options->dsn->label = _("Name for output OGR/PostGIS data source");
+    options->dsn->description = _("Examples:\n"
+				  "\t\tESRI Shapefile: directory containing a shapefile\n"
+				  "\t\tMapInfo File: directory containing a mapinfo file\n"
+				  "\t\tPostGIS: connection string, eg. 'PG:dbname=db user=grass'");
+
     options->dsn->required = YES;
     options->dsn->type = TYPE_STRING;
 
     options->format = G_define_option();
     options->format->key = "format";
-    options->format->description = _("Format of output files");
+    options->format->label = _("Format for output vector data");
+    options->format->description = _("PostGIS is supported via PG ('format=PostGIS') or "
+				     "OGR (format=PostgreSQL) data driver");
     options->format->required = YES;
     options->format->type = TYPE_STRING;
     options->format->options = format_list();
  
     options->opts = G_define_option();
     options->opts->key = "options";
-    options->opts->description = _("Creation options");
+    options->opts->label = _("Creation options");
+    options->opts->description = _("Examples:\n"
+				  "\t\t'SHPT=POINTZ': use 3D point Shapefile data\n"
+				  "\t\t'GEOM_TYPE=geography': use geography PostGIS data\n"
+				  "\t\t'SCHEMA=grass': create new PostGIS tables in 'grass' schema");
     options->opts->required = NO;
     options->opts->multiple = YES;
     options->opts->type = TYPE_STRING;
@@ -36,7 +47,7 @@
 
     flags->r = G_define_flag();
     flags->r->key = 'r';
-    flags->r->description = _("Cease using OGR, revert to native output and exit");
+    flags->r->description = _("Cease using OGR/PostGIS, revert to native output and exit");
     flags->r->suppress_required = YES;
     flags->r->guisection = _("Native");
     

Modified: grass/trunk/vector/v.external.out/format.c
===================================================================
--- grass/trunk/vector/v.external.out/format.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/format.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -1,10 +1,16 @@
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
+#ifdef HAVE_OGR
 #include "ogr_api.h"
+#endif
 
 void check_format(char *format)
 {
+    if(strcmp(format, "PostGIS") == 0)
+	return;
+    
+#ifdef HAVE_OGR
     OGRSFDriverH driver;
     
     G_strchg(format, '_', ' ');
@@ -17,4 +23,5 @@
 	return;
 
     G_fatal_error(_("Format <%s> does not support writing"), format);
+#endif
 }

Modified: grass/trunk/vector/v.external.out/link.c
===================================================================
--- grass/trunk/vector/v.external.out/link.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/link.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -5,46 +5,78 @@
 #include <grass/glocale.h>
 
 void make_link(const char *dsn,
-	       const char *format, char **options)
+	       const char *format,
+	       char *option_str, char **options)
 {
-    struct Key_Value *key_val = G_create_key_value();
-    char *opt_str = NULL;
+    int i, use_ogr;
+    char *filename, *pg_schema;
     FILE *fp;
+    
+    struct Key_Value *key_val;
+    
+    key_val = G_create_key_value();
 
-    if (options && *options) {
-	int n_opts = 0, opt_len = 0, i;
-	char *p;
-
+    /* use OGR ? */
+    if (strcmp(format, "PostGIS") == 0) {
+	use_ogr  = FALSE;
+	filename = "PG";
+	G_remove("", "OGR");
+    }
+    else {
+	use_ogr  = TRUE;
+	filename = "OGR";
+	G_remove("", "PG");
+    }
+    
+    /* parse options */
+    pg_schema = NULL;
+    if (options && *options && !use_ogr) {
+	int   opt_len;
+	char *opt_schema;
+	
+	opt_schema = NULL;
 	for (i = 0; options[i]; i++) {
-	    n_opts++;
-	    opt_len += strlen(options[i]) + 1;
+	    if (G_strncasecmp("schema=", options[i], 6) == 0)
+		opt_schema = options[i];
+	    else
+		G_warning(_("Option '%s' ignored for 'PostGIS' format"),
+			  options[i]);
 	}
-
-	opt_str = G_malloc(opt_len);
-	p = opt_str;
-
-	for (i = 0; i < n_opts; i++) {
-	    if (i > 0)
-		*p++ = ',';
-	    strcpy(p, options[i]);
-	    p += strlen(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';
 	}
-	*p++ = '\0';
     }
-
-    if (dsn)
-	G_set_key_value("dsn", dsn, key_val);
-    if (format)
+    
+    /* add key/value items */
+    if (dsn) {
+	if (use_ogr)
+	    G_set_key_value("dsn", dsn, key_val);
+	else
+	    G_set_key_value("conninfo", dsn, key_val);
+    }
+    
+    if (format && use_ogr)
 	G_set_key_value("format", format, key_val);
-    if (opt_str)
-	G_set_key_value("options", opt_str, key_val);
-
-    fp = G_fopen_new("", "OGR");
+    
+    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);
+    
+    /* save file - OGR or PG */
+    fp = G_fopen_new("", filename);
     if (!fp)
-	G_fatal_error(_("Unable to create OGR file"));
+	G_fatal_error(_("Unable to create %s file"), filename);
 
     if (G_fwrite_key_value(fp, key_val) < 0)
-	G_fatal_error(_("Error writing OGR file"));
+	G_fatal_error(_("Error writing %s file"), filename);
 
     fclose(fp);
 }

Modified: grass/trunk/vector/v.external.out/list.c
===================================================================
--- grass/trunk/vector/v.external.out/list.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/list.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -1,22 +1,33 @@
+#include <string.h>
+
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
+#ifdef HAVE_OGR
 #include "ogr_api.h"
 
-static int cmp(const void *, const void *);
+int cmp(const void *a, const void *b) 
+{
+    return (strcmp(*(char **)a, *(char **)b));
+}
+#endif /* HAVE_OGR */
 
 char *format_list(void)
 {
     int i, count;
     size_t len;
-    OGRSFDriverH Ogr_driver;
-    char buf[2000];
     
     char **list, *ret;
 
     list = NULL;
     count = len = 0;
+    ret = NULL;
+
+#ifdef HAVE_OGR
+    char buf[2000];
     
+    OGRSFDriverH Ogr_driver;
+    
     /* Open OGR DSN */
     OGRRegisterAll();
     G_debug(2, "driver count = %d", OGRGetDriverCount());
@@ -39,7 +50,12 @@
     }
 
     qsort(list, count, sizeof(char *), cmp);
-
+#endif
+#ifdef HAVE_POSTGRES
+    list = G_realloc(list, (count + 1) * sizeof(char *));
+    list[count++] = G_store("PostGIS");
+    len += strlen("PostGIS") + 1;
+#endif 
     if (len > 0) {
 	ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
 	*ret = '\0';
@@ -56,12 +72,15 @@
     }
     
     G_debug(2, "all drivers: %s", ret);
-
+    
     return ret;
 }
 
 void list_formats(void)
 {
+    G_message(_("List of supported formats:"));
+
+#ifdef HAVE_OGR
     /* -------------------------------------------------------------------- */
     /*      List supported formats and exit.                                */
     /*         code from GDAL 1.2.5  gcore/gdal_misc.cpp                    */
@@ -70,19 +89,18 @@
     int iDr;
     OGRSFDriverH driver;
 
-    G_message(_("Supported Formats:"));
+
     for (iDr = 0; iDr < OGRGetDriverCount(); iDr++) {
 	driver = OGRGetDriver(iDr);
 
 	if (!OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
 	    continue;
 
-	fprintf(stdout, " %s\n", OGR_Dr_GetName(driver));
+	fprintf(stdout, "%s\n", OGR_Dr_GetName(driver));
     }
+#endif
+#ifdef HAVE_POSTGRES
+    fprintf(stdout, "PostGIS\n");
+#endif
     fflush(stdout);
 }
-
-int cmp(const void *a, const void *b) 
-{
-    return (strcmp(*(char **)a, *(char **)b));
-}

Modified: grass/trunk/vector/v.external.out/local_proto.h
===================================================================
--- grass/trunk/vector/v.external.out/local_proto.h	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/local_proto.h	2012-03-19 21:38:57 UTC (rev 51114)
@@ -18,7 +18,7 @@
 
 /* link.c */
 void make_link(const char *,
-	       const char *, char **);
+	       const char *, const char *, char **);
 
 /* list.c */
 char *format_list(void);

Modified: grass/trunk/vector/v.external.out/main.c
===================================================================
--- grass/trunk/vector/v.external.out/main.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/main.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -15,11 +15,16 @@
  *
  **************************************************************/
 
+#include <stdlib.h>
+
 #include <grass/gis.h>
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#ifdef HAVE_OGR
 #include "ogr_api.h"
+#endif
+
 #include "local_proto.h"
 
 int main(int argc, char *argv[])
@@ -35,11 +40,13 @@
     G_add_keyword(_("export"));
     G_add_keyword(_("output"));
     G_add_keyword(_("external"));
-    module->description =
-	_("Defines vector output format utilizing OGR library.");
+    G_add_keyword(_("OGR"));
+    G_add_keyword(_("PostGIS"));
+    module->description = _("Defines vector output format.");
 
+#ifdef HAVE_OGR
     OGRRegisterAll();
-
+#endif
     parse_args(argc, argv, &options, &flags);
 
     if (flags.f->answer) {
@@ -48,7 +55,8 @@
     }
 
     if (flags.r->answer) {
-	G_remove("", "OGR");
+	if (G_remove("", "OGR") == 0)
+	    G_remove("", "PG");
 	exit(EXIT_SUCCESS);
     }
 
@@ -56,8 +64,8 @@
 	check_format(options.format->answer);
 
     if (options.dsn->answer)
-	make_link(options.dsn->answer,
-		  options.format->answer, options.opts->answers);
+	make_link(options.dsn->answer,  options.format->answer,
+		  options.opts->answer, options.opts->answers);
     
     if (flags.p->answer || flags.g->answer) {
 	print_status(flags.g->answer ? 1 : 0);

Modified: grass/trunk/vector/v.external.out/status.c
===================================================================
--- grass/trunk/vector/v.external.out/status.c	2012-03-19 20:23:14 UTC (rev 51113)
+++ grass/trunk/vector/v.external.out/status.c	2012-03-19 21:38:57 UTC (rev 51114)
@@ -1,49 +1,81 @@
 #include <stdlib.h>
+#include <string.h>
 
 #include <grass/gis.h>
 #include <grass/glocale.h>
 
+static int print_status_file(const char *, int shell);
+
 void print_status(int shell)
 {
+    /* try to print OGR first then PG if needed */
+    if (print_status_file("OGR", shell))
+	return;
+    
+    if (print_status_file("PG", shell))
+	return;
+
+    if (shell)
+	fprintf(stdout, "format=%s\n", "native");
+    else
+	fprintf(stdout, _("format: native\n"));
+}
+
+int print_status_file(const char *file, int shell)
+{
     FILE *fp;
-    struct Key_Value *key_val;
     const char *p;
 
-    if (!G_find_file2("", "OGR", G_mapset())) {
-	if (shell)
-	    fprintf(stdout, "format=%s\n", "native");
-	else
-	    fprintf(stdout, _("format: native\n"));
-	return;
-    }
-
-    fp = G_fopen_old("", "OGR", G_mapset());
+    struct Key_Value *key_val;
+    
+    fp = G_fopen_old("", file, G_mapset());
     if (!fp)
-	G_fatal_error(_("Unable to open OGR file"));
+	return FALSE;
+    
     key_val = G_fread_key_value(fp);
     fclose(fp);
 
-    p = G_find_key_value("dsn", key_val);
-    if (shell)
-	fprintf(stdout, "dsn=%s\n",
-		p ? p : "ogr");
-    else 
-	fprintf(stdout, _("dsn: %s\n"),
-		p ? p : _("not set (default 'ogr')"));
+    if (strcmp(file, "OGR") == 0) {
+	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);
+    
+	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);
+	
+	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>"));
+    }
+    else { /* PG */
+	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);
+	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);
+	}
+    }
 
-    p = G_find_key_value("format", key_val);
-    if (shell)
-	fprintf(stdout, "format=%s\n",
-		p ? p : "ESRI_Shapefile");
-    else
-	fprintf(stdout, _("format: %s\n"),
-		p ? p : _("not set (default ESRI_Shapefile)"));
-    
-    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>"));
-    
     G_free_key_value(key_val);
+
+    return TRUE;
 }



More information about the grass-commit mailing list