svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / ShortestPathExtension.java @ 29994
History | View | Annotate | Download (9.79 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 | 22182 | fpenarrubia | package org.gvsig.graph; |
42 | 8063 | fjp | |
43 | import java.awt.BasicStroke; |
||
44 | import java.awt.Color; |
||
45 | 8659 | azabala | import java.util.ArrayList; |
46 | 23262 | fpenarrubia | import java.util.Arrays; |
47 | 8063 | fjp | import java.util.Collection; |
48 | 23262 | fpenarrubia | import java.util.Collections; |
49 | 8063 | fjp | import java.util.Iterator; |
50 | 8659 | azabala | import java.util.List; |
51 | 8063 | fjp | |
52 | 8637 | fjp | import javax.swing.JComponent; |
53 | import javax.swing.JOptionPane; |
||
54 | |||
55 | 22182 | fpenarrubia | import org.gvsig.graph.core.GraphException; |
56 | import org.gvsig.graph.core.GvFlag; |
||
57 | import org.gvsig.graph.core.Network; |
||
58 | 23262 | fpenarrubia | import org.gvsig.graph.core.NetworkUtils; |
59 | 22182 | fpenarrubia | import org.gvsig.graph.gui.RouteControlPanel; |
60 | import org.gvsig.graph.gui.RouteReportPanel; |
||
61 | 23262 | fpenarrubia | import org.gvsig.graph.solvers.OneToManySolver; |
62 | 22182 | fpenarrubia | import org.gvsig.graph.solvers.Route; |
63 | import org.gvsig.graph.solvers.ShortestPathSolverAStar; |
||
64 | 23262 | fpenarrubia | import org.gvsig.graph.solvers.TspSolverAnnealing; |
65 | 22182 | fpenarrubia | |
66 | 8063 | fjp | import com.iver.andami.PluginServices; |
67 | import com.iver.andami.plugins.Extension; |
||
68 | 8567 | fjp | import com.iver.andami.ui.mdiManager.IWindow; |
69 | 8063 | fjp | 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 | 13620 | fjp | 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 | 25339 | fpenarrubia | import com.iver.cit.gvsig.fmap.core.symbols.ArrowMarkerSymbol; |
78 | 13620 | fjp | import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
79 | 8063 | fjp | 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 | 8573 | azabala | import com.iver.cit.gvsig.util.GvSession; |
85 | 23262 | fpenarrubia | import com.iver.utiles.Utils; |
86 | 8063 | fjp | |
87 | public class ShortestPathExtension extends Extension { |
||
88 | |||
89 | 28552 | fpenarrubia | // public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
|
90 | 8063 | fjp | private int idSymbolLine = -1; |
91 | 8188 | jaume | |
92 | 8063 | fjp | public void initialize() { |
93 | 15909 | fpenarrubia | PluginServices.getIconTheme().registerDefault( |
94 | "shortest_path",
|
||
95 | this.getClass().getClassLoader().getResource("images/shortest_path.png") |
||
96 | ); |
||
97 | 8063 | fjp | } |
98 | |||
99 | public void execute(String actionCommand) { |
||
100 | View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
||
101 | 8573 | azabala | MapControl mapCtrl = v.getMapControl(); |
102 | MapContext map = mapCtrl.getMapContext(); |
||
103 | 8063 | fjp | SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
104 | while (it.hasNext())
|
||
105 | { |
||
106 | FLayer aux = it.next(); |
||
107 | 8123 | fjp | if (!aux.isActive())
|
108 | continue;
|
||
109 | 8063 | fjp | Network net = (Network) aux.getProperty("network");
|
110 | 8188 | jaume | |
111 | 8063 | fjp | if ( net != null) |
112 | { |
||
113 | Route route; |
||
114 | 8188 | jaume | try {
|
115 | 23262 | fpenarrubia | 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 | 24380 | fpenarrubia | odMatrixSolver.putDestinationsOnNetwork(net.getFlags()); |
125 | 23262 | fpenarrubia | |
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 | 24380 | fpenarrubia | odMatrixSolver.removeDestinationsFromNetwork(net.getFlags()); |
151 | 23262 | fpenarrubia | |
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 | 8637 | fjp | return;
|
168 | 23262 | fpenarrubia | |
169 | 8659 | azabala | 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 | 25339 | fpenarrubia | |
175 | if (route.getFeatureList().size() == 0) |
||
176 | return;
|
||
177 | |||
178 | 8659 | azabala | routes.add(route); |
179 | 8340 | azabala | |
180 | 25339 | fpenarrubia | |
181 | 8063 | fjp | createGraphicsFrom(route.getFeatureList(), v.getMapControl()); |
182 | 8263 | azabala | RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
|
183 | PluginServices.getMDIManager().addWindow(routeReport); |
||
184 | 8659 | azabala | 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 | 23262 | fpenarrubia | |
191 | 8762 | fjp | if (controlPanel != null) |
192 | controlPanel.refresh(); |
||
193 | 8263 | azabala | |
194 | 8063 | fjp | } catch (GraphException e) {
|
195 | // TODO Auto-generated catch block
|
||
196 | e.printStackTrace(); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | 8188 | jaume | |
201 | |||
202 | |||
203 | 8063 | fjp | } |
204 | |||
205 | 23262 | fpenarrubia | private Route calculateRoute(Network net) throws GraphException { |
206 | Route route = null;
|
||
207 | 28552 | fpenarrubia | ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
|
208 | 23262 | fpenarrubia | solver.setNetwork(net); |
209 | // solver.setFielStreetName("STREET_NAM");
|
||
210 | 28552 | fpenarrubia | String fieldStreetName = (String) net.getLayer().getProperty("network_fieldStreetName"); |
211 | solver.setFielStreetName(fieldStreetName); |
||
212 | 23262 | fpenarrubia | 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 | 8063 | fjp | private void createGraphicsFrom(Collection featureList, MapControl mapControl) { |
222 | Iterator it = featureList.iterator();
|
||
223 | GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
||
224 | 8100 | fjp | // if (idSymbolLine == -1)
|
225 | 8063 | fjp | { |
226 | 13620 | fjp | SimpleLineSymbol arrowSymbol = new SimpleLineSymbol();
|
227 | 8357 | fjp | // FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
|
228 | 13620 | fjp | arrowSymbol.setLineWidth(3.0f);
|
229 | ILineStyle lineStyle = new SimpleLineStyle();
|
||
230 | |||
231 | ArrowDecoratorStyle arrowDecoratorStyle = new ArrowDecoratorStyle();
|
||
232 | 25339 | fpenarrubia | ArrowMarkerSymbol marker = (ArrowMarkerSymbol) arrowDecoratorStyle.getMarker(); |
233 | marker.setSize(16);
|
||
234 | marker.setColor(Color.RED);
|
||
235 | 13620 | fjp | arrowDecoratorStyle.setArrowMarkerCount(1);
|
236 | lineStyle.setArrowDecorator(arrowDecoratorStyle ); |
||
237 | 15971 | fpenarrubia | lineStyle.setLineWidth(3.0f);
|
238 | arrowSymbol.setLineColor(Color.RED);
|
||
239 | 25339 | fpenarrubia | arrowSymbol.setAlpha(120);
|
240 | 13620 | fjp | arrowSymbol.setLineStyle(lineStyle); |
241 | 8340 | azabala | idSymbolLine = graphicLayer.addSymbol(arrowSymbol); |
242 | |||
243 | 8063 | fjp | } |
244 | 8762 | fjp | // 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 | 8063 | fjp | while (it.hasNext()) {
|
249 | IFeature feat = (IFeature) it.next(); |
||
250 | IGeometry gAux = feat.getGeometry(); |
||
251 | FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
||
252 | 8567 | fjp | graphic.setTag("ROUTE");
|
253 | 8762 | fjp | graphicsRoute.add(graphic); |
254 | // graphicLayer.insertGraphic(0, graphic);
|
||
255 | 8063 | fjp | } |
256 | 8762 | fjp | // Lo insertamos al principio de la lista para que los
|
257 | // pushpins se dibujen despu?s.
|
||
258 | |||
259 | graphicLayer.inserGraphics(0, graphicsRoute);
|
||
260 | 8063 | fjp | mapControl.drawGraphics(); |
261 | 8188 | jaume | |
262 | 8063 | fjp | } |
263 | |||
264 | public boolean isEnabled() { |
||
265 | 8349 | azabala | 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 | 8639 | fjp | 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 | 8349 | azabala | } |
291 | } |
||
292 | return false; |
||
293 | |||
294 | } |
||
295 | return false; |
||
296 | 8063 | fjp | } |
297 | |||
298 | public boolean isVisible() { |
||
299 | 8567 | fjp | 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 | 8063 | fjp | } |
310 | |||
311 | } |
||
312 |