svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / ShortestPathExtension.java @ 39203
History | View | Annotate | Download (9.61 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.Color; |
44 |
import java.util.ArrayList; |
45 |
import java.util.Collection; |
46 |
import java.util.Collections; |
47 |
import java.util.Iterator; |
48 |
import java.util.List; |
49 |
|
50 |
import javax.swing.JComponent; |
51 |
import javax.swing.JOptionPane; |
52 |
|
53 |
import org.gvsig.graph.core.GraphException; |
54 |
import org.gvsig.graph.core.GvFlag; |
55 |
import org.gvsig.graph.core.Network; |
56 |
import org.gvsig.graph.gui.RouteControlPanel; |
57 |
import org.gvsig.graph.gui.RouteReportPanel; |
58 |
import org.gvsig.graph.solvers.OneToManySolver; |
59 |
import org.gvsig.graph.solvers.Route; |
60 |
import org.gvsig.graph.solvers.ShortestPathSolverAStar; |
61 |
import org.gvsig.graph.solvers.TspSolverAnnealing; |
62 |
|
63 |
import com.iver.andami.PluginServices; |
64 |
import com.iver.andami.plugins.Extension; |
65 |
import com.iver.andami.ui.mdiManager.IWindow; |
66 |
import com.iver.cit.gvsig.fmap.MapContext; |
67 |
import com.iver.cit.gvsig.fmap.MapControl; |
68 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
69 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
70 |
import com.iver.cit.gvsig.fmap.core.styles.ArrowDecoratorStyle; |
71 |
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle; |
72 |
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle; |
73 |
import com.iver.cit.gvsig.fmap.core.symbols.ArrowMarkerSymbol; |
74 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
75 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
76 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
77 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
78 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
79 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
80 |
import com.iver.cit.gvsig.util.GvSession; |
81 |
|
82 |
public class ShortestPathExtension extends Extension { |
83 |
|
84 |
// public static ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
|
85 |
private int idSymbolLine = -1; |
86 |
|
87 |
public void initialize() { |
88 |
PluginServices.getIconTheme().registerDefault( |
89 |
"shortest_path",
|
90 |
this.getClass().getClassLoader().getResource("images/shortest_path.png") |
91 |
); |
92 |
} |
93 |
|
94 |
public void execute(String actionCommand) { |
95 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
96 |
MapControl mapCtrl = v.getMapControl(); |
97 |
MapContext map = mapCtrl.getMapContext(); |
98 |
SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
99 |
while (it.hasNext())
|
100 |
{ |
101 |
FLayer aux = it.next(); |
102 |
if (!aux.isActive())
|
103 |
continue;
|
104 |
Network net = (Network) aux.getProperty("network");
|
105 |
|
106 |
if ( net != null) |
107 |
{ |
108 |
Route route; |
109 |
try {
|
110 |
RouteControlPanel controlPanel = (RouteControlPanel) GvSession.getInstance().get(mapCtrl, "RouteControlPanel");
|
111 |
if (controlPanel != null) { |
112 |
boolean returnToOrigin = controlPanel.isReturnToOriginSelected();
|
113 |
if (returnToOrigin) {
|
114 |
net.addFlag(net.getFlags()[0]);
|
115 |
} |
116 |
if (controlPanel.isTspSelected()) {
|
117 |
OneToManySolver odMatrixSolver = new OneToManySolver();
|
118 |
odMatrixSolver.setNetwork(net); |
119 |
odMatrixSolver.putDestinationsOnNetwork(net.getFlags()); |
120 |
|
121 |
GvFlag[] flags = net.getFlags();
|
122 |
|
123 |
double[][] odMatrix = new double[flags.length][flags.length]; |
124 |
|
125 |
for (int i=0; i < flags.length; i++) |
126 |
{ |
127 |
|
128 |
odMatrixSolver.setSourceFlag(flags[i]); |
129 |
long t1 = System.currentTimeMillis(); |
130 |
|
131 |
odMatrixSolver.calculate(); |
132 |
long t2 = System.currentTimeMillis(); |
133 |
System.out.println("Punto " + i + " de " + flags.length + ". " + (t2-t1) + " msecs."); |
134 |
|
135 |
for (int j=0; j < flags.length; j++) |
136 |
{ |
137 |
long secs = Math.round(flags[j].getCost()); |
138 |
// long meters = Math.round(flags[j].getAccumulatedLength());
|
139 |
// String strAux = i + "\t" + j + "\t" + secs + "\t" + meters;
|
140 |
odMatrix[i][j] = flags[j].getCost(); |
141 |
} |
142 |
|
143 |
} |
144 |
|
145 |
odMatrixSolver.removeDestinationsFromNetwork(net.getFlags()); |
146 |
|
147 |
|
148 |
TspSolverAnnealing solverTsp = new TspSolverAnnealing();
|
149 |
solverTsp.setReturnToOrigin(returnToOrigin); |
150 |
solverTsp.setStops(net.getFlags()); |
151 |
solverTsp.setODMatrix(odMatrix); |
152 |
GvFlag[] orderedFlags = solverTsp.calculate();
|
153 |
ArrayList<GvFlag> orderedArray = new ArrayList<GvFlag>(orderedFlags.length); |
154 |
Collections.addAll(orderedArray, orderedFlags);
|
155 |
net.setFlags(orderedArray); |
156 |
// TODO: Si ya existe el flag0, no a?adirlo al final.
|
157 |
|
158 |
} |
159 |
} |
160 |
route = calculateRoute(net); |
161 |
if (route == null) |
162 |
return;
|
163 |
|
164 |
List routes = (List) GvSession.getInstance().get(mapCtrl, "Route"); |
165 |
if(routes == null){ |
166 |
routes = new ArrayList(); |
167 |
GvSession.getInstance().put(mapCtrl, "Route", routes);
|
168 |
} |
169 |
|
170 |
if (route.getFeatureList().size() == 0) |
171 |
return;
|
172 |
|
173 |
routes.add(route); |
174 |
|
175 |
|
176 |
createGraphicsFrom(route.getFeatureList(), v.getMapControl()); |
177 |
RouteReportPanel routeReport = new RouteReportPanel(route, v.getMapControl());
|
178 |
PluginServices.getMDIManager().addWindow(routeReport); |
179 |
List reportsPanels = (List) GvSession.getInstance().get(mapCtrl, "RouteReport"); |
180 |
if(reportsPanels == null){ |
181 |
reportsPanels = new ArrayList(); |
182 |
GvSession.getInstance().put(mapCtrl, "RouteReport", reportsPanels);
|
183 |
} |
184 |
reportsPanels.add(routeReport); |
185 |
|
186 |
if (controlPanel != null) |
187 |
controlPanel.refresh(); |
188 |
|
189 |
} catch (GraphException e) {
|
190 |
// TODO Auto-generated catch block
|
191 |
e.printStackTrace(); |
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
|
197 |
|
198 |
} |
199 |
|
200 |
private Route calculateRoute(Network net) throws GraphException { |
201 |
Route route = null;
|
202 |
ShortestPathSolverAStar solver = new ShortestPathSolverAStar();
|
203 |
solver.setNetwork(net); |
204 |
// solver.setFielStreetName("STREET_NAM");
|
205 |
String fieldStreetName = (String) net.getLayer().getProperty("network_fieldStreetName"); |
206 |
solver.setFielStreetName(fieldStreetName); |
207 |
route = solver.calculateRoute(); |
208 |
if (route.getFeatureList().size() == 0) |
209 |
{ |
210 |
JOptionPane.showMessageDialog((JComponent) PluginServices.getMDIManager().getActiveWindow(), |
211 |
PluginServices.getText(this, "shortest_path_not_found")); |
212 |
} |
213 |
return route;
|
214 |
} |
215 |
|
216 |
private void createGraphicsFrom(Collection featureList, MapControl mapControl) { |
217 |
Iterator it = featureList.iterator();
|
218 |
GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
219 |
// if (idSymbolLine == -1)
|
220 |
{ |
221 |
SimpleLineSymbol arrowSymbol = new SimpleLineSymbol();
|
222 |
// FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
|
223 |
arrowSymbol.setLineWidth(3.0f);
|
224 |
ILineStyle lineStyle = new SimpleLineStyle();
|
225 |
|
226 |
ArrowDecoratorStyle arrowDecoratorStyle = new ArrowDecoratorStyle();
|
227 |
ArrowMarkerSymbol marker = (ArrowMarkerSymbol) arrowDecoratorStyle.getMarker(); |
228 |
marker.setSize(16);
|
229 |
marker.setColor(Color.RED);
|
230 |
arrowDecoratorStyle.setArrowMarkerCount(1);
|
231 |
lineStyle.setArrowDecorator(arrowDecoratorStyle ); |
232 |
lineStyle.setLineWidth(3.0f);
|
233 |
arrowSymbol.setLineColor(Color.RED);
|
234 |
arrowSymbol.setAlpha(120);
|
235 |
arrowSymbol.setLineStyle(lineStyle); |
236 |
idSymbolLine = graphicLayer.addSymbol(arrowSymbol); |
237 |
|
238 |
} |
239 |
// Para evitar hacer reallocate de los elementos de la
|
240 |
// graphicList cada vez, creamos primero la lista
|
241 |
// y la insertamos toda de una vez.
|
242 |
ArrayList graphicsRoute = new ArrayList(); |
243 |
while (it.hasNext()) {
|
244 |
IFeature feat = (IFeature) it.next(); |
245 |
IGeometry gAux = feat.getGeometry(); |
246 |
FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
247 |
graphic.setTag("ROUTE");
|
248 |
graphicsRoute.add(graphic); |
249 |
// graphicLayer.insertGraphic(0, graphic);
|
250 |
} |
251 |
// Lo insertamos al principio de la lista para que los
|
252 |
// pushpins se dibujen despu?s.
|
253 |
|
254 |
graphicLayer.inserGraphics(0, graphicsRoute);
|
255 |
mapControl.drawGraphics(); |
256 |
|
257 |
} |
258 |
|
259 |
public boolean isEnabled() { |
260 |
com.iver.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager() |
261 |
.getActiveWindow(); |
262 |
if (f == null) { |
263 |
return false; |
264 |
} |
265 |
if (f instanceof View) { |
266 |
View v = (View) f; |
267 |
MapContext map = v.getMapControl().getMapContext(); |
268 |
SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
269 |
while (it.hasNext())
|
270 |
{ |
271 |
FLayer aux = it.next(); |
272 |
if (!aux.isActive())
|
273 |
continue;
|
274 |
Network net = (Network) aux.getProperty("network");
|
275 |
|
276 |
if ( net != null) |
277 |
{ |
278 |
int count = 0; |
279 |
for (int i=0; i < net.getOriginaFlags().size(); i++) |
280 |
{ |
281 |
GvFlag flag = (GvFlag) net.getOriginaFlags().get(i); |
282 |
if (flag.isEnabled()) count++;
|
283 |
} |
284 |
if (count > 1) return true; |
285 |
} |
286 |
} |
287 |
return false; |
288 |
|
289 |
} |
290 |
return false; |
291 |
} |
292 |
|
293 |
public boolean isVisible() { |
294 |
IWindow f = PluginServices.getMDIManager() |
295 |
.getActiveWindow(); |
296 |
if (f == null) { |
297 |
return false; |
298 |
} |
299 |
if (f instanceof View) { |
300 |
return true; |
301 |
} |
302 |
return false; |
303 |
|
304 |
} |
305 |
|
306 |
} |
307 |
|
308 |
|