[GRASS-SVN] r39525 - grass/trunk/vector/v.out.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 14 17:31:40 EDT 2009


Author: martinl
Date: 2009-10-14 17:31:40 -0400 (Wed, 14 Oct 2009)
New Revision: 39525

Added:
   grass/trunk/vector/v.out.ogr/create.c
Modified:
   grass/trunk/vector/v.out.ogr/args.c
   grass/trunk/vector/v.out.ogr/local_proto.h
   grass/trunk/vector/v.out.ogr/main.c
Log:
v.out.ogr: flag to create empty OGR layer added


Modified: grass/trunk/vector/v.out.ogr/args.c
===================================================================
--- grass/trunk/vector/v.out.ogr/args.c	2009-10-14 17:49:28 UTC (rev 39524)
+++ grass/trunk/vector/v.out.ogr/args.c	2009-10-14 21:31:40 UTC (rev 39525)
@@ -8,11 +8,15 @@
     options->input = G_define_standard_option(G_OPT_V_INPUT);
 
     options->type = G_define_standard_option(G_OPT_V3_TYPE);
+    options->type->label = _("Feature type(s)");
     options->type->description =
-	_("Feature type(s). Combination of types is not supported "
-	  "by all output formats. Default is to use first type found in input map.");
+	_("Combination of types is not supported "
+	  "by all output formats. Default is to use first type found in input vector map.");
     options->type->guisection = _("Input");
 
+    options->field = G_define_standard_option(G_OPT_V_FIELD);
+    options->field->guisection = _("Input");
+
     options->dsn = G_define_option();
     options->dsn->key = "dsn";
     options->dsn->type = TYPE_STRING;
@@ -30,9 +34,6 @@
     options->layer->description = _("For example: ESRI Shapefile: shapefile name");
     options->layer->guisection = _("Creation");
 
-    options->field = G_define_standard_option(G_OPT_V_FIELD);
-    options->field->guisection = _("Input");
-
     options->format = G_define_option();
     options->format->key = "format";
     options->format->type = TYPE_STRING;
@@ -85,6 +86,10 @@
     flags->poly->key = 'p';
     flags->poly->description = _("Export lines as polygons");
 
+    flags->new = G_define_flag();
+    flags->new->key = 'n';
+    flags->new->description = _("Create a new empty OGR layer in defined OGR datasource and exit. Nothing is read from input.");
+    
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 }

Added: grass/trunk/vector/v.out.ogr/create.c
===================================================================
--- grass/trunk/vector/v.out.ogr/create.c	                        (rev 0)
+++ grass/trunk/vector/v.out.ogr/create.c	2009-10-14 21:31:40 UTC (rev 39525)
@@ -0,0 +1,36 @@
+#include <grass/glocale.h>
+
+#include "local_proto.h"
+
+void create_ogr_layer(const char *dsn, const char *format, const char *layer,
+		      unsigned int wkbtype, const char **papszDSCO, const char **papszLCO)
+{
+    char *pszDriverName;
+    OGRSFDriverH hDriver;
+    OGRDataSourceH hDS;
+    OGRLayerH hLayer;
+    
+    pszDriverName = G_store(format);
+    G_strchg(pszDriverName, '_', ' '); /* '_' -> ' ' */
+
+    /* start driver */
+    hDriver = OGRGetDriverByName(pszDriverName);
+    if(hDriver == NULL) {
+        G_fatal_error(_("OGR driver <%s> not available"), pszDriverName);
+    }
+
+    /* create datasource */
+    hDS = OGR_Dr_CreateDataSource(hDriver, dsn, papszDSCO);
+    if(hDS == NULL) {
+        G_fatal_error(_("Creation of output OGR datasource <%s> failed"), dsn);
+    }
+    
+    G_free(pszDriverName);
+
+    /* create layer */
+    /* todo: SRS */
+    hLayer = OGR_DS_CreateLayer(hDS, layer, NULL, wkbtype, papszLCO);
+    if(hLayer == NULL) {
+        G_fatal_error(_("Creation of OGR layer <%s> failed"), layer);
+    }
+}


Property changes on: grass/trunk/vector/v.out.ogr/create.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:keywords
   + Author Date Id
Added: svn:eol-style
   + native

Modified: grass/trunk/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ogr/local_proto.h	2009-10-14 17:49:28 UTC (rev 39524)
+++ grass/trunk/vector/v.out.ogr/local_proto.h	2009-10-14 21:31:40 UTC (rev 39525)
@@ -11,7 +11,7 @@
 };
 
 struct Flags {
-    struct Flag *cat, *esristyle, *poly, *update, *nocat;
+    struct Flag *cat, *esristyle, *poly, *update, *nocat, *new;
 };
 
 /* args.c */
@@ -24,3 +24,7 @@
 
 /* list.c */
 char *OGR_list_write_drivers();
+
+/* create.c */
+void create_ogr_layer(const char *, const char *, const char *,
+		      unsigned int, const char **, const char **);

Modified: grass/trunk/vector/v.out.ogr/main.c
===================================================================
--- grass/trunk/vector/v.out.ogr/main.c	2009-10-14 17:49:28 UTC (rev 39524)
+++ grass/trunk/vector/v.out.ogr/main.c	2009-10-14 21:31:40 UTC (rev 39525)
@@ -5,7 +5,7 @@
  *
  * AUTHOR(S):    Radim Blazek
  *               Some extensions: Markus Neteler, Benjamin Ducke
- *               Uodate for GRASS 7 by Martin Landa <landa.martin gmail.com>
+ *               Update for GRASS 7 by Martin Landa <landa.martin gmail.com> (create new OGR layer)
  *
  * PURPOSE:      Converts GRASS vector to one of supported OGR vector formats.
  *
@@ -90,6 +90,99 @@
 	       &options, &flags);
     field = atoi(options.field->answer);
 
+    /* parse dataset creation options */
+    i = 0;
+    while (options.dsco->answers[i]) {
+	tokens = G_tokenize(options.dsco->answers[i], "=");
+	if (G_number_of_tokens(tokens))
+	    papszDSCO = CSLSetNameValue(papszDSCO, tokens[0], tokens[1]);
+	G_free_tokens(tokens);
+	i++;
+    }
+
+    /* parse layer creation options */
+    i = 0;
+    while (options.lco->answers[i]) {
+	tokens = G_tokenize(options.lco->answers[i], "=");
+	if (G_number_of_tokens(tokens))
+	    papszLCO = CSLSetNameValue(papszLCO, tokens[0], tokens[1]);
+	G_free_tokens(tokens);
+	i++;
+    }
+
+    /* check output feature type */
+    otype = Vect_option_to_types(options.type);
+
+    if (!options.layer->answer) {
+	char xname[GNAME_MAX],	xmapset[GMAPSET_MAX];
+
+	if (G_name_is_fully_qualified(options.input->answer, xname, xmapset))
+	    options.layer->answer = G_store(xname);
+	else
+	    options.layer->answer = G_store(options.input->answer);
+    }
+
+    if (otype & GV_POINTS)
+	wkbtype = wkbPoint;
+    else if (otype & GV_LINES)
+	wkbtype = wkbLineString;
+    else if (otype & GV_AREA)
+	wkbtype = wkbPolygon;
+    else if (otype & GV_FACE)
+	wkbtype = wkbPolygon25D;
+    else if (otype & GV_VOLUME)
+	wkbtype = wkbPolygon25D;
+
+    if (flags.poly->answer)
+	wkbtype = wkbPolygon;
+
+    if (((GV_POINTS & otype) && (GV_LINES & otype)) ||
+	((GV_POINTS & otype) && (GV_AREA & otype)) ||
+	((GV_POINTS & otype) && (GV_FACE & otype)) ||
+	((GV_POINTS & otype) && (GV_KERNEL & otype)) ||
+	((GV_POINTS & otype) && (GV_VOLUME & otype)) ||
+	((GV_LINES & otype) && (GV_AREA & otype)) ||
+	((GV_LINES & otype) && (GV_FACE & otype)) ||
+	((GV_LINES & otype) && (GV_KERNEL & otype)) ||
+	((GV_LINES & otype) && (GV_VOLUME & otype)) ||
+	((GV_KERNEL & otype) && (GV_POINTS & otype)) ||
+	((GV_KERNEL & otype) && (GV_LINES & otype)) ||
+	((GV_KERNEL & otype) && (GV_AREA & otype)) ||
+	((GV_KERNEL & otype) && (GV_FACE & otype)) ||
+	((GV_KERNEL & otype) && (GV_VOLUME & otype))
+
+	) {
+	G_warning(_("The combination of types is not supported"
+		    " by all formats."));
+	wkbtype = wkbUnknown;
+    }
+
+    /* fetch PROJ info */
+    G_get_default_window(&cellhd);
+    if (cellhd.proj == PROJECTION_XY)
+	Ogr_projection = NULL;
+    else {
+	projinfo = G_get_projinfo();
+	projunits = G_get_projunits();
+	Ogr_projection = GPJ_grass_to_osr(projinfo, projunits);
+	if (flags.esristyle->answer &&
+	    (strcmp(options.format->answer, "ESRI_Shapefile") == 0))
+	    OSRMorphToESRI(Ogr_projection);
+    }
+
+    /* create new OGR layer in datasource */
+    if (flags.new->answer) {
+	const char *name;
+	name = options.layer->answer ? options.layer->answer : options.input->answer;
+	
+	create_ogr_layer(options.dsn->answer, options.format->answer, name,
+			 wkbtype, papszDSCO, papszLCO);
+	
+	G_message(_("OGR layer <%s> created in datasource <%s> (format '%s')"),
+		  name, options.dsn->answer, options.format->answer);
+	exit(EXIT_SUCCESS);
+    }
+
     /* open input vector (topology required) */
     Vect_set_open_level(2);
     Vect_open_old(&In, options.input->answer, "");
@@ -162,53 +255,6 @@
             G_fatal_error(_("Could not determine input map's feature type(s)."));
     }
 
-    /* Check output type */
-    otype = Vect_option_to_types(options.type);
-
-    if (!options.layer->answer) {
-	char xname[GNAME_MAX],	xmapset[GMAPSET_MAX];
-
-	if (G_name_is_fully_qualified(options.input->answer, xname, xmapset))
-	    options.layer->answer = G_store(xname);
-	else
-	    options.layer->answer = G_store(options.input->answer);
-    }
-
-    if (otype & GV_POINTS)
-	wkbtype = wkbPoint;
-    else if (otype & GV_LINES)
-	wkbtype = wkbLineString;
-    else if (otype & GV_AREA)
-	wkbtype = wkbPolygon;
-    else if (otype & GV_FACE)
-	wkbtype = wkbPolygon25D;
-    else if (otype & GV_VOLUME)
-	wkbtype = wkbPolygon25D;
-
-    if (flags.poly->answer)
-	wkbtype = wkbPolygon;
-
-    if (((GV_POINTS & otype) && (GV_LINES & otype)) ||
-	((GV_POINTS & otype) && (GV_AREA & otype)) ||
-	((GV_POINTS & otype) && (GV_FACE & otype)) ||
-	((GV_POINTS & otype) && (GV_KERNEL & otype)) ||
-	((GV_POINTS & otype) && (GV_VOLUME & otype)) ||
-	((GV_LINES & otype) && (GV_AREA & otype)) ||
-	((GV_LINES & otype) && (GV_FACE & otype)) ||
-	((GV_LINES & otype) && (GV_KERNEL & otype)) ||
-	((GV_LINES & otype) && (GV_VOLUME & otype)) ||
-	((GV_KERNEL & otype) && (GV_POINTS & otype)) ||
-	((GV_KERNEL & otype) && (GV_LINES & otype)) ||
-	((GV_KERNEL & otype) && (GV_AREA & otype)) ||
-	((GV_KERNEL & otype) && (GV_FACE & otype)) ||
-	((GV_KERNEL & otype) && (GV_VOLUME & otype))
-
-	) {
-	G_warning(_("The combination of types is not supported"
-		    " by all formats."));
-	wkbtype = wkbUnknown;
-    }
-
     if (flags.cat->answer)
 	donocat = 1;
     else
@@ -221,19 +267,6 @@
 	G_warning(_("The map contains islands. With the -c flag, "
 	            "islands will appear as filled areas, not holes in the output map."));
 
-    /* fetch PROJ info */
-    G_get_default_window(&cellhd);
-    if (cellhd.proj == PROJECTION_XY)
-	Ogr_projection = NULL;
-    else {
-	projinfo = G_get_projinfo();
-	projunits = G_get_projunits();
-	Ogr_projection = GPJ_grass_to_osr(projinfo, projunits);
-	if (flags.esristyle->answer &&
-	    (strcmp(options.format->answer, "ESRI_Shapefile") == 0))
-	    OSRMorphToESRI(Ogr_projection);
-    }
-
     /* Open OGR DSN */
     G_debug(2, "driver count = %d", OGRGetDriverCount());
     drn = -1;
@@ -252,16 +285,6 @@
 	G_fatal_error(_("OGR driver <%s> not found"), options.format->answer);
     Ogr_driver = OGRGetDriver(drn);
 
-    /* parse dataset creation options */
-    i = 0;
-    while (options.dsco->answers[i]) {
-	tokens = G_tokenize(options.dsco->answers[i], "=");
-	if (G_number_of_tokens(tokens))
-	    papszDSCO = CSLSetNameValue(papszDSCO, tokens[0], tokens[1]);
-	G_free_tokens(tokens);
-	i++;
-    }
-
     if (flags.update->answer)  {
     	G_debug(1, "Update OGR data source");
         Ogr_ds = OGR_Dr_Open(Ogr_driver, options.dsn->answer, TRUE);
@@ -275,16 +298,6 @@
 	G_fatal_error(_("Unable to open OGR data source '%s'"),
 		      options.dsn->answer);
 
-    /* parse layer creation options */
-    i = 0;
-    while (options.lco->answers[i]) {
-	tokens = G_tokenize(options.lco->answers[i], "=");
-	if (G_number_of_tokens(tokens))
-	    papszLCO = CSLSetNameValue(papszLCO, tokens[0], tokens[1]);
-	G_free_tokens(tokens);
-	i++;
-    }
-
     /* check if the map is 3d */
     if (Vect_is_3d(&In)) {
 	/* specific check for shp */



More information about the grass-commit mailing list