[GRASS-SVN] r53759 - grass/trunk/vector/v.hull

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Nov 9 13:18:11 PST 2012


Author: mmetz
Date: 2012-11-09 13:18:10 -0800 (Fri, 09 Nov 2012)
New Revision: 53759

Modified:
   grass/trunk/vector/v.hull/Makefile
   grass/trunk/vector/v.hull/hull.h
   grass/trunk/vector/v.hull/main.c
   grass/trunk/vector/v.hull/read.c
Log:
v.hull: remove -a flag, add -r flag and cats and where options

Modified: grass/trunk/vector/v.hull/Makefile
===================================================================
--- grass/trunk/vector/v.hull/Makefile	2012-11-09 18:51:23 UTC (rev 53758)
+++ grass/trunk/vector/v.hull/Makefile	2012-11-09 21:18:10 UTC (rev 53759)
@@ -2,8 +2,8 @@
 
 PGM = v.hull
 
-LIBES = $(VECTORLIB) $(GISLIB)
-DEPENDENCIES = $(VECTORDEP) $(GISDEP)
+LIBES = $(VECTORLIB) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTORDEP) $(DBMIDEP) $(GISDEP)
 EXTRA_INC = $(VECT_INC)
 EXTRA_CFLAGS = $(VECT_CFLAGS)
 

Modified: grass/trunk/vector/v.hull/hull.h
===================================================================
--- grass/trunk/vector/v.hull/hull.h	2012-11-09 18:51:23 UTC (rev 53758)
+++ grass/trunk/vector/v.hull/hull.h	2012-11-09 21:18:10 UTC (rev 53759)
@@ -9,8 +9,11 @@
 
 #define ALLOC_CHUNK 256
 
+struct cat_list *parse_filter_options(struct Map_info *, int , char *,
+                                    char *);
+
 int loadSiteCoordinates(struct Map_info *, struct Point **, int,
-			struct Cell_head *, int);
+			struct Cell_head *, int, struct cat_list *);
   
 int convexHull(struct Point *, int, int **);
 

Modified: grass/trunk/vector/v.hull/main.c
===================================================================
--- grass/trunk/vector/v.hull/main.c	2012-11-09 18:51:23 UTC (rev 53758)
+++ grass/trunk/vector/v.hull/main.c	2012-11-09 21:18:10 UTC (rev 53759)
@@ -31,9 +31,11 @@
 int main(int argc, char **argv)
 {
     struct GModule *module;
-    struct Option *input, *output, *field;
-    struct Flag *all, *flat;
+    struct Option *input, *output, *field, *cats_opt, *where_opt;
+    struct Flag *region_flag, *flat;
     struct Cell_head window;
+    int layer;
+    struct cat_list *cat_list = NULL;
 
     char *sitefile;
 
@@ -58,11 +60,14 @@
 
     output = G_define_standard_option(G_OPT_V_OUTPUT);
     
-    all = G_define_flag();
-    all->key = 'a';
-    all->description =
-	_("Use all vector points (do not limit to current region)");
+    cats_opt = G_define_standard_option(G_OPT_V_CATS);
+    
+    where_opt = G_define_standard_option(G_OPT_DB_WHERE);
 
+    region_flag = G_define_flag();
+    region_flag->key = 'r';
+    region_flag->description = _("Limit to current region");
+
     flat = G_define_flag();
     flat->key = 'f';
     flat->description =
@@ -80,17 +85,21 @@
     if (Vect_open_old2(&Map, sitefile, "", field->answer) < 0)
 	G_fatal_error(_("Unable to open vector map <%s>"), sitefile);
     
+    layer = Vect_get_field_number(&Map, field->answer);
+    cat_list = parse_filter_options(&Map, layer, where_opt->answer,
+                                  cats_opt->answer);
+    
     /* load site coordinates */
     G_get_window(&window);
-    numSitePoints = loadSiteCoordinates(&Map, &points, all->answer, &window,
-					Vect_get_field_number(&Map, field->answer));
+    numSitePoints = loadSiteCoordinates(&Map, &points, region_flag->answer, &window,
+					layer, cat_list);
     if (numSitePoints < 0)
 	G_fatal_error(_("Error loading vector points from <%s>"), sitefile);
     
     if (numSitePoints < 3)
 	G_fatal_error(_("Convex hull calculation requires at least three points (%d found)"), numSitePoints);
     
-    G_verbose_message(_("%d points read from vector map <%s>"), sitefile);
+    G_verbose_message(_("%d points read from vector map <%s>"), numSitePoints, sitefile);
     
     /* create a 2D or a 3D hull? */
     MODE2D = 1;
@@ -101,7 +110,10 @@
 	MODE2D = 1;
     }
 
-    /* create vector map */
+    /* close input vector */
+    Vect_close(&Map);
+
+    /* create output vector map */
     if (0 > Vect_open_new(&Map, output->answer, MODE2D ? WITHOUT_Z : WITH_Z)) {
 	G_fatal_error(_("Unable to create vector map <%s>"), output->answer);
     }

Modified: grass/trunk/vector/v.hull/read.c
===================================================================
--- grass/trunk/vector/v.hull/read.c	2012-11-09 18:51:23 UTC (rev 53758)
+++ grass/trunk/vector/v.hull/read.c	2012-11-09 21:18:10 UTC (rev 53759)
@@ -1,9 +1,11 @@
 #include <grass/gis.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
 
 #include "hull.h"
 
-int loadSiteCoordinates(struct Map_info *Map, struct Point **points, int all,
-			struct Cell_head *window, int field)
+int loadSiteCoordinates(struct Map_info *Map, struct Point **points, int region,
+			struct Cell_head *window, int field, struct cat_list *cat_list)
 {
     int i, pointIdx;
     struct line_pnts *sites;
@@ -25,14 +27,28 @@
 	if (type != GV_POINT && !(type & GV_LINES))
 	    continue;
 
-	if (field != -1 && Vect_cat_get(cats, field, &cat) == 0)
+	if (cat_list) {
+	    int found = 0;
+
+	    for (i = 0; i < cats->n_cats; i++) {
+		if (cats->field[i] == field &&
+		    Vect_cat_in_cat_list(cats->cat[i], cat_list)) {
+		    
+		    found = 1;
+		    break;
+		}
+	    }
+	    if (!found)
+		continue;
+	}
+	else if (field > 0 && Vect_cat_get(cats, field, &cat) == 0)
 	    continue;
 	
 	for (i = 0; i < sites->n_points; i++) {
 	    G_debug(4, "Point: %f|%f|%f|#%d", sites->x[i], sites->y[i],
 		    sites->z[i], cat);
 	    
-	    if (!all && !Vect_point_in_box(sites->x[i], sites->y[i], sites->z[i], &box))
+	    if (region && !Vect_point_in_box(sites->x[i], sites->y[i], sites->z[i], &box))
 		continue;
 	    
 	    G_debug(4, "Point in the box");
@@ -54,3 +70,94 @@
     
     return pointIdx;
 }
+
+static int cmp_int(const void *a, const void *b)
+{
+    int ai = *(int *)a;
+    int bi = *(int *)b;
+    
+    return (ai < bi ? -1 : (ai > bi));
+}
+
+/* parse filter options */
+/* return cat list or NULL */
+struct cat_list *parse_filter_options(struct Map_info *Map, int layer,
+                      char *where, char *cats)
+{
+    struct cat_list *list = NULL;
+
+    if (where) {
+	struct field_info *Fi = NULL;
+	dbDriver *driver = NULL;
+	int ncats, *cats = NULL;
+	int i, j;
+	
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
+	if (cats)
+	    G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
+
+	Fi = Vect_get_field(Map, layer);
+	if (!Fi) {
+	    G_fatal_error(_("Database connection not defined for layer %d"),
+			  layer);
+	}
+
+	G_verbose_message(_("Loading categories from table <%s>..."), Fi->table);
+
+	driver = db_start_driver_open_database(Fi->driver, Fi->database);
+	if (driver == NULL)
+	    G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
+			  Fi->database, Fi->driver);
+	
+	ncats = db_select_int(driver, Fi->table, Fi->key, where,
+			      &cats);
+	if (ncats == -1)
+		G_fatal_error(_("Unable select records from table <%s>"),
+			      Fi->table);
+	G_verbose_message(_("%d categories loaded"), ncats);
+	    
+	db_close_database_shutdown_driver(driver);
+
+	/* sort */
+	qsort(cats, ncats, sizeof(int), cmp_int);
+	
+	/* remove duplicates */
+	j = 1;
+	for (i = 1; i < ncats; i++) {
+	    if (cats[i] != cats[j - 1]) {
+		cats[j] = cats[i];
+		j++;
+	    }
+	}
+	ncats = j;
+	
+	/* convert to cat list */
+	list = Vect_new_cat_list();
+	
+	Vect_array_to_cat_list(cats, ncats, list);
+	
+	if (cats)
+	    G_free(cats);
+
+
+    }
+    else if (cats) {
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", GV_KEY_COLUMN);
+	list = Vect_new_cat_list();
+
+	if (Vect_str_to_cat_list(cats, list) > 0) {
+	    G_warning(_("Problem loading category values"));
+	}
+    }
+    
+    if (list) {
+	if (list->n_ranges < 1) {
+	    Vect_destroy_cat_list(list);
+	    list = NULL;
+	}
+    }
+	
+    return list;
+}



More information about the grass-commit mailing list