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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Mar 17 11:27:57 EDT 2012


Author: martinl
Date: 2012-03-17 08:27:57 -0700 (Sat, 17 Mar 2012)
New Revision: 51092

Modified:
   grass/trunk/include/vect/dig_structs.h
   grass/trunk/lib/vector/Vlib/open_ogr.c
   grass/trunk/lib/vector/Vlib/read_ogr.c
   grass/trunk/lib/vector/Vlib/read_pg.c
   grass/trunk/lib/vector/Vlib/read_sfa.c
Log:
vlib(pg): improve reading PostGIS features
	  eliminate feature_cache_id (use cache.fid)


Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h	2012-03-17 15:26:58 UTC (rev 51091)
+++ grass/trunk/include/vect/dig_structs.h	2012-03-17 15:27:57 UTC (rev 51092)
@@ -562,12 +562,8 @@
 #else
     void *feature_cache;
 #endif
+  
     /*!
-      \brief Feature id read in feature_cache (level 2)
-    */
-    int feature_cache_id;
-
-    /*!
       \brief Offset list used for building pseudo-topology
     */
     struct Format_info_offset offset;

Modified: grass/trunk/lib/vector/Vlib/open_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_ogr.c	2012-03-17 15:26:58 UTC (rev 51091)
+++ grass/trunk/lib/vector/Vlib/open_ogr.c	2012-03-17 15:27:57 UTC (rev 51092)
@@ -119,7 +119,7 @@
 	break;
     }
     
-    ogr_info->feature_cache_id = -1;	/* FID >= 0 */
+    ogr_info->cache.fid = -1;	/* FID >= 0 */
     
     return 0;
 #else

Modified: grass/trunk/lib/vector/Vlib/read_ogr.c
===================================================================
--- grass/trunk/lib/vector/Vlib/read_ogr.c	2012-03-17 15:26:58 UTC (rev 51091)
+++ grass/trunk/lib/vector/Vlib/read_ogr.c	2012-03-17 15:27:57 UTC (rev 51092)
@@ -204,7 +204,7 @@
     /* coordinates */
     if (line_p != NULL) {
 	/* read feature to cache if necessary */
-	if (ogr_info->feature_cache_id != fid) {
+	if (ogr_info->cache.fid != fid) {
 	    G_debug(4, "Read feature (fid = %ld) to cache", fid);
 	    if (ogr_info->feature_cache) {
 		OGR_F_Destroy(ogr_info->feature_cache);
@@ -216,7 +216,7 @@
 			  fid);
 		return -1;
 	    }
-	    ogr_info->feature_cache_id = fid;
+	    ogr_info->cache.fid = fid;
 	}
 	
 	hGeom = OGR_F_GetGeometryRef(ogr_info->feature_cache);
@@ -385,8 +385,8 @@
 	    }
 
 	    /* cache OGR feature */
-	    ogr_info->feature_cache_id = (int)OGR_F_GetFID(hFeature);
-	    if (ogr_info->feature_cache_id == OGRNullFID) {
+	    ogr_info->cache.fid = (int)OGR_F_GetFID(hFeature);
+	    if (ogr_info->cache.fid == OGRNullFID) {
 		G_warning(_("OGR feature without ID"));
 	    }
 
@@ -429,8 +429,8 @@
 	    Vect_append_points(line_p,
 			       ogr_info->cache.lines[ogr_info->cache.lines_next], GV_FORWARD);
 
-	if (line_c != NULL && ogr_info->feature_cache_id != OGRNullFID)
-	    Vect_cat_set(line_c, 1, ogr_info->feature_cache_id);
+	if (line_c != NULL && ogr_info->cache.fid != OGRNullFID)
+	    Vect_cat_set(line_c, 1, ogr_info->cache.fid);
 
 	ogr_info->cache.lines_next++;
 	G_debug(4, "next line read, type = %d", itype);

Modified: grass/trunk/lib/vector/Vlib/read_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/read_pg.c	2012-03-17 15:26:58 UTC (rev 51091)
+++ grass/trunk/lib/vector/Vlib/read_pg.c	2012-03-17 15:27:57 UTC (rev 51092)
@@ -172,6 +172,9 @@
 	else {
 	    /* ignore constraints, Map->next_line incremented */
 	    ret = read_next_line_pg(Map, line_p, line_c, TRUE);
+	    if (ret != Line->type)
+		G_fatal_error(_("Unexpected feature type (%s) - should be (%d)"),
+			      ret, Line->type);
 	}
 
 	if (Map->constraint.region_flag) {
@@ -215,13 +218,13 @@
 {
 #ifdef HAVE_POSTGRES
     long fid;
-    int i, ipart, type;
-    SF_FeatureType sf_type;
+    int  ipart, type;
+    static SF_FeatureType sf_type;
     
-    struct line_pnts      *line_i;
-    struct Format_info_pg *pg_info;
+    struct Format_info_pg     *pg_info;
     
     pg_info = &(Map->fInfo.pg);
+    
     G_debug(3, "V1_read_line_pg(): offset = %lu offset_num = %lu",
 	    (long) offset, (long) pg_info->offset.array_num);
     
@@ -249,7 +252,7 @@
 	if ((int) sf_type < 0) /* -1 || - 2 */
 	    return (int) sf_type;
     }
-
+    
     /* get data from cache */
     if (sf_type == SF_POINT || sf_type == SF_LINESTRING)
 	ipart = 0;
@@ -259,20 +262,13 @@
     G_debug(4, "read feature part: %d -> type = %d",
 	    ipart, type);    
 	
-    if (line_p != NULL) {
-	/* coordinates */
-	line_i = pg_info->cache.lines[ipart];
-	for (i = 0; i < line_i->n_points; i++) {
-	    Vect_append_point(line_p,
-			      line_i->x[i], line_i->y[i], line_i->z[i]);
-	}
-    }
+    if (line_p)
+	Vect_append_points(line_p,
+			   pg_info->cache.lines[ipart], GV_FORWARD);
     
-    if (line_c != NULL) {
-	/* category */
+    if (line_c)
 	Vect_cat_set(line_c, 1, (int) fid);
-    }
-
+    
     return type;
 #else
     G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
@@ -299,20 +295,20 @@
 		      struct line_pnts *line_p, struct line_cats *line_c,
 		      int ignore_constraints)
 {
-    int i, ltype;
+    int line, itype;
     SF_FeatureType sf_type;
     
-    struct Format_info_pg *pg_info;
+    struct Format_info_pg    *pg_info;
     struct bound_box mbox, lbox;
-    struct line_pnts *line_i;
+    struct line_pnts *iline;
 
     pg_info = &(Map->fInfo.pg);
-
+    
     if (Map->constraint.region_flag && !ignore_constraints)
 	Vect_get_constraint_box(Map, &mbox);
     
     while (TRUE) {
-	Map->next_line++; /* level 2 only */
+	line = Map->next_line++; /* level 2 only */
 	
 	/* reset data structures */
 	if (line_p != NULL)
@@ -322,13 +318,12 @@
 
 	/* read feature to cache if necessary */
 	while (pg_info->cache.lines_next == pg_info->cache.lines_num) {
-	    pg_info->cache.lines_next = pg_info->cache.lines_num = 0;
 	    /* cache feature -> line_p & line_c */
 	    sf_type = get_feature(pg_info, -1);
 	    
 	    if (sf_type == SF_NONE) {
 		G_warning(_("Feature %d without geometry skipped"),
-			  Map->next_line);
+			  line);
 		return -1;
 	    }
 
@@ -342,33 +337,28 @@
 	    }
 	    
 	    G_debug(4, "%d lines read to cache", pg_info->cache.lines_num);
+	    
+	    /* next to be read from cache */
+	    pg_info->cache.lines_next = 0;	
 	}
 	
 	/* get data from cache */
-	ltype  = pg_info->cache.lines_types[pg_info->cache.lines_next];
-	if (line_p) {
-	    line_i = pg_info->cache.lines[pg_info->cache.lines_next];
-	    for (i = 0; i < line_i->n_points; i++) {
-		Vect_append_point(line_p,
-				  line_i->x[i], line_i->y[i], line_i->z[i]);
-	    }
-	}
-	if (line_c) {
-	    Vect_cat_set(line_c, 1, (int) pg_info->cache.fid);
-	}
-	pg_info->cache.lines_next++;
-	
+	G_debug(4, "read next cached line %d", pg_info->cache.lines_next);
+
+	itype = pg_info->cache.lines_types[pg_info->cache.lines_next];
+	iline = pg_info->cache.lines[pg_info->cache.lines_next];
+
 	/* apply constraints */
 	if (Map->constraint.type_flag && !ignore_constraints) {
 	    /* skip feature by type */
-	    if (!(ltype & Map->constraint.type))
+	    if (!(itype & Map->constraint.type))
 		continue;
 	}
 
 	if (line_p && Map->constraint.region_flag &&
 	    !ignore_constraints) {
 	    /* skip feature by region */
-	    Vect_line_box(line_p, &lbox);
+	    Vect_line_box(iline, &lbox);
 	    
 	    if (!Vect_box_overlap(&lbox, &mbox))
 		continue;
@@ -376,7 +366,16 @@
 	
 	/* skip feature by field ignored */
 	
-	return ltype;
+	if (line_p)
+	    Vect_append_points(line_p, iline, GV_FORWARD);
+	
+	if (line_c)
+	    Vect_cat_set(line_c, 1, (int) pg_info->cache.fid);
+	
+	pg_info->cache.lines_next++;
+	G_debug(4, "next line read, type = %d", itype);
+	
+	return itype;
     }
 
     return -1; /* not reached */
@@ -571,6 +570,12 @@
     unsigned char *wkb_data;
     unsigned int wkb_flags;
     SF_FeatureType ftype;
+
+    /* reset cache */
+    cache->lines_num = 0;
+    cache->fid       = -1;
+    if (fparts)
+	fparts->n_parts = 0;
     
     wkb_flags = 0;
     wkb_data  = hex_to_wkb(data, &nbytes);
@@ -642,9 +647,6 @@
     if (!cache->lines) {
 	reallocate_cache(cache, 1);
     }
-    cache->lines_num = 0;
-    if (fparts)
-	fparts->n_parts = 0;
     
     ret = -1;
     if (ftype == SF_POINT) {
@@ -855,6 +857,9 @@
     /* get the rings */
     nsize = 9;
     for (i = 0; i < nrings; i++ ) {
+	if (cache->lines_next >= cache->lines_num)
+	    G_fatal_error(_("Invalid cache index %d (max: %d)"),
+			  cache->lines_next, cache->lines_num);
 	line_i = cache->lines[cache->lines_next];
 	cache->lines_types[cache->lines_next++] = GV_BOUNDARY;
 	

Modified: grass/trunk/lib/vector/Vlib/read_sfa.c
===================================================================
--- grass/trunk/lib/vector/Vlib/read_sfa.c	2012-03-17 15:26:58 UTC (rev 51091)
+++ grass/trunk/lib/vector/Vlib/read_sfa.c	2012-03-17 15:27:57 UTC (rev 51092)
@@ -39,6 +39,7 @@
 		     struct line_cats *line_c, int line)
 {
 #if defined HAVE_OGR || defined HAVE_POSTGRES
+    int type;
     struct P_line *Line;
     
     G_debug(4, "V2_read_line_sfa() line = %d", line);
@@ -101,9 +102,15 @@
 	return Line->type;
     
     if (Map->format == GV_FORMAT_POSTGIS)
-	return V1_read_line_pg(Map, line_p, line_c, Line->offset);
-    
-    return V1_read_line_ogr(Map, line_p, line_c, Line->offset);
+	type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
+    else
+	type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
+
+    if (type != Line->type)
+	G_fatal_error(_("Unexpected feature type (%s) - should be (%d)"),
+		      type, Line->type);
+
+    return type;
 #else
     G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
     return -1;



More information about the grass-commit mailing list