svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TestExtension.java @ 8207
History | View | Annotate | Download (6.63 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.IGraph; |
71 |
import com.iver.cit.gvsig.graph.core.JungGraph; |
72 |
import com.iver.cit.gvsig.graph.core.Network; |
73 |
import com.iver.cit.gvsig.graph.core.NetworkRedLoader; |
74 |
import com.iver.cit.gvsig.graph.gui.DlgDirections; |
75 |
import com.iver.cit.gvsig.graph.solvers.Route; |
76 |
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolver; |
77 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
78 |
|
79 |
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath; |
80 |
|
81 |
public class TestExtension extends Extension { |
82 |
|
83 |
public static ShortestPathSolver solver = new ShortestPathSolver(); |
84 |
private int idSymbolLine = -1; |
85 |
|
86 |
public void initialize() { |
87 |
} |
88 |
|
89 |
public void execute(String actionCommand) { |
90 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
91 |
MapContext map = v.getMapControl().getMapContext(); |
92 |
SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
93 |
while (it.hasNext())
|
94 |
{ |
95 |
FLayer aux = it.next(); |
96 |
if (!aux.isActive())
|
97 |
continue;
|
98 |
Network net = new Network();
|
99 |
net.setLayer((FLyrVect) aux); |
100 |
NetworkRedLoader netLoader = new NetworkRedLoader();
|
101 |
|
102 |
netLoader.setNetFile(new File("c:/ejes.red")); |
103 |
|
104 |
IGraph g = netLoader.loadNetwork(); |
105 |
|
106 |
net.setGraph(g); |
107 |
|
108 |
// Primer punto
|
109 |
net.addFlag(433647.09375, 4488029, 10); |
110 |
|
111 |
// Segundo punto
|
112 |
net.addFlag(437290.96875, 4481547, 10); |
113 |
|
114 |
|
115 |
|
116 |
Route route; |
117 |
try {
|
118 |
solver.setNetwork(net); |
119 |
solver.setFielStreetName("Nombre");
|
120 |
route = solver.calculateRoute(); |
121 |
DlgDirections dlg = new DlgDirections((Frame) PluginServices.getMainFrame(), false); |
122 |
dlg.setModel(route.getFeatureList()); |
123 |
|
124 |
createGraphicsFrom(route.getFeatureList(), v.getMapControl()); |
125 |
|
126 |
dlg.setVisible(true);
|
127 |
System.out.println("distancia fjp:" + route.getCost()); |
128 |
|
129 |
// PROBAMOS JUNG
|
130 |
JungGraph jG = new JungGraph(net.getGraph());
|
131 |
|
132 |
// Probamos la algoritmia: distancia entre nodo 1 y nodo 1000
|
133 |
DijkstraShortestPath distCalculator = new DijkstraShortestPath(jG, new EdgeWeightLabeller()); |
134 |
FNode vOrig = jG.getVertexByID(1);
|
135 |
FNode vEnd = jG.getVertexByID(1000);
|
136 |
long t1 = System.currentTimeMillis(); |
137 |
Number dist = distCalculator.getDistance(vOrig, vEnd);
|
138 |
long t2 = System.currentTimeMillis(); |
139 |
|
140 |
// assertEquals(dist.doubleValue(), 8887, 0);
|
141 |
|
142 |
System.out.println("dist =" + dist + " meters. msecs: " + (t2-t1)); |
143 |
|
144 |
List path = distCalculator.getPath(vOrig, vEnd);
|
145 |
createGraphicsFrom(path.listIterator(), v.getMapControl()); |
146 |
|
147 |
System.out.println(vOrig + " - " + vEnd); |
148 |
System.out.println(vOrig.getX() + ", " + vOrig.getY() |
149 |
+ " - " + vEnd.getX() + ", " + vEnd.getY()); |
150 |
|
151 |
|
152 |
} catch (GraphException e) {
|
153 |
// TODO Auto-generated catch block
|
154 |
e.printStackTrace(); |
155 |
} catch (DriverException e) {
|
156 |
// TODO Auto-generated catch block
|
157 |
e.printStackTrace(); |
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
|
163 |
} |
164 |
|
165 |
private void createGraphicsFrom(Collection featureList, MapControl mapControl) { |
166 |
Iterator it = featureList.iterator();
|
167 |
GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
168 |
if (idSymbolLine == -1) |
169 |
{ |
170 |
Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f); |
171 |
FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
172 |
lineSymbol.setStroke(new BasicStroke(3.0f)); |
173 |
idSymbolLine = graphicLayer.addSymbol(lineSymbol); |
174 |
} |
175 |
while (it.hasNext()) {
|
176 |
IFeature feat = (IFeature) it.next(); |
177 |
IGeometry gAux = feat.getGeometry(); |
178 |
FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
179 |
graphicLayer.insertGraphic(0, graphic);
|
180 |
} |
181 |
mapControl.drawGraphics(); |
182 |
|
183 |
} |
184 |
|
185 |
private void createGraphicsFrom(ListIterator it, MapControl mapControl) throws DriverException { |
186 |
GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
187 |
Color color = new Color(0.5f, 0.8f, 0.0f, 0.5f); |
188 |
FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
189 |
lineSymbol.setStroke(new BasicStroke(7.0f)); |
190 |
int idSymbol = graphicLayer.addSymbol(lineSymbol);
|
191 |
VectorialAdapter va = (VectorialAdapter) solver.getNetwork().getLayer().getSource(); |
192 |
while (it.hasNext()) {
|
193 |
FEdge edge = (FEdge) it.next(); |
194 |
IFeature feat = va.getFeature(edge.getArcID()); |
195 |
IGeometry gAux = feat.getGeometry(); |
196 |
FGraphic graphic = new FGraphic(gAux, idSymbol);
|
197 |
graphicLayer.insertGraphic(0, graphic);
|
198 |
} |
199 |
mapControl.drawGraphics(); |
200 |
|
201 |
} |
202 |
|
203 |
|
204 |
public boolean isEnabled() { |
205 |
// TODO Auto-generated method stub
|
206 |
return true; |
207 |
} |
208 |
|
209 |
public boolean isVisible() { |
210 |
// TODO Auto-generated method stub
|
211 |
return true; |
212 |
} |
213 |
|
214 |
} |
215 |
|
216 |
|