[GRASS-SVN] r64673 - grass/trunk/vector/v.to.db

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Feb 17 14:46:26 PST 2015


Author: mmetz
Date: 2015-02-17 14:46:26 -0800 (Tue, 17 Feb 2015)
New Revision: 64673

Modified:
   grass/trunk/vector/v.to.db/find.c
   grass/trunk/vector/v.to.db/global.h
   grass/trunk/vector/v.to.db/lines.c
   grass/trunk/vector/v.to.db/main.c
   grass/trunk/vector/v.to.db/report.c
Log:
v.to.db: faster

Modified: grass/trunk/vector/v.to.db/find.c
===================================================================
--- grass/trunk/vector/v.to.db/find.c	2015-02-17 22:45:00 UTC (rev 64672)
+++ grass/trunk/vector/v.to.db/find.c	2015-02-17 22:46:26 UTC (rev 64673)
@@ -6,7 +6,8 @@
 static int bsearch_cat(int cat)
 {
     int mid, lo, hi;
-    
+
+    /* tests */
     if (vstat.rcat < 1)
 	return -1;
 
@@ -22,6 +23,7 @@
     if (Values[lo].cat == cat)
 	return lo;
 
+    /* bsearch */
     while (lo < hi) {
 	mid = (lo + hi) / 2;
 	
@@ -39,36 +41,15 @@
     return -1;
 }
 
-/* returns index to array of values, inserts new if necessary */
-int find_cat(int cat, int add)
+/* returns index to array of values, mark as used if requested */
+int find_cat(int cat, int used)
 {
     int i;
 
-    if ((i = bsearch_cat(cat)) >= 0)
-	    return i;
+    i = bsearch_cat(cat);
 
-    if (!add)
-	return -1;
+    if (i >= 0 && used)
+	Values[i].used = 1;
 
-    /* Not found -> add new */
-    for (i = vstat.rcat; i > 0; i--) {
-	if (Values[i - 1].cat < cat)
-	    break;
-
-	Values[i] = Values[i - 1];
-    }
-    Values[i].cat = cat;
-    Values[i].count1 = 0;
-    Values[i].count1 = 0;
-    Values[i].i1 = -1;
-    Values[i].i2 = -1;
-    Values[i].d1 = 0.0;
-    Values[i].d2 = 0.0;
-    Values[i].qcat = NULL;
-    Values[i].nqcats = 0;
-    Values[i].aqcats = 0;
-
-    vstat.rcat++;
-
     return (i);
 }

Modified: grass/trunk/vector/v.to.db/global.h
===================================================================
--- grass/trunk/vector/v.to.db/global.h	2015-02-17 22:45:00 UTC (rev 64672)
+++ grass/trunk/vector/v.to.db/global.h	2015-02-17 22:46:26 UTC (rev 64673)
@@ -4,6 +4,7 @@
 struct value
 {
     int cat;			/* category */
+    char used;			/* update/report if used else not */
     int count1, count2;		/* Count of found values; i1: count, coor, sides; i2: sides */
     /* for sides set to 2, if more than 1 area category was found, */
     /* including no category (cat = -1)! */

Modified: grass/trunk/vector/v.to.db/lines.c
===================================================================
--- grass/trunk/vector/v.to.db/lines.c	2015-02-17 22:45:00 UTC (rev 64672)
+++ grass/trunk/vector/v.to.db/lines.c	2015-02-17 22:46:26 UTC (rev 64673)
@@ -76,6 +76,7 @@
     
     /* Cycle through all lines */
     nlines = Vect_get_num_lines(Map);
+    G_percent(0, nlines, 2);
     for (line_num = 1; line_num <= nlines; line_num++) {
 	type = Vect_read_line(Map, Points, Cats, line_num);
 	if (!(type & options.type))

Modified: grass/trunk/vector/v.to.db/main.c
===================================================================
--- grass/trunk/vector/v.to.db/main.c	2015-02-17 22:45:00 UTC (rev 64672)
+++ grass/trunk/vector/v.to.db/main.c	2015-02-17 22:46:26 UTC (rev 64673)
@@ -25,7 +25,7 @@
 
 int main(int argc, char *argv[])
 {
-    int n;
+    int n, i, j, cat, lastcat, type, id, findex;
     struct Map_info Map;
     struct GModule *module;
     struct field_info *Fi;
@@ -69,18 +69,63 @@
 
     /* allocate array for values */
     /* (+ 1 is for cat -1 (no category) reported at the end ) */
+    findex = Vect_cidx_get_field_index(&Map, options.field);
     if (Vect_cidx_get_field_index(&Map, options.field) > -1) {
-	n = Vect_cidx_get_num_unique_cats_by_index(&Map,
-						   Vect_cidx_get_field_index
-						   (&Map, options.field));
+	n = Vect_cidx_get_num_unique_cats_by_index(&Map, findex);
     }
     else {
 	n = 0;
     }
     G_debug(2, "%d unique cats", n);
     Values = (struct value *) G_calloc(n + 1, sizeof(struct value));
-    vstat.rcat = 0;
 
+    /* prepopulate Values */
+    n = Vect_cidx_get_num_cats_by_index(&Map, findex);
+    i = 0;
+    Values[i].cat = -1;		/* features without category */
+    Values[i].used = 0;
+    Values[i].count1 = 0;
+    Values[i].count1 = 0;
+    Values[i].i1 = -1;
+    Values[i].i2 = -1;
+    Values[i].d1 = 0.0;
+    Values[i].d2 = 0.0;
+    Values[i].qcat = NULL;
+    Values[i].nqcats = 0;
+    Values[i].aqcats = 0;
+
+    i = 1;
+    lastcat = -1;
+    /* category index must be sorted,
+     * i.e. topology must have been built with GV_BUILD_ALL */
+    for (j = 0; j < n; j++) {
+	Vect_cidx_get_cat_by_index(&Map, findex, j, &cat, &type, &id);
+	if (lastcat > cat) {
+	    Vect_close(&Map);
+	    G_fatal_error(_("Category index for vector map <%s> is not sorted"),
+	                  options.name);
+	}
+
+	if (lastcat != cat) {
+	    Values[i].cat = cat;
+	    Values[i].used = 0;
+	    Values[i].count1 = 0;
+	    Values[i].count1 = 0;
+	    Values[i].i1 = -1;
+	    Values[i].i2 = -1;
+	    Values[i].d1 = 0.0;
+	    Values[i].d2 = 0.0;
+	    Values[i].qcat = NULL;
+	    Values[i].nqcats = 0;
+	    Values[i].aqcats = 0;
+
+	    lastcat = cat;
+	    i++;
+	}
+    }
+
+    vstat.rcat = i;
+
     /* Read values from map */
     if (options.option == O_QUERY) {
 	query(&Map);
@@ -93,6 +138,17 @@
 	read_lines(&Map);
     }
 
+    /* prune unused values */
+    n = vstat.rcat;
+    j = 0;
+    for (i = 0; i < n; i++) {
+	if (Values[i].used) {
+	    Values[j] = Values[i];
+	    j++;
+	}
+    }
+    vstat.rcat = j;
+
     conv_units();
 
     if (options.print || options.total) {

Modified: grass/trunk/vector/v.to.db/report.c
===================================================================
--- grass/trunk/vector/v.to.db/report.c	2015-02-17 22:45:00 UTC (rev 64672)
+++ grass/trunk/vector/v.to.db/report.c	2015-02-17 22:46:26 UTC (rev 64673)
@@ -186,7 +186,7 @@
 {
     if (vstat.rcat > 0) {
 	int rcat_report;
-	if(find_cat(-1, 0) != -1)
+	if (find_cat(-1, 0) != -1)
 	    rcat_report = vstat.rcat - 1;
 	else
 	    rcat_report = vstat.rcat;



More information about the grass-commit mailing list