Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / ShortestPathExtension.java @ 35010

History | View | Annotate | Download (9.79 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;
42

    
43
import java.awt.BasicStroke;
44
import java.awt.Color;
45
import java.util.ArrayList;
46
import java.util.Arrays;
47
import java.util.Collection;
48
import java.util.Collections;
49
import java.util.Iterator;
50
import java.util.List;
51

    
52
import javax.swing.JComponent;
53
import javax.swing.JOptionPane;
54

    
55
import org.gvsig.graph.core.GraphException;
56
import org.gvsig.graph.core.GvFlag;
57
import org.gvsig.graph.core.Network;
58
import org.gvsig.graph.core.NetworkUtils;
59
import org.gvsig.graph.gui.RouteControlPanel;
60
import org.gvsig.graph.gui.RouteReportPanel;
61
import org.gvsig.graph.solvers.OneToManySolver;
62
import org.gvsig.graph.solvers.Route;
63
import org.gvsig.graph.solvers.ShortestPathSolverAStar;
64
import org.gvsig.graph.solvers.TspSolverAnnealing;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.andami.plugins.Extension;
68
import com.iver.andami.ui.mdiManager.IWindow;
69
import com.iver.cit.gvsig.fmap.MapContext;
70
import com.iver.cit.gvsig.fmap.MapControl;
71
import com.iver.cit.gvsig.fmap.core.IFeature;
72
import com.iver.cit.gvsig.fmap.core.IGeometry;
73
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
74
import com.iver.cit.gvsig.fmap.core.styles.ArrowDecoratorStyle;
75
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle;
76
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle;
77
import com.iver.cit.gvsig.fmap.core.symbols.ArrowMarkerSymbol;
78
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol;
79
import com.iver.cit.gvsig.fmap.layers.FLayer;
80
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
81
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
82
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
83
import com.iver.cit.gvsig.project.documents.view.gui.View;
84
import com.iver.cit.gvsig.util.GvSession;
85
import com.iver.utiles.Utils;
86

    
87
public class ShortestPathExtension extends Extension {
88

    
89
//        public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
90
        private int idSymbolLine = -1;
91

    
92
        public void initialize() {
93
                PluginServices.getIconTheme().registerDefault(
94
                                "shortest_path",
95
                                this.getClass().getClassLoader().getResource("images/shortest_path.png")
96
                        );                
97
        }
98

    
99
        public void execute(String actionCommand) {
100
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
101
                MapControl mapCtrl = v.getMapControl();
102
                MapContext map = mapCtrl.getMapContext();
103
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
104
                while (it.hasNext())
105
                {
106
                        FLayer aux = it.next();
107
                        if (!aux.isActive())
108
                                continue;
109
                        Network net = (Network) aux.getProperty("network");
110

    
111
                        if ( net != null)
112
                        {
113
                                Route route;
114
                                try {
115
                                        RouteControlPanel controlPanel = (RouteControlPanel) GvSession.getInstance().get(mapCtrl, "RouteControlPanel");
116
                                        if (controlPanel != null) {
117
                                                boolean returnToOrigin = controlPanel.isReturnToOriginSelected(); 
118
                                                if (returnToOrigin) {
119
                                                        net.addFlag(net.getFlags()[0]);
120
                                                }
121
                                                if (controlPanel.isTspSelected()) {
122
                                                        OneToManySolver odMatrixSolver = new OneToManySolver();
123
                                                        odMatrixSolver.setNetwork(net);
124
                                                        odMatrixSolver.putDestinationsOnNetwork(net.getFlags());
125
                                                        
126
                                                        GvFlag[] flags = net.getFlags();
127
                                                        
128
                                                        double[][] odMatrix = new double[flags.length][flags.length];
129
                                                        
130
                                                        for (int i=0; i < flags.length; i++)
131
                                                        {
132
                                                                
133
                                                                odMatrixSolver.setSourceFlag(flags[i]);
134
                                                                long t1 = System.currentTimeMillis();
135
                                                                
136
                                                                odMatrixSolver.calculate();
137
                                                                long t2 = System.currentTimeMillis();
138
                                                                System.out.println("Punto " + i + " de " + flags.length + ". " + (t2-t1) + " msecs.");
139
                                                                
140
                                                                for (int j=0; j < flags.length; j++)
141
                                                                {
142
                                                                        long secs = Math.round(flags[j].getCost());
143
        //                                                                long meters = Math.round(flags[j].getAccumulatedLength());
144
        //                                                                String strAux = i + "\t" + j + "\t" + secs + "\t" + meters;
145
                                                                        odMatrix[i][j] = flags[j].getCost();
146
                                                                }
147
                                                                
148
                                                        }
149
                                                        
150
                                                        odMatrixSolver.removeDestinationsFromNetwork(net.getFlags());
151
        
152
                                                        
153
                                                        TspSolverAnnealing solverTsp = new TspSolverAnnealing();
154
                                                        solverTsp.setReturnToOrigin(returnToOrigin);
155
                                                        solverTsp.setStops(net.getFlags());
156
                                                        solverTsp.setODMatrix(odMatrix);
157
                                                        GvFlag[] orderedFlags = solverTsp.calculate();
158
                                                        ArrayList<GvFlag> orderedArray = new ArrayList<GvFlag>(orderedFlags.length);
159
                                                        Collections.addAll(orderedArray, orderedFlags);
160
                                                        net.setFlags(orderedArray);
161
                                                        // TODO: Si ya existe el flag0, no a?adirlo al final.
162
                                                        
163
                                                }
164
                                        }
165
                                        route = calculateRoute(net);
166
                                        if (route == null)
167
                                                return;
168
                                        
169
                                        List routes = (List) GvSession.getInstance().get(mapCtrl, "Route");
170
                                        if(routes == null){
171
                                                routes = new ArrayList();
172
                                                GvSession.getInstance().put(mapCtrl, "Route", routes);
173
                                        }
174
                                        
175
                                        if (route.getFeatureList().size() == 0) 
176
                                                return;
177
                                        
178
                                        routes.add(route);
179
                                        
180
                                        
181
                                        createGraphicsFrom(route.getFeatureList(), v.getMapControl());
182
                                        RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
183
                                        PluginServices.getMDIManager().addWindow(routeReport);
184
                                        List reportsPanels = (List) GvSession.getInstance().get(mapCtrl, "RouteReport");
185
                                        if(reportsPanels == null){
186
                                                reportsPanels = new ArrayList();
187
                                                GvSession.getInstance().put(mapCtrl, "RouteReport", reportsPanels);
188
                                        }        
189
                                        reportsPanels.add(routeReport);
190
                                        
191
                                        if (controlPanel != null)
192
                                                controlPanel.refresh();
193
                                        
194
                                } catch (GraphException e) {
195
                                        // TODO Auto-generated catch block
196
                                        e.printStackTrace();
197
                                }
198
                        }
199
                }
200

    
201

    
202

    
203
        }
204

    
205
        private Route calculateRoute(Network net) throws GraphException {
206
                Route route = null;
207
                ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
208
                solver.setNetwork(net);
209
//                                        solver.setFielStreetName("STREET_NAM");
210
                String fieldStreetName = (String) net.getLayer().getProperty("network_fieldStreetName");
211
                solver.setFielStreetName(fieldStreetName);
212
                route = solver.calculateRoute();
213
                if (route.getFeatureList().size() == 0)
214
                {
215
                        JOptionPane.showMessageDialog((JComponent) PluginServices.getMDIManager().getActiveWindow(),
216
                                        PluginServices.getText(this, "shortest_path_not_found"));
217
                }
218
                return route;
219
        }
220

    
221
        private void createGraphicsFrom(Collection featureList, MapControl mapControl) {
222
                Iterator it = featureList.iterator();
223
                GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer();
224
//                if (idSymbolLine == -1)
225
                {
226
                        SimpleLineSymbol arrowSymbol = new SimpleLineSymbol();
227
                        // FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
228
                        arrowSymbol.setLineWidth(3.0f);
229
                        ILineStyle lineStyle = new SimpleLineStyle();
230
                        
231
                        ArrowDecoratorStyle arrowDecoratorStyle = new ArrowDecoratorStyle();
232
                        ArrowMarkerSymbol marker = (ArrowMarkerSymbol) arrowDecoratorStyle.getMarker(); 
233
                        marker.setSize(16);
234
                        marker.setColor(Color.RED);
235
                        arrowDecoratorStyle.setArrowMarkerCount(1);
236
                        lineStyle.setArrowDecorator(arrowDecoratorStyle );
237
                        lineStyle.setLineWidth(3.0f);
238
                        arrowSymbol.setLineColor(Color.RED);
239
                        arrowSymbol.setAlpha(120);
240
                        arrowSymbol.setLineStyle(lineStyle);
241
                        idSymbolLine = graphicLayer.addSymbol(arrowSymbol);
242
                        
243
                }
244
                // Para evitar hacer reallocate de los elementos de la
245
                // graphicList cada vez, creamos primero la lista
246
                // y la insertamos toda de una vez.
247
                ArrayList graphicsRoute = new ArrayList();
248
                while (it.hasNext()) {
249
                        IFeature feat = (IFeature) it.next();
250
                        IGeometry gAux = feat.getGeometry();
251
                        FGraphic graphic = new FGraphic(gAux, idSymbolLine);
252
                        graphic.setTag("ROUTE");
253
                        graphicsRoute.add(graphic);
254
//                        graphicLayer.insertGraphic(0, graphic);
255
                }
256
                // Lo insertamos al principio de la lista para que los
257
                // pushpins se dibujen despu?s.
258

    
259
                graphicLayer.inserGraphics(0, graphicsRoute);
260
                mapControl.drawGraphics();
261

    
262
        }
263

    
264
        public boolean isEnabled() {
265
                com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
266
                 .getActiveWindow();
267
                if (f == null) {
268
                    return false;
269
                }
270
                if (f instanceof View) {
271
                    View v = (View) f;
272
                        MapContext map = v.getMapControl().getMapContext();
273
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
274
                        while (it.hasNext())
275
                        {
276
                                FLayer aux = it.next();
277
                                if (!aux.isActive())
278
                                        continue;
279
                                Network net = (Network) aux.getProperty("network");
280

    
281
                                if ( net != null)
282
                                {
283
                                        int count = 0;
284
                                        for (int i=0; i < net.getOriginaFlags().size(); i++)
285
                                        {
286
                                                GvFlag flag = (GvFlag) net.getOriginaFlags().get(i);
287
                                                if (flag.isEnabled()) count++;
288
                                        }
289
                                        if (count > 1) return true;
290
                                }
291
                        }
292
                        return false;
293

    
294
                }
295
                return false;
296
        }
297

    
298
        public boolean isVisible() {
299
                IWindow f = PluginServices.getMDIManager()
300
                 .getActiveWindow();
301
                if (f == null) {
302
                    return false;
303
                }
304
                if (f instanceof View) {
305
                        return true;
306
                }
307
                return false;
308

    
309
        }
310

    
311
}
312

    
313