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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 25 02:06:38 PST 2013


Author: martinl
Date: 2013-11-25 02:06:37 -0800 (Mon, 25 Nov 2013)
New Revision: 58299

Modified:
   grass/trunk/lib/vector/Vlib/build_pg.c
   grass/trunk/lib/vector/Vlib/open_pg.c
   grass/trunk/lib/vector/Vlib/read_pg.c
Log:
vlib/pg: fix sequatial access when features cached
         update category index


Modified: grass/trunk/lib/vector/Vlib/build_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_pg.c	2013-11-25 10:05:31 UTC (rev 58298)
+++ grass/trunk/lib/vector/Vlib/build_pg.c	2013-11-25 10:06:37 UTC (rev 58299)
@@ -20,6 +20,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 #ifdef HAVE_POSTGRES
 #include "pg_local_proto.h"
 
@@ -156,6 +158,10 @@
     if (build < GV_BUILD_BASE)
         return 1; /* nothing to print */
     
+    /* cache features to speed-up random access (when attaching isles
+       to areas) */
+    if (build >= GV_BUILD_BASE)
+        pg_info->cache.ctype = CACHE_MAP;
     /* update TopoGeometry based on GRASS-like topology */
     Vect_build_nat(Map, build);
     
@@ -547,6 +553,10 @@
     const struct Format_info_offset *offset;
 
     offset = &(pg_info->offset);
+    
+    if (offset->array_num < 1) /* nothing to write */
+        return 0;
+    
     if (plus->n_nodes != offset->array_num) {
         G_warning(_("Unable to write nodes, offset array mismatch"));
         return -1;

Modified: grass/trunk/lib/vector/Vlib/open_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_pg.c	2013-11-25 10:05:31 UTC (rev 58298)
+++ grass/trunk/lib/vector/Vlib/open_pg.c	2013-11-25 10:06:37 UTC (rev 58299)
@@ -346,8 +346,9 @@
     
     /* free and init plus structure */
     dig_init_plus(plus);
-    plus->Spidx_new = TRUE;
-    
+    plus->Spidx_new = TRUE;   /* force to create new spatial index */
+    plus->update_cidx = TRUE; /* force to update category index */
+
     return Vect__load_plus_pg(Map, head_only);
 #else
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
@@ -779,7 +780,8 @@
         node->z = 0.0;
     
     /* update spatial index */
-    dig_spidx_add_node(plus, n, node->x, node->y, node->z);
+    if (plus->Spidx_new)
+        dig_spidx_add_node(plus, n, node->x, node->y, node->z);
     
     if (plus->uplist.do_uplist)
         /* collect updated nodes if requested */
@@ -812,12 +814,9 @@
                            const struct line_data *data,
                            struct Format_info_cache *cache)
 {
-    int tp;
+    int tp, cat;
     struct P_line *line;
     
-    struct line_pnts *points;
-    struct bound_box box;
-    
     if (data->start_node == 0 && data->end_node == 0) {
         if (data->left_face == 0)
             tp = GV_POINT;
@@ -880,12 +879,22 @@
     }
 
     Vect__cache_feature_pg(data->wkb_geom, FALSE, tp, cache, NULL);
-    cache->lines_cats[cache->lines_num-1] = data->fid > 0 ? data->fid : -1;
+    cat = cache->lines_cats[cache->lines_num-1] = data->fid > 0 ? data->fid : -1;
 
-    points = cache->lines[cache->lines_num-1];
-    dig_line_box(points, &box);
-    dig_spidx_add_line(plus, n, &box);
-
+    /* update spatial index */
+    if (plus->Spidx_new) {
+        struct line_pnts *points;
+        struct bound_box box;
+    
+        points = cache->lines[cache->lines_num-1];
+        dig_line_box(points, &box);
+        dig_spidx_add_line(plus, n, &box);
+    }
+    
+    /* update category index */
+    if (plus->update_cidx)
+        dig_cidx_add_cat(plus, cat > 0 ? 1 : 0, cat > 0 ? cat : 0, n, tp);
+    
     if (plus->uplist.do_uplist) {
         /* collect updated lines if requested */
         dig_line_add_updated(plus, n);
@@ -1202,6 +1211,7 @@
     struct P_line *Line;
     struct line_pnts *Points;
     struct ilist *List;
+    struct bound_box box;
     
     PGresult *res;
   
@@ -1357,7 +1367,6 @@
 
         id = plus->n_plines + i + 1; /* points already registered */
         read_p_line(plus, id, &line_data, &(pg_info->cache));
-        /* TODO: update category index */
     }
     PQclear(res);
 
@@ -1433,6 +1442,8 @@
         }
     }
     else {
+        int cat;
+        
         /* read areas from 'area_grass' table */
         sprintf(stmt,
                 "SELECT area_id,lines,centroid,isles FROM \"%s\".%s ORDER BY area_id",
@@ -1456,11 +1467,18 @@
             read_p_area(plus, i + 1, (char *)PQgetvalue(res, i, 1),
                         atoi(PQgetvalue(res, i, 2)), (char *)PQgetvalue(res, i, 3));
             
-            /* update spatial index -- needed ?
-            Vect_get_area_points(Map, i+1, Points);
-            dig_line_box(Points, &box);
-            dig_spidx_add_area(&(Map->plus), i+1, &box);
-            */
+            if (plus->Spidx_new) {
+                /* update spatial index */
+                Vect_get_area_points(Map, i+1, Points);
+                dig_line_box(Points, &box);
+                dig_spidx_add_area(&(Map->plus), i+1, &box);
+            }
+
+            if (plus->update_cidx) {
+                /* update category index */
+                cat = pg_info->cache.lines_cats[plus->Area[i+1]->centroid-1];
+                dig_cidx_add_cat(plus, cat > 0 ? 1 : 0, cat > 0 ? cat : 0, i+1, GV_AREA);
+            }
         }
         PQclear(res);
     }
@@ -1494,11 +1512,12 @@
             read_p_isle(plus, i + 1, (char *)PQgetvalue(res, i, 1),
                         atoi(PQgetvalue(res, i, 2)));
 
-            /* update spatial index -- needed ?
-            Vect_get_isle_points(Map, i+1, Points);
-            dig_line_box(Points, &box);
-            dig_spidx_add_isle(&(Map->plus), i+1, &box);
-            */
+            if (plus->Spidx_new) {
+                /* update spatial index */
+                Vect_get_isle_points(Map, i+1, Points);
+                dig_line_box(Points, &box);
+                dig_spidx_add_isle(&(Map->plus), i+1, &box);
+            }
         }
         PQclear(res);
     }

Modified: grass/trunk/lib/vector/Vlib/read_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/read_pg.c	2013-11-25 10:05:31 UTC (rev 58298)
+++ grass/trunk/lib/vector/Vlib/read_pg.c	2013-11-25 10:06:37 UTC (rev 58299)
@@ -362,6 +362,9 @@
     if (pg_info->cache.ctype == CACHE_MAP) {
         cache_idx = line - 1;
         
+        if (cache_idx >= pg_info->cache.lines_num)
+            G_fatal_error(_("Requesting invalid feature from cache (%d). Number of features in cache: %d"),
+                          cache_idx, pg_info->cache.lines_num);
         if (pg_info->cache.lines_types[cache_idx] != Line->type)
             G_warning(_("Feature %d: unexpected type (%d) - should be %d"), 
                       line, pg_info->cache.lines_types[cache_idx], Line->type);
@@ -455,8 +458,7 @@
             Vect_reset_cats(line_c);
 
         /* read feature to cache if necessary */
-        while (pg_info->cache.ctype != CACHE_MAP &&
-               pg_info->cache.lines_next == pg_info->cache.lines_num) {
+        while (pg_info->cache.lines_next == pg_info->cache.lines_num) {
             /* cache feature -> line_p & line_c */
             sf_type = get_feature(pg_info, -1, -1);
             
@@ -529,7 +531,7 @@
                 Vect_cat_set(line_c, 1, cat);
         }
 
-        pg_info->cache.lines_next++;
+        pg_info->cache.lines_next++; /* read next line from cache */
 
         return itype;
     }
@@ -749,11 +751,12 @@
     /* reset cache */
     if (cache->ctype == CACHE_MAP)
         cache->lines_num++;
-    else
+    else {
+        /* next to be read from cache */
+        cache->lines_next = 0;
         cache->lines_num = 1;
+    }
     cache->fid = -1;
-    /* next to be read from cache */
-    cache->lines_next = 0;
 
     if (fparts)
         fparts->n_parts = 0;
@@ -865,8 +868,10 @@
         G_warning(_("Unsupported feature type %d"), ftype);
     }
 
-    /* read next feature from cache */
-    cache->lines_next = 0;
+    if (cache->ctype != CACHE_MAP) {
+        /* read next feature from cache */
+        cache->lines_next = 0;
+    }
 
     /* G_free(wkb_data); */
 



More information about the grass-commit mailing list