[GRASS-SVN] r29787 - in grass/trunk/vector/v.edit: cmd lib

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jan 21 13:18:13 EST 2008


Author: martinl
Date: 2008-01-21 13:18:13 -0500 (Mon, 21 Jan 2008)
New Revision: 29787

Added:
   grass/trunk/vector/v.edit/lib/select.c
Modified:
   grass/trunk/vector/v.edit/cmd/select.c
   grass/trunk/vector/v.edit/lib/vedit.h
Log:
veditlib: select_by_query() moved from cmd to lib, used by wxGUI/vdigit

Modified: grass/trunk/vector/v.edit/cmd/select.c
===================================================================
--- grass/trunk/vector/v.edit/cmd/select.c	2008-01-21 17:28:21 UTC (rev 29786)
+++ grass/trunk/vector/v.edit/cmd/select.c	2008-01-21 18:18:13 UTC (rev 29787)
@@ -150,9 +150,33 @@
 
     /* selecy by query */
     if (params -> query -> answer != NULL) {
-	sel_by_query(Map,
-		     type, layer, thresh, params -> query -> answer,
-		     List);
+	int query_type;
+	struct ilist *List_tmp;
+	if (first_selection) {
+	    List_tmp = List;
+	    first_selection = 0;
+	}
+	else {
+	    List_tmp = Vect_new_list();
+	}
+
+	query_type = QUERY_UNKNOWN;
+	if (strcmp(params->query->answer, "length") == 0) {
+	    query_type = QUERY_LENGTH;
+	}
+	else if (strcmp(params->query->answer, "dangle") == 0) {
+	    query_type = QUERY_DANGLE;
+	}
+
+	Vedit_select_by_query(Map,
+			      type, layer, thresh, query_type,
+			      List_tmp);
+
+	/* merge lists (only duplicate items) */
+	if (List_tmp != List) {
+	    merge_lists (List, List_tmp);
+	    Vect_destroy_list (List_tmp);
+	}
     }
 
     if (params -> reverse -> answer) {
@@ -602,144 +626,3 @@
 
     return 1;
 }
-
-/**
-   \brief Select features by query (based on geometry)
-
-   \param[in] Map vector map
-   \param[in] type feature type
-   \param[in] layer layer number
-   \param[in] thresh threshold value (< 0 for 'shorter', > 0 for 'longer')
-   \param[in] query query (length, dangle, ...)
-   \param[in,out] List list of selected features
- 
-   \return number of selected lines
-*/
-int sel_by_query(struct Map_info *Map,
-		 int type, int layer, double thresh, const char *query,
-		 struct ilist* List)
-{
-    int num, line, ltype, cat;
-    double length;
-    struct ilist *List_tmp;
-    struct line_pnts *Points;
-    struct line_cats *Cats;
-
-    if (first_selection) {
-	List_tmp = List;
-	first_selection = 0;
-    }
-    else {
-	List_tmp = Vect_new_list();
-    }
-
-    Points = Vect_new_line_struct();
-    Cats   = Vect_new_cats_struct();
-
-    num = Vect_get_num_lines (Map);
-
-    for (line = 1; line <= num; line++) {
-	if (!Vect_line_alive(Map, line))
-	    continue;
-	
-	ltype = Vect_read_line(Map, Points, Cats, line);
-	Vect_cat_get(Cats, layer, &cat); /* get first category from layer */
-
-	if (!(ltype & type))
-	    continue;
-
-	if (strcmp(query, "length") == 0) {
-	    length = Vect_line_length(Points);
-	    if (thresh <= 0.0) { /* shorter then */
-		if (length <= fabs(thresh))
-		    Vect_list_append(List_tmp, line);
-	    }
-	    else { /* longer then */
-		if (length > thresh)
-		    Vect_list_append(List_tmp, line);
-	    }
-	}
-	else if (strcmp(query, "dangle") == 0) {
-	    if (!(type & GV_LINES))
-		continue;
-	    /* check if line is dangle */
-
-	    int i, cat_curr;
-	    int node1, node2, node;        /* nodes */
-	    int nnode1, nnode2;            /* number of line in node */
-	    double nx, ny, nz;             /* node coordinates */
-	    struct ilist *exclude, *found; /* line id of nearest lines */
-	    struct line_cats *Cats_curr;   
-
-	    Vect_get_line_nodes(Map, line, &node1, &node2);
-	    
-	    node = -1;
-	    nnode1 = Vect_get_node_n_lines(Map, node1);
-	    nnode2 = Vect_get_node_n_lines(Map, node2);
-
-	    if ((nnode1 == 4 && nnode2 == 1) ||
-		(nnode1 == 1 && nnode2 == 4)) {
-		if (nnode1 == 4)
-		    node = node1;
-		else
-		    node = node2;
-	    }
-
-	    /* no dangle ? */
-	    if (node == -1)
-		continue;
-
-	    length = Vect_line_length(Points);
-	    if (thresh <= 0.0) { /* shorter then */
-		if (length > fabs(thresh))
-		    continue;
-	    }
-	    else { /* longer then */
-		if (length <= thresh)
-		    continue;
-	    }
-	    
-	    /* at least one of the lines need to have same category number */
-	    exclude = Vect_new_list();
-	    found   = Vect_new_list();
-
-	    Vect_get_node_coor(Map, node, &nx, &ny, &nz);
-
-	    Vect_list_append(exclude, line);
-	    Vect_find_line_list(Map, nx, ny, nz,
-				GV_LINES, 0.0, WITHOUT_Z,
-				exclude, found);
-
-	    Cats_curr = Vect_new_cats_struct();
-
-	    for (i = 0; i < found->n_values; i++) {
-		Vect_read_line(Map, NULL, Cats_curr, found->value[i]);
-		if (Vect_cat_get(Cats_curr, layer, &cat_curr) > -1) {
-		    if (cat == cat_curr)
-			Vect_list_append(List_tmp, line);
-		}
-	    }
-
-	    Vect_destroy_cats_struct(Cats_curr);
-	    Vect_destroy_list(exclude);
-	    Vect_destroy_list(found);
-	}
-	else {
-	    /* this shouldn't happen */
-	    G_fatal_error (_("Unknown query tool '%s'"), query);
-	}
-    }
-
-    G_debug (1, "  %d lines selected (by query '%s')", List_tmp -> n_values, query);
-
-    /* merge lists (only duplicate items) */
-    if (List_tmp != List) {
-	merge_lists (List, List_tmp);
-	Vect_destroy_list (List_tmp);
-    }
-
-    Vect_destroy_line_struct(Points);
-    Vect_destroy_cats_struct(Cats);
-
-    return List -> n_values; 
-}

Added: grass/trunk/vector/v.edit/lib/select.c
===================================================================
--- grass/trunk/vector/v.edit/lib/select.c	                        (rev 0)
+++ grass/trunk/vector/v.edit/lib/select.c	2008-01-21 18:18:13 UTC (rev 29787)
@@ -0,0 +1,199 @@
+/**
+   \brief Vedit library - select vector features (by query)
+
+   This program is free software under the
+   GNU General Public License (>=v2).
+   Read the file COPYING that comes with GRASS
+   for details.
+
+   \author (C) 2007-2008 by the GRASS Development Team
+   Martin Landa <landa.martin gmail.com>
+
+   \date 2007-2008
+*/
+
+#include "vedit.h"
+
+static int select_by_query(struct Map_info *, int, int, int,
+			   int, double,
+			   struct line_pnts *, struct line_cats *);
+
+/**
+   \brief Select features by query (based on geometry)
+
+   Currently supported:
+    - QUERY_LENGTH, select all lines longer then threshold (or shorter if threshold is negative)
+    - QUERY_DANGLE, select all dangles then threshold (or shorter if threshold is negative)
+
+   If <em>List</em> is not empty query only those feature, otherwise query all
+   vector features stored in the vector map.
+
+   \todo Rewrite dangle part to use Vector library functionality
+
+   \param[in] Map vector map
+   \param[in] type feature type
+   \param[in] layer layer number
+   \param[in] thresh threshold value (< 0 for 'shorter', > 0 for 'longer')
+   \param[in] query query (length, dangle, ...)
+   \param[in,out] List list of selected features
+ 
+   \return number of selected lines
+*/
+int Vedit_select_by_query(struct Map_info *Map,
+			  int type, int layer, double thresh, int query,
+			  struct ilist* List)
+{
+    int num, line, i;
+    struct line_pnts *Points;
+    struct line_cats *Cats;
+    struct ilist *List_query;
+    
+    Points = Vect_new_line_struct();
+    Cats   = Vect_new_cats_struct();
+
+    List_query = Vect_new_list();
+
+    if (List->n_values > 0) { /* query only selected */
+	for (i = 0; i < List->n_values; i++) {
+	    line = List->value[i];
+	    if (select_by_query(Map, line, type, layer,
+				query, thresh,
+				Points, Cats) == 1) {
+		if (!Vect_val_in_list(List, line)) {
+		    Vect_list_append(List, line);
+		}
+	    }
+	    else {
+		if (Vect_val_in_list(List, line)) {
+		    Vect_list_delete(List, line);
+		    i--;
+		}
+	    }
+	}
+    }
+    else { /* global query */
+	num = Vect_get_num_lines (Map);
+	for (line = 1; line <= num; line++) {
+	    if (select_by_query(Map, line, type, layer,
+				query, thresh,
+				Points, Cats) == 1) {
+		Vect_list_append(List, line);
+	    }
+	}
+    }
+
+    G_debug (3, "Vedit_select_by_query(): %d lines selected (by query %d)", List -> n_values, query);
+
+    Vect_destroy_line_struct(Points);
+    Vect_destroy_cats_struct(Cats);
+    Vect_destroy_list(List_query);
+
+    return List -> n_values; 
+}
+
+/**
+   \brief Query given line
+
+   \return 1 line test positive 
+   \return 0 line test negative
+   \return -1 on error (line is dead)
+*/
+static int select_by_query(struct Map_info *Map, int line, int type, int layer,
+			   int query, double thresh,
+			   struct line_pnts* Points, struct line_cats* Cats) 
+{
+    int ltype, cat;
+    double length;
+
+    if (!Vect_line_alive(Map, line))
+	return -1;
+    
+    ltype = Vect_read_line(Map, Points, Cats, line);
+    Vect_cat_get(Cats, layer, &cat); /* get first category from layer */
+    
+    if (!(ltype & type))
+	return -1;
+    
+    if (query == QUERY_LENGTH) {
+	length = Vect_line_length(Points);
+	if (thresh <= 0.0) { /* shorter then */
+	    if (length <= fabs(thresh))
+		return 1;
+	}
+	else { /* longer then */
+	    if (length > thresh)
+		return 1;
+	}
+    }
+    else if (query == QUERY_DANGLE) {
+	if (!(type & GV_LINES))
+	    return -1;
+	/* check if line is dangle */
+	
+	int i, cat_curr;
+	int node1, node2, node;        /* nodes */
+	int nnode1, nnode2;            /* number of line in node */
+	double nx, ny, nz;             /* node coordinates */
+	struct ilist *exclude, *found; /* line id of nearest lines */
+	struct line_cats *Cats_curr;   
+	
+	Vect_get_line_nodes(Map, line, &node1, &node2);
+	
+	node = -1;
+	nnode1 = Vect_get_node_n_lines(Map, node1);
+	nnode2 = Vect_get_node_n_lines(Map, node2);
+	
+	if ((nnode1 == 4 && nnode2 == 1) ||
+	    (nnode1 == 1 && nnode2 == 4)) {
+	    if (nnode1 == 4)
+		node = node1;
+	    else
+		node = node2;
+	}
+	
+	/* no dangle ? */
+	if (node == -1)
+	    return -1;
+	
+	length = Vect_line_length(Points);
+	if (thresh <= 0.0) { /* shorter then */
+		if (length > fabs(thresh))
+		    return -1;
+	}
+	else { /* longer then */
+	    if (length <= thresh)
+		return -1;
+	}
+	
+	/* at least one of the lines need to have same category number */
+	exclude = Vect_new_list();
+	found   = Vect_new_list();
+	
+	Vect_get_node_coor(Map, node, &nx, &ny, &nz);
+	
+	Vect_list_append(exclude, line);
+	Vect_find_line_list(Map, nx, ny, nz,
+			    GV_LINES, 0.0, WITHOUT_Z,
+				exclude, found);
+	
+	Cats_curr = Vect_new_cats_struct();
+	
+	for (i = 0; i < found->n_values; i++) {
+	    Vect_read_line(Map, NULL, Cats_curr, found->value[i]);
+	    if (Vect_cat_get(Cats_curr, layer, &cat_curr) > -1) {
+		if (cat == cat_curr)
+		    return 1;
+	    }
+	    }
+	
+	Vect_destroy_cats_struct(Cats_curr);
+	Vect_destroy_list(exclude);
+	Vect_destroy_list(found);
+    }
+    else {
+	/* this shouldn't happen */
+	G_fatal_error ("Vedit_select_by_query(): %s", _("Unknown query tool"));
+    }
+
+    return 0;
+}

Modified: grass/trunk/vector/v.edit/lib/vedit.h
===================================================================
--- grass/trunk/vector/v.edit/lib/vedit.h	2008-01-21 17:28:21 UTC (rev 29786)
+++ grass/trunk/vector/v.edit/lib/vedit.h	2008-01-21 18:18:13 UTC (rev 29787)
@@ -9,6 +9,10 @@
 #define SNAP       1 /* snapping enabled for nodes */
 #define SNAPVERTEX 2 /* snapping enabled for vertex also */
 
+#define QUERY_UNKNOWN -1
+#define QUERY_LENGTH   0 /* select by line length */
+#define QUERY_DANGLE   1 /* select dangles */
+
 /* break.c */
 int Vedit_split_lines(struct Map_info *, struct ilist *,
 		      struct line_pnts *, double,
@@ -42,6 +46,11 @@
 int Vedit_move_lines(struct Map_info *, struct ilist *, 
 		     double, double, double, int, double);
 
+/* select.c */
+int Vedit_select_by_query(struct Map_info *,
+			  int, int, double, int,
+			  struct ilist *);
+
 /* snap.c */
 int Vedit_snap_point(struct Map_info *,
 		     int, double *, double *, double *, double,



More information about the grass-commit mailing list