[GRASS-SVN] r47857 - grass/trunk/lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Aug 23 14:58:55 EDT 2011


Author: martinl
Date: 2011-08-23 11:58:55 -0700 (Tue, 23 Aug 2011)
New Revision: 47857

Modified:
   grass/trunk/lib/vector/Vlib/write_ogr.c
Log:
vlib: define field width for character type
      simplify sqltype_to_ogrtype()


Modified: grass/trunk/lib/vector/Vlib/write_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/write_ogr.c	2011-08-23 18:44:44 UTC (rev 47856)
+++ grass/trunk/lib/vector/Vlib/write_ogr.c	2011-08-23 18:58:55 UTC (rev 47857)
@@ -363,7 +363,7 @@
 dbDriver *create_table(OGRLayerH hLayer, const struct field_info *Fi)
 {
     int col, ncols;
-    int sqltype, ogrtype;
+    int sqltype, ogrtype, length;
     
     const char *colname;
     
@@ -416,7 +416,8 @@
 	colname = db_get_column_name(column);	
 	sqltype = db_get_column_sqltype(column);
 	ogrtype = sqltype_to_ogrtype(sqltype);
-		
+	length = db_get_column_length(column);
+	
 	if (strcmp(OGR_L_GetFIDColumn(hLayer), colname) == 0 ||
 	    OGR_FD_GetFieldIndex(hFeatureDefn, colname) > -1) {
 	    /* field already exists */
@@ -424,7 +425,9 @@
 	}
 
 	hFieldDefn = OGR_Fld_Create(colname, ogrtype);
-	/* OGR_Fld_SetWidth(hFieldDefn, length); */
+	/* GDAL 1.9.0 (r22968) uses VARCHAR instead of CHAR */
+	if (ogrtype == OFTString && length > 0)
+	    OGR_Fld_SetWidth(hFieldDefn, length);
 	if (OGR_L_CreateField(hLayer, hFieldDefn, TRUE) != OGRERR_NONE) {
 	    G_warning(_("Creating field <%s> failed"), colname);
 	    db_close_database_shutdown_driver(driver);
@@ -442,7 +445,7 @@
 {
     int j, ogrfieldnum;
     char buf[2000];
-    int ncol, colsqltype, colctype, more;
+    int ncol, sqltype, ctype, ogrtype, more;
     const char *fidcol, *colname;
     dbTable *table;
     dbString dbstring;
@@ -502,15 +505,15 @@
 	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);
+	sqltype = db_get_column_sqltype(column);
+	ctype = db_sqltype_to_Ctype(sqltype);
+	ogrtype = sqltype_to_ogrtype(sqltype);
+	G_debug(2, "  colctype = %d", ctype);
 	
 	ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
 	if (ogrfieldnum < 0) {
 	    /* create field if not exists */
-	    hFieldDefn = OGR_Fld_Create(colname, 
-					sqltype_to_ogrtype(colsqltype));
+	    hFieldDefn = OGR_Fld_Create(colname, ogrtype);
 	    if (OGR_L_CreateField(Ogr_layer, hFieldDefn, TRUE) != OGRERR_NONE)
 		G_warning(_("Unable to create field <%s>"), colname);
 	    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
@@ -521,7 +524,7 @@
 	
 	/* prevent writing NULL values */
 	if (!db_test_value_isnull(value)) {
-	    switch (colctype) {
+	    switch (ctype) {
 	    case DB_C_TYPE_INT:
 		OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum,
 				      db_get_value_int(value));
@@ -553,30 +556,29 @@
 
 int sqltype_to_ogrtype(int sqltype)
 {
-    switch(sqltype) {
-    case DB_SQL_TYPE_CHARACTER:
-    case DB_SQL_TYPE_TEXT:
-	return OFTString;
-    case DB_SQL_TYPE_SMALLINT:
-    case DB_SQL_TYPE_INTEGER:
-    case DB_SQL_TYPE_SERIAL:
-	return OFTInteger;
-    case DB_SQL_TYPE_REAL:
-    case DB_SQL_TYPE_DOUBLE_PRECISION:
-    case DB_SQL_TYPE_DECIMAL:
-    case DB_SQL_TYPE_NUMERIC:
-	return OFTReal;
-    case DB_SQL_TYPE_DATE:
-	return OFTDate;
-    case DB_SQL_TYPE_TIME:	
-	return OFTTime;
-    case DB_SQL_TYPE_TIMESTAMP:
-	return OFTDateTime;
-    case DB_SQL_TYPE_INTERVAL:
-	return OFTString; /* ??? */
+    int ctype, ogrtype;
+
+    ctype = db_sqltype_to_Ctype(sqltype);
+    
+    switch(ctype) {
+    case DB_C_TYPE_INT:
+	ogrtype = OFTInteger;
+	break;
+    case DB_C_TYPE_DOUBLE:
+	ogrtype = OFTReal;
+	break;
+    case DB_C_TYPE_STRING:
+	ogrtype = OFTString;
+	break;
+    case DB_C_TYPE_DATETIME:
+	ogrtype = OFTString;
+	break;
+    default:
+	ogrtype = OFTString;
+	break;
     }
-
-    return OFTString;
+    
+    return ogrtype;
 }
 
 #endif /* HAVE_OGR */



More information about the grass-commit mailing list