[GRASS-SVN] r69650 - grass/branches/releasebranch_7_2/vector/v.external

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 3 08:11:43 PDT 2016


Author: martinl
Date: 2016-10-03 08:11:43 -0700 (Mon, 03 Oct 2016)
New Revision: 69650

Modified:
   grass/branches/releasebranch_7_2/vector/v.external/args.c
   grass/branches/releasebranch_7_2/vector/v.external/list.c
   grass/branches/releasebranch_7_2/vector/v.external/local_proto.h
   grass/branches/releasebranch_7_2/vector/v.external/main.c
Log:
v.external: change layer option to be not required (relbr72: merge r69649 from trunk)

Modified: grass/branches/releasebranch_7_2/vector/v.external/args.c
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.external/args.c	2016-10-03 15:08:44 UTC (rev 69649)
+++ grass/branches/releasebranch_7_2/vector/v.external/args.c	2016-10-03 15:11:43 UTC (rev 69650)
@@ -29,7 +29,7 @@
 				    "\t\tESRI Shapefile: shapefile name\n"
 				    "\t\tMapInfo File: mapinfo file name\n"
 				    "\t\tPostGIS database: table name");
-    options->layer->required = YES;
+    options->layer->required = NO;
     options->layer->key_desc = "name";
     options->layer->gisprompt = "old,datasource_layer,datasource_layer";
         

Modified: grass/branches/releasebranch_7_2/vector/v.external/list.c
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.external/list.c	2016-10-03 15:08:44 UTC (rev 69649)
+++ grass/branches/releasebranch_7_2/vector/v.external/list.c	2016-10-03 15:11:43 UTC (rev 69650)
@@ -18,10 +18,10 @@
 static char **format_list(int *, size_t *);
 static char *feature_type(const char *);
 #ifdef HAVE_OGR
-static int list_layers_ogr(FILE *, const char *, const char *, int);
+static int list_layers_ogr(FILE *, const char *, char **, int);
 #endif /* HAVE_OGR */
 #ifdef HAVE_POSTGRES
-static int list_layers_pg(FILE *, const char *, const char *, int);
+static int list_layers_pg(FILE *, const char *, char **, int);
 #endif /* HAVE_POSTGRES */
 
 int cmp(const void *a, const void *b)
@@ -102,7 +102,7 @@
     G_free(list);
 }
 
-int list_layers(FILE *fd, const char *dsn, const char *layer, int print_types, int use_ogr)
+int list_layers(FILE *fd, const char *dsn, char **layer, int print_types, int use_ogr)
 {
     if (!use_ogr) {
 #ifdef HAVE_POSTGRES
@@ -139,7 +139,7 @@
 }
 
 #ifdef HAVE_POSTGRES
-int list_layers_pg(FILE *fd, const char *conninfo, const char *table, int print_types)
+int list_layers_pg(FILE *fd, const char *conninfo, char **table, int print_types)
 {
     int   row, ntables, ret, print_schema;
     char *value_schema, *value_table, *value_type, *value_column;
@@ -171,7 +171,7 @@
     /* get schema & table_name */
     table_name = schema_name = NULL;
     if (table)
-	get_table_name(table, &table_name, &schema_name);
+	get_table_name(*table, &table_name, &schema_name);
     
     ntables = PQntuples(res);
     G_debug(3, "   nrows = %d", ntables);
@@ -235,7 +235,7 @@
 #endif /* HAVE_POSTGRES */
 
 #ifdef HAVE_OGR
-int list_layers_ogr(FILE *fd, const char *dsn, const char *layer, int print_types)
+int list_layers_ogr(FILE *fd, const char *dsn, char **layer, int print_types)
 {
     int i, ret;
     int nlayers;
@@ -263,12 +263,22 @@
     /* Make a list of available layers */
     nlayers = OGR_DS_GetLayerCount(Ogr_ds);
 
-    if (fd)
+    if (fd) {
 	G_message(n_("Data source <%s> (format '%s') contains %d layer:",
                      "Data source <%s> (format '%s') contains %d layers:",
                      nlayers),
 		  dsn, OGR_Dr_GetName(OGR_DS_GetDriver(Ogr_ds)), nlayers);
-    
+    }
+    else if (layer && !(*layer)) {
+        /* return first layer by default (if layer not defined) */
+        if (nlayers > 0) {
+            Ogr_layer = OGR_DS_GetLayer(Ogr_ds, 0);
+            Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
+            *layer = G_store((char *) OGR_FD_GetName(Ogr_featuredefn));
+            return 0;
+        }
+        return -1;
+    }
 
     G_get_default_window(&loc_wind);
     if (print_types && loc_wind.proj != PROJECTION_XY) {
@@ -333,7 +343,7 @@
 	    }
 	}
 	if (layer)
-	    if (strcmp(layer_name, layer) == 0) {
+	    if (strcmp(layer_name, *layer) == 0) {
 		ret = i;
 	    }
     }

Modified: grass/branches/releasebranch_7_2/vector/v.external/local_proto.h
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.external/local_proto.h	2016-10-03 15:08:44 UTC (rev 69649)
+++ grass/branches/releasebranch_7_2/vector/v.external/local_proto.h	2016-10-03 15:11:43 UTC (rev 69650)
@@ -18,7 +18,7 @@
 
 /* list.c */
 void list_formats();
-int list_layers(FILE *, const char *, const char *, int, int);
+int list_layers(FILE *, const char *, char **, int, int);
 void get_table_name(const char *, char **, char **);
 
 #endif

Modified: grass/branches/releasebranch_7_2/vector/v.external/main.c
===================================================================
--- grass/branches/releasebranch_7_2/vector/v.external/main.c	2016-10-03 15:08:44 UTC (rev 69649)
+++ grass/branches/releasebranch_7_2/vector/v.external/main.c	2016-10-03 15:11:43 UTC (rev 69650)
@@ -41,7 +41,7 @@
     FILE *fd;
     
     int ilayer, use_ogr;
-    char buf[GPATH_MAX], *dsn;
+    char buf[GPATH_MAX], *dsn, *layer;
     const char *output;
     
     G_gisinit(argv[0]);
@@ -108,22 +108,26 @@
         exit(EXIT_SUCCESS);
     }
 
+    /* get layer index/name */
+    layer = NULL;
+    if (options.layer->answer)
+        layer = G_store(options.layer->answer);
+    ilayer = list_layers(NULL, dsn, &layer,
+                         FALSE, use_ogr);
+    if (ilayer == -1) {
+        if (options.layer->answer)
+            G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
+        else
+            G_fatal_error(_("No layer defined"));
+    }
+    G_debug(2, "layer '%s' was found", layer);
+
     /* define name for output */
     if (!options.output->answer)
-        output = options.layer->answer;
+        output = layer;
     else
         output = options.output->answer;
-    
 
-    /* get layer index */
-    ilayer = list_layers(NULL, dsn, options.layer->answer,
-                         FALSE, use_ogr);
-    if (ilayer == -1) {
-        G_fatal_error(_("Layer <%s> not available"), options.layer->answer);
-    }
-    
-    G_debug(2, "layer '%s' was found", options.layer->answer);
-
     if (G_find_vector2(output, G_mapset()) && !G_check_overwrite(argc, argv)) {
         G_fatal_error(_("option <%s>: <%s> exists. To overwrite, use the --overwrite flag"),
                       options.output->key, output);
@@ -254,7 +258,7 @@
     if (!use_ogr) {
         char *table_name, *schema_name;
         
-        get_table_name(options.layer->answer, &table_name, &schema_name);
+        get_table_name(layer, &table_name, &schema_name);
         
         fprintf(fd, "format: postgis\n");
         fprintf(fd, "conninfo: %s\n", dsn);
@@ -268,7 +272,7 @@
     else {
         fprintf(fd, "format: ogr\n");
         fprintf(fd, "dsn: %s\n", dsn);
-        fprintf(fd, "layer: %s\n", options.layer->answer);
+        fprintf(fd, "layer: %s\n", layer);
     }
     fclose(fd);
     



More information about the grass-commit mailing list