[GRASS-SVN] r45036 - grass/branches/develbranch_6/vector/v.out.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 14 16:04:44 EST 2011


Author: benducke
Date: 2011-01-14 13:04:44 -0800 (Fri, 14 Jan 2011)
New Revision: 45036

Modified:
   grass/branches/develbranch_6/vector/v.out.ogr/main.c
Log:
This commit contains some small bugfixes, code clean-ups and two new feature for v.out.ogr.
1. Fixed a but that would spawn ugly error messages in conjunction with the "-s" flag
2. Reduced the SQL SELECT statements to just one instead of one per feature, increasing output speed by several magnitudes.
3. Added a new "-z" flag, which provides a simple and robust means of producing 3D ESRI Shapefiles
--> Please test intensively!

Modified: grass/branches/develbranch_6/vector/v.out.ogr/main.c
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ogr/main.c	2011-01-14 20:47:26 UTC (rev 45035)
+++ grass/branches/develbranch_6/vector/v.out.ogr/main.c	2011-01-14 21:04:44 UTC (rev 45036)
@@ -27,14 +27,20 @@
 #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[])
 {
@@ -45,9 +51,9 @@
     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 +72,7 @@
     dbTable *Table;
     dbString dbstring;
     dbColumn *Column;
+    dbCursor cursor;
 
     /* OGR */
     int drn, ogr_ftype = OFTInteger;
@@ -168,6 +175,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");
@@ -312,6 +324,139 @@
     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 =
+		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 (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);
+    }
+
+    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 = 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)
@@ -378,6 +523,36 @@
 	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 */
@@ -388,7 +563,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);
 	    }
 	}
@@ -505,137 +680,20 @@
 
     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 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."));
+    /* 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 (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);
-    }
-
-    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 = 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);
-    }
-
     /* Lines (run always to count features of different type) */
     if ((otype & GV_POINTS) || (otype & GV_LINES)) {
 	G_message(_("Exporting %i geometries..."), Vect_get_num_lines(&In));
@@ -710,7 +768,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);
@@ -785,7 +843,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);
@@ -846,7 +904,7 @@
 		    }
 
 		    mk_att(cat, Fi, Driver, ncol, doatt, nocat_flag->answer,
-			   Ogr_feature);
+			   Ogr_feature, cursor);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 
@@ -901,7 +959,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);
@@ -926,6 +984,7 @@
     Vect_close(&In);
 
     if (doatt) {
+    db_close_cursor(&cursor);
 	db_close_database(Driver);
 	db_shutdown_driver(Driver);
     }
@@ -953,15 +1012,13 @@
 
 
 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);
@@ -979,16 +1036,6 @@
     /* 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) {
@@ -1023,11 +1070,16 @@
 							  (Column));
 
 			/* Reset */
-			OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
+			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)) {
-			    switch (colctype) {
+				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,
 						      ogrfieldnum,
@@ -1053,10 +1105,9 @@
 				break;
 			    }
 			}
+			}
 		    }
 		}
-		db_close_cursor(&cursor);
-	    }
 	}
 	else {			/* Use cat only */
 	    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, "cat");



More information about the grass-commit mailing list