Dear all,

I have attached a very small patch to v.out.org against the
version in 6.4.SVN. It adds a "-u" flag that allows users
to update data in existing OGR sources. It's the same as
ogr2ogr's "-update" option and _very_ important for being able
to write layers as tables into existing spatial databases.
It should not be able to break any existing functionality.

If a data source does not support updates (like Shapefiles),
then Ogr_Dr_open() behaves the same as Ogr_Dr_CreateDataSource().

Despite the update, I have not been able to successfully
write OGR features to an existing (or new) SQLite DB.

OGR_L_CreateFeature() results in this error from the SQLite driver
after the first feature has been written:

ERROR 1: sqlite3_step() failed:
  SQL logic error or missing database

Strangely, the first feature is written into the table just fine,
but the error appears for all consecutive writes.

I will have to do more tests to see if this is an SQLite
and GRASS specific problem (ogr2ogr works fine).

Cheers,

Ben

--
Benjamin Ducke
Senior Geospatial Consultant

Oxford Archaeology
Janus House
Osney Mead
OX2 0ES
Oxford, U.K.

Tel: +44 (0)1865 263 800 (switchboard)
Tel: +44 (0)1865 980 758 (direct)
Fax :+44 (0)1865 793 496
[email protected]





------
Files attached to this email may be in ISO 26300 format (OASIS Open Document 
Format). If you have difficulty opening them, please visit http://iso26300.info 
for more information.
--- old/main.c	2009-05-11 10:16:32.000000000 +0100
+++ new/main.c	2009-05-11 10:16:25.000000000 +0100
@@ -44,7 +44,7 @@
     struct GModule *module;
     struct Option *in_opt, *dsn_opt, *layer_opt, *type_opt, *frmt_opt,
 	*field_opt, *dsco, *lco;
-    struct Flag *cat_flag, *esristyle, *poly_flag;
+    struct Flag *cat_flag, *esristyle, *poly_flag, *update_flag;
     char buf[2000];
     char key1[200], key2[200];
     struct Key_Value *projinfo, *projunits;
@@ -161,6 +161,12 @@
     poly_flag->key = 'p';
     poly_flag->description = _("Export lines as polygons");
 
+
+    update_flag = G_define_flag();
+    update_flag->key = 'u';
+    update_flag->description = _("Update an existing data source");
+        
+
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
@@ -249,22 +255,39 @@
 	G_fatal_error(_("OGR driver <%s> not found"), frmt_opt->answer);
     Ogr_driver = OGRGetDriver(drn);
 
-    /* parse dataset creation options */
-    i = 0;
-    while (dsco->answers[i]) {
-	tokens = G_tokenize(dsco->answers[i], "=");
-	if (G_number_of_tokens(tokens))
-	    papszDSCO = CSLSetNameValue(papszDSCO, tokens[0], tokens[1]);
-	G_free_tokens(tokens);
-	i++;
+    if ( update_flag->answer && dsco->answers ) {
+        G_warning (_("Running in update mode. Ignoring 'dsco' parameter"));
+    } else {
+        /* parse dataset creation options */
+        i = 0;
+        while (dsco->answers[i]) {
+	    tokens = G_tokenize(dsco->answers[i], "=");
+	    if (G_number_of_tokens(tokens))
+	        papszDSCO = CSLSetNameValue(papszDSCO, tokens[0], tokens[1]);
+	    G_free_tokens(tokens);
+	    i++;
+        }
     }
 
     papszDSCO = dsco->answers;
-    Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, dsn_opt->answer, papszDSCO);
+    
+    if ( update_flag->answer ) {
+        Ogr_ds = 
+	    OGR_Dr_Open(Ogr_driver, dsn_opt->answer, TRUE);    
+    } else {
+        Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, dsn_opt->answer, papszDSCO);
+    }
     CSLDestroy(papszDSCO);
-    if (Ogr_ds == NULL)
-	G_fatal_error(_("Unable to open OGR data source '%s'"),
-		      dsn_opt->answer);
+    if (Ogr_ds == NULL) {
+        if ( !update_flag->answer ) {
+	    G_fatal_error(_("Unable to create OGR data source '%s'. "
+	                    "Use the -u flag to update an existing data source"),
+	                  dsn_opt->answer);	
+	} else {
+	    G_fatal_error(_("Unable to open OGR data source '%s'"),
+		          dsn_opt->answer);
+        }	
+    }
 
     /* parse layer creation options */
     i = 0;
_______________________________________________
grass-dev mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-dev

Reply via email to