[GRASS-SVN] r37248 - in grass-addons/vector/net.analyze: . v.net.bridge v.net.components

svn_grass at osgeo.org svn_grass at osgeo.org
Sat May 16 14:14:05 EDT 2009


Author: dano
Date: 2009-05-16 14:14:04 -0400 (Sat, 16 May 2009)
New Revision: 37248

Added:
   grass-addons/vector/net.analyze/netalib/
   grass-addons/vector/net.analyze/v.net.bridge/
   grass-addons/vector/net.analyze/v.net.bridge/Makefile
   grass-addons/vector/net.analyze/v.net.bridge/bridge.c
   grass-addons/vector/net.analyze/v.net.bridge/main.c
   grass-addons/vector/net.analyze/v.net.components/
   grass-addons/vector/net.analyze/v.net.components/Makefile
   grass-addons/vector/net.analyze/v.net.components/components.c
   grass-addons/vector/net.analyze/v.net.components/main.c
Log:
Initial commit of net.analyze modules

Added: grass-addons/vector/net.analyze/v.net.bridge/Makefile
===================================================================
--- grass-addons/vector/net.analyze/v.net.bridge/Makefile	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.bridge/Makefile	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,14 @@
+
+MODULE_TOPDIR = ../../..
+
+PGM=v.net.bridge
+
+LIBES     = $(VECTLIB) $(VECTLIB_REAL) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTDEP) $(DBMIDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+ 
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd	
+


Property changes on: grass-addons/vector/net.analyze/v.net.bridge/Makefile
___________________________________________________________________
Name: svn:executable
   + *

Added: grass-addons/vector/net.analyze/v.net.bridge/bridge.c
===================================================================
--- grass-addons/vector/net.analyze/v.net.bridge/bridge.c	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.bridge/bridge.c	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,125 @@
+
+/****************************************************************
+ *
+ * MODULE:     v.net.components
+ *
+ * AUTHOR(S):  Daniel Bundala
+ *
+ * PURPOSE:    Computes strongly and weakly connected components
+ *
+ * COPYRIGHT:  (C) 2002-2005 by the GRASS Development Team
+ *
+ *             This program is free software under the
+ *             GNU General Public License (>=v2).
+ *             Read the file COPYING that comes with GRASS
+ *             for details.
+ *
+ ****************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+
+/* return the number of bridges in the graph.
+ * bridge is an array containing the indices of the bridges
+ */
+int compute_bridges(struct dglGraph_s *graph, struct ilist *bridge_list)
+{
+    int nnodes;
+    int bridges = 0;
+
+    dglEdgesetTraverser_s *current;	/*edge to be processed when the node is visited */
+    int *tin, *min_tin;		/*time in, and smallest tin over all successors. 0 if not yet visited */
+    dglInt32_t **parent;	/*parents of the nodes */
+    dglInt32_t **stack;		/*stack of nodes */
+    dglInt32_t **current_edge;	/*current edge for each node */
+    dglNodeTraverser_s nt;
+    dglInt32_t *current_node;
+    int stack_size;
+    int i, time;
+    nnodes = dglGet_NodeCount(graph);
+    current =
+	(dglEdgesetTraverser_s *) G_calloc(nnodes + 1,
+					   sizeof(dglEdgesetTraverser_s));
+    tin = (int *)G_calloc(nnodes + 1, sizeof(int));
+    min_tin = (int *)G_calloc(nnodes + 1, sizeof(int));
+    parent = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
+    stack = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
+    current_edge = (dglInt32_t **) G_calloc(nnodes + 1, sizeof(dglInt32_t *));
+    if (!tin || !min_tin || !parent || !stack || !current) {
+	G_fatal_error(_("Out of memory"));
+	return -1;
+    }
+
+    for (i = 1; i <= nnodes; i++) {
+	dglEdgeset_T_Initialize(&current[i], graph,
+				dglNodeGet_OutEdgeset(graph,
+						      dglGetNode(graph, i)));
+	current_edge[i] = dglEdgeset_T_First(&current[i]);
+	tin[i] = 0;
+    }
+
+    dglNode_T_Initialize(&nt, graph);
+
+    time = 0;
+    for (current_node = dglNode_T_First(&nt); current_node;
+	 current_node = dglNode_T_Next(&nt)) {
+	dglInt32_t current_id = dglNodeGet_Id(graph, current_node);
+	if (tin[current_id] == 0) {
+	    stack[0] = current_node;
+	    stack_size = 1;
+	    parent[current_id] = NULL;
+	    while (stack_size) {
+		dglInt32_t *node = stack[stack_size - 1];
+		dglInt32_t node_id = dglNodeGet_Id(graph, node);
+		if (tin[node_id] == 0)	/*vertex visited for the first time */
+		    min_tin[node_id] = tin[node_id] = ++time;
+		else {		/*return from the recursion */
+		    dglInt32_t to =
+			dglNodeGet_Id(graph,
+				      dglEdgeGet_Tail(graph,
+						      current_edge[node_id]));
+		    if (min_tin[to] > tin[node_id]) {	/*no path from the subtree above the current node */
+			Vect_list_append(bridge_list, dglEdgeGet_Id(graph, current_edge[node_id]));	/*so it must be a bridge */
+			bridges++;
+		    }
+		    if (min_tin[to] < min_tin[node_id])
+			min_tin[node_id] = min_tin[to];
+		    current_edge[node_id] = dglEdgeset_T_Next(&current[node_id]);	/*proceed to the next edge */
+		}
+		for (; current_edge[node_id]; current_edge[node_id] = dglEdgeset_T_Next(&current[node_id])) {	//try next edges
+		    dglInt32_t *to =
+			dglEdgeGet_Tail(graph, current_edge[node_id]);
+		    if (to == parent[node_id])
+			continue;	/*skip parrent */
+		    int to_id = dglNodeGet_Id(graph, to);
+		    if (tin[to_id]) {	/*back edge, cannot be a bridge/articualtion point */
+			if (min_tin[to_id] < min_tin[node_id])
+			    min_tin[node_id] = min_tin[to_id];
+		    }
+		    else {	/*forward edge */
+			parent[to_id] = node;
+			stack[stack_size++] = to;
+			break;
+		    }
+		}
+		if (!current_edge[node_id])
+		    stack_size--;	/*current node completely processed */
+	    }
+	}
+    }
+
+    dglNode_T_Release(&nt);
+    for (i = 1; i <= nnodes; i++)
+	dglEdgeset_T_Release(&current[i]);
+
+    G_free(current);
+    G_free(tin);
+    G_free(min_tin);
+    G_free(parent);
+    G_free(stack);
+    G_free(current_edge);
+    return bridges;
+}


Property changes on: grass-addons/vector/net.analyze/v.net.bridge/bridge.c
___________________________________________________________________
Name: svn:executable
   + *

Added: grass-addons/vector/net.analyze/v.net.bridge/main.c
===================================================================
--- grass-addons/vector/net.analyze/v.net.bridge/main.c	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.bridge/main.c	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,142 @@
+
+/****************************************************************
+ *
+ * MODULE:     v.net.components
+ *
+ * AUTHOR(S):  Daniel Bundala
+ *
+ * PURPOSE:    Computes bridges in the network
+ *
+ * COPYRIGHT:  (C) 2002-2005 by the GRASS Development Team
+ *
+ *             This program is free software under the
+ *             GNU General Public License (>=v2).
+ *             Read the file COPYING that comes with GRASS
+ *             for details.
+ *
+ ****************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+
+int compute_bridges(struct dglGraph_s *graph, struct ilist *bridge_list);
+
+int main(int argc, char *argv[])
+{
+    struct Map_info In, Out;
+    static struct line_pnts *Points;
+    struct line_cats *Cats;
+    char *mapset;
+    struct GModule *module;	/* GRASS module for parsing arguments */
+    struct Option *map_in, *map_out;
+    struct Option *cat_opt, *field_opt, *where_opt;
+    int chcat, with_z;
+    int layer, mask_type;
+    VARRAY *varray;
+    dglGraph_s *graph;
+    int i, bridges;
+    struct ilist *bridge_list;
+
+    /* initialize GIS environment */
+    G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
+
+    /* initialize module */
+    module = G_define_module();
+    module->keywords = _("network, bridges, articulation points");
+    module->description = _("Computes bridges and articulation points.");
+
+    /* Define the different options as defined in gis.h */
+    map_in = G_define_standard_option(G_OPT_V_INPUT);
+    map_out = G_define_standard_option(G_OPT_V_OUTPUT);
+
+    field_opt = G_define_standard_option(G_OPT_V_FIELD);
+    cat_opt = G_define_standard_option(G_OPT_V_CATS);
+    where_opt = G_define_standard_option(G_OPT_WHERE);
+
+    /* options and flags parser */
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    /* TODO: make an option for this */
+    mask_type = GV_LINE | GV_BOUNDARY;
+
+    Points = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+
+    Vect_check_input_output_name(map_in->answer, map_out->answer,
+				 GV_FATAL_EXIT);
+
+    if ((mapset = G_find_vector2(map_in->answer, "")) == NULL)
+	G_fatal_error(_("Vector map <%s> not found"), map_in->answer);
+
+    Vect_set_open_level(2);
+
+    if (1 > Vect_open_old(&In, map_in->answer, mapset))
+	G_fatal_error(_("Unable to open vector map <%s>"),
+		      G_fully_qualified_name(map_in->answer, mapset));
+
+    with_z = Vect_is_3d(&In);
+
+    if (0 > Vect_open_new(&Out, map_out->answer, with_z)) {
+	Vect_close(&In);
+	G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer);
+    }
+
+
+    /* parse filter option and select appropriate lines */
+    layer = atoi(field_opt->answer);
+    if (where_opt->answer) {
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
+	if (cat_opt->answer)
+	    G_warning(_
+		      ("'where' and 'cats' parameters were supplied, cat will be ignored"));
+	chcat = 1;
+	varray = Vect_new_varray(Vect_get_num_lines(&In));
+	if (Vect_set_varray_from_db
+	    (&In, layer, where_opt->answer, mask_type, 1, varray) == -1) {
+	    G_warning(_("Unable to load data from database"));
+	}
+    }
+    else if (cat_opt->answer) {
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "cat");
+	varray = Vect_new_varray(Vect_get_num_lines(&In));
+	chcat = 1;
+	if (Vect_set_varray_from_cat_string
+	    (&In, layer, cat_opt->answer, mask_type, 1, varray) == -1) {
+	    G_warning(_("Problem loading category values"));
+	}
+    }
+    else {
+	chcat = 0;
+	varray = NULL;
+    }
+
+    Vect_net_build_graph(&In, mask_type, 0, 0, NULL, NULL, NULL, 0, 0);
+    graph = &(In.graph);
+    bridge_list = Vect_new_list();
+    bridges = compute_bridges(graph, bridge_list);
+
+    G_debug(3, "Bridges: %d\n", bridges);
+
+    Vect_copy_head_data(&In, &Out);
+    Vect_hist_copy(&In, &Out);
+    Vect_hist_command(&Out);
+
+    for (i = 0; i < bridges; i++) {
+	int type =
+	    Vect_read_line(&In, Points, Cats, abs(bridge_list->value[i]));
+	Vect_write_line(&Out, type, Points, Cats);
+    }
+    Vect_destroy_list(bridge_list);
+
+    Vect_build(&Out);
+
+    Vect_close(&In);
+    Vect_close(&Out);
+
+    exit(EXIT_SUCCESS);
+}


Property changes on: grass-addons/vector/net.analyze/v.net.bridge/main.c
___________________________________________________________________
Name: svn:executable
   + *

Added: grass-addons/vector/net.analyze/v.net.components/Makefile
===================================================================
--- grass-addons/vector/net.analyze/v.net.components/Makefile	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.components/Makefile	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,14 @@
+
+MODULE_TOPDIR = ../../..
+
+PGM=v.net.components
+
+LIBES     = $(VECTLIB) $(VECTLIB_REAL) $(DBMILIB) $(GISLIB)
+DEPENDENCIES = $(VECTDEP) $(DBMIDEP) $(GISDEP)
+EXTRA_INC = $(VECT_INC)
+EXTRA_CFLAGS = $(VECT_CFLAGS)
+ 
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd	
+


Property changes on: grass-addons/vector/net.analyze/v.net.components/Makefile
___________________________________________________________________
Name: svn:executable
   + *

Added: grass-addons/vector/net.analyze/v.net.components/components.c
===================================================================
--- grass-addons/vector/net.analyze/v.net.components/components.c	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.components/components.c	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,174 @@
+
+/****************************************************************
+ *
+ * MODULE:     v.net.components
+ *
+ * AUTHOR(S):  Daniel Bundala
+ *
+ * PURPOSE:    Computes strongly and weakly connected components
+ *
+ * COPYRIGHT:  (C) 2002-2005 by the GRASS Development Team
+ *
+ *             This program is free software under the
+ *             GNU General Public License (>=v2).
+ *             Read the file COPYING that comes with GRASS
+ *             for details.
+ *
+ ****************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+
+int weakly_connected_components(struct dglGraph_s *graph, int *component)
+{
+    int nnodes;
+    dglInt32_t *stack;
+    int *visited;
+    int stack_size, components;
+    dglInt32_t *cur_node;
+    dglNodeTraverser_s nt;
+
+    components = 0;
+    nnodes = dglGet_NodeCount(graph);
+    stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
+    visited = (int *)G_calloc(nnodes + 1, sizeof(int));
+    if (!stack || !visited) {
+	G_fatal_error(_("Out of memory"));
+	return -1;
+    }
+
+    dglNode_T_Initialize(&nt, graph);
+
+    for (cur_node = dglNode_T_First(&nt); cur_node;
+	 cur_node = dglNode_T_Next(&nt)) {
+	dglInt32_t node_id = dglNodeGet_Id(graph, cur_node);
+	if (!visited[node_id]) {
+	    visited[node_id] = 1;
+	    stack[0] = node_id;
+	    stack_size = 1;
+	    component[node_id] = ++components;
+	    while (stack_size) {
+		dglInt32_t *node, *edgeset, *edge;
+		dglEdgesetTraverser_s et;
+		node = dglGetNode(graph, stack[--stack_size]);
+		edgeset = dglNodeGet_OutEdgeset(graph, node);
+		dglEdgeset_T_Initialize(&et, graph, edgeset);
+		for (edge = dglEdgeset_T_First(&et); edge;
+		     edge = dglEdgeset_T_Next(&et)) {
+		    dglInt32_t to;
+		    to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
+		    if (!visited[to]) {
+			visited[to] = 1;
+			component[to] = components;
+			stack[stack_size++] = to;
+		    }
+		}
+		dglEdgeset_T_Release(&et);
+	    }
+	}
+    }
+    dglNode_T_Release(&nt);
+    G_free(visited);
+    return components;
+}
+
+int strongly_connected_components(struct dglGraph_s *graph, int *component)
+{
+    int nnodes;
+    dglInt32_t *stack, *order;
+    int *visited, *processed;
+    int stack_size, order_size, components;
+    dglInt32_t *node;
+    dglNodeTraverser_s nt;
+
+    components = 0;
+    nnodes = dglGet_NodeCount(graph);
+    stack = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
+    order = (dglInt32_t *) G_calloc(nnodes + 1, sizeof(dglInt32_t));
+    visited = (int *)G_calloc(nnodes + 1, sizeof(int));
+    processed = (int *)G_calloc(nnodes + 1, sizeof(int));
+    if (!stack || !visited || !order || !processed) {
+	G_fatal_error(_("Out of memory"));
+	return -1;
+    }
+
+    order_size = 0;
+    dglNode_T_Initialize(&nt, graph);
+
+    for (node = dglNode_T_First(&nt); node; node = dglNode_T_Next(&nt)) {
+	dglInt32_t node_id = dglNodeGet_Id(graph, node);
+	component[node_id] = 0;
+	if (!visited[node_id]) {
+	    visited[node_id] = 1;
+	    stack[0] = node_id;
+	    stack_size = 1;
+	    while (stack_size) {
+		dglInt32_t *node, *edgeset, *edge;
+		dglEdgesetTraverser_s et;
+		dglInt32_t cur_node_id = stack[stack_size - 1];
+		if (processed[cur_node_id]) {
+		    stack_size--;
+		    order[order_size++] = cur_node_id;
+		    continue;
+		}
+		processed[cur_node_id] = 1;
+		node = dglGetNode(graph, cur_node_id);
+		edgeset = dglNodeGet_OutEdgeset(graph, node);
+		dglEdgeset_T_Initialize(&et, graph, edgeset);
+		for (edge = dglEdgeset_T_First(&et); edge;
+		     edge = dglEdgeset_T_Next(&et)) {
+		    dglInt32_t to;
+		    if (dglEdgeGet_Id(graph, edge) < 0)
+			continue;	/*ignore backward edges */
+		    to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
+		    if (!visited[to]) {
+			visited[to] = 1;
+			stack[stack_size++] = to;
+		    }
+		}
+		dglEdgeset_T_Release(&et);
+	    }
+	}
+    }
+
+    dglNode_T_Release(&nt);
+
+    while (order_size) {
+	dglInt32_t node_id = order[--order_size];
+	if (component[node_id])
+	    continue;
+	components++;
+	component[node_id] = components;
+	stack[0] = node_id;
+	stack_size = 1;
+	while (stack_size) {
+	    dglInt32_t *node, *edgeset, *edge;
+	    dglEdgesetTraverser_s et;
+	    dglInt32_t cur_node_id = stack[--stack_size];
+	    node = dglGetNode(graph, cur_node_id);
+	    edgeset = dglNodeGet_OutEdgeset(graph, node);
+	    dglEdgeset_T_Initialize(&et, graph, edgeset);
+	    for (edge = dglEdgeset_T_First(&et); edge;
+		 edge = dglEdgeset_T_Next(&et)) {
+		dglInt32_t to;
+		if (dglEdgeGet_Id(graph, edge) > 0)
+		    continue;	/*ignore forward edges */
+		to = dglNodeGet_Id(graph, dglEdgeGet_Tail(graph, edge));
+		if (!component[to]) {
+		    component[to] = components;
+		    stack[stack_size++] = to;
+		}
+	    }
+	    dglEdgeset_T_Release(&et);
+	}
+    }
+
+    G_free(stack);
+    G_free(visited);
+    G_free(order);
+    G_free(processed);
+    return components;
+}


Property changes on: grass-addons/vector/net.analyze/v.net.components/components.c
___________________________________________________________________
Name: svn:executable
   + *

Added: grass-addons/vector/net.analyze/v.net.components/main.c
===================================================================
--- grass-addons/vector/net.analyze/v.net.components/main.c	                        (rev 0)
+++ grass-addons/vector/net.analyze/v.net.components/main.c	2009-05-16 18:14:04 UTC (rev 37248)
@@ -0,0 +1,178 @@
+
+/****************************************************************
+ *
+ * MODULE:     v.net.components
+ *
+ * AUTHOR(S):  Daniel Bundala
+ *
+ * PURPOSE:    Computes strongly and weakly connected components
+ *
+ * COPYRIGHT:  (C) 2002-2005 by the GRASS Development Team
+ *
+ *             This program is free software under the
+ *             GNU General Public License (>=v2).
+ *             Read the file COPYING that comes with GRASS
+ *             for details.
+ *
+ ****************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/Vect.h>
+#include <grass/glocale.h>
+
+int weakly_connected_components(struct dglGraph_s *graph, int *component);
+int strongly_connected_components(struct dglGraph_s *graph, int *component);
+
+int main(int argc, char *argv[])
+{
+    struct Map_info In, Out;
+    static struct line_pnts *Points;
+    struct line_cats *Cats;
+    char *mapset;
+    struct GModule *module;	/* GRASS module for parsing arguments */
+    struct Option *map_in, *map_out;
+    struct Option *cat_opt, *field_opt, *where_opt, *method_opt;
+    int chcat, with_z;
+    int layer, mask_type;
+    VARRAY *varray;
+    dglGraph_s *graph;
+    int *component, nnodes, type, i, nlines, components;
+
+    /* initialize GIS environment */
+    G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
+
+    /* initialize module */
+    module = G_define_module();
+    module->keywords = _("network, connected components");
+    module->description =
+	_("Computes strongly and weakly connected components.");
+
+    /* Define the different options as defined in gis.h */
+    map_in = G_define_standard_option(G_OPT_V_INPUT);
+    map_out = G_define_standard_option(G_OPT_V_OUTPUT);
+
+    field_opt = G_define_standard_option(G_OPT_V_FIELD);
+    cat_opt = G_define_standard_option(G_OPT_V_CATS);
+    where_opt = G_define_standard_option(G_OPT_WHERE);
+
+    method_opt = G_define_option();
+    method_opt->key = "method";
+    method_opt->type = TYPE_STRING;
+    method_opt->required = YES;
+    method_opt->multiple = NO;
+    method_opt->options = "weak,strong";
+    method_opt->descriptions = _("weak;Weakly connected components;"
+				 "strong;Strongly connected components;");
+    method_opt->description = _("Type of components");
+
+    /* options and flags parser */
+    if (G_parser(argc, argv))
+	exit(EXIT_FAILURE);
+    /* TODO: make an option for this */
+    mask_type = GV_LINE | GV_BOUNDARY;
+
+    Points = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+
+    Vect_check_input_output_name(map_in->answer, map_out->answer,
+				 GV_FATAL_EXIT);
+
+    if ((mapset = G_find_vector2(map_in->answer, "")) == NULL)
+	G_fatal_error(_("Vector map <%s> not found"), map_in->answer);
+
+    Vect_set_open_level(2);
+
+    if (1 > Vect_open_old(&In, map_in->answer, mapset))
+	G_fatal_error(_("Unable to open vector map <%s>"),
+		      G_fully_qualified_name(map_in->answer, mapset));
+
+    with_z = Vect_is_3d(&In);
+
+    if (0 > Vect_open_new(&Out, map_out->answer, with_z)) {
+	Vect_close(&In);
+	G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer);
+    }
+
+
+    /* parse filter option and select appropriate lines */
+    layer = atoi(field_opt->answer);
+    if (where_opt->answer) {
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
+	if (cat_opt->answer)
+	    G_warning(_
+		      ("'where' and 'cats' parameters were supplied, cat will be ignored"));
+	chcat = 1;
+	varray = Vect_new_varray(Vect_get_num_lines(&In));
+	if (Vect_set_varray_from_db
+	    (&In, layer, where_opt->answer, mask_type, 1, varray) == -1) {
+	    G_warning(_("Unable to load data from database"));
+	}
+    }
+    else if (cat_opt->answer) {
+	if (layer < 1)
+	    G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "cat");
+	varray = Vect_new_varray(Vect_get_num_lines(&In));
+	chcat = 1;
+	if (Vect_set_varray_from_cat_string
+	    (&In, layer, cat_opt->answer, mask_type, 1, varray) == -1) {
+	    G_warning(_("Problem loading category values"));
+	}
+    }
+    else {
+	chcat = 0;
+	varray = NULL;
+    }
+
+    Vect_net_build_graph(&In, mask_type, 0, 0, NULL, NULL, NULL, 0, 0);
+    graph = &(In.graph);
+    nnodes = Vect_get_num_nodes(&In);
+    component = (int *)G_calloc(nnodes + 1, sizeof(int));
+    if (!component) {
+	G_fatal_error(_("Out of memory"));
+	exit(EXIT_FAILURE);
+    }
+    if (method_opt->answer[0] == 'w')
+	components = weakly_connected_components(graph, component);
+    else
+	components = strongly_connected_components(graph, component);
+
+    G_debug(3, "Components: %d\n", components);
+
+    Vect_copy_head_data(&In, &Out);
+    Vect_hist_copy(&In, &Out);
+    Vect_hist_command(&Out);
+
+    nlines = Vect_get_num_lines(&In);
+    for (i = 1; i <= nlines; i++) {
+	type = Vect_read_line(&In, Points, NULL, i);
+	Vect_reset_cats(Cats);
+	if (type == GV_LINE || type == GV_BOUNDARY) {
+	    int node1, node2;
+	    Vect_get_line_nodes(&In, i, &node1, &node2);
+	    if (component[node1] == component[node2]) {
+		Vect_cat_set(Cats, 1, component[node1]);
+	    }
+	    else {
+		continue;
+	    }
+	}
+	else if (type == GV_POINT) {
+	    int node;
+	    Vect_get_line_nodes(&In, i, &node, NULL);
+	    Vect_cat_set(Cats, 1, component[node]);
+	}
+	else
+	    continue;
+	Vect_write_line(&Out, type, Points, Cats);
+    };
+
+    Vect_build(&Out);
+
+    Vect_close(&In);
+    Vect_close(&Out);
+
+    exit(EXIT_SUCCESS);
+}


Property changes on: grass-addons/vector/net.analyze/v.net.components/main.c
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list