[GRASS-SVN] r60585 - grass/trunk/vector/v.external.out
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu May 29 05:54:13 PDT 2014
Author: martinl
Date: 2014-05-29 05:54:13 -0700 (Thu, 29 May 2014)
New Revision: 60585
Modified:
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/local_proto.h
grass/trunk/vector/v.external.out/main.c
grass/trunk/vector/v.external.out/status.c
grass/trunk/vector/v.external.out/v.external.out.html
Log:
v.external.out: implement input/output options
Modified: grass/trunk/vector/v.external.out/args.c
===================================================================
--- grass/trunk/vector/v.external.out/args.c 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/args.c 2014-05-29 12:54:13 UTC (rev 60585)
@@ -16,13 +16,13 @@
"\t\tESRI Shapefile: directory containing a shapefile\n"
"\t\tMapInfo File: directory containing a mapinfo file\n"
"\t\tPostGIS database: connection string, eg. 'PG:dbname=db user=grass'");
- options->dsn->required = YES;
+ options->dsn->required = NO;
+ options->dsn->guisection = _("Settings");
-
options->format = G_define_option();
options->format->key = "format";
options->format->description = _("Format for output vector data");
- options->format->required = YES;
+ options->format->required = NO;
options->format->type = TYPE_STRING;
options->format->options = format_options();
#ifdef HAVE_OGR
@@ -32,7 +32,8 @@
options->format->answer = "PostgreSQL";
#endif /* HAVE_POSTGRES */
#endif /* HAVE_OGR */
-
+ options->format->guisection = _("Settings");
+
options->opts = G_define_option();
options->opts->key = "options";
options->opts->label = _("Creation options");
@@ -43,7 +44,17 @@
options->opts->required = NO;
options->opts->multiple = YES;
options->opts->type = TYPE_STRING;
+ options->opts->guisection = _("Settings");
+ options->input = G_define_standard_option(G_OPT_F_INPUT);
+ options->input->required = NO;
+ options->input->description = _("Name of input file to read settings from");
+ options->input->guisection = _("Settings");
+
+ options->output = G_define_standard_option(G_OPT_F_OUTPUT);
+ options->output->required = NO;
+ options->output->description = _("Name for output file where to save current settings");
+
flags->f = G_define_flag();
flags->f->key = 'f';
flags->f->description = _("List supported formats and exit");
@@ -70,4 +81,21 @@
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
+
+ /* check options */
+ if (options->dsn->answer && options->format->answer &&
+ options->input->answer)
+ G_fatal_error(_("Options <%s/%s> and <%s> are mutually exclusive"),
+ options->dsn->key, options->format->key,
+ options->input->key);
+ if (flags->f->answer || flags->p->answer || flags->r->answer || flags->g->answer ||
+ options->output->answer)
+ return;
+
+ if (!options->dsn->answer && !options->input->answer)
+ G_fatal_error(_("One of options <%s> or <%s> must be specified"),
+ options->dsn->key, options->input->key);
+ if (options->dsn->answer && !options->format->answer)
+ G_fatal_error(_("Option <%s> must be specified"),
+ options->format->key);
}
Modified: grass/trunk/vector/v.external.out/format.c
===================================================================
--- grass/trunk/vector/v.external.out/format.c 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/format.c 2014-05-29 12:54:13 UTC (rev 60585)
@@ -7,23 +7,47 @@
#include "ogr_api.h"
#endif
+int is_ogr(const char *format)
+{
+ int use_ogr = TRUE;
+
+ if(strcmp(format, "PostgreSQL") == 0) {
+#if defined HAVE_OGR && defined HAVE_POSTGRES
+ if (!getenv("GRASS_VECTOR_OGR"))
+ use_ogr = FALSE;
+#else
+#ifdef HAVE_POSTGRES
+ if (getenv("GRASS_VECTOR_OGR"))
+ G_warning(_("Environment variable GRASS_VECTOR_OGR defined, "
+ "but GRASS is compiled with OGR support. "
+ "Using GRASS-PostGIS data driver instead."));
+ use_ogr = FALSE;
+#else /* -> force using OGR */
+ G_warning(_("GRASS is not compiled with PostgreSQL support. "
+ "Using OGR-PostgreSQL driver instead of native "
+ "GRASS-PostGIS data driver."));
+#endif /* HAVE_POSTRES */
+#endif /* HAVE_OGR && HAVE_POSTGRES */
+ }
+
+ return use_ogr;
+}
+
void check_format(char *format)
{
- if(strcmp(format, "PostgreSQL") == 0)
- return;
+ if (!is_ogr(format))
+ return;
#ifdef HAVE_OGR
OGRSFDriverH driver;
G_strchg(format, '_', ' ');
driver = OGRGetDriverByName(format);
-
+
if (!driver)
- G_fatal_error(_("Format <%s> not supported"), format);
-
- if (OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
- return;
-
- G_fatal_error(_("Format <%s> does not support writing"), format);
+ G_fatal_error(_("Format <%s> not supported"), format);
+
+ if (!OGR_Dr_TestCapability(driver, ODrCCreateDataSource))
+ 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 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/link.c 2014-05-29 12:54:13 UTC (rev 60585)
@@ -4,6 +4,8 @@
#include <grass/gis.h>
#include <grass/glocale.h>
+#include "local_proto.h"
+
static int parse_option_pg(const char *, char **, char **);
void make_link(const char *dsn_opt,
@@ -25,44 +27,16 @@
"format (\"%s\" given)"), format);
/* use OGR ? */
- if (strcmp(format, "PostgreSQL") == 0) {
-#if defined HAVE_OGR && defined HAVE_POSTGRES
- if (getenv("GRASS_VECTOR_OGR")) {
- use_ogr = TRUE;
- filename = "OGR";
- G_remove("", "PG");
- }
- else {
- use_ogr = FALSE;
- filename = "PG";
- G_remove("", "OGR");
-
- }
-#else
-#ifdef HAVE_POSTGRES
- if (getenv("GRASS_VECTOR_OGR"))
- G_warning(_("Environment variable GRASS_VECTOR_OGR defined, "
- "but GRASS is compiled with OGR support. "
- "Using GRASS-PostGIS data driver instead."));
- use_ogr = FALSE;
- filename = "PG";
- G_remove("", "OGR");
-#else /* -> force using OGR */
- G_warning(_("GRASS is not compiled with PostgreSQL support. "
- "Using OGR-PostgreSQL driver instead of native "
- "GRASS-PostGIS data driver."));
- use_ogr = TRUE;
- filename = "OGR";
- G_remove("", "PG");
-#endif /* HAVE_POSTRES */
-#endif /* HAVE_OGR && HAVE_POSTGRES */
- } /* format=PostgreSQL */
+ use_ogr = is_ogr(format);
+ if (use_ogr) {
+ filename = "OGR";
+ G_remove("", "PG");
+ }
else {
- use_ogr = TRUE;
- filename = "OGR";
- G_remove("", "PG");
+ filename = "PG";
+ G_remove("", "OGR");
}
-
+
/* be friendly, ignored 'PG:' prefix for GRASS-PostGIS data driver */
if (!use_ogr && strcmp(format, "PostgreSQL") == 0 &&
G_strncasecmp(dsn_opt, "PG:", 3) == 0) {
@@ -99,21 +73,22 @@
G_set_key_value("conninfo", dsn, key_val);
}
- /* OGR only */
- if (use_ogr) {
+ if (use_ogr) { /* OGR */
if (format)
G_set_key_value("format", format, key_val);
if (option_str)
G_set_key_value("options", option_str, key_val);
}
-
+ else { /* PG */
+ G_set_key_value("format", "PostgreSQL", key_val);
+ }
/* save file - OGR or PG */
fp = G_fopen_new("", filename);
if (!fp)
- G_fatal_error(_("Unable to create <%s> file"), filename);
+ G_fatal_error(_("Unable to create settings file"));
if (G_fwrite_key_value(fp, key_val) < 0)
- G_fatal_error(_("Error writing <%s> file"), filename);
+ G_fatal_error(_("Error writing settings file"));
fclose(fp);
@@ -146,3 +121,4 @@
return 0;
}
+
Modified: grass/trunk/vector/v.external.out/local_proto.h
===================================================================
--- grass/trunk/vector/v.external.out/local_proto.h 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/local_proto.h 2014-05-29 12:54:13 UTC (rev 60585)
@@ -2,7 +2,7 @@
#define V_EXTERNAL_OUT_LOCAL_PROTO_H
struct _options {
- struct Option *dsn, *format, *opts;
+ struct Option *dsn, *format, *opts, *input, *output;
};
struct _flags {
@@ -14,11 +14,12 @@
struct _options *, struct _flags*);
/* format.c */
-void check_format(char *);
+int check_format(char *);
+int is_ogr(const char *);
/* link.c */
void make_link(const char *,
- const char *, const char *, char **);
+ const char *, char *, char **);
/* list.c */
char *format_options(void);
@@ -26,5 +27,7 @@
/* status.c */
void print_status(int);
+int save_status_file(const struct Option *);
+int read_status_file(const struct Option *);
#endif
Modified: grass/trunk/vector/v.external.out/main.c
===================================================================
--- grass/trunk/vector/v.external.out/main.c 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/main.c 2014-05-29 12:54:13 UTC (rev 60585)
@@ -79,10 +79,17 @@
make_link(options.dsn->answer, format,
options.opts->answer, options.opts->answers);
}
+ else if (options.input->answer) {
+ read_status_file(options.input);
+ }
if (flags.p->answer || flags.g->answer) {
print_status(flags.g->answer ? TRUE : FALSE);
}
+ if (options.output->answer) {
+ save_status_file(options.output);
+ }
+
exit(EXIT_SUCCESS);
}
Modified: grass/trunk/vector/v.external.out/status.c
===================================================================
--- grass/trunk/vector/v.external.out/status.c 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/status.c 2014-05-29 12:54:13 UTC (rev 60585)
@@ -4,8 +4,11 @@
#include <grass/gis.h>
#include <grass/glocale.h>
+#include "local_proto.h"
+
static int print_status_file(const char *, int);
static void print_key_value(const char *, const char *, int);
+static void check_required_options(struct Key_Value *, int);
void print_status(int shell)
{
@@ -37,7 +40,6 @@
{
int i;
FILE *fp;
- const char *p;
struct Key_Value *key_val;
@@ -49,18 +51,71 @@
fclose(fp);
/* check required options */
- if (strcmp(file, "OGR") == 0) {
+ check_required_options(key_val, strcmp(file, "OGR") == 0);
+
+ /* print all options */
+ for (i = 0; i < key_val->nitems; i++)
+ print_key_value(key_val->key[i], key_val->value[i], shell);
+
+ G_free_key_value(key_val);
+
+ return TRUE;
+}
+
+int save_status_file(const struct Option *file)
+{
+ int use_ogr;
+ FILE *fp_input, *fp_output;
+
+ struct Key_Value *key_val;
+
+ /* read settings file */
+ use_ogr = FALSE;
+ fp_input = G_fopen_old("", "PG", G_mapset());
+ if (!fp_input) {
+ use_ogr = TRUE;
+ fp_input = G_fopen_old("", "OGR", G_mapset());
+ }
+ if (!fp_input)
+ G_fatal_error(_("No settings defined"));
+
+ /* read settings from file */
+ key_val = G_fread_key_value(fp_input);
+ fclose(fp_input);
+
+ /* check required options */
+ check_required_options(key_val, use_ogr);
+
+ /* open output file */
+ fp_output = G_open_option_file(file);
+
+ /* write settings to output file */
+ G_fwrite_key_value(fp_output, key_val);
+
+ G_close_option_file(fp_output);
+
+ G_free_key_value(key_val);
+
+ return TRUE;
+}
+
+void check_required_options(struct Key_Value *key_val, int use_ogr)
+{
+ const char *p;
+
+ /* format (required) */
+ p = G_find_key_value("format", key_val);
+ if (!p)
+ G_fatal_error(_("Format not defined"));
+
+ /* check required options */
+ if (use_ogr) { /* OGR */
/* dsn (required) */
p = G_find_key_value("dsn", key_val);
if (!p)
G_fatal_error(_("OGR datasource (dsn) not defined"));
-
- /* format (required) */
- p = G_find_key_value("format", key_val);
- if (!p)
- G_fatal_error(_("OGR format not defined"));
}
- else { /* PG */
+ else { /* PG */
char dsn_name[GNAME_MAX];
/* conninfo (required) */
@@ -68,17 +123,43 @@
if (!p)
G_fatal_error(_("PG connection info (conninfo) not defined"));
- /* print also dsn for compatibility */
+ /* add dsn for compatibility */
sprintf(dsn_name, "PG:%s", p);
G_set_key_value("dsn", dsn_name, key_val);
- /* force format */
- print_key_value("format", "PostgreSQL", shell);
}
+}
- /* print all options */
- for (i = 0; i < key_val->nitems; i++)
- print_key_value(key_val->key[i], key_val->value[i], shell);
+int read_status_file(const struct Option *file)
+{
+ int use_ogr;
+ const char *format;
+ FILE *fp_input, *fp_output;
+
+ struct Key_Value *key_val;
+ /* read settings file */
+ fp_input = G_open_option_file(file);
+
+ /* read settings from file */
+ key_val = G_fread_key_value(fp_input);
+ G_close_option_file(fp_input);
+
+ format = G_find_key_value("format", key_val);
+ if (!format)
+ G_fatal_error(_("Format not defined"));
+ use_ogr = is_ogr(format);
+
+ /* check required options */
+ check_required_options(key_val, use_ogr);
+
+ /* write settings to output file */
+ fp_output = G_fopen_new("", use_ogr ? "OGR" : "PG");
+ if (!fp_output)
+ G_fatal_error(_("Unable to create settings file"));
+ if (G_fwrite_key_value(fp_output, key_val) < 0)
+ G_fatal_error(_("Error writing settings file"));
+ fclose(fp_output);
+
G_free_key_value(key_val);
return TRUE;
Modified: grass/trunk/vector/v.external.out/v.external.out.html
===================================================================
--- grass/trunk/vector/v.external.out/v.external.out.html 2014-05-29 11:25:48 UTC (rev 60584)
+++ grass/trunk/vector/v.external.out/v.external.out.html 2014-05-29 12:54:13 UTC (rev 60585)
@@ -147,6 +147,33 @@
v.external.out -r
</pre></div>
+<h3>Restore settings</h3>
+
+Current settings can be stored to file by specifying <b>output</b> option.
+
+<div class="code"><pre>
+# define output PostGIS database for GRASS calculation results stored as topological elements:
+v.external.out dsn=PG:dbname=gisdb format=PostgreSQL options=topology=YES output=gisdb_topo.txt
+
+# do some processing in PostGIS Topology
+</pre></div>
+
+Back to native format:
+
+<div class="code"><pre>
+v.external.out -r
+
+# do some processing in native format
+</pre></div>
+
+Restore previous settings from "gisdb_topo.txt" file by specifying <b>input</b> option.
+
+<div class="code"><pre>
+v.external.out input=gisdb_topo.txt
+
+# do some processing in PostGIS Topology
+</pre></div>
+
<h2>REFERENCES</h2>
<ul>
More information about the grass-commit
mailing list