Statistics
| Revision:

svn-gvsig-desktop / tags / PilotoRedes_Build_2 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TestExtension.java @ 11410

History | View | Annotate | Download (8.14 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 com.iver.cit.gvsig.graph;
42

    
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45
import java.awt.Frame;
46
import java.io.File;
47
import java.util.Collection;
48
import java.util.Iterator;
49
import java.util.List;
50
import java.util.ListIterator;
51

    
52
import com.iver.andami.PluginServices;
53
import com.iver.andami.plugins.Extension;
54
import com.iver.cit.gvsig.fmap.DriverException;
55
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
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
66
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
67
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
import com.iver.cit.gvsig.graph.core.GvFlag;
71
import com.iver.cit.gvsig.graph.core.IGraph;
72
import com.iver.cit.gvsig.graph.core.JungGraph;
73
import com.iver.cit.gvsig.graph.core.Network;
74
import com.iver.cit.gvsig.graph.core.loaders.NetworkRedLoader;
75
import com.iver.cit.gvsig.graph.gui.DlgDirections;
76
import com.iver.cit.gvsig.graph.gui.RouteControlPanel;
77
import com.iver.cit.gvsig.graph.gui.RouteReportPanel;
78
import com.iver.cit.gvsig.graph.solvers.Route;
79
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverAStar;
80
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverDijkstra;
81
import com.iver.cit.gvsig.project.documents.view.gui.View;
82

    
83
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
84

    
85
public class TestExtension extends Extension {
86

    
87
        public static ShortestPathSolverDijkstra solver = new ShortestPathSolverDijkstra();
88
        private int idSymbolLine = -1;
89

    
90
        public void initialize() {
91
        }
92

    
93
        public void execute(String actionCommand) {
94
                
95
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
96
                MapContext map = v.getMapControl().getMapContext();
97
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
98
                
99
                if (actionCommand.equals("TEST2")) {
100
                        while (it.hasNext())
101
                        {
102
                                FLayer aux = it.next();
103
                                if (!aux.isActive())
104
                                        continue;
105
                                Network net = (Network) aux.getProperty("network");
106

    
107
                                if ( net != null)
108
                                {
109
                                        GvFlag[] flags = net.getFlags();
110
                                        if(flags.length > 0)
111
                                                PluginServices.getMDIManager().addWindow(new RouteControlPanel(net));
112
                                        return;
113
                                }
114
                        }
115
                }
116
                
117
                while (it.hasNext())
118
                {
119
                        FLayer aux = it.next();
120
                        if (!aux.isActive())
121
                                continue;
122
                        Network net = new Network();
123
                        net.setLayer((FLyrVect) aux);
124
                        NetworkRedLoader netLoader = new NetworkRedLoader();
125

    
126
                        netLoader.setNetFile(new File("c:/ejes.red"));
127

    
128
                        IGraph g = netLoader.loadNetwork();
129

    
130
                        net.setGraph(g);
131
                        try {
132

    
133
                                // Primer punto
134
                                net.addFlag(433647.09375, 4488029, 10);
135
        
136
                                // Segundo punto
137
                                net.addFlag(437290.96875, 4481547, 10);
138
        
139
        
140
        
141
                                Route route;
142
                                solver.setNetwork(net);
143
                                solver.setFielStreetName("Nombre");
144
                                route = solver.calculateRoute();
145
                                
146
                                
147
                                
148
                                
149
//                                DlgDirections dlg = new DlgDirections((Frame) PluginServices.getMainFrame(), false);
150
//                                dlg.setModel(route.getFeatureList());
151

    
152
                                createGraphicsFrom(route.getFeatureList(), v.getMapControl());
153

    
154
//                                dlg.setVisible(true);
155
                                
156
                                RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
157
                                PluginServices.getMDIManager().addWindow(routeReport);
158
                                
159
                                System.out.println("distancia fjp:" + route.getCost());
160

    
161
                                Route route2;
162
                                ShortestPathSolverAStar solver2 = new ShortestPathSolverAStar();
163
                                solver2.setNetwork(net);
164
                                solver2.setFielStreetName("Nombre");
165
                                route2 = solver2.calculateRoute();
166
                                
167
                                
168
                                
169
                                
170
//                                DlgDirections dlg = new DlgDirections((Frame) PluginServices.getMainFrame(), false);
171
//                                dlg.setModel(route.getFeatureList());
172

    
173
                                createGraphicsFrom(route2.getFeatureList(), v.getMapControl());
174

    
175
//                                dlg.setVisible(true);
176
                                
177
                                RouteReportPanel routeReport2 = new RouteReportPanel(route2, v.getMapControl());
178
                                PluginServices.getMDIManager().addWindow(routeReport2);
179
                                
180
                                System.out.println("distancia AStar:" + route2.getCost());
181
                                
182
                                
183
                                // PROBAMOS JUNG
184
//                                JungGraph jG = new JungGraph(net.getGraph());
185

    
186
                                // Probamos la algoritmia: distancia entre nodo 1 y nodo 1000
187
//                                DijkstraShortestPath distCalculator = new DijkstraShortestPath(jG, new EdgeWeightLabeller());
188
//                                FNode vOrig = jG.getVertexByID(1);
189
//                                FNode vEnd = jG.getVertexByID(1000);
190
//                                long t1 = System.currentTimeMillis();
191
//                                Number dist = distCalculator.getDistance(vOrig, vEnd);
192
//                                long t2 = System.currentTimeMillis();
193

    
194
//                                assertEquals(dist.doubleValue(), 8887, 0);
195

    
196
//                                System.out.println("dist =" + dist + " meters. msecs: " + (t2-t1));
197

    
198
//                                List path = distCalculator.getPath(vOrig, vEnd);
199
//                        createGraphicsFrom(path.listIterator(), v.getMapControl());
200
//
201
//                                System.out.println(vOrig + " - " + vEnd);
202
//                                System.out.println(vOrig.getX() + ", " + vOrig.getY()
203
//                                                                + " - " + vEnd.getX() + ", " + vEnd.getY());
204

    
205

    
206
                        } catch (GraphException e) {
207
                                // TODO Auto-generated catch block
208
                                e.printStackTrace();
209
                        }/* catch (DriverException e) {
210
                                // TODO Auto-generated catch block
211
                                e.printStackTrace();
212
                        }*/
213
                }
214

    
215

    
216

    
217
        }
218

    
219
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
220
                Iterator it = featureList.iterator();
221
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
222
                if (idSymbolLine == -1)
223
                {
224
                        Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f);
225
                        FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
226
                        lineSymbol.setStroke(new BasicStroke(3.0f));
227
                        idSymbolLine = graphicLayer.addSymbol(lineSymbol);
228
                }
229
                while (it.hasNext()) {
230
                        IFeature feat = (IFeature) it.next();
231
                        IGeometry gAux = feat.getGeometry();
232
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
233
                        graphicLayer.insertGraphic(0, graphic);
234
                }
235
                mapControl.drawGraphics();
236

    
237
        }
238

    
239
        private void createGraphicsFrom(ListIterator it, MapControl mapControl) throws DriverException {
240
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
241
                Color color = new Color(0.5f, 0.8f, 0.0f, 0.5f);
242
                FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
243
                lineSymbol.setStroke(new BasicStroke(7.0f));
244
                int idSymbol = graphicLayer.addSymbol(lineSymbol);
245
                VectorialAdapter va = (VectorialAdapter) solver.getNetwork().getLayer().getSource();
246
                while (it.hasNext()) {
247
                        FEdge edge = (FEdge) it.next();
248
                        IFeature feat = va.getFeature(edge.getArcID());
249
                        IGeometry gAux = feat.getGeometry();
250
                        FGraphic graphic = new FGraphic(gAux, idSymbol);
251
                        graphicLayer.insertGraphic(0, graphic);
252
                }
253
                mapControl.drawGraphics();
254

    
255
        }
256

    
257

    
258
        public boolean isEnabled() {
259
                // TODO Auto-generated method stub
260
                return true;
261
        }
262

    
263
        public boolean isVisible() {
264
                // TODO Auto-generated method stub
265
                return false;
266
        }
267

    
268
}
269

    
270