[GRASS-SVN] r54681 - grass/trunk/vector/v.out.postgis

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 17 02:07:15 PST 2013


Author: martinl
Date: 2013-01-17 02:07:15 -0800 (Thu, 17 Jan 2013)
New Revision: 54681

Added:
   grass/trunk/vector/v.out.postgis/table.c
Modified:
   grass/trunk/vector/v.out.postgis/create.c
   grass/trunk/vector/v.out.postgis/local_proto.h
   grass/trunk/vector/v.out.postgis/main.c
Log:
v.out.postgis: check for fid and geometry column


Modified: grass/trunk/vector/v.out.postgis/create.c
===================================================================
--- grass/trunk/vector/v.out.postgis/create.c	2013-01-17 10:05:40 UTC (rev 54680)
+++ grass/trunk/vector/v.out.postgis/create.c	2013-01-17 10:07:15 UTC (rev 54681)
@@ -29,7 +29,8 @@
 }
 
 char *create_pgfile(const char *dsn, const char *schema, const char *olink,
-                    char **options, int topo)
+                    char **options, int topo,
+		    char **fid_column, char **geom_column)
 {
     int   i;
     char *filename, *conninfo;
@@ -85,6 +86,11 @@
             G_str_to_lower(tokens[0]);
 	    G_set_key_value(tokens[0], tokens[1], key_val);
 	    
+	    if (strcmp(tokens[0], "fid") == 0)
+		G_asprintf(fid_column, tokens[1]);
+	    if (strcmp(tokens[0], "geometry_name") == 0)
+		G_asprintf(geom_column, tokens[1]);
+
 	    G_free_tokens(tokens);
 	}
     }

Modified: grass/trunk/vector/v.out.postgis/local_proto.h
===================================================================
--- grass/trunk/vector/v.out.postgis/local_proto.h	2013-01-17 10:05:40 UTC (rev 54680)
+++ grass/trunk/vector/v.out.postgis/local_proto.h	2013-01-17 10:07:15 UTC (rev 54681)
@@ -13,9 +13,13 @@
 
 /* create.c */
 void create_table(struct Map_info *, struct Map_info *);
-char *create_pgfile(const char *, const char *, const char *, char **, int);
+char *create_pgfile(const char *, const char *, const char *, char **, int,
+		    char **, char **);
 
 /* options.c */
 void define_options(struct params *, struct flags *);
 
+/* table.c */
+void check_columns(const struct Map_info *, const char *, const char *, const char *);
+
 #endif /* __LOCAL_PROTO_V_OUT_POSTGIS__ */

Modified: grass/trunk/vector/v.out.postgis/main.c
===================================================================
--- grass/trunk/vector/v.out.postgis/main.c	2013-01-17 10:05:40 UTC (rev 54680)
+++ grass/trunk/vector/v.out.postgis/main.c	2013-01-17 10:07:15 UTC (rev 54681)
@@ -31,6 +31,8 @@
     
     int ret, field;
     char *schema, *olayer, *pg_file;
+    char *fid_column, *geom_column;
+    
     struct Map_info In, Out;
     
     G_gisinit(argv[0]);
@@ -101,10 +103,22 @@
         G_warning(_("Unable to open vector map <%s> on topological level"),
                   params.input->answer);
     
+    /* default columns */
+    fid_column  = GV_PG_FID_COLUMN;
+    geom_column = GV_PG_GEOMETRY_COLUMN;
+    
     /* create output for writing */
     pg_file = create_pgfile(params.dsn->answer, schema, params.olink->answer,
-                            params.opts->answers, flags.topo->answer ? TRUE : FALSE);
+                            params.opts->answers, flags.topo->answer ? TRUE : FALSE,
+			    &fid_column, &geom_column);
+    G_debug(1, "fid_column: %s", fid_column);
+    G_debug(1, "geom_column: %s", geom_column);
     
+    if (!flags.table->answer) {
+	/* check fid column */
+	check_columns(&In, params.layer->answer, fid_column, geom_column);
+    }
+    
     if (-1 == Vect_open_new(&Out, olayer, Vect_is_3d(&In)))
         G_fatal_error(_("Unable to create PostGIS layer <%s>"),
                       olayer);

Added: grass/trunk/vector/v.out.postgis/table.c
===================================================================
--- grass/trunk/vector/v.out.postgis/table.c	                        (rev 0)
+++ grass/trunk/vector/v.out.postgis/table.c	2013-01-17 10:07:15 UTC (rev 54681)
@@ -0,0 +1,62 @@
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+/* Check columns 
+
+   1) FID column - must be integer
+
+   @todo check for unique values
+
+   2) Geometry column
+
+   calls G_fatal_error() on error
+*/
+void check_columns(const struct Map_info *Map, const char *layer,
+		   const char *fid_column, const char *geom_column)
+{
+    struct field_info *fi;
+    
+    dbDriver *driver;
+    dbTable *table;
+    dbColumn *column;
+    dbString stmt;
+    
+    fi = Vect_get_field2(Map, layer);
+    if (!fi) {
+	G_verbose_message(_("No database connection for layer <%s>"), layer);
+	return;
+    }
+    
+    /* open connection */
+    driver = db_start_driver_open_database(fi->driver, fi->database);
+    if (!driver)
+	G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+		      fi->database, fi->driver);
+    
+    db_init_string(&stmt);
+    db_set_string(&stmt, fi->table);
+    if (db_describe_table(driver, &stmt, &table) != DB_OK) {
+	db_close_database_shutdown_driver(driver);
+	G_fatal_error(_("Unable to describe table <%s>"), fi->table);
+    }
+    
+    /* check for fid column in attribute table */
+    column = db_get_table_column_by_name(table, fid_column);
+    if (column) {
+	int ctype;
+	ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column));
+	if (ctype != DB_C_TYPE_INT)
+	    G_fatal_error(_("Invalid FID column (%s). FID column must be integer. "
+			    "Please specify different FID column by 'options=\"FID=<name>\"'."),
+			  fid_column);
+    }
+    
+    /* check if geometry column already exists in the attribute table */
+    if (db_get_table_column_by_name(table, geom_column))
+	G_fatal_error(_("Column (%s) already exists in the table. "
+			"Please specify different geometry column by 'options=\"GEOMETRY_NAME=<name>\"'."),
+		      geom_column);
+    
+    db_close_database_shutdown_driver(driver);
+}


Property changes on: grass/trunk/vector/v.out.postgis/table.c
___________________________________________________________________
Added: svn:mime-type
   + text/x-csrc
Added: svn:eol-style
   + native



More information about the grass-commit mailing list