浏览代码

vlib/pg: fix clean-up GRASS DB topology tables in full mode

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@58325 15284696-431f-4ddb-bdfa-cd5b030d7da7
Martin Landa 11 年之前
父节点
当前提交
025c13437b
共有 3 个文件被更改,包括 68 次插入29 次删除
  1. 61 7
      lib/vector/Vlib/build_pg.c
  2. 4 22
      lib/vector/Vlib/open_pg.c
  3. 3 0
      lib/vector/Vlib/pg_local_proto.h

+ 61 - 7
lib/vector/Vlib/build_pg.c

@@ -132,7 +132,7 @@ int Vect_build_pg(struct Map_info *Map, int build)
 */
 */
 int build_topo(struct Map_info *Map, int build)
 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 area, nareas, isle, nisles;
     int face[2];
     int face[2];
     char stmt[DB_SQL_MAX];
     char stmt[DB_SQL_MAX];
@@ -162,7 +162,10 @@ int build_topo(struct Map_info *Map, int build)
     /* cache features to speed-up random access (when attaching isles
     /* cache features to speed-up random access (when attaching isles
        to areas) */
        to areas) */
     if (build >= GV_BUILD_BASE) {
     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 &&
         if (Map->mode == GV_MODE_RW &&
             pg_info->cache.lines_num > 0) {
             pg_info->cache.lines_num > 0) {
 
 
@@ -178,16 +181,29 @@ int build_topo(struct Map_info *Map, int build)
 
 
             /* reset cache for reading features */
             /* reset cache for reading features */
             Vect__free_cache(&(pg_info->cache));
             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 */
     /* update TopoGeometry based on GRASS-like topology */
     Vect_build_nat(Map, build);
     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 */
     /* store map boundig box in DB */
     save_map_bbox(pg_info, &(plus->box));
     save_map_bbox(pg_info, &(plus->box));
     
     
@@ -888,4 +904,42 @@ void build_stmt_id(const void *array, int nitems, int is_int, const struct Plus_
     *stmt_size = stmt_id_size;
     *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
 #endif

+ 4 - 22
lib/vector/Vlib/open_pg.c

@@ -322,7 +322,7 @@ int V1_open_new_pg(struct Map_info *Map, const char *name, int with_z)
   
   
   \param[in,out] Map pointer to Map_info structure
   \param[in,out] Map pointer to Map_info structure
   \param head_only TRUE to read only header
   \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 0 on success
   \return 1 topology layer does not exist
   \return 1 topology layer does not exist
@@ -352,28 +352,10 @@ int Vect__open_topo_pg(struct Map_info *Map, int head_only, int update)
     
     
     ret = Vect__load_plus_pg(Map, head_only);
     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];
-
-        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);
+    if (update)
+        Vect__clean_grass_db_topo(pg_info);
 
 
-        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
 #else
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
     return -1;
     return -1;

+ 3 - 0
lib/vector/Vlib/pg_local_proto.h

@@ -61,6 +61,9 @@ struct feat_parts
 /* area_pg.c */
 /* area_pg.c */
 int Vect__get_area_points_pg(const struct Map_info *, const plus_t *, int, struct line_pnts *);
 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 */
 /* read_pg.c */
 SF_FeatureType Vect__cache_feature_pg(const char *, int, int,
 SF_FeatureType Vect__cache_feature_pg(const char *, int, int,
                                       struct Format_info_cache *,
                                       struct Format_info_cache *,