[GRASS-dev] table only creation in v.in.ascii

Benjamin Ducke benjamin.ducke at ufg.uni-kiel.de
Fri Jan 11 03:51:33 EST 2008


Yes, it was meant for svn-trunk.

I just re-generated it and it works without fuzz on
my fresh SVN checkout (09:45 am CET).
Try the attached version if you like.

I will put work on this feature on hold until there is some
clarification whether these changes are actually wanted ...

However, I'd like to add the ability to read field names
from the ascii input file to v.in.ascii.
Should I add a flag to interpret the first line as
field labels?
Does anyone know what the restrictions on DBF field
names are? Length? Special characters?

Cheers,

Benjamin

Martin Landa wrote:
> hi,
> 
> the patch is for svn-trunk?
> 
> $ patch -p0 < ~/temp/v.in.ascii.patch
> patching file v.in.ascii/description.html
> patching file v.in.ascii/in.c
> Hunk #3 FAILED at 164.
> Hunk #4 succeeded at 187 (offset 1 line).
> Hunk #6 succeeded at 289 (offset 1 line).
> Hunk #8 succeeded at 444 (offset 1 line).
> Hunk #10 succeeded at 479 (offset 1 line).
> Hunk #12 succeeded at 589 (offset 1 line).
> Hunk #14 succeeded at 655 (offset 1 line).
> 1 out of 15 hunks FAILED -- saving rejects to file v.in.ascii/in.c.rej
> patching file v.in.ascii/local_proto.h
> patching file v.in.ascii/points.c
> 
> Martin
> 
> 2008/1/10, Benjamin Ducke <benjamin.ducke at ufg.uni-kiel.de>:
>> OK, this seems to be a useful hint, so here it goes again:
>>
>> Martin Landa wrote:
>>> hi,
>>>
>>> it is better to create one diff file instead of bunch of them, e.g.
>>>
>>> svn diff vector/v.in.ascii > v-in-ascii.diff
>>>
>>> sorry for bothering you:-)
>>>
>>> Martin
>>>
>>> 2008/1/10, Benjamin Ducke <benjamin.ducke at ufg.uni-kiel.de>:
>>>> Alright, here is the patches vor v.in.ascii.
>>>>
>>>> Cheers,
>>>>
>>>> Benjamin
>>>>
>>>> Benjamin Ducke wrote:
>>>>> Unfortunately, it returns DB_OK.
>>>>> There seems to be some caching going on which I don't quite
>>>>> understand at the moment.
>>>>> Starting and stopping the db driver in the right sequence seems
>>>>> to be important.
>>>>> Anyway, I solved the problem by doing the deletion at an earlier
>>>>> point in the program.
>>>>>
>>>>> Everything seems to work OK now. I'll do some tests with more
>>>>> complex tables, then post a patch.
>>>>>
>>>>> Benjamin
>>>>>
>>>>> Moritz Lennert wrote:
>>>>>> On 10/01/08 13:52, Benjamin Ducke wrote:
>>>>>>> OK, I have it almost working except for one annoyance:
>>>>>>>
>>>>>>> If the table already exists, then --o should allow the user to
>>>>>>> overwrite it. Thus, I check for that flag and delete the old
>>>>>>> table, if it exists, using:
>>>>>>>
>>>>>>>   db_delete_table ( connection.driverName, connection.databaseName,
>>>>>>>                     new->answer );
>>>>>>>
>>>>>>> But even so, the next call to
>>>>>>>
>>>>>>>   db_execute_immediate()
>>>>>>>
>>>>>>> complains about the table still being there and creation of the
>>>>>>> new table fails.
>>>>>>> I suppose that db_delete_table() does not delete the table immediately?
>>>>>> db_delete_table() sends a "drop table XYZ" sql query to the database
>>>>>> backend, so it should delete the table.
>>>>>>
>>>>>> Launching
>>>>>>
>>>>>> echo "drop table ggg" | db.execute
>>>>>> followed by
>>>>>> echo "create table ggg (cat int, value double)" | db.execute
>>>>>>
>>>>>> works without a problem.
>>>>>>
>>>>>> What does db_delete_table return ?
>>>>>>
>>>>>> Moritz
>>>>>>
>>>>>> _______________________________________________
>>>>>> grass-dev mailing list
>>>>>> grass-dev at lists.osgeo.org
>>>>>> http://lists.osgeo.org/mailman/listinfo/grass-dev
>>>>>>
>>>>>>
>>>> --
>>>> Benjamin Ducke, M.A.
>>>> Archäoinformatik
>>>> (Archaeoinformation Science)
>>>> Institut für Ur- und Frühgeschichte
>>>> (Inst. of Prehistoric and Historic Archaeology)
>>>> Christian-Albrechts-Universität zu Kiel
>>>> Johanna-Mestorf-Straße 2-6
>>>> D 24098 Kiel
>>>> Germany
>>>>
>>>> Tel.: ++49 (0)431 880-3378 / -3379
>>>> Fax : ++49 (0)431 880-7300
>>>> www.uni-kiel.de/ufg
>>>>
>>>>
>>>> _______________________________________________
>>>> grass-dev mailing list
>>>> grass-dev at lists.osgeo.org
>>>> http://lists.osgeo.org/mailman/listinfo/grass-dev
>>>>
>>>>
>>>
>> --
>> Benjamin Ducke, M.A.
>> Archäoinformatik
>> (Archaeoinformation Science)
>> Institut für Ur- und Frühgeschichte
>> (Inst. of Prehistoric and Historic Archaeology)
>> Christian-Albrechts-Universität zu Kiel
>> Johanna-Mestorf-Straße 2-6
>> D 24098 Kiel
>> Germany
>>
>> Tel.: ++49 (0)431 880-3378 / -3379
>> Fax : ++49 (0)431 880-7300
>> www.uni-kiel.de/ufg
>>
>>
>> _______________________________________________
>> grass-dev mailing list
>> grass-dev at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/grass-dev
>>
>>
> 
> 

-- 
Benjamin Ducke, M.A.
Archäoinformatik
(Archaeoinformation Science)
Institut für Ur- und Frühgeschichte
(Inst. of Prehistoric and Historic Archaeology)
Christian-Albrechts-Universität zu Kiel
Johanna-Mestorf-Straße 2-6
D 24098 Kiel
Germany

Tel.: ++49 (0)431 880-3378 / -3379
Fax : ++49 (0)431 880-7300
www.uni-kiel.de/ufg

-------------- next part --------------
diff -Naur v.in.ascii/description.html v.in.ascii2/description.html
--- v.in.ascii/description.html	2008-01-09 10:52:33.000000000 +0100
+++ v.in.ascii2/description.html	2008-01-10 15:46:27.000000000 +0100
@@ -187,6 +187,13 @@
 cut -d<the_field_separator_character> -f<comma-separated_list_of_columns> input_file | v.in.ascii <your_options>
 </pre></div>
 
+<h3>Importing only a database table</h3>
+The <b>-g</b> flag may be used to skip creation of vector objects (geometries) entirely and create only
+an attribute data table. The table name will be as specified by the <b>output=</b> parameter.<br>
+In the case of DBase format tables, a new DBF file will be created in the mapset's <b>dbf</b> directory.<br>
+All parameters relating to x, y or z coordinate fields will be ignored, but category field handling remains
+the same as in the case of importing a full vector map.
+
 
 <h2>EXAMPLES</h2>
 
diff -Naur v.in.ascii/in.c v.in.ascii2/in.c
--- v.in.ascii/in.c	2008-01-09 15:16:49.000000000 +0100
+++ v.in.ascii2/in.c	2008-01-10 15:38:37.000000000 +0100
@@ -27,6 +27,19 @@
 
 #define	A_DIR	"dig_ascii"
 
+
+/* this will attempt to delete a table only if it exists */
+void remove_table ( char* driver, char *db, char *table ) {
+
+	if ( db_table_exists ( driver , db , table ) ) {
+		if ( db_delete_table ( driver, db, table ) != DB_OK ) {
+		  	G_warning(_("Table <%s> exists and could not be deleted"), table );		  		    	
+		}
+	}
+	
+}
+
+
 int main(int argc, char *argv[])
 {
     FILE *ascii;
@@ -34,11 +47,11 @@
     struct Option *old, *new, *delim_opt, *columns_opt, *xcol_opt,
 	*ycol_opt, *zcol_opt, *catcol_opt, *format_opt, *skip_opt;
     int xcol, ycol, zcol, catcol, format, skip_lines;
-    struct Flag *zcoorf, *t_flag, *e_flag, *noheader_flag, *notopol_flag,
+    struct Flag *zcoorf, *t_flag, *g_flag, *e_flag, *noheader_flag, *notopol_flag,
 	*region_flag;
     char *table;
     char *fs;
-    int zcoor = WITHOUT_Z, make_table;
+    int zcoor = WITHOUT_Z, make_table, table_only;
 
     struct Map_info Map;
 
@@ -151,6 +164,10 @@
     t_flag->key = 't';
     t_flag->description = _("Do not create table in points mode");
     
+    g_flag = G_define_flag();
+    g_flag->key = 'g';
+    g_flag->description = _("Create only table, no vector objects/geometries");    
+
     notopol_flag = G_define_flag();
     notopol_flag->key = 'b';
     notopol_flag->description = _("Do not build topology in points mode");
@@ -169,23 +186,63 @@
     else
 	format = FORMAT_ALL;
 
+
+    /* check if flags and options make sense for the case where
+    	the user choose to create _only_ a table (-g).
+    */
+    
+    table_only = 0;
+    
+    if ( g_flag->answer )  {    
+    
+    	make_table = 1;
+	table_only = 1;
+    
+    	if  ( ( notopol_flag->answer ) ||
+	      ( e_flag->answer ) ||
+	      ( region_flag->answer ) ||
+	      ( t_flag->answer ) ||
+	      ( zcoorf->answer ) )
+	{
+		G_fatal_error (_("Cannot combine -g flag with -b, -e, -r, -t, or -z flag(s)"));
+	}
+
+    	if  ( format == FORMAT_ALL ) 
+	{
+		G_fatal_error (_("Flag -t only works for point format input files"));
+	}
+    	
+	if ( ( strcmp ( "1", xcol_opt->answer) ) ||
+	     ( strcmp ( "2", ycol_opt->answer) ) ||
+	     ( strcmp ( "0", zcol_opt->answer) ) )
+	{
+	  	G_warning (_("Table creation only. Any settings for x=, y= or z= will be ignored"));
+	}
+	
+    }
+
+
     skip_lines = atoi(skip_opt->answer);
     if (skip_lines < 0)
 	G_fatal_error(_("Please specify reasonable number of lines to skip"));
 
-    if (zcoorf->answer && format == FORMAT_POINT && !zcol_opt->answer)
+    /* everything related to creating vector objects can be skipped if -g flag was given (create only a table) */
+    if ( !table_only ) {
+      if (zcoorf->answer && format == FORMAT_POINT && !zcol_opt->answer)
 	G_fatal_error(_("Please specify z column"));
 
-    xcol = atoi(xcol_opt->answer) - 1;
-    ycol = atoi(ycol_opt->answer) - 1;
-    zcol = atoi(zcol_opt->answer) - 1;
-
-    /* specifying zcol= implies that a 3D map is needed */
-    if (zcol >= 0 && !zcoorf->answer)
-	zcoorf->answer = 1;
+    
+      xcol = atoi(xcol_opt->answer) - 1;
+      ycol = atoi(ycol_opt->answer) - 1;
+      zcol = atoi(zcol_opt->answer) - 1;
+
+      /* specifying zcol= implies that a 3D map is needed */
+      if (zcol >= 0 && !zcoorf->answer)
+	  zcoorf->answer = 1;
 
-    if (zcoorf->answer && format == FORMAT_POINT && zcol < 0)
-	G_fatal_error(_("Please specify reasonable z column"));
+      if (zcoorf->answer && format == FORMAT_POINT && zcol < 0)
+	  G_fatal_error(_("Please specify reasonable z column"));      
+    }
 
     catcol = atoi(catcol_opt->answer) - 1;
 
@@ -206,20 +263,24 @@
     if (strcmp(fs, "space") == 0)
 	fs = " ";
 
-    /* check dimension */
-    if (zcoorf->answer) {
-	zcoor = 1;
-    }
 
-    Vect_open_new(&Map, new->answer, zcoor);
-    Vect_hist_command(&Map);
+    if ( !table_only ) {
+      /* check dimension */
+      if (zcoorf->answer) {
+	  zcoor = 1;
+      }
 
-    if (e_flag->answer) {
+      Vect_open_new(&Map, new->answer, zcoor);
+      Vect_hist_command(&Map);
+
+      if (e_flag->answer) {
 	Vect_build(&Map, stdout);
 	Vect_close(&Map);
 	exit(EXIT_SUCCESS);
+      }
     }
 
+
     if (format == FORMAT_POINT) {
 	int i, rowlen, ncols, minncols, *coltype, *coltype2, *collen;
 	int n_int = 0, n_double = 0, n_string = 0;
@@ -227,6 +288,7 @@
 	struct field_info *Fi;
 	char *tmp, *key;
 	dbDriver *driver;
+	dbConnection connection;
 	dbString sql;
 	FILE *tmpascii;
 
@@ -239,90 +301,139 @@
 
 	points_analyse(ascii, tmpascii, fs, &rowlen, &ncols, &minncols,
 		       &coltype, &collen, skip_lines, xcol, ycol,
-		       region_flag->answer);
+		       region_flag->answer, table_only );
 
 	G_message(_("Maximum input row length: %d"), rowlen);
 	G_message(_("Maximum number of columns: %d"), ncols);
 	G_message(_("Minimum number of columns: %d"), minncols);
 
 	/* check column numbers */
-	if (xcol >= minncols) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_
-			  ("x column number > minimum last column number\n(incorrect field separator?)"));
-	}
-	if (ycol >= minncols) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_
-			  ("y column number > minimum last column number\n(incorrect field separator?)"));
-	}
-	if (zcol >= minncols) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("z column number > minimum last column number "
+	if ( !table_only ) {
+	  if (xcol >= minncols) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_
+			    ("x column number > minimum last column number\n(incorrect field separator?)"));
+	  }
+	  if (ycol >= minncols) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_
+			    ("y column number > minimum last column number\n(incorrect field separator?)"));
+	  }
+	  if (zcol >= minncols) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("z column number > minimum last column number "
+			      "(incorrect field separator?)"));
+	  }	      
+  	  if (catcol >= minncols) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("cat column number > minimum last column number "
 			    "(incorrect field separator?)"));
-	}
-	if (catcol >= minncols) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("cat column number > minimum last column number "
+	  }
+	  if (coltype[xcol] == DB_C_TYPE_STRING) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("x column is not of number type"));
+	  }
+	  if (coltype[ycol] == DB_C_TYPE_STRING) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("y column is not of number type"));
+	  }
+	  if (zcol >= 0 && coltype[zcol] == DB_C_TYPE_STRING) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("z column is not of number type"));
+	  }
+	  if (catcol >= 0 && coltype[catcol] == DB_C_TYPE_STRING) {
+	      Vect_delete(new->answer);
+	      G_fatal_error(_("cat column is not of number type"));
+	  }
+	} else {
+  	  if (catcol >= minncols) {
+	      G_fatal_error(_("cat column number > minimum last column number "
 			    "(incorrect field separator?)"));
-	}
+	  }
+        }
 
-	if (coltype[xcol] == DB_C_TYPE_STRING) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("x column is not of number type"));
-	}
-	if (coltype[ycol] == DB_C_TYPE_STRING) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("y column is not of number type"));
-	}
-	if (zcol >= 0 && coltype[zcol] == DB_C_TYPE_STRING) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("z column is not of number type"));
-	}
-	if (catcol >= 0 && coltype[catcol] == DB_C_TYPE_STRING) {
-	    Vect_delete(new->answer);
-	    G_fatal_error(_("cat column is not of number type"));
-	}
-
-	/* Create table */
-	make_table = 0;
-	for (i = 0; i < ncols; i++) {
-	    if (xcol != i && ycol != i && zcol != i && catcol != i) {
-		make_table = 1;
-		break;
-	    }
+
+	if ( !table_only ) {
+	
+    	  /* Create table? */
+	  make_table = 0;
+	
+	  for (i = 0; i < ncols; i++) {
+	      if (xcol != i && ycol != i && zcol != i && catcol != i) {
+		  make_table = 1;
+		  break;
+	      }
+	  }	  
 	}
+	
 	if (t_flag->answer) {
 	    make_table = 0;
 	}
 
 	if (make_table) {
-	    Fi = Vect_default_field_info(&Map, 1, NULL, GV_1TABLE);
-	    driver =
-		db_start_driver_open_database(Fi->driver,
-					      Vect_subst_var(Fi->database,
-							     &Map));
-	    if (driver == NULL) {
-		Vect_delete(new->answer);
-		G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
-			      Vect_subst_var(Fi->database, &Map), Fi->driver);
+	
+	    if ( !table_only ) {
+	    	      Fi = Vect_default_field_info(&Map, 1, NULL, GV_1TABLE);
+	      driver =
+		  db_start_driver_open_database(Fi->driver,
+						Vect_subst_var(Fi->database,
+							       &Map));
+	      if (driver == NULL) {	    	
+		  Vect_delete(new->answer);		
+		  G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+				Vect_subst_var(Fi->database, &Map), Fi->driver);
+	      }
+	    }
+	    
+	    if ( table_only ) {	 
+		db_get_connection ( &connection );
+		    	  
+		if ( db_table_exists ( connection.driverName, connection.databaseName, new->answer ) ) {
+	          if ( module->overwrite ) {
+		    if ( db_delete_table ( connection.driverName, connection.databaseName, new->answer ) != DB_OK ) {
+		  	G_fatal_error(_("Table <%s> exists and could not be deleted"),
+				new->answer );		  		    	
+		    }
+	          } else {
+		  	G_fatal_error(_("Table <%s> exists. Use --o to overwrite"),
+				new->answer );		  
+		  }		
+		}
+	    
+	    	/* start the db driver directly */		
+		driver=db_start_driver_open_database( connection.driverName, 
+				connection.databaseName );
+		if (driver == NULL) {
+		  G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+				connection.databaseName, connection.driverName );
+	      }
 	    }
+	    
 	    db_begin_transaction(driver);
-
+	    
 	    db_init_string(&sql);
-	    sprintf(buf, "create table %s ( ", Fi->table);
+	    
+	    if (!table_only ) {
+	    	sprintf(buf, "create table %s ( ", Fi->table );
+	    } else {
+	        sprintf(buf, "create table %s ( ", new->answer );
+	    }
 	    db_append_string(&sql, buf);
 
 	    if (catcol < 0) {
-		db_append_string(&sql, "cat integer, ");
+	      db_append_string(&sql, "cat integer, ");
 	    }
 
 	    for (i = 0; i < ncols; i++) {
+	    
 		if (i > 0 && !columns_opt->answer) {
 		    db_append_string(&sql, ", ");
 		}
+		
 		if (catcol == i && coltype[i] != DB_C_TYPE_INT) {
-		    Vect_delete(new->answer);
+		    if ( !table_only ) {    	    	
+		      Vect_delete(new->answer);		    
+		    }
 		    G_fatal_error(_("Category column is not of integer type"));
 		}
 
@@ -332,7 +443,7 @@
 		    if (!columns_opt->answer) {
 			sprintf(buf, "int_%d integer", n_int + 1);
 			db_append_string(&sql, buf);
-			if (catcol == i) {
+			if  ( catcol == i ) {
 			    sprintf(buf, "int_%d", n_int + 1);
 			    key = G_store(buf);
 			}
@@ -359,6 +470,7 @@
 		    break;
 		}
 	    }
+	    
 	    if (columns_opt->answer) {
 		db_append_string(&sql, columns_opt->answer);
 	    }
@@ -366,25 +478,45 @@
 
 	    /* this link is added with default 'cat' key, later deleted and replaced by true key name,
 	     * otherwise if module crashes when the table exists but link is not written it makes troubles */
-	    Vect_map_add_dblink(&Map, 1, NULL, Fi->table, "cat", Fi->database,
+	     
+	    if ( !table_only ) {
+	      Vect_map_add_dblink(&Map, 1, NULL, Fi->table, "cat", Fi->database,
 				Fi->driver);
+	    }
 
 	    /* Create table */
 	    G_debug(3, db_get_string(&sql));
-	    if (db_execute_immediate(driver, &sql) != DB_OK) {
-		Vect_delete(new->answer);
-		G_fatal_error(_("Unable to create table: %s"),
-			      db_get_string(&sql));
+	    	    
+	    if (db_execute_immediate(driver, &sql) != DB_OK) {	    
+	    	if ( !table_only ) {
+		  Vect_delete(new->answer);
+		  G_fatal_error(_("Unable to create table: %s"),
+			      db_get_string(&sql));		  
+		} else {
+		  G_fatal_error(_("Unable to create table: %s"),
+			      db_get_string(&sql));		
+		}
 	    }
 
 	    /* Grant */
-	    if (db_grant_on_table
-		(driver, Fi->table, DB_PRIV_SELECT,
-		 DB_GROUP | DB_PUBLIC) != DB_OK) {
-		Vect_delete(new->answer);
-		G_fatal_error(_("Unable to grant privileges on table <%s>"),
-			      Fi->table);
+	    if ( !table_only ) {
+	      if (db_grant_on_table
+		  (driver, Fi->table, DB_PRIV_SELECT,
+		   DB_GROUP | DB_PUBLIC) != DB_OK) {
+		  Vect_delete(new->answer);
+		  G_fatal_error(_("Unable to grant privileges on table <%s>"),
+				Fi->table);
+	      }
+	    } else {
+	      if (db_grant_on_table
+		  (driver, new->answer, DB_PRIV_SELECT,
+		   DB_GROUP | DB_PUBLIC) != DB_OK) {
+		   remove_table ( connection.driverName, connection.databaseName, new->answer );
+		  G_fatal_error(_("Unable to grant privileges on table <%s>"),
+				new->answer);
+	      }
 	    }
+	    
 
 	    /* Check column types */
 	    if (columns_opt->answer) {
@@ -392,22 +524,36 @@
 		dbTable *table;
 		dbColumn *column;
 
-		db_set_string(&sql, Fi->table);
-		if (db_describe_table(driver, &sql, &table) != DB_OK) {
-		    Vect_delete(new->answer);
-		    G_fatal_error(_("Unable to describe table <%s>"),
-				  Fi->table);
+                if ( !table_only ) {
+		  db_set_string(&sql, Fi->table);
+		  if (db_describe_table(driver, &sql, &table) != DB_OK) {
+			Vect_delete(new->answer);
+			G_fatal_error(_("Unable to describe table <%s>"),
+				      Fi->table);
+		  }
+		} else {
+		  db_set_string(&sql, new->answer);
+		  if (db_describe_table(driver, &sql, &table) != DB_OK) {
+		        remove_table ( connection.driverName, connection.databaseName, new->answer );
+			G_fatal_error(_("Unable to describe table <%s>"),
+				      new->answer);
+		  }		
 		}
 
 		nc = db_get_table_number_of_columns(table);
 
+		
 		if ((catcol >= 0 && nc != ncols) ||
-		    (catcol < 0 && (nc - 1) != ncols)) {
-		    Vect_delete(new->answer);
-		    G_fatal_error(_
-				  ("Number of columns defined (%d) does not match number "
-				   "of columns (%d) in input"),
-				  catcol < 0 ? nc - 1 : nc, ncols);
+		      (catcol < 0 && (nc - 1) != ncols)) {
+		        if ( !table_only ) {
+			  Vect_delete(new->answer);
+			} else {
+			  remove_table ( connection.driverName, connection.databaseName, new->answer );
+			}
+		        G_fatal_error(_
+				    ("Number of columns defined (%d) does not match number "
+				     "of columns (%d) in input"),
+				    catcol < 0 ? nc - 1 : nc, ncols);
 		}
 
 		coltype2 = (int *)G_malloc(ncols * sizeof(int));
@@ -442,7 +588,12 @@
 			break;
 		    case DB_C_TYPE_DOUBLE:
 			if (ctype == DB_C_TYPE_INT) {
-			    Vect_delete(new->answer);
+			
+			    if ( !table_only ) {
+			      Vect_delete(new->answer);
+			    } else {
+			      remove_table ( connection.driverName, connection.databaseName, new->answer );
+			    }
 			    G_fatal_error(_
 					  ("Column number %d defined as integer "
 					   "has double values"), i + 1);
@@ -454,19 +605,34 @@
 			break;
 		    case DB_C_TYPE_STRING:
 			if (ctype == DB_C_TYPE_INT) {
-			    Vect_delete(new->answer);
+			
+			    if ( !table_only ) {
+			      Vect_delete(new->answer);
+			    } else {
+			      remove_table ( connection.driverName, connection.databaseName, new->answer );
+			    }
 			    G_fatal_error(_
 					  ("Column number %d defined as integer "
 					   "has string values"), i + 1);
 			}
 			else if (ctype == DB_C_TYPE_DOUBLE) {
-			    Vect_delete(new->answer);
+			
+			    if ( !table_only ) {
+			      Vect_delete(new->answer);
+			    } else {
+			      remove_table ( connection.driverName, connection.databaseName, new->answer );
+			    }
 			    G_fatal_error(_
 					  ("Column number %d defined as double "
 					   "has string values"), i + 1);
 			}
 			if (length < collen[i]) {
-			    Vect_delete(new->answer);
+			
+			    if ( !table_only ) {
+			      Vect_delete(new->answer);
+			    } else {
+			      remove_table ( connection.driverName, connection.databaseName, new->answer );
+			    }
 			    G_fatal_error(_
 					  ("Length of column %d (%d) is less than "
 					   "maximum value " "length (%d)"),
@@ -488,24 +654,35 @@
 
 	    }
 
-	    if (db_create_index2(driver, Fi->table, key) != DB_OK)
-		G_warning(_("Unable to create index for table <%s>, key <%s>"),
-			  Fi->table, key);
+	    if (!table_only) {
+	      if (db_create_index2(driver, Fi->table, key) != DB_OK)
+		  G_warning(_("Unable to create index for table <%s>, key <%s>"),
+			    Fi->table, key);
+	    } else {
+	      if (db_create_index2(driver, new->answer, key) != DB_OK)
+		  G_warning(_("Unable to create index for table <%s>, key <%s>"),
+			    new->answer, key);	    
+	    }
 
-	    Vect_map_del_dblink(&Map, 1);
-	    Vect_map_add_dblink(&Map, 1, NULL, Fi->table, key, Fi->database,
+	    if ( !table_only ) {
+	      Vect_map_del_dblink(&Map, 1);
+	      Vect_map_add_dblink(&Map, 1, NULL, Fi->table, key, Fi->database,
 				Fi->driver);
-
-	    table = Fi->table;
+              table = Fi->table;
+            } else {
+	      table = new->answer;
+	    }
+	    
+	    
 	}
 	else {
 	    driver = NULL;
 	    table = NULL;
 	}
 
-	points_to_bin(tmpascii, rowlen, &Map, driver, table, fs, ncols,
-		      coltype2, xcol, ycol, zcol, catcol, skip_lines);
-
+	  points_to_bin(tmpascii, rowlen, &Map, driver, table, fs, ncols,
+		      coltype2, xcol, ycol, zcol, catcol, skip_lines, table_only );
+	
 	if (driver) {
 	    G_message(_("Populating table..."));
 	    db_commit_transaction(driver);
@@ -524,12 +701,14 @@
     if (old->answer != NULL)
 	fclose(ascii);
 
-    if (notopol_flag->answer) {
-	Vect_close(&Map);
-    }
-    else {
-	Vect_build(&Map, stdout);
-	Vect_close(&Map);
+    if (!table_only) {
+      if (notopol_flag->answer) {
+	  Vect_close(&Map);
+      }
+      else {
+	  Vect_build(&Map, stdout);
+	  Vect_close(&Map);
+      }
     }
 
     G_done_msg(" ");
diff -Naur v.in.ascii/local_proto.h v.in.ascii2/local_proto.h
--- v.in.ascii/local_proto.h	2008-01-09 10:52:33.000000000 +0100
+++ v.in.ascii2/local_proto.h	2008-01-10 13:25:48.000000000 +0100
@@ -12,11 +12,11 @@
 
 
 int points_analyse (FILE *, FILE *, char *, int *, int *, int *, int **,
-		     int **, int, int, int, int);
+		     int **, int, int, int, int, int );
 
 int points_to_bin (FILE *, int, struct Map_info *, dbDriver *,
 		   char *, char *, int, int *,
-		   int, int, int, int, int);
+		   int, int, int, int, int, int);
 
 int read_head (FILE *, struct Map_info *);
 
diff -Naur v.in.ascii/points.c v.in.ascii2/points.c
--- v.in.ascii/points.c	2008-01-09 10:52:33.000000000 +0100
+++ v.in.ascii2/points.c	2008-01-10 13:28:43.000000000 +0100
@@ -50,12 +50,16 @@
  * minncolumns: minimum number of columns
  * column_type: column types
  * column_length: column lengths (string only)
+ *
+ * if table_only == 1, then coordinate fields don't need to be checked for validity
+ *                     (there might not even be any in the input file) 		       
+ *
  */
 
 int points_analyse(FILE * ascii_in, FILE * ascii, char *fs,
 		   int *rowlength, int *ncolumns, int *minncolumns,
 		   int **column_type, int **column_length, int skip_lines,
-		   int xcol, int ycol, int region_flag)
+		   int xcol, int ycol, int region_flag, int table_only )
 {
     int i;
     int buflen;			/* buffer length */
@@ -138,6 +142,8 @@
 	/* Determine column types */
 	for (i = 0; i < ntokens; i++) {
 	    if ((G_projection() == PROJECTION_LL)) {
+	    
+	       if (!table_only) {
 		if (i == xcol || i == ycol) {
 		    if (i == 0) {	/* Save position of original internal token buffer */
 			/* Prevent memory leaks */
@@ -177,6 +183,8 @@
 			    G_fatal_error(_("Unparsable latitude value: %s"), tokens[i]);
 		    }
  		} /* if (x or y) */
+	      }
+		
 
 		if (i == ntokens - 1 && sav_buf != NULL) {
 		    /* Restore original token buffer so free_tokens works */
@@ -185,7 +193,7 @@
 		    sav_buf = NULL;
 		}
 	    }		/* PROJECTION_LL */
-	    else {
+	    else {	      
 		if (region_flag) {
 		    /* consider z range if -z flag is used? */
 		    /* change to if(>= east,north){skip=1;} to allow correct tiling */
@@ -263,7 +271,7 @@
 int points_to_bin(FILE * ascii, int rowlen, struct Map_info *Map,
 		  dbDriver * driver, char *table, char *fs, int ncols,
 		  int *coltype, int xcol, int ycol, int zcol, int catcol,
-		  int skip_lines)
+		  int skip_lines, int table_only )
 {
     char *buf, buf2[1000];
     int cat = 0;
@@ -296,13 +304,15 @@
 	char **tokens;
 	int ntokens;		/* number of tokens */
 
-	if (row <= skip_lines) {
-	    G_debug(4, "writing skip line %d to hist : %d chars", row,
-		    (int) strlen(buf));
-	    Vect_hist_write(Map, buf);
-	    Vect_hist_write(Map, "\n");
-	    row++;
-	    continue;
+	if (!table_only) {
+	  if (row <= skip_lines) {
+	      G_debug(4, "writing skip line %d to hist : %d chars", row,
+		      (int) strlen(buf));
+	      Vect_hist_write(Map, buf);
+	      Vect_hist_write(Map, "\n");
+	      row++;
+	      continue;
+	  }
 	}
 
 	len = strlen(buf);
@@ -314,33 +324,39 @@
 	tokens = G_tokenize(buf, fs);
 	ntokens = G_number_of_tokens(tokens);
 
-	if ((G_projection() == PROJECTION_LL)) {
-	    G_scan_easting(tokens[xcol], &x, window.proj);
-	    G_scan_northing(tokens[ycol], &y, window.proj);
-	}
-	else {
-	    x = atof(tokens[xcol]);
-	    y = atof(tokens[ycol]);
-	}
-	G_debug(4, "x: %f, y: %f", x, y);
 
-	if (zcol >= 0)
-	    z = atof(tokens[zcol]);
-	else
-	    z = 0.0;
+	if ( !table_only ) {
+	  if ((G_projection() == PROJECTION_LL)) {
+	      G_scan_easting(tokens[xcol], &x, window.proj);
+	      G_scan_northing(tokens[ycol], &y, window.proj);
+	  }
+	  else {
+	      x = atof(tokens[xcol]);
+	      y = atof(tokens[ycol]);
+	  }
+	  G_debug(4, "x: %f, y: %f", x, y);
+
+	  if (zcol >= 0)
+	      z = atof(tokens[zcol]);
+	  else
+	      z = 0.0;
 
+	}
+	
 	if (catcol >= 0)
 	    cat = atof(tokens[catcol]);
 	else
 	    cat++;
 
-	Vect_reset_line(Points);
-	Vect_reset_cats(Cats);
+	if (!table_only) {
+	  Vect_reset_line(Points);
+	  Vect_reset_cats(Cats);
 
-	Vect_append_point(Points, x, y, z);
-	Vect_cat_set(Cats, 1, cat);
+	  Vect_append_point(Points, x, y, z);
+	  Vect_cat_set(Cats, 1, cat);
 
-	Vect_write_line(Map, GV_POINT, Points, Cats);
+	  Vect_write_line(Map, GV_POINT, Points, Cats);
+	}
 
 	/* Attributes */
 	if (driver) {


More information about the grass-dev mailing list