[GRASS-SVN] r39610 - grass/trunk/db/drivers/ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 22 14:11:53 EDT 2009


Author: martinl
Date: 2009-10-22 14:11:52 -0400 (Thu, 22 Oct 2009)
New Revision: 39610

Modified:
   grass/trunk/db/drivers/ogr/describe.c
Log:
ogrdriver: include fidcol if defined


Modified: grass/trunk/db/drivers/ogr/describe.c
===================================================================
--- grass/trunk/db/drivers/ogr/describe.c	2009-10-21 20:33:25 UTC (rev 39609)
+++ grass/trunk/db/drivers/ogr/describe.c	2009-10-22 18:11:52 UTC (rev 39610)
@@ -64,35 +64,37 @@
 /* describe table, if c is not NULL cur->cols and cur->ncols is also set 
  * cursor may be null
  */
-int describe_table(OGRLayerH hLayer, dbTable ** table, cursor * c)
+int describe_table(OGRLayerH hLayer, dbTable **table, cursor *c)
 {
-    int i, ncols, kcols;
+    int i, ifield, ncols, kcols;
     dbColumn *column;
     OGRFeatureDefnH hFeatureDefn;
     OGRFieldDefnH hFieldDefn;
-    const char *fieldName;
+    const char *fieldName, *fidcol;
     int ogrType;
     int *cols;
 
-    G_debug(3, "describe_table()");
+    G_debug(1, "describe_table()");
 
     hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
     ncols = OGR_FD_GetFieldCount(hFeatureDefn);
-
-    G_debug(3, "ncols = %d", ncols);
-
-    /* Identify known columns */
-    cols = (int *)G_malloc(ncols * sizeof(int));
+    G_debug(2, "   ncols = %d (without fid column)", ncols);
+    
+    /* for some formats fid column is not defined, e.g. ESRI Shapefile */
+    fidcol = OGR_L_GetFIDColumn(hLayer); 
+    G_debug(2, "   fidcol = %s", fidcol);
+    
+    /* identify known columns */
+    cols = (int *) G_malloc(ncols * sizeof(int));
     kcols = 0;
-    for (i = 0; i < ncols; i++) {
+    for(i = 0; i < ncols; i++) {
 	hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
 	ogrType = OGR_Fld_GetType(hFieldDefn);
-	OGR_Fld_GetNameRef(hFieldDefn);
 	fieldName = OGR_Fld_GetNameRef(hFieldDefn);
 
 	if (ogrType != OFTInteger && ogrType != OFTReal &&
 	    ogrType != OFTString) {
-	    G_warning(_("OGR driver: column '%s', OGR type %d  is not supported"),
+	    G_warning(_("OGR driver: column '%s', OGR type %d is not supported"),
 		      fieldName, ogrType);
 	    cols[i] = 0;
 	}
@@ -101,15 +103,20 @@
 	    kcols++;
 	}
     }
-
+    
+    if (*fidcol)
+	kcols++;
+    G_debug(2, "   kcols = %d (including fid column)", kcols);
+    
+    /* allocate dbTable */
     if (!(*table = db_alloc_table(kcols))) {
 	return DB_FAILED;
     }
-
+    
     /* set the table name */
     /* TODO */
     db_set_table_name(*table, "");
-
+    
     /* set the table description */
     db_set_table_description(*table, "");
 
@@ -119,19 +126,25 @@
        db_set_table_insert_priv_granted (*table);
        db_set_table_delete_priv_not_granted (*table);
        db_set_table_insert_priv_not_granted (*table);
-     */
+    */
 
-    for (i = 0; i < ncols; i++) {
+    for (i = 0, ifield = 0; ifield < ncols; i++) {
 	int sqlType;
 	int size, precision, scale;
 
-	if (!(cols[i]))
-	    continue;		/* unknown type */
+	if (*fidcol && i == 0) {
+	    ogrType = OFTInteger; /* ??? */
+	    fieldName = fidcol;
+	}
+	else {
+	    hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, ifield);
+	    ogrType = OGR_Fld_GetType(hFieldDefn);
+	    fieldName = OGR_Fld_GetNameRef(hFieldDefn);
+	    
+	    if (!(cols[ifield++]))
+	      continue;		/* unknown type */
+	}
 
-	hFieldDefn = OGR_FD_GetFieldDefn(hFeatureDefn, i);
-	ogrType = OGR_Fld_GetType(hFieldDefn);
-	fieldName = OGR_Fld_GetNameRef(hFieldDefn);
-
 	switch (ogrType) {
 	case OFTInteger:
 	    sqlType = DB_SQL_TYPE_INTEGER;
@@ -160,10 +173,10 @@
 	    G_warning(_("Unknown type"));
 	    break;
 	}
-
-	G_debug(3, "field %d : ogrType = %d, name = %s, size=%d precision=%d",
-		i, ogrType, fieldName, size, precision);
-
+	
+	G_debug(3, "   %d: field %d : ogrType = %d, name = %s, size=%d precision=%d",
+		i, ifield, ogrType, fieldName, size, precision);
+	
 	column = db_get_table_column(*table, i);
 
 	db_set_column_host_type(column, ogrType);



More information about the grass-commit mailing list