[GRASS-SVN] r45943 - grass/branches/releasebranch_6_4/vector/v.out.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 13 09:12:21 EDT 2011


Author: martinl
Date: 2011-04-13 06:12:21 -0700 (Wed, 13 Apr 2011)
New Revision: 45943

Modified:
   grass/branches/releasebranch_6_4/vector/v.out.ogr/description.html
   grass/branches/releasebranch_6_4/vector/v.out.ogr/main.c
Log:
v.out.ogr: sync code with devbr6


Modified: grass/branches/releasebranch_6_4/vector/v.out.ogr/description.html
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.out.ogr/description.html	2011-04-13 13:10:38 UTC (rev 45942)
+++ grass/branches/releasebranch_6_4/vector/v.out.ogr/description.html	2011-04-13 13:12:21 UTC (rev 45943)
@@ -25,8 +25,10 @@
 
 To export areas with holes into, e.g., a Shapefile, while keeping the 
 holes as holes, the flag <em>-c</em> has to be used.
+<p>
+The "-z" flag can be used to automatically export a 3D map to a 3D Shapefile, instead 
+of setting the correct <em>lco=</em> option manually.
 
-
 <h2>EXAMPLES</h2>
 
 <h3>Export to Shapefile</h3>
@@ -55,6 +57,11 @@
 <div class="code"><pre>
 v.out.ogr input=objects_3d type=face dsn=faces_3d.shp lco="SHPT=POLYGONZ"
 </pre></div>
+<p>
+Export 3D faces from GRASS vector map to Shapefile format, automatic 3D setting:
+<div class="code"><pre>
+v.out.ogr input=objects_3d type=face dsn=faces_3d.shp -z"
+</pre></div>
 
 <h3>Export to GML</h3>
 

Modified: grass/branches/releasebranch_6_4/vector/v.out.ogr/main.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.out.ogr/main.c	2011-04-13 13:10:38 UTC (rev 45942)
+++ grass/branches/releasebranch_6_4/vector/v.out.ogr/main.c	2011-04-13 13:12:21 UTC (rev 45943)
@@ -27,14 +27,21 @@
 #include "ogr_api.h"
 #include "cpl_string.h"
 
+
+/* some hard limits */
+#define SQL_BUFFER_SIZE 2000
+#define MAX_OGR_DRIVERS 2000
+
+
 int fout, fskip;		/* features written/ skip */
 int nocat, noatt, nocatskip;	/* number of features without cats/atts written/skip */
 
 int mk_att(int cat, struct field_info *Fi, dbDriver * Driver,
-	   int ncol, int doatt, int nocat, OGRFeatureH Ogr_feature);
+	   int ncol, int doatt, int nocat, OGRFeatureH Ogr_feature,
+	   dbCursor cursor);
 
 char *OGR_list_write_drivers();
-char OGRdrivers[2000];
+char OGRdrivers[MAX_OGR_DRIVERS];
 
 int main(int argc, char *argv[])
 {
@@ -42,12 +49,14 @@
     int num_to_export;
     char *mapset;
     int field;
+    int overwrite, found;
     struct GModule *module;
     struct Option *in_opt, *dsn_opt, *layer_opt, *type_opt, *frmt_opt,
 	*field_opt, *dsco, *lco;
-    struct Flag *cat_flag, *esristyle, *poly_flag, *update_flag, *nocat_flag;
-    char buf[2000];
-    char key1[200], key2[200];
+    struct Flag *cat_flag, *esristyle, *poly_flag, *update_flag, *nocat_flag,
+	*shapez_flag;
+    char buf[SQL_BUFFER_SIZE];
+    char key1[SQL_BUFFER_SIZE], key2[SQL_BUFFER_SIZE];
     struct Key_Value *projinfo, *projunits;
     struct Cell_head cellhd;
     char **tokens;
@@ -66,6 +75,7 @@
     dbTable *Table;
     dbString dbstring;
     dbColumn *Column;
+    dbCursor cursor;
 
     /* OGR */
     int drn, ogr_ftype = OFTInteger;
@@ -87,7 +97,7 @@
     module = G_define_module();
     module->keywords = _("vector, export");
     module->description =
-	_("Converts to one of the supported OGR vector formats.");
+	_("Converts GRASS vector map to one of the supported OGR vector formats.");
 
     in_opt = G_define_standard_option(G_OPT_V_INPUT);
 
@@ -95,8 +105,8 @@
     type_opt->options = "point,line,boundary,centroid,area,face,kernel,auto";
     type_opt->answer = "line,boundary";
     type_opt->description =
-	_("Feature type. Combination of types is not supported "
-	  "by all formats.");
+	_("Feature type(s). Combinations not supported "
+	  "by all output formats. Default: first type found in input.");
     type_opt->guisection = _("Input");
 
     dsn_opt = G_define_option();
@@ -168,6 +178,11 @@
     esristyle->description = _("Use ESRI-style .prj file format "
 			       "(applies to Shapefile output only)");
 
+    shapez_flag = G_define_flag();
+    shapez_flag->key = 'z';
+    shapez_flag->description = _("Create 3D output if input is 3D "
+				 "(applies to Shapefile output only)");
+
     poly_flag = G_define_flag();
     poly_flag->key = 'p';
     poly_flag->description = _("Export lines as polygons");
@@ -231,6 +246,24 @@
 	    G_debug(3, "Adding faces to export list.");
 	    num_types++;
 	}
+	/* this check HAS TO FOLLOW RIGHT AFTER check for GV_FACE! */
+	if (Vect_get_num_volumes(&In) > 0) {
+	    G_warning(_("Volumes will be exported as sets of faces."));
+	    if (num_types == 0) {
+		/* no other types yet? */
+		type_opt->answers[num_types] = strdup("volume");
+		G_debug(3, "Adding volumes to export list.");
+		num_types++;
+	    }
+	    else {
+		if (strcmp(type_opt->answers[num_types - 1], "face")) {
+		    /* only put faces on export list if that's not the case already */
+		    type_opt->answers[num_types] = strdup("volume");
+		    G_debug(3, "Adding volumes to export list.");
+		    num_types++;
+		}
+	    }
+	}
 
 	if (num_types == 0)
 	    G_fatal_error(_("Could not determine input map's feature type(s)."));
@@ -276,6 +309,7 @@
 	((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."));
@@ -293,6 +327,133 @@
     if (Vect_get_num_islands(&In) > 0 && !cat_flag->answer)
 	G_warning(_("The map contains islands. To preserve them in the output map, use the -c flag"));
 
+
+    /* check what users wants to export and what's present in the map */
+    if (Vect_get_num_primitives(&In, GV_POINT) > 0 && !(otype & GV_POINTS))
+	G_warning(_("%d point(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									 GV_POINT));
+
+    if (Vect_get_num_primitives(&In, GV_LINE) > 0 && !(otype & GV_LINES))
+	G_warning(_("%d line(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									 GV_LINE));
+
+    if (Vect_get_num_primitives(&In, GV_BOUNDARY) > 0 &&
+	!(otype & GV_BOUNDARY) && !(otype & GV_AREA))
+	G_warning(_("%d boundary(ies) found, but not requested to be exported. "
+		   "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									GV_BOUNDARY));
+
+    if (Vect_get_num_primitives(&In, GV_CENTROID) > 0 &&
+	!(otype & GV_CENTROID) && !(otype & GV_AREA))
+	G_warning(_("%d centroid(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									 GV_CENTROID));
+
+    if (Vect_get_num_areas(&In) > 0 && !(otype & GV_AREA))
+	G_warning(_("%d areas found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_areas(&In));
+
+    if (Vect_get_num_primitives(&In, GV_FACE) > 0 && !(otype & GV_FACE))
+	G_warning(_("%d face(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									 GV_FACE));
+
+    if (Vect_get_num_primitives(&In, GV_KERNEL) > 0 &&
+	!(otype & GV_KERNEL) && !(otype & GV_VOLUME))
+	G_warning(_("%d kernel(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
+									 GV_KERNEL));
+
+    if (Vect_get_num_volumes(&In) > 0 && !(otype & GV_VOLUME))
+	G_warning(_("%d volume(s) found, but not requested to be exported. "
+		    "Verify 'type' parameter."), Vect_get_num_volumes(&In));
+
+    /* warn and eventually abort if there is nothing to be exported */
+    num_to_export = 0;
+    if (Vect_get_num_primitives(&In, GV_POINT) < 1 && (otype & GV_POINTS)) {
+	G_warning(_("No points found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_POINT)
+	    num_to_export += Vect_get_num_primitives(&In, GV_POINT);
+    }
+
+    if (Vect_get_num_primitives(&In, GV_LINE) < 1 && (otype & GV_LINE)) {
+	G_warning(_("No lines found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_LINE)
+	    num_to_export += Vect_get_num_primitives(&In, GV_LINE);
+    }
+
+    if (Vect_get_num_primitives(&In, GV_BOUNDARY) < 1 &&
+	(otype & GV_BOUNDARY)) {
+	G_warning(_("No boundaries found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_BOUNDARY)
+	    num_to_export += Vect_get_num_primitives(&In, GV_BOUNDARY);
+    }
+
+    if (Vect_get_num_areas(&In) < 1 && (otype & GV_AREA)) {
+	G_warning(_("No areas found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_AREA)
+	    num_to_export += Vect_get_num_areas(&In);
+    }
+
+    if (Vect_get_num_primitives(&In, GV_CENTROID) < 1 &&
+	(otype & GV_CENTROID)) {
+	G_warning(_("No centroids found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_CENTROID)
+	    num_to_export += Vect_get_num_primitives(&In, GV_CENTROID);
+    }
+
+    if (Vect_get_num_primitives(&In, GV_FACE) < 1 && (otype & GV_FACE)) {
+	G_warning(_("No faces found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_FACE)
+	    num_to_export += Vect_get_num_primitives(&In, GV_FACE);
+    }
+
+    if (Vect_get_num_primitives(&In, GV_KERNEL) < 1 && (otype & GV_KERNEL)) {
+	G_warning(_("No kernels found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_KERNEL)
+	    num_to_export += Vect_get_num_primitives(&In, GV_KERNEL);
+    }
+
+    if (Vect_get_num_volumes(&In) < 1 && (otype & GV_VOLUME)) {
+	G_warning(_("No volumes found, but requested to be exported. "
+		    "Will skip this geometry type."));
+    }
+    else {
+	if (otype & GV_VOLUME)
+	    num_to_export += Vect_get_num_volumes(&In);
+    }
+
+    G_debug(1, "Requested to export %d geometries", num_to_export);
+
+    if (num_to_export < 1) {
+	G_warning(_("Nothing to export"));
+	exit(EXIT_SUCCESS);
+    }
+
+
     /* fetch PROJ info */
     G_get_default_window(&cellhd);
     if (cellhd.proj == PROJECTION_XY)
@@ -349,6 +510,28 @@
 	G_fatal_error(_("Unable to open OGR data source '%s'"),
 		      dsn_opt->answer);
 
+    /* check if OGR layer exists */
+    overwrite = G_check_overwrite(argc, argv);
+    found = FALSE;
+    for (i = 0; i < OGR_DS_GetLayerCount(Ogr_ds); i++) {
+	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
+	Ogr_field = OGR_L_GetLayerDefn(Ogr_layer);
+	if (strcmp(OGR_FD_GetName(Ogr_field), layer_opt->answer))
+	    continue;
+
+	found = TRUE;
+	if (!overwrite) {
+	    G_fatal_error(_("Layer <%s> already exists in OGR data source '%s'"),
+			  layer_opt->answer, dsn_opt->answer);
+	}
+	else if (overwrite) {
+	    G_warning(_("OGR layer <%s> already exists and will be overwritten"),
+		      layer_opt->answer);
+	    OGR_DS_DeleteLayer(Ogr_ds, i);
+	    break;
+	}
+    }
+
     /* parse layer creation options */
     i = 0;
     while (lco->answers[i]) {
@@ -359,6 +542,38 @@
 	i++;
     }
 
+    /* Automatically append driver options for 3D output to
+       layer creation options if 'z' is given. */
+    if ((shapez_flag->answer) && (Vect_is_3d(&In)) &&
+	(strcmp(frmt_opt->answer, "ESRI_Shapefile") == 0)) {
+	/* find right option */
+	char shape_geom[20];
+
+	if ((otype & GV_POINTS) || (otype & GV_KERNEL))
+	    sprintf(shape_geom, "POINTZ");
+	if ((otype & GV_LINES))
+	    sprintf(shape_geom, "ARCZ");
+	if ((otype & GV_AREA) || (otype & GV_FACE) || (otype & GV_VOLUME))
+	    sprintf(shape_geom, "POLYGONZ");
+	/* check if the right LCO is already present */
+	const char *shpt;
+
+	shpt = CSLFetchNameValue(papszLCO, "SHPT");
+	if ((!shpt)) {
+	    /* Not set at all? Good! */
+	    papszLCO = CSLSetNameValue(papszLCO, "SHPT", shape_geom);
+	}
+	else {
+	    if (strcmp(shpt, shape_geom) != 0) {
+		/* Set but to a different value? Override! */
+		G_warning(_("Overriding existing user-defined 'SHPT=' LCO."));
+	    }
+	    /* Set correct LCO for this geometry type */
+	    papszLCO = CSLSetNameValue(papszLCO, "SHPT", shape_geom);
+	}
+    }
+
+
     /* check if the map is 3d */
     if (Vect_is_3d(&In)) {
 	/* specific check for shp */
@@ -369,7 +584,7 @@
 	    if (!shpt || shpt[strlen(shpt) - 1] != 'Z') {
 		G_warning(_("Vector map <%s> is 3D. "
 			    "Use format specific layer creation options (parameter 'lco') "
-			    "to export in 3D rather than 2D (default)"),
+			    "or '-z' flag to export in 3D rather than 2D (default)"),
 			  in_opt->answer);
 	    }
 	}
@@ -486,127 +701,23 @@
 
     fout = fskip = nocat = noatt = nocatskip = 0;
 
-    /* check what users wants to export and what's present in the map */
-    if (Vect_get_num_primitives(&In, GV_POINT) > 0 && !(otype & GV_POINTS))
-	G_warning(_("%d point(s) found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									 GV_POINT));
-
-    if (Vect_get_num_primitives(&In, GV_LINE) > 0 && !(otype & GV_LINES))
-	G_warning(_("%d line(s) found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									 GV_LINE));
-
-    if (Vect_get_num_primitives(&In, GV_BOUNDARY) > 0 &&
-	!(otype & GV_BOUNDARY) && !(otype & GV_AREA))
-	G_warning(_("%d boundary(ies) found, but not requested to be exported. "
-		   "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									GV_BOUNDARY));
-
-    if (Vect_get_num_primitives(&In, GV_CENTROID) > 0 &&
-	!(otype & GV_CENTROID) && !(otype & GV_AREA))
-	G_warning(_("%d centroid(s) found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									 GV_CENTROID));
-
-    if (Vect_get_num_areas(&In) > 0 && !(otype & GV_AREA))
-	G_warning(_("%d areas found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_areas(&In));
-
-    if (Vect_get_num_primitives(&In, GV_FACE) > 0 && !(otype & GV_FACE))
-	G_warning(_("%d faces found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									 GV_FACE));
-
-    if (Vect_get_num_primitives(&In, GV_KERNEL) > 0 &&
-	!(otype & GV_KERNEL) && !(otype & GV_VOLUME))
-	G_warning(_("%d kernel(s) found, but not requested to be exported. "
-		    "Verify 'type' parameter."), Vect_get_num_primitives(&In,
-									 GV_KERNEL));
-
-    /* warn and eventually abort if there is nothing to be exported */
-    num_to_export = 0;
-    if (Vect_get_num_primitives(&In, GV_POINT) < 1 && (otype & GV_POINTS)) {
-	G_warning(_("No points found, but requested to be exported. "
-		    "Will skip this geometry type."));
+    /* Fetch all attribute records */
+    if (doatt) {
+	sprintf(buf, "SELECT * FROM %s", Fi->table);
+	G_debug(2, "SQL: %s", buf);
+	db_set_string(&dbstring, buf);
+	if (db_open_select_cursor
+	    (Driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
+	    G_fatal_error(_("Cannot select attributes for cat = %d"), cat);
+	}
     }
-    else {
-	if (otype & GV_POINT)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_POINT);
-    }
 
-    if (Vect_get_num_primitives(&In, GV_LINE) < 1 && (otype & GV_LINE)) {
-	G_warning(_("No lines found, but requested to be exported. "
-		    "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_LINE)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_LINE);
-    }
+    if (OGR_L_TestCapability(Ogr_layer, OLCTransactions))
+	OGR_L_StartTransaction(Ogr_layer);
 
-    if (Vect_get_num_primitives(&In, GV_BOUNDARY) < 1 &&
-	(otype & GV_BOUNDARY)) {
-	G_warning(_("No boundaries found, but requested to be exported. "
-		   "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_BOUNDARY)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_BOUNDARY);
-    }
-
-    if (Vect_get_num_areas(&In) < 1 && (otype & GV_AREA)) {
-	G_warning(_("No areas found, but requested to be exported. "
-		    "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_AREA)
-	    num_to_export = num_to_export + Vect_get_num_areas(&In);
-    }
-
-    if (Vect_get_num_primitives(&In, GV_CENTROID) < 1 &&
-	(otype & GV_CENTROID)) {
-	G_warning(_("No centroids found, but requested to be exported. "
-		   "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_CENTROID)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_CENTROID);
-    }
-
-    if (Vect_get_num_primitives(&In, GV_FACE) < 1 && (otype & GV_FACE)) {
-	G_warning(_("No faces found, but requested to be exported. "
-		    "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_FACE)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_FACE);
-    }
-
-    if (Vect_get_num_primitives(&In, GV_KERNEL) < 1 && (otype & GV_KERNEL)) {
-	G_warning(_("No kernels found, but requested to be exported. "
-		    "Will skip this geometry type."));
-    }
-    else {
-	if (otype & GV_KERNEL)
-	    num_to_export =
-		num_to_export + Vect_get_num_primitives(&In, GV_KERNEL);
-    }
-
-    G_debug(1, "Requested to export %d geometries", num_to_export);
-
-    if (num_to_export < 1) {
-	G_warning(_("Nothing to export"));
-	exit(EXIT_SUCCESS);
-    }
-
     /* Lines (run always to count features of different type) */
     if ((otype & GV_POINTS) || (otype & GV_LINES)) {
-	G_message(_("Exporting %i points/lines..."), Vect_get_num_lines(&In));
+	G_message(_("Exporting %i geometries..."), Vect_get_num_lines(&In));
 	for (i = 1; i <= Vect_get_num_lines(&In); i++) {
 
 	    G_percent(i, Vect_get_num_lines(&In), 1);
@@ -678,7 +789,7 @@
 		}
 
 		mk_att(cat, Fi, Driver, ncol, doatt, nocat_flag->answer,
-		       Ogr_feature);
+		       Ogr_feature, cursor);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -753,7 +864,7 @@
 		}
 
 		mk_att(cat, Fi, Driver, ncol, doatt, nocat_flag->answer,
-		       Ogr_feature);
+		       Ogr_feature, cursor);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -814,7 +925,7 @@
 		    }
 
 		    mk_att(cat, Fi, Driver, ncol, doatt, nocat_flag->answer,
-			   Ogr_feature);
+			   Ogr_feature, cursor);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 
@@ -826,8 +937,7 @@
 
     /* Kernels */
     if (otype & GV_KERNEL) {
-	G_message(_("Exporting %i kernels..."),
-		  Vect_get_num_primitives(&In, GV_KERNEL));
+	G_message(_("Exporting %i kernels..."), Vect_get_num_kernels(&In));
 	for (i = 1; i <= Vect_get_num_lines(&In); i++) {
 
 	    G_percent(i, Vect_get_num_lines(&In), 1);
@@ -870,7 +980,7 @@
 		    }
 
 		    mk_att(cat, Fi, Driver, ncol, doatt, nocat_flag->answer,
-			   Ogr_feature);
+			   Ogr_feature, cursor);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 		OGR_G_DestroyGeometry(Ogr_geometry);
@@ -885,18 +995,25 @@
        NOTE: We can probably just merge this with faces export function.
        Except for GRASS, which output format would know the difference?
      */
+    if ((otype & GV_VOLUME)) {
+	G_message(_("Exporting %i volumes..."), Vect_get_num_volumes(&In));
+	G_warning(_("Export of volumes not implemented yet. Skipping."));
+    }
 
+    if (OGR_L_TestCapability(Ogr_layer, OLCTransactions))
+	OGR_L_CommitTransaction(Ogr_layer);
+
     OGR_DS_Destroy(Ogr_ds);
 
     Vect_close(&In);
 
     if (doatt) {
+	db_close_cursor(&cursor);
 	db_close_database(Driver);
 	db_shutdown_driver(Driver);
     }
 
     /* Summary */
-    G_message(_("%d features written"), fout);
     if (nocat > 0)
 	G_warning(_("%d features without category were written"), nocat);
     if (noatt > 0)
@@ -913,20 +1030,21 @@
        G_warning ("%d features of different type skip", fskip);
      */
 
+    G_done_msg(_("%d features written to <%s> (%s)."), fout,
+	       layer_opt->answer, frmt_opt->answer);
+
     exit(EXIT_SUCCESS);
 }
 
 
 int mk_att(int cat, struct field_info *Fi, dbDriver * Driver, int ncol,
-	   int doatt, int nocat, OGRFeatureH Ogr_feature)
+	   int doatt, int nocat, OGRFeatureH Ogr_feature, dbCursor cursor)
 {
     int j, ogrfieldnum;
-    char buf[2000];
     int colsqltype, colctype, more;
     dbTable *Table;
     dbString dbstring;
     dbColumn *Column;
-    dbCursor cursor;
     dbValue *Value;
 
     G_debug(2, "mk_att() cat = %d, doatt = %d", cat, doatt);
@@ -944,66 +1062,59 @@
     /* Read & set attributes */
     if (cat >= 0) {		/* Line with category */
 	if (doatt) {
-	    sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key,
-		    cat);
-	    G_debug(2, "SQL: %s", buf);
-	    db_set_string(&dbstring, buf);
-	    if (db_open_select_cursor
-		(Driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
-		G_fatal_error(_("Cannot select attributes for cat = %d"),
-			      cat);
-	    }
-	    else {
+	    if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
+		G_fatal_error(_("Unable to fetch data from table"));
+	    if (!more) {
+		/* start from the beginning in case multiple grass vector features
+		 * share the same category */
 		if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
 		    G_fatal_error(_("Unable to fetch data from table"));
-		if (!more) {
-		    /* start from the beginning in case multiple grass vector features
-		     * share the same category */
-		    if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
-			G_fatal_error(_("Unable to fetch data from table"));
-		}
+	    }
 
-		if (!more) {
-		    /* G_warning ("No database record for cat = %d", cat); */
-		    /* Set at least key column to category */
-		    if (!nocat) {
-			ogrfieldnum =
-			    OGR_F_GetFieldIndex(Ogr_feature, Fi->key);
-			OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
-			noatt++;
-		    }
-		    else {
-			G_fatal_error(_("No database record for cat = %d and export of 'cat' disabled"),
-				      cat);
-		    }
+	    if (!more) {
+		/* G_warning ("No database record for cat = %d", cat); */
+		/* Set at least key column to category */
+		if (!nocat) {
+		    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, Fi->key);
+		    OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
+		    noatt++;
 		}
 		else {
-		    Table = db_get_cursor_table(&cursor);
-		    for (j = 0; j < ncol; j++) {
-			Column = db_get_table_column(Table, j);
-			Value = db_get_column_value(Column);
-			db_convert_column_value_to_string(Column, &dbstring);	/* for debug only */
-			G_debug(2, "col %d : val = %s", j,
-				db_get_string(&dbstring));
+		    G_fatal_error(_("No database record for cat = %d and export of 'cat' disabled"),
+				  cat);
+		}
+	    }
+	    else {
+		Table = db_get_cursor_table(&cursor);
+		for (j = 0; j < ncol; j++) {
+		    Column = db_get_table_column(Table, j);
+		    Value = db_get_column_value(Column);
+		    db_convert_column_value_to_string(Column, &dbstring);	/* for debug only */
+		    G_debug(2, "col %d : val = %s", j,
+			    db_get_string(&dbstring));
 
-			colsqltype = db_get_column_sqltype(Column);
-			colctype = db_sqltype_to_Ctype(colsqltype);
-			G_debug(2, "  colctype = %d", colctype);
+		    colsqltype = db_get_column_sqltype(Column);
+		    colctype = db_sqltype_to_Ctype(colsqltype);
+		    G_debug(2, "  colctype = %d", colctype);
 
-			ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
-							  db_get_column_name
-							  (Column));
+		    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
+						      db_get_column_name
+						      (Column));
 
-			/* Reset */
-			if ( ( ( nocat ) && (strcmp(Fi->key, db_get_column_name(Column)) == 0) ) == 0 ) {
-				/* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
-				OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
-			}
+		    /* Reset */
+		    if (((nocat) &&
+			 (strcmp(Fi->key, db_get_column_name(Column)) ==
+			  0)) == 0) {
+			/* if this is 'cat', then execute the following only if the '-s' flag was NOT given */
+			OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
+		    }
 
-			/* prevent writing NULL values */
-			if (!db_test_value_isnull(Value)) {
-				if ( ( (nocat) && (strcmp(Fi->key, db_get_column_name(Column)) == 0) ) == 0 ) {
-				/* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
+		    /* prevent writing NULL values */
+		    if (!db_test_value_isnull(Value)) {
+			if (((nocat) &&
+			     (strcmp(Fi->key, db_get_column_name(Column)) ==
+			      0)) == 0) {
+			    /* if this is 'cat', then execute the following only if the '-s' flag was NOT given */
 			    switch (colctype) {
 			    case DB_C_TYPE_INT:
 				OGR_F_SetFieldInteger(Ogr_feature,
@@ -1030,10 +1141,8 @@
 				break;
 			    }
 			}
-			}
 		    }
 		}
-		db_close_cursor(&cursor);
 	    }
 	}
 	else {			/* Use cat only */
@@ -1042,7 +1151,7 @@
 	}
     }
     else {
-	/* G_warning ( "Line without cat of layer %d", field); */
+	/* G_warning ("Line without cat of layer %d", field); */
 	nocat++;
     }
     fout++;



More information about the grass-commit mailing list