[GRASS-SVN] r58325 - grass/trunk/lib/vector/Vlib

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Nov 29 01:43:40 PST 2013


Author: martinl
Date: 2013-11-29 01:43:40 -0800 (Fri, 29 Nov 2013)
New Revision: 58325

Modified:
   grass/trunk/lib/vector/Vlib/build_pg.c
   grass/trunk/lib/vector/Vlib/open_pg.c
   grass/trunk/lib/vector/Vlib/pg_local_proto.h
Log:
vlib/pg: fix clean-up GRASS DB topology tables in full mode


Modified: grass/trunk/lib/vector/Vlib/build_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_pg.c	2013-11-29 08:27:30 UTC (rev 58324)
+++ grass/trunk/lib/vector/Vlib/build_pg.c	2013-11-29 09:43:40 UTC (rev 58325)
@@ -132,7 +132,7 @@
 */
 int build_topo(struct Map_info *Map, int build)
 {
-    int line, type, s;
+    int line, type, s, n_nodes;
     int area, nareas, isle, nisles;
     int face[2];
     char stmt[DB_SQL_MAX];
@@ -162,7 +162,10 @@
     /* cache features to speed-up random access (when attaching isles
        to areas) */
     if (build >= GV_BUILD_BASE) {
-        pg_info->cache.ctype = CACHE_MAP;
+        /* clean-up GRASS topology tables in DB */
+        if (!pg_info->topo_geo_only)
+            Vect__clean_grass_db_topo(&(Map->fInfo.pg));
+        
         if (Map->mode == GV_MODE_RW &&
             pg_info->cache.lines_num > 0) {
 
@@ -178,16 +181,29 @@
 
             /* reset cache for reading features */
             Vect__free_cache(&(pg_info->cache));
-
-            /* force loading nodes from DB to get up-to-date node
-             * offsets */
-            Vect__free_offset(&(pg_info->offset));
-            Map->plus.n_nodes = Vect__load_map_nodes_pg(Map, TRUE);
         }
     }
+
+    if (plus->built < GV_BUILD_BASE) {
+        /* force loading nodes from DB to get up-to-date node
+         * offsets, see write_nodes() for details */
+        Vect__free_offset(&(pg_info->offset));
+
+        pg_info->cache.ctype = CACHE_FEATURE; /* do not cache nodes */
+        n_nodes = Map->plus.n_nodes = Vect__load_map_nodes_pg(Map, TRUE);
+        Vect__free_cache(&(pg_info->cache));
+    }
+
+    if (build > GV_BUILD_BASE)
+        pg_info->cache.ctype = CACHE_MAP; /* cache all features */
+
     /* update TopoGeometry based on GRASS-like topology */
     Vect_build_nat(Map, build);
     
+    if (n_nodes != Map->plus.n_nodes) 
+        G_warning(_("Inconsistency in topology: number of nodes %d (should be %d)"),
+                  Map->plus.n_nodes, n_nodes);
+
     /* store map boundig box in DB */
     save_map_bbox(pg_info, &(plus->box));
     
@@ -888,4 +904,42 @@
     *stmt_size = stmt_id_size;
 }
 
+/*!
+  \brief Clean-up GRASS Topology tables
+
+  \param pg_info pointer to Format_info_pg pg_info
+
+  \return 0 on success
+  \return -1 on error
+*/
+int Vect__clean_grass_db_topo(struct Format_info_pg *pg_info)
+{
+    char stmt[DB_SQL_MAX];
+
+    sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
+            pg_info->toposchema_name, TOPO_TABLE_NODE);
+    if (-1 == Vect__execute_pg(pg_info->conn, stmt))
+        return -1;
+    
+    sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
+            pg_info->toposchema_name, TOPO_TABLE_LINE);
+    if (-1 == Vect__execute_pg(pg_info->conn, stmt))
+        return -1;
+
+    
+    sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
+            pg_info->toposchema_name, TOPO_TABLE_AREA);
+    if (-1 == Vect__execute_pg(pg_info->conn, stmt))
+        return -1;
+
+    
+    sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
+            pg_info->toposchema_name, TOPO_TABLE_ISLE);
+    if (-1 == Vect__execute_pg(pg_info->conn, stmt))
+        return -1;
+
+
+    return 0;
+}
+
 #endif

Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c	2013-11-29 08:27:30 UTC (rev 58324)
+++ grass/trunk/lib/vector/Vlib/open_pg.c	2013-11-29 09:43:40 UTC (rev 58325)
@@ -322,7 +322,7 @@
   
   \param[in,out] Map pointer to Map_info structure
   \param head_only TRUE to read only header
-  \param update TRUE for update mode (reloading topology)
+  \param update TRUE to clean GRASS topology in update mode
 
   \return 0 on success
   \return 1 topology layer does not exist
@@ -352,28 +352,10 @@
     
     ret = Vect__load_plus_pg(Map, head_only);
 
-    /* clean-up GRASS-like topology tables on update mode */
-    if (update && !pg_info->topo_geo_only && ret == 0) {
-        char stmt[DB_SQL_MAX];
+    if (update)
+        Vect__clean_grass_db_topo(pg_info);
 
-        sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
-                pg_info->toposchema_name, TOPO_TABLE_NODE);
-        Vect__execute_pg(pg_info->conn, stmt);
-
-        sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
-                pg_info->toposchema_name, TOPO_TABLE_LINE);
-        Vect__execute_pg(pg_info->conn, stmt);
-
-        sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
-                pg_info->toposchema_name, TOPO_TABLE_AREA);
-        Vect__execute_pg(pg_info->conn, stmt);
-
-        sprintf(stmt, "DELETE FROM \"%s\".\"%s\"",
-                pg_info->toposchema_name, TOPO_TABLE_ISLE);
-        Vect__execute_pg(pg_info->conn, stmt);
-    }
-
-    return 0;
+    return ret;
 #else
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
     return -1;

Modified: grass/trunk/lib/vector/Vlib/pg_local_proto.h
===================================================================
--- grass/trunk/lib/vector/Vlib/pg_local_proto.h	2013-11-29 08:27:30 UTC (rev 58324)
+++ grass/trunk/lib/vector/Vlib/pg_local_proto.h	2013-11-29 09:43:40 UTC (rev 58325)
@@ -61,6 +61,9 @@
 /* area_pg.c */
 int Vect__get_area_points_pg(const struct Map_info *, const plus_t *, int, struct line_pnts *);
 
+/* build_pg.c */
+int Vect__clean_grass_db_topo(struct Format_info_pg *);
+
 /* read_pg.c */
 SF_FeatureType Vect__cache_feature_pg(const char *, int, int,
                                       struct Format_info_cache *,



More information about the grass-commit mailing list