[GRASS-SVN] r47943 - in grass/trunk: include/vect lib/vector/Vlib
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Aug 29 12:29:13 EDT 2011
Author: martinl
Date: 2011-08-29 09:29:13 -0700 (Mon, 29 Aug 2011)
New Revision: 47943
Modified:
grass/trunk/include/vect/dig_structs.h
grass/trunk/lib/vector/Vlib/close_ogr.c
grass/trunk/lib/vector/Vlib/open_ogr.c
grass/trunk/lib/vector/Vlib/write_ogr.c
Log:
vlib: optimize writing attributes for OGR output
Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h 2011-08-29 16:27:30 UTC (rev 47942)
+++ grass/trunk/include/vect/dig_structs.h 2011-08-29 16:29:13 UTC (rev 47943)
@@ -24,9 +24,10 @@
#include <grass/shapefil.h>
#include <grass/rbtree.h>
#include <grass/rtree.h>
+#include <grass/dbmi.h>
#ifdef HAVE_OGR
-#include "ogr_api.h"
+#include <ogr_api.h>
#endif
/*!
@@ -429,6 +430,14 @@
#endif
/*!
+ \brief Open DB driver when writing attributes
+
+ This driver is open by V2_open_new_ogr() and closed by
+ V1_close_ogr().
+ */
+ dbDriver *dbdriver;
+
+ /*!
\brief Array of OGR DSN options
*/
char **dsn_options;
Modified: grass/trunk/lib/vector/Vlib/close_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close_ogr.c 2011-08-29 16:27:30 UTC (rev 47942)
+++ grass/trunk/lib/vector/Vlib/close_ogr.c 2011-08-29 16:29:13 UTC (rev 47943)
@@ -33,29 +33,36 @@
{
int i;
+ struct Format_info_ogr *fInfo;
+
if (!VECT_OPEN(Map))
return -1;
+ fInfo = &(Map->fInfo.ogr);
if (Map->format != GV_FORMAT_OGR_DIRECT &&
(Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW))
Vect__write_head(Map);
- if (Map->fInfo.ogr.feature_cache)
- OGR_F_Destroy(Map->fInfo.ogr.feature_cache);
+ if (fInfo->feature_cache)
+ OGR_F_Destroy(fInfo->feature_cache);
- OGR_DS_Destroy(Map->fInfo.ogr.ds);
+ OGR_DS_Destroy(fInfo->ds);
- for (i = 0; i < Map->fInfo.ogr.lines_alloc; i++) {
- Vect_destroy_line_struct(Map->fInfo.ogr.lines[i]);
+ for (i = 0; i < fInfo->lines_alloc; i++) {
+ Vect_destroy_line_struct(fInfo->lines[i]);
}
- G_free(Map->fInfo.ogr.lines);
- G_free(Map->fInfo.ogr.lines_types);
+ if (fInfo->dbdriver) {
+ db_close_database_shutdown_driver(fInfo->dbdriver);
+ }
- G_free(Map->fInfo.ogr.driver_name);
- G_free(Map->fInfo.ogr.dsn);
- G_free(Map->fInfo.ogr.layer_name);
+ G_free(fInfo->lines);
+ G_free(fInfo->lines_types);
+ G_free(fInfo->driver_name);
+ G_free(fInfo->dsn);
+ G_free(fInfo->layer_name);
+
return 0;
}
Modified: grass/trunk/lib/vector/Vlib/open_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_ogr.c 2011-08-29 16:27:30 UTC (rev 47942)
+++ grass/trunk/lib/vector/Vlib/open_ogr.c 2011-08-29 16:29:13 UTC (rev 47943)
@@ -30,7 +30,7 @@
#include <cpl_string.h>
static int sqltype_to_ogrtype(int);
-static int create_table(OGRLayerH, const struct field_info *);
+static dbDriver *create_table(OGRLayerH, const struct field_info *);
/*!
\brief Open existing OGR layer (level 1 - without feature index file)
@@ -314,7 +314,9 @@
projinfo = G_get_projinfo();
projunits = G_get_projunits();
Ogr_spatial_ref = GPJ_grass_to_osr(projinfo, projunits);
-
+ G_free_key_value(projinfo);
+ G_free_key_value(projunits);
+
switch(type) {
case GV_POINT:
Ogr_geom_type = wkbPoint;
@@ -359,7 +361,7 @@
if (ndblinks > 1)
G_warning(_("More layers defined, using driver <%s> and "
"database <%s>"), Fi->driver, Fi->database);
- create_table(Map->fInfo.ogr.layer, Fi);
+ Map->fInfo.ogr.dbdriver = create_table(Map->fInfo.ogr.layer, Fi);
G_free(Fi);
}
else
@@ -373,7 +375,7 @@
return 0;
}
-int create_table(OGRLayerH hLayer, const struct field_info *Fi)
+dbDriver *create_table(OGRLayerH hLayer, const struct field_info *Fi)
{
int col, ncols;
int sqltype, ogrtype, length;
@@ -396,14 +398,14 @@
driver = db_start_driver(Fi->driver);
if (!driver) {
G_warning(_("Unable to start driver <%s>"), Fi->driver);
- return -1;
+ return NULL;
}
db_set_handle(&handle, Fi->database, NULL);
if (db_open_database(driver, &handle) != DB_OK) {
G_warning(_("Unable to open database <%s> by driver <%s>"),
Fi->database, Fi->driver);
db_close_database_shutdown_driver(driver);
- return -1;
+ return NULL;
}
/* to get no data */
@@ -416,7 +418,7 @@
G_warning(_("Unable to open select cursor: '%s'"),
db_get_string(&sql));
db_close_database_shutdown_driver(driver);
- return -1;
+ return NULL;
}
table = db_get_cursor_table(&cursor);
@@ -444,15 +446,13 @@
if (OGR_L_CreateField(hLayer, hFieldDefn, TRUE) != OGRERR_NONE) {
G_warning(_("Creating field <%s> failed"), colname);
db_close_database_shutdown_driver(driver);
- return -1;
+ return NULL;
}
OGR_Fld_Destroy(hFieldDefn);
}
- db_close_database_shutdown_driver(driver);
-
- return 0;
+ return driver;
}
int sqltype_to_ogrtype(int sqltype)
Modified: grass/trunk/lib/vector/Vlib/write_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/write_ogr.c 2011-08-29 16:27:30 UTC (rev 47942)
+++ grass/trunk/lib/vector/Vlib/write_ogr.c 2011-08-29 16:29:13 UTC (rev 47943)
@@ -28,7 +28,7 @@
#include <ogr_api.h>
static int sqltype_to_ogrtype(int);
-static int write_attributes(int, const struct field_info *,
+static int write_attributes(dbDriver *, int, const struct field_info *,
OGRLayerH, OGRFeatureH);
void V2__add_line_to_topo_ogr(struct Map_info *Map, int line,
@@ -73,9 +73,10 @@
cat = -1; /* no attributes to be written */
if (cats->n_cats > 0) {
/* check for attributes */
- Fi = Vect_get_field(Map, cats->field[0]);
+ Fi = Vect_get_dblink(Map, 0);
if (Fi) {
- cat = cats->cat[0];
+ if (!Vect_cat_get(cats, Fi->number, &cat))
+ G_warning(_("No category defined for layer %d"), Fi->number);
if (cats->n_cats > 1) {
G_warning(_("Feature has more categories, using "
"category %d (from layer %d)"),
@@ -165,9 +166,11 @@
OGR_F_SetGeometry(Ogr_feature, Ogr_geometry);
/* write attributes */
- if (cat > -1)
- write_attributes(cat, Fi, Map->fInfo.ogr.layer, Ogr_feature);
-
+ if (cat > -1 && Map->fInfo.ogr.dbdriver) {
+ write_attributes(Map->fInfo.ogr.dbdriver,
+ cat, Fi, Map->fInfo.ogr.layer, Ogr_feature);
+ G_free(Fi);
+ }
/* write feature into layer */
ret = OGR_L_CreateFeature(Map->fInfo.ogr.layer, Ogr_feature);
@@ -385,17 +388,15 @@
return ret;
}
-int write_attributes(int cat, const struct field_info *Fi,
+int write_attributes(dbDriver *driver, int cat, const struct field_info *Fi,
OGRLayerH Ogr_layer, OGRFeatureH Ogr_feature)
{
int j, ogrfieldnum;
char buf[2000];
int ncol, sqltype, ctype, ogrtype, more;
const char *fidcol, *colname;
- dbDriver *driver;
dbTable *table;
dbString dbstring;
- dbHandle handle;
dbColumn *column;
dbCursor cursor;
dbValue *value;
@@ -410,21 +411,7 @@
}
db_init_string(&dbstring);
- db_init_handle(&handle);
- driver = db_start_driver(Fi->driver);
- if (!driver) {
- G_warning(_("Unable to start driver <%s>"), Fi->driver);
- return -1;
- }
- db_set_handle(&handle, Fi->database, NULL);
- if (db_open_database(driver, &handle) != DB_OK) {
- G_warning(_("Unable to open database <%s> by driver <%s>"),
- Fi->database, Fi->driver);
- db_close_database_shutdown_driver(driver);
- return -1;
- }
-
/* read & set attributes */
sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key,
cat);
@@ -509,7 +496,6 @@
}
db_close_cursor (&cursor);
- db_close_database_shutdown_driver(driver);
db_free_string(&dbstring);
More information about the grass-commit
mailing list