Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / core / JungConverter.java @ 35157

History | View | Annotate | Download (2.87 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.graph.core;
42

    
43
import edu.uci.ics.jung.graph.ArchetypeGraph;
44
import edu.uci.ics.jung.graph.Graph;
45
import edu.uci.ics.jung.graph.Vertex;
46
import edu.uci.ics.jung.graph.decorators.Indexer;
47
import edu.uci.ics.jung.graph.impl.SparseGraph;
48

    
49
public class JungConverter {
50
        
51
        IGraph g;
52
        Graph gJ;
53
        Indexer indexer;
54
        
55
        
56
        public void setGraph(IGraph g) {
57
                this.g = g;
58
        }
59
        
60
        public void convert() {
61
                // TODO: Hacer y lanzar una excepci?n si algo ha ido mal
62
                if (g == null)
63
                        throw new RuntimeException("Please, use setGraph() before convert()");
64
                gJ = new SparseGraph();
65
                long t1 = System.currentTimeMillis();
66
                
67
                for (int i=0; i < g.numVertices(); i++)
68
                {
69
                        GvNode n = g.getNodeByID(i);
70
                        FNode v = new FNode(i, n.getX(), n.getY());
71
                        gJ.addVertex(v);                                
72
                }
73
                indexer = Indexer.getIndexer(gJ);
74
                
75
                for (int i=0; i < g.numEdges(); i++)
76
                {
77
                        GvEdge e = g.getEdgeByID(i);
78
                        Vertex vFrom = (Vertex) indexer.getVertex(e.getIdNodeOrig());
79
                        Vertex vTo = (Vertex) indexer.getVertex(e.getIdNodeEnd());
80
                        
81
                        FEdge edge = new FEdge(vFrom, vTo);
82
                        edge.setArcID(e.getIdArc());
83
                        edge.setDirection(e.getDirec());
84
                        edge.setIdNodeOrig(e.getIdNodeOrig());
85
                        edge.setIdNodeEnd(e.getIdNodeEnd());
86
                        edge.setType(e.getType());
87
                        edge.setWeight(e.getDistance());
88
                        edge.setCost2(e.getWeight());
89
                        
90
                        gJ.addEdge(edge);
91
                }
92
                long t2 = System.currentTimeMillis();
93
                System.out.println("Tiempo de carga desde nodes.dbf y edges.dbf y generando JUNG network: " + (t2-t1) + " msecs");
94
        }
95
        
96
        public ArchetypeGraph getJungGraph() {
97
                if (gJ == null)
98
                        throw new RuntimeException("You should call convert() before use this method.");
99
                return gJ;
100
        }
101
        
102
        public Indexer getIndexer() {
103
                return indexer;
104
        }
105

    
106
}
107

    
108