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

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 18 06:02:03 EST 2008


Author: hamish
Date: 2008-11-18 06:02:03 -0500 (Tue, 18 Nov 2008)
New Revision: 34356

Modified:
   grass/branches/develbranch_6/vector/v.out.ogr/main.c
Log:
Don't assume that the GRASS db column ID is the same as the OGRFieldID,
they can be different (eg OGR GPX driver has standard fields 1-9)
- removed keycol from mk_att() as unused
- OGR_F_UnsetField() moved into set loop as table needs to be open to
    query the OGR field ID by column name.
    (bug #354, allows v.out.gpsbabel to work)


Modified: grass/branches/develbranch_6/vector/v.out.ogr/main.c
===================================================================
--- grass/branches/develbranch_6/vector/v.out.ogr/main.c	2008-11-18 10:45:30 UTC (rev 34355)
+++ grass/branches/develbranch_6/vector/v.out.ogr/main.c	2008-11-18 11:02:03 UTC (rev 34356)
@@ -30,8 +30,8 @@
 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 keycol, int doatt, OGRFeatureH Ogr_feature);
+int mk_att(int cat, struct field_info *Fi, dbDriver *Driver,
+	   int ncol, int doatt, OGRFeatureH Ogr_feature);
 
 char *OGR_list_write_drivers();
 char OGRdrivers[2000];
@@ -487,7 +487,7 @@
 			continue;
 		}
 
-		mk_att(cat, Fi, Driver, ncol, keycol, doatt, Ogr_feature);
+		mk_att(cat, Fi, Driver, ncol, doatt, Ogr_feature);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -559,7 +559,7 @@
 			continue;
 		}
 
-		mk_att(cat, Fi, Driver, ncol, keycol, doatt, Ogr_feature);
+		mk_att(cat, Fi, Driver, ncol, doatt, Ogr_feature);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
 	    OGR_G_DestroyGeometry(Ogr_geometry);
@@ -614,7 +614,7 @@
 			    continue;
 		    }
 
-		    mk_att(cat, Fi, Driver, ncol, keycol, doatt, Ogr_feature);
+		    mk_att(cat, Fi, Driver, ncol, doatt, Ogr_feature);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
 
@@ -658,11 +658,11 @@
     exit(EXIT_SUCCESS);
 }
 
-int
-mk_att(int cat, struct field_info *Fi, dbDriver * Driver, int ncol,
-       int keycol, int doatt, OGRFeatureH Ogr_feature)
+
+int mk_att(int cat, struct field_info *Fi, dbDriver *Driver, int ncol,
+       int doatt, OGRFeatureH Ogr_feature)
 {
-    int j;
+    int j, ogrfieldnum;
     char buf[2000];
     int colsqltype, colctype, more;
     dbTable *Table;
@@ -676,13 +676,12 @@
 
     /* Attributes */
     /* Reset */
-    if (doatt) {
-	for (j = 0; j < ncol; j++)
-	    OGR_F_UnsetField(Ogr_feature, j);
+    if (!doatt) {
+	ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, "cat");
+	OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
+    /* doatt reset moved into have cat loop as the table needs to be
+	open to know the OGR field ID. Hopefully this has no ill consequences */
     }
-    else {
-	OGR_F_UnsetField(Ogr_feature, 0);
-    }
 
     /* Read & set attributes */
     if (cat >= 0) {		/* Line with category */
@@ -702,7 +701,8 @@
 		if (!more) {
 		    /* G_warning ("No database record for cat = %d", cat); */
 		    /* Set at least key column to category */
-		    OGR_F_SetFieldInteger(Ogr_feature, keycol, cat);
+		    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, Fi->key);
+		    OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
 		    noatt++;
 		}
 		else {
@@ -717,26 +717,32 @@
 			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));
+
+			/* Reset */
+			OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
+
 			/* prevent writing NULL values */
 			if (!db_test_value_isnull(Value)) {
 				switch (colctype) {
 				case DB_C_TYPE_INT:
-					OGR_F_SetFieldInteger(Ogr_feature, j,
+					OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum,
 							db_get_value_int(Value));
 					break;
 				case DB_C_TYPE_DOUBLE:
-					OGR_F_SetFieldDouble(Ogr_feature, j,
+					OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
 							db_get_value_double(Value));
 					break;
 				case DB_C_TYPE_STRING:
-					OGR_F_SetFieldString(Ogr_feature, j,
+					OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
 							db_get_value_string(Value));
 					break;
 				case DB_C_TYPE_DATETIME:
 					db_convert_column_value_to_string(Column,
 									&dbstring);
-					OGR_F_SetFieldString(Ogr_feature, j,
+					OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
 							db_get_string(&dbstring));
 					break;
 				}
@@ -747,7 +753,8 @@
 
 	}
 	else {			/* Use cat only */
-	    OGR_F_SetFieldInteger(Ogr_feature, 0, cat);
+	    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, "cat");
+	    OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum, cat);
 	}
     }
     else {
@@ -759,6 +766,7 @@
     return 1;
 }
 
+
 /* to print available drivers in help text */
 char *OGR_list_write_drivers(void)
 {



More information about the grass-commit mailing list