Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TestExtension.java @ 8207

History | View | Annotate | Download (6.63 KB)

1 8063 fjp
/* 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 com.iver.cit.gvsig.graph;
42
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45 8133 fjp
import java.awt.Frame;
46 8063 fjp
import java.io.File;
47
import java.util.Collection;
48
import java.util.Iterator;
49 8133 fjp
import java.util.List;
50
import java.util.ListIterator;
51 8063 fjp
52
import com.iver.andami.PluginServices;
53
import com.iver.andami.plugins.Extension;
54 8133 fjp
import com.iver.cit.gvsig.fmap.DriverException;
55 8063 fjp
import com.iver.cit.gvsig.fmap.MapContext;
56
import com.iver.cit.gvsig.fmap.MapControl;
57
import com.iver.cit.gvsig.fmap.core.FShape;
58
import com.iver.cit.gvsig.fmap.core.IFeature;
59
import com.iver.cit.gvsig.fmap.core.IGeometry;
60
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
61
import com.iver.cit.gvsig.fmap.layers.FLayer;
62
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
63
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
64
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
65 8133 fjp
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
66 8063 fjp
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
67 8133 fjp
import com.iver.cit.gvsig.graph.core.EdgeWeightLabeller;
68
import com.iver.cit.gvsig.graph.core.FEdge;
69
import com.iver.cit.gvsig.graph.core.FNode;
70 8063 fjp
import com.iver.cit.gvsig.graph.core.IGraph;
71 8162 fjp
import com.iver.cit.gvsig.graph.core.JungGraph;
72 8063 fjp
import com.iver.cit.gvsig.graph.core.Network;
73
import com.iver.cit.gvsig.graph.core.NetworkRedLoader;
74 8188 jaume
import com.iver.cit.gvsig.graph.gui.DlgDirections;
75 8063 fjp
import com.iver.cit.gvsig.graph.solvers.Route;
76
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolver;
77
import com.iver.cit.gvsig.project.documents.view.gui.View;
78
79 8133 fjp
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
80
81 8063 fjp
public class TestExtension extends Extension {
82
83
        public static ShortestPathSolver solver = new ShortestPathSolver();
84
        private int idSymbolLine = -1;
85 8188 jaume
86 8063 fjp
        public void initialize() {
87
        }
88
89
        public void execute(String actionCommand) {
90
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
91
                MapContext map = v.getMapControl().getMapContext();
92
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
93
                while (it.hasNext())
94
                {
95
                        FLayer aux = it.next();
96 8133 fjp
                        if (!aux.isActive())
97
                                continue;
98 8063 fjp
                        Network net = new Network();
99
                        net.setLayer((FLyrVect) aux);
100
                        NetworkRedLoader netLoader = new NetworkRedLoader();
101 8188 jaume
102 8063 fjp
                        netLoader.setNetFile(new File("c:/ejes.red"));
103 8188 jaume
104 8063 fjp
                        IGraph g = netLoader.loadNetwork();
105
106
                        net.setGraph(g);
107 8133 fjp
108 8063 fjp
                        // Primer punto
109 8162 fjp
                        net.addFlag(433647.09375, 4488029, 10);
110 8188 jaume
111 8063 fjp
                        // Segundo punto
112 8162 fjp
                        net.addFlag(437290.96875, 4481547, 10);
113 8188 jaume
114
115
116 8063 fjp
                        Route route;
117 8188 jaume
                        try {
118 8063 fjp
                                solver.setNetwork(net);
119
                                solver.setFielStreetName("Nombre");
120
                                route = solver.calculateRoute();
121 8133 fjp
                                DlgDirections dlg = new DlgDirections((Frame) PluginServices.getMainFrame(), false);
122
                                dlg.setModel(route.getFeatureList());
123
124 8063 fjp
                                createGraphicsFrom(route.getFeatureList(), v.getMapControl());
125 8188 jaume
126 8133 fjp
                                dlg.setVisible(true);
127
                                System.out.println("distancia fjp:" + route.getCost());
128 8188 jaume
129 8133 fjp
                                // PROBAMOS JUNG
130 8162 fjp
                                JungGraph jG = new JungGraph(net.getGraph());
131 8188 jaume
132 8133 fjp
                                // Probamos la algoritmia: distancia entre nodo 1 y nodo 1000
133 8162 fjp
                                DijkstraShortestPath distCalculator = new DijkstraShortestPath(jG, new EdgeWeightLabeller());
134
                                FNode vOrig = jG.getVertexByID(1);
135
                                FNode vEnd = jG.getVertexByID(1000);
136 8133 fjp
                                long t1 = System.currentTimeMillis();
137
                                Number dist = distCalculator.getDistance(vOrig, vEnd);
138
                                long t2 = System.currentTimeMillis();
139 8188 jaume
140 8133 fjp
//                                assertEquals(dist.doubleValue(), 8887, 0);
141 8188 jaume
142 8133 fjp
                                System.out.println("dist =" + dist + " meters. msecs: " + (t2-t1));
143
144
                                List path = distCalculator.getPath(vOrig, vEnd);
145
                        createGraphicsFrom(path.listIterator(), v.getMapControl());
146
147
                                System.out.println(vOrig + " - " + vEnd);
148
                                System.out.println(vOrig.getX() + ", " + vOrig.getY()
149
                                                                + " - " + vEnd.getX() + ", " + vEnd.getY());
150
151 8188 jaume
152 8063 fjp
                        } catch (GraphException e) {
153
                                // TODO Auto-generated catch block
154
                                e.printStackTrace();
155 8133 fjp
                        } catch (DriverException e) {
156
                                // TODO Auto-generated catch block
157
                                e.printStackTrace();
158 8063 fjp
                        }
159
                }
160 8188 jaume
161
162
163 8063 fjp
        }
164
165
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
166
                Iterator it = featureList.iterator();
167
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
168
                if (idSymbolLine == -1)
169
                {
170 8133 fjp
                        Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f);
171
                        FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
172 8063 fjp
                        lineSymbol.setStroke(new BasicStroke(3.0f));
173
                        idSymbolLine = graphicLayer.addSymbol(lineSymbol);
174
                }
175
                while (it.hasNext()) {
176
                        IFeature feat = (IFeature) it.next();
177
                        IGeometry gAux = feat.getGeometry();
178
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
179 8207 fjp
                        graphicLayer.insertGraphic(0, graphic);
180 8063 fjp
                }
181
                mapControl.drawGraphics();
182 8188 jaume
183 8063 fjp
        }
184 8188 jaume
185 8133 fjp
        private void createGraphicsFrom(ListIterator it, MapControl mapControl) throws DriverException {
186
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
187
                Color color = new Color(0.5f, 0.8f, 0.0f, 0.5f);
188
                FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
189 8162 fjp
                lineSymbol.setStroke(new BasicStroke(7.0f));
190 8133 fjp
                int idSymbol = graphicLayer.addSymbol(lineSymbol);
191
                VectorialAdapter va = (VectorialAdapter) solver.getNetwork().getLayer().getSource();
192
                while (it.hasNext()) {
193
                        FEdge edge = (FEdge) it.next();
194
                        IFeature feat = va.getFeature(edge.getArcID());
195
                        IGeometry gAux = feat.getGeometry();
196
                        FGraphic graphic = new FGraphic(gAux, idSymbol);
197 8207 fjp
                        graphicLayer.insertGraphic(0, graphic);
198 8133 fjp
                }
199
                mapControl.drawGraphics();
200 8188 jaume
201 8133 fjp
        }
202 8063 fjp
203 8188 jaume
204 8063 fjp
        public boolean isEnabled() {
205
                // TODO Auto-generated method stub
206
                return true;
207
        }
208
209
        public boolean isVisible() {
210
                // TODO Auto-generated method stub
211
                return true;
212
        }
213
214
}
215