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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Apr 18 13:35:58 EDT 2012


Author: mmetz
Date: 2012-04-18 10:35:58 -0700 (Wed, 18 Apr 2012)
New Revision: 51474

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:
v.out.ogr speed-up, depends on export format

Modified: grass/trunk/vector/v.out.ogr/attrb.c
===================================================================
--- grass/trunk/vector/v.out.ogr/attrb.c	2012-04-18 17:05:00 UTC (rev 51473)
+++ grass/trunk/vector/v.out.ogr/attrb.c	2012-04-18 17:35:58 UTC (rev 51474)
@@ -3,21 +3,28 @@
 #include "local_proto.h"
 
 int mk_att(int cat, struct field_info *Fi, dbDriver *Driver, int ncol,
-	   int doatt, int nocat, OGRFeatureH Ogr_feature, int *noatt,
-	   int *fout)
+	   int *colctype, const char **colname, int doatt, int nocat,
+	   OGRFeatureH Ogr_feature, int *noatt, int *fout)
 {
     int j, ogrfieldnum;
-    int colsqltype, colctype, more;
+    int more;
     dbTable *Table;
-    dbString dbstring;
+    static int first = 1;
+    static 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);
-    
+
+    /* init constants */
+    if (first) {
+	db_init_string(&dbstring);
+	first = 0;
+    }
+
     /* Attributes */
     /* Reset */
     if (!doatt) {
@@ -68,37 +75,34 @@
 		    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);
+		    G_debug(2, "  colctype = %d", colctype[j]);
 
-		    if (nocat && strcmp(Fi->key, db_get_column_name(Column)) == 0)
+		    if (nocat && strcmp(Fi->key, colname[j]) == 0)
 			continue;
 
 		    ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature,
-						      db_get_column_name
-						      (Column));
+						      colname[j]);
 		    G_debug(2, "  column = %s -> fieldnum = %d",
-			    db_get_column_name(Column), ogrfieldnum);
+			    colname[j], ogrfieldnum);
 
 		    if (ogrfieldnum < 0) {
 			G_debug(4, "Could not get OGR field number for column %s",
-				                         db_get_column_name(Column));
+				                         colname[j]);
 			continue;
 		    }
 
 		    /* Reset */
-		    if ((nocat && strcmp(Fi->key, db_get_column_name(Column)) == 0) == 0) {
+		    if ((nocat && strcmp(Fi->key, colname[j]) == 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 ((nocat && strcmp(Fi->key, colname[j]) == 0) == 0) {
 			/* if this is 'cat', then execute the following only if the '-s' flag was NOT given*/
 
-			    switch (colctype) {
+			    switch (colctype[j]) {
 			    case DB_C_TYPE_INT:
 				OGR_F_SetFieldInteger(Ogr_feature,
 						      ogrfieldnum,
@@ -140,7 +144,9 @@
     }
     (*fout)++;
 
+    /*
     db_free_string(&dbstring);
+    */
 
     return 1;
 }

Modified: grass/trunk/vector/v.out.ogr/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.ogr/local_proto.h	2012-04-18 17:05:00 UTC (rev 51473)
+++ grass/trunk/vector/v.out.ogr/local_proto.h	2012-04-18 17:35:58 UTC (rev 51474)
@@ -25,7 +25,8 @@
 
 /* attributes.c */
 int mk_att(int cat, struct field_info *Fi, dbDriver *Driver,
-	   int ncol, int doatt, int nocat, OGRFeatureH Ogr_feature, int *, int *);
+	   int ncol, int *colctype, const char **colname, 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	2012-04-18 17:05:00 UTC (rev 51473)
+++ grass/trunk/vector/v.out.ogr/main.c	2012-04-18 17:35:58 UTC (rev 51474)
@@ -50,8 +50,9 @@
     int type, cat;
 
     /* Attributes */
-    int doatt = 0, ncol = 0, colsqltype, colctype, colwidth, keycol = -1;
-    const char *colname;
+    int doatt = 0, ncol = 0, colsqltype, colwidth, keycol = -1;
+    int *colctype = NULL;
+    const char **colname = NULL;
     struct field_info *Fi = NULL;
     dbDriver *Driver = NULL;
     dbTable *Table;
@@ -577,17 +578,19 @@
 
 	     ncol = db_get_table_number_of_columns(Table);
 	     G_debug(2, "ncol = %d", ncol);
+	     colctype = G_malloc(ncol * sizeof(int));
+	     colname = G_malloc(ncol * sizeof(char *));
 	     keycol = -1;
 	     for (i = 0; i < ncol; i++) {
 		 Column = db_get_table_column(Table, i);
-		 colname =  db_get_column_name(Column);
+		 colname[i] =  G_store(db_get_column_name(Column));
 		 colsqltype = db_get_column_sqltype(Column);
-		 colctype = db_sqltype_to_Ctype(colsqltype);
+		 colctype[i] = db_sqltype_to_Ctype(colsqltype);
 		 colwidth = db_get_column_length(Column);
 		 G_debug(3, "col %d: %s sqltype=%d ctype=%d width=%d",
-			 i, colname, colsqltype, colctype, colwidth);
+			 i, colname[i], colsqltype, colctype[i], colwidth);
 		 
-		 switch (colctype) {
+		 switch (colctype[i]) {
 		 case DB_C_TYPE_INT:
 		     ogr_ftype = OFTInteger;
 		     break;
@@ -605,29 +608,29 @@
 
 		 strcpy(key1, Fi->key);
 		 G_tolcase(key1);
-		 strcpy(key2, colname);
+		 strcpy(key2, colname[i]);
 		 G_tolcase(key2);
 		 if (strcmp(key1, key2) == 0)
 		     keycol = i;
 		 G_debug(2, "%s x %s -> %s x %s -> keycol = %d", Fi->key,
-			 colname, key1, key2, keycol);
+			 colname[i], key1, key2, keycol);
 
 		 if (flags.nocat->answer &&
-		     strcmp(Fi->key, colname) == 0)
+		     strcmp(Fi->key, colname[i]) == 0)
 		     /* skip export of 'cat' field */
 		     continue;
 
 		 if (flags.append->answer) {
 		     Ogr_field = OGR_L_GetLayerDefn(Ogr_layer);
-		     if (OGR_FD_GetFieldIndex(Ogr_field, colname) > -1)
+		     if (OGR_FD_GetFieldIndex(Ogr_field, colname[i]) > -1)
 			 /* skip existing fields */
 			 continue;
 		     else
 			 G_warning(_("New attribute column <%s> added to the table"),
-				   colname);
+				   colname[i]);
 		 }
 		 
-		 Ogr_field = OGR_Fld_Create(colname, ogr_ftype);
+		 Ogr_field = OGR_Fld_Create(colname[i], ogr_ftype);
 		 if (ogr_ftype == OFTString && colwidth > 0)
 		     OGR_Fld_SetWidth(Ogr_field, colwidth);
 		 OGR_L_CreateField(Ogr_layer, Ogr_field, 0);
@@ -718,7 +721,7 @@
 			continue;
 		}
 
-		mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
+		mk_att(cat, Fi, Driver, ncol, colctype, colname, doatt, flags.nocat->answer,
 		       Ogr_feature, &noatt, &fout);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
@@ -793,7 +796,7 @@
 			continue;
 		}
 
-		mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
+		mk_att(cat, Fi, Driver, ncol, colctype, colname, doatt, flags.nocat->answer,
 		       Ogr_feature, &noatt, &fout);
 		OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 	    }
@@ -853,7 +856,7 @@
 			    continue;
 		    }
 
-		    mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
+		    mk_att(cat, Fi, Driver, ncol, colctype, colname, doatt, flags.nocat->answer,
 			   Ogr_feature, &noatt, &fout);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}
@@ -908,7 +911,7 @@
 			    continue;
 		    }
 
-		    mk_att(cat, Fi, Driver, ncol, doatt, flags.nocat->answer,
+		    mk_att(cat, Fi, Driver, ncol, colctype, colname, doatt, flags.nocat->answer,
 			   Ogr_feature, &noatt, &fout);
 		    OGR_L_CreateFeature(Ogr_layer, Ogr_feature);
 		}



More information about the grass-commit mailing list