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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Apr 28 06:58:44 EDT 2011


Author: mmetz
Date: 2011-04-28 03:58:44 -0700 (Thu, 28 Apr 2011)
New Revision: 46119

Modified:
   grass/trunk/vector/v.out.ogr/attrb.c
   grass/trunk/vector/v.out.ogr/local_proto.h
   grass/trunk/vector/v.out.ogr/main.c
Log:
partially revert r45044, because attributes where mixed up: select only attributes for current cat, not attributes for all cats

Modified: grass/trunk/vector/v.out.ogr/attrb.c
===================================================================
--- grass/trunk/vector/v.out.ogr/attrb.c	2011-04-27 15:51:08 UTC (rev 46118)
+++ grass/trunk/vector/v.out.ogr/attrb.c	2011-04-28 10:58:44 UTC (rev 46119)
@@ -4,7 +4,7 @@
 
 int mk_att(int cat, struct field_info *Fi, dbDriver *Driver, int ncol,
 	   int doatt, int nocat, OGRFeatureH Ogr_feature, int *noatt,
-	   int *fout, dbCursor cursor)
+	   int *fout)
 {
     int j, ogrfieldnum;
     int colsqltype, colctype, more;
@@ -12,10 +12,12 @@
     dbString dbstring;
     dbColumn *Column;
     dbValue *Value;
+    char buf[SQL_BUFFER_SIZE];
+    dbCursor cursor;
 
     G_debug(2, "mk_att() cat = %d, doatt = %d", cat, doatt);
     db_init_string(&dbstring);
-
+    
     /* Attributes */
     /* Reset */
     if (!doatt) {
@@ -28,58 +30,80 @@
     /* Read & set attributes */
     if (cat >= 0) {		/* Line with category */
 	if (doatt) {
+	    /* Fetch all attribute records for cat <cat> */ 
+	    /* opening and closing the cursor is slow, 
+	     * but the cursor really needs to be opened for each cat separately */
+	    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);
+	    }
+
+	    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));
-			G_debug(2, "  column = %s -> fieldnum = %d",
-				db_get_column_name(Column), ogrfieldnum);
+		    if (nocat && strcmp(Fi->key, db_get_column_name(Column)) == 0)
+			continue;
 
-			/* 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);
-			}
+		    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
+						      db_get_column_name
+						      (Column));
+		    G_debug(2, "  column = %s -> fieldnum = %d",
+			    db_get_column_name(Column), ogrfieldnum);
+			    
+		    if (ogrfieldnum < 0) {
+			G_debug(4, "Could not get OGR field number for column %s",
+				                         db_get_column_name(Column));
+			continue;
+		    }
 
-			/* 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*/
+		    /* 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*/
+
 			    switch (colctype) {
 			    case DB_C_TYPE_INT:
 				OGR_F_SetFieldInteger(Ogr_feature,
@@ -107,8 +131,9 @@
 			    }
 			}
 		    }
-		    }
 		}
+	    }
+	    db_close_cursor(&cursor);
 	}
 	else {			/* Use cat only */
 	    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, "cat");

Modified: grass/trunk/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ogr/local_proto.h	2011-04-27 15:51:08 UTC (rev 46118)
+++ grass/trunk/vector/v.out.ogr/local_proto.h	2011-04-28 10:58:44 UTC (rev 46119)
@@ -25,7 +25,7 @@
 
 /* attributes.c */
 int mk_att(int cat, struct field_info *Fi, dbDriver *Driver,
-	   int ncol, int doatt, int nocat, OGRFeatureH Ogr_feature, int *, int *, dbCursor cursor);
+	   int ncol, int doatt, int nocat, OGRFeatureH Ogr_feature, int *, int *);
 
 /* list.c */
 char *OGR_list_write_drivers();

Modified: grass/trunk/vector/v.out.ogr/main.c
===================================================================
--- grass/trunk/vector/v.out.ogr/main.c	2011-04-27 15:51:08 UTC (rev 46118)
+++ grass/trunk/vector/v.out.ogr/main.c	2011-04-28 10:58:44 UTC (rev 46119)
@@ -38,7 +38,8 @@
     struct Flags flags;
 
     char buf[SQL_BUFFER_SIZE];
-    char key1[SQL_BUFFER_SIZE], key2[SQL_BUFFER_SIZE];    struct Key_Value *projinfo, *projunits;
+    char key1[SQL_BUFFER_SIZE], key2[SQL_BUFFER_SIZE];
+    struct Key_Value *projinfo, *projunits;
     struct Cell_head cellhd;
     char **tokens;
 
@@ -55,7 +56,6 @@
     dbTable *Table;
     dbString dbstring;
     dbColumn *Column;
-    dbCursor cursor;
 
     int fout, fskip;		/* features written/ skip */
     int nocat, noatt, nocatskip;	/* number of features without cats/atts written/skip */
@@ -627,6 +627,9 @@
 		    OGR_Fld_Create(db_get_column_name(Column),
 				   ogr_ftype);
 		OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
+		
+		G_debug(0, "OGR field: %s, column %s", OGR_Fld_GetNameRef(Ogr_field), db_get_column_name(Column));
+		
 		OGR_Fld_Destroy(Ogr_field);
 	    }
 	    if (keycol == -1)
@@ -638,18 +641,6 @@
 
     fout = fskip = nocat = noatt = nocatskip = 0;
 
-    /* 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);
-    	}
-    }
-    
     if (OGR_L_TestCapability(Ogr_layer, OLCTransactions))
 	OGR_L_StartTransaction(Ogr_layer);
     
@@ -726,7 +717,7 @@
 		}
 
 		mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
-		       Ogr_feature, &noatt, &fout, cursor);
+		       Ogr_feature, &noatt, &fout);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -801,7 +792,7 @@
 		}
 
 		mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
-		       Ogr_feature, &noatt, &fout, cursor);
+		       Ogr_feature, &noatt, &fout);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -861,7 +852,7 @@
 		    }
 
 		    mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
-			   Ogr_feature, &noatt, &fout, cursor);
+			   Ogr_feature, &noatt, &fout);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 
@@ -916,7 +907,7 @@
 		    }
 
 		    mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
-			   Ogr_feature, &noatt, &fout, cursor);
+			   Ogr_feature, &noatt, &fout);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 		OGR_G_DestroyGeometry(Ogr_geometry);
@@ -944,7 +935,6 @@
     Vect_close(&In);
 
     if (doatt) {
-    db_close_cursor(&cursor);
 	db_close_database(Driver);
 	db_shutdown_driver(Driver);
     }



More information about the grass-commit mailing list