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 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 8513 azabala
import com.iver.cit.gvsig.graph.core.GvFlag;
71 8063 fjp
import com.iver.cit.gvsig.graph.core.IGraph;
72 8162 fjp
import com.iver.cit.gvsig.graph.core.JungGraph;
73 8063 fjp
import com.iver.cit.gvsig.graph.core.Network;
74 8487 fjp
import com.iver.cit.gvsig.graph.core.loaders.NetworkRedLoader;
75 8188 jaume
import com.iver.cit.gvsig.graph.gui.DlgDirections;
76 8326 jaume
import com.iver.cit.gvsig.graph.gui.RouteControlPanel;
77 8216 azabala
import com.iver.cit.gvsig.graph.gui.RouteReportPanel;
78 8063 fjp
import com.iver.cit.gvsig.graph.solvers.Route;
79 8522 fjp
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverAStar;
80 8499 fjp
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverDijkstra;
81 8063 fjp
import com.iver.cit.gvsig.project.documents.view.gui.View;
82
83 8133 fjp
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
84
85 8063 fjp
public class TestExtension extends Extension {
86
87 8499 fjp
        public static ShortestPathSolverDijkstra solver = new ShortestPathSolverDijkstra();
88 8063 fjp
        private int idSymbolLine = -1;
89 8188 jaume
90 8063 fjp
        public void initialize() {
91
        }
92
93
        public void execute(String actionCommand) {
94 8513 azabala
95 8063 fjp
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
96
                MapContext map = v.getMapControl().getMapContext();
97
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
98 8513 azabala
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 8573 azabala
                                                PluginServices.getMDIManager().addWindow(new RouteControlPanel(net));
112 8513 azabala
                                        return;
113
                                }
114
                        }
115
                }
116
117 8063 fjp
                while (it.hasNext())
118
                {
119
                        FLayer aux = it.next();
120 8133 fjp
                        if (!aux.isActive())
121
                                continue;
122 8063 fjp
                        Network net = new Network();
123
                        net.setLayer((FLyrVect) aux);
124
                        NetworkRedLoader netLoader = new NetworkRedLoader();
125 8188 jaume
126 8063 fjp
                        netLoader.setNetFile(new File("c:/ejes.red"));
127 8188 jaume
128 8063 fjp
                        IGraph g = netLoader.loadNetwork();
129
130
                        net.setGraph(g);
131 8188 jaume
                        try {
132 8261 fjp
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 8063 fjp
                                solver.setNetwork(net);
143
                                solver.setFielStreetName("Nombre");
144
                                route = solver.calculateRoute();
145 8216 azabala
146
147
148
149
//                                DlgDirections dlg = new DlgDirections((Frame) PluginServices.getMainFrame(), false);
150
//                                dlg.setModel(route.getFeatureList());
151 8133 fjp
152 8063 fjp
                                createGraphicsFrom(route.getFeatureList(), v.getMapControl());
153 8188 jaume
154 8216 azabala
//                                dlg.setVisible(true);
155
156
                                RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
157
                                PluginServices.getMDIManager().addWindow(routeReport);
158
159 8133 fjp
                                System.out.println("distancia fjp:" + route.getCost());
160 8188 jaume
161 8522 fjp
                                Route route2;
162
                                ShortestPathSolverAStar solver2 = new ShortestPathSolverAStar();
163
                                solver2.setNetwork(net);
164
                                solver2.setFielStreetName("Nombre");
165
                                route2 = solver2.calculateRoute();
166 8216 azabala
167
168 8522 fjp
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 8133 fjp
                                // PROBAMOS JUNG
184 8216 azabala
//                                JungGraph jG = new JungGraph(net.getGraph());
185 8188 jaume
186 8133 fjp
                                // Probamos la algoritmia: distancia entre nodo 1 y nodo 1000
187 8216 azabala
//                                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 8188 jaume
194 8133 fjp
//                                assertEquals(dist.doubleValue(), 8887, 0);
195 8188 jaume
196 8216 azabala
//                                System.out.println("dist =" + dist + " meters. msecs: " + (t2-t1));
197 8133 fjp
198 8216 azabala
//                                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 8133 fjp
205
206 8063 fjp
                        } catch (GraphException e) {
207
                                // TODO Auto-generated catch block
208
                                e.printStackTrace();
209 8216 azabala
                        }/* catch (DriverException e) {
210 8133 fjp
                                // TODO Auto-generated catch block
211
                                e.printStackTrace();
212 8216 azabala
                        }*/
213 8063 fjp
                }
214 8188 jaume
215
216
217 8063 fjp
        }
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 8133 fjp
                        Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f);
225
                        FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
226 8063 fjp
                        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 8207 fjp
                        graphicLayer.insertGraphic(0, graphic);
234 8063 fjp
                }
235
                mapControl.drawGraphics();
236 8188 jaume
237 8063 fjp
        }
238 8188 jaume
239 8133 fjp
        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 8162 fjp
                lineSymbol.setStroke(new BasicStroke(7.0f));
244 8133 fjp
                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 8207 fjp
                        graphicLayer.insertGraphic(0, graphic);
252 8133 fjp
                }
253
                mapControl.drawGraphics();
254 8188 jaume
255 8133 fjp
        }
256 8063 fjp
257 8188 jaume
258 8063 fjp
        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 11252 fjp
                return false;
266 8063 fjp
        }
267
268
}
269