[GRASS-SVN] r54680 - in grass/trunk: include/vect lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 17 02:05:40 PST 2013


Author: martinl
Date: 2013-01-17 02:05:40 -0800 (Thu, 17 Jan 2013)
New Revision: 54680

Modified:
   grass/trunk/include/vect/dig_defines.h
   grass/trunk/lib/vector/Vlib/open.c
   grass/trunk/lib/vector/Vlib/open_pg.c
   grass/trunk/lib/vector/Vlib/pg_local_proto.h
   grass/trunk/lib/vector/Vlib/write_pg.c
Log:
vlib/pg: don't skip fid column if exists, check data type - must be integer


Modified: grass/trunk/include/vect/dig_defines.h
===================================================================
--- grass/trunk/include/vect/dig_defines.h	2013-01-17 09:00:29 UTC (rev 54679)
+++ grass/trunk/include/vect/dig_defines.h	2013-01-17 10:05:40 UTC (rev 54680)
@@ -248,3 +248,8 @@
 } SF_FeatureType;
 
 #define HEADSTR	50
+
+/*! \brief GRASS-PostGIS data provider - default fid column */
+#define GV_PG_FID_COLUMN       "fid"
+/*! Simple features access */
+#define GV_PG_GEOMETRY_COLUMN "geom"

Modified: grass/trunk/lib/vector/Vlib/open.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open.c	2013-01-17 09:00:29 UTC (rev 54679)
+++ grass/trunk/lib/vector/Vlib/open.c	2013-01-17 10:05:40 UTC (rev 54680)
@@ -1206,7 +1206,7 @@
                 pg_info->fid_column = G_store(p);
             else
 #ifdef HAVE_POSTGRES
-                pg_info->fid_column = G_store(FID_COLUMN);
+                pg_info->fid_column = G_store(GV_PG_FID_COLUMN);
 #endif
             G_debug(1, "PG: fid_column = '%s'", pg_info->fid_column);
             
@@ -1216,7 +1216,7 @@
                 pg_info->geom_column = G_store(p);
             else
 #ifdef HAVE_POSTGRES
-                pg_info->geom_column = G_store(GEOMETRY_COLUMN);
+                pg_info->geom_column = G_store(GV_PG_GEOMETRY_COLUMN);
 #endif
             G_debug(1, "PG: geom_column = '%s'", pg_info->geom_column);
             

Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c	2013-01-17 09:00:29 UTC (rev 54679)
+++ grass/trunk/lib/vector/Vlib/open_pg.c	2013-01-17 10:05:40 UTC (rev 54680)
@@ -255,11 +255,11 @@
 
     /* if fid_column not defined, use 'fid' */
     if (!pg_info->fid_column)
-        pg_info->fid_column = G_store(FID_COLUMN);
+        pg_info->fid_column = G_store(GV_PG_FID_COLUMN);
 
     /* if geom_column not defined, use 'geom' */
     if (!pg_info->geom_column)
-        pg_info->geom_column = G_store(GEOMETRY_COLUMN);
+        pg_info->geom_column = G_store(GV_PG_GEOMETRY_COLUMN);
     
     /* check if feature table already exists */
     sprintf(stmt, "SELECT * FROM pg_tables "

Modified: grass/trunk/lib/vector/Vlib/pg_local_proto.h
===================================================================
--- grass/trunk/lib/vector/Vlib/pg_local_proto.h	2013-01-17 09:00:29 UTC (rev 54679)
+++ grass/trunk/lib/vector/Vlib/pg_local_proto.h	2013-01-17 10:05:40 UTC (rev 54680)
@@ -8,10 +8,6 @@
 
 #define CURSOR_PAGE 500
 
-#define FID_COLUMN       "fid"
-/*! Simple features access */
-#define GEOMETRY_COLUMN "geom"
-
 /*! Topological access */
 #define TOPO_SCHEMA "topology"
 #define TOPO_ID     "topology_id"

Modified: grass/trunk/lib/vector/Vlib/write_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/write_pg.c	2013-01-17 09:00:29 UTC (rev 54679)
+++ grass/trunk/lib/vector/Vlib/write_pg.c	2013-01-17 10:05:40 UTC (rev 54680)
@@ -1006,7 +1006,7 @@
     stmt = NULL;
     if (Fi && cat > -1) { /* write attributes (simple features and topology elements) */
         int col, ncol, more;
-        int sqltype, ctype;
+        int sqltype, ctype, is_fid;
         char buf_val[DB_SQL_MAX], buf_tmp[DB_SQL_MAX];
         char *str_val;
 
@@ -1054,17 +1054,7 @@
                     column = db_get_table_column(table, col);
                     colname = db_get_column_name(column);
 
-                    /* skip fid column */
-                    if (strcmp(pg_info->fid_column, colname) == 0)
-                        continue;
-
-                    /* -> columns */
-                    sprintf(buf_tmp, "%s", colname);
-                    strcat(buf, buf_tmp);
-                    if (col < ncol - 1)
-                        strcat(buf, ",");
-                    
-                    /* -> values */
+		    /* -> values */
                     value = db_get_column_value(column);
                     /* for debug only */
                     db_convert_column_value_to_string(column, &dbstmt);
@@ -1074,6 +1064,22 @@
                     sqltype = db_get_column_sqltype(column);
                     ctype = db_sqltype_to_Ctype(sqltype);
 
+		    is_fid = strcmp(pg_info->fid_column, colname) == 0;
+		    
+		    /* check fid column (must be integer) */
+                    if (is_fid == TRUE &&
+			ctype != DB_C_TYPE_INT) {
+			G_warning(_("FID column must be integer, column <%s> ignored!"),
+				  colname);
+                        continue;
+		    }
+
+                    /* -> columns */
+                    sprintf(buf_tmp, "%s", colname);
+                    strcat(buf, buf_tmp);
+                    if (col < ncol - 1)
+                        strcat(buf, ",");
+		    
                     /* prevent writing NULL values */
                     if (!db_test_value_isnull(value)) {
                         switch (ctype) {
@@ -1102,6 +1108,8 @@
                         }
                     }
                     else {
+			if (is_fid == TRUE)
+			    G_warning(_("Invalid value for FID column: NULL"));
                         sprintf(buf_tmp, "NULL");
                     }
                     strcat(buf_val, buf_tmp);
@@ -1117,6 +1125,10 @@
                 else {
                     /* PostGIS topology access, write geometry in
                      * topology schema, skip geometry at this point */
+		    if (buf[strlen(buf)-1] == ',') { /* last column skipped */
+			buf[strlen(buf)-1] = '\0';
+			buf_val[strlen(buf_val)-1] = '\0';
+		    }
                     G_asprintf(&stmt, "%s) VALUES (%s)",
                                buf, buf_val);
                 }
@@ -1132,11 +1144,12 @@
                        pg_info->schema_name, pg_info->table_name,
                        pg_info->geom_column, geom_data);
         }
-        else if (cat > 0)
+        else if (cat > 0) {
             /* no attributes (topology elements) */
             G_asprintf(&stmt, "INSERT INTO \"%s\".\"%s\" (%s) VALUES (NULL)",
                        pg_info->schema_name, pg_info->table_name,
                        pg_info->geom_column); 
+	}
     }
     
     return stmt;



More information about the grass-commit mailing list