svn-gvsig-desktop / tags / PilotoRedes_Build_3 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TestExtension.java @ 11678
History | View | Annotate | Download (7.41 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.Component; |
46 |
import java.io.BufferedWriter; |
47 |
import java.io.FileWriter; |
48 |
import java.io.IOException; |
49 |
import java.util.ArrayList; |
50 |
import java.util.Collection; |
51 |
import java.util.Iterator; |
52 |
import java.util.ListIterator; |
53 |
|
54 |
import javax.swing.JFileChooser; |
55 |
import javax.swing.JOptionPane; |
56 |
|
57 |
import com.iver.andami.PluginServices; |
58 |
import com.iver.andami.plugins.Extension; |
59 |
import com.iver.cit.gvsig.fmap.DriverException; |
60 |
import com.iver.cit.gvsig.fmap.MapContext; |
61 |
import com.iver.cit.gvsig.fmap.MapControl; |
62 |
import com.iver.cit.gvsig.fmap.core.FShape; |
63 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
64 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
65 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
66 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
67 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
68 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
69 |
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter; |
70 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
71 |
import com.iver.cit.gvsig.graph.core.FEdge; |
72 |
import com.iver.cit.gvsig.graph.core.GvEdge; |
73 |
import com.iver.cit.gvsig.graph.core.GvFlag; |
74 |
import com.iver.cit.gvsig.graph.core.Network; |
75 |
import com.iver.cit.gvsig.graph.solvers.OneToManySolver; |
76 |
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverDijkstra; |
77 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
78 |
|
79 |
public class TestExtension extends Extension { |
80 |
|
81 |
public static ShortestPathSolverDijkstra solver = new ShortestPathSolverDijkstra(); |
82 |
private int idSymbolLine = -1; |
83 |
|
84 |
public void initialize() { |
85 |
} |
86 |
|
87 |
public void execute(String actionCommand) { |
88 |
|
89 |
View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
90 |
MapContext map = v.getMapControl().getMapContext(); |
91 |
SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
92 |
|
93 |
if (actionCommand.equals("ODMATRIX")) { |
94 |
while (it.hasNext())
|
95 |
{ |
96 |
FLayer aux = it.next(); |
97 |
if (!aux.isActive())
|
98 |
continue;
|
99 |
Network net = (Network) aux.getProperty("network");
|
100 |
|
101 |
if ( net != null) |
102 |
{ |
103 |
GvFlag[] flags = net.getFlags();
|
104 |
if(flags.length == 0) |
105 |
{ |
106 |
JOptionPane.showMessageDialog(null, "Primero carga las paradas."); |
107 |
return;
|
108 |
} |
109 |
setVelocities(net); |
110 |
// PluginServices.getMDIManager().addWindow(new RouteControlPanel(net));
|
111 |
JFileChooser dlg = new JFileChooser(); |
112 |
if (dlg.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) |
113 |
{ |
114 |
// RandomAccessFile file = null;
|
115 |
// try {
|
116 |
// file = new RandomAccessFile(dlg.getSelectedFile(), "rw");
|
117 |
// } catch (FileNotFoundException e1) {
|
118 |
// // TODO Auto-generated catch block
|
119 |
// e1.printStackTrace();
|
120 |
// }
|
121 |
BufferedWriter output;
|
122 |
try {
|
123 |
output = new BufferedWriter(new FileWriter(dlg.getSelectedFile())); |
124 |
// output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
|
125 |
|
126 |
OneToManySolver solver = new OneToManySolver();
|
127 |
solver.setNetwork(net); |
128 |
for (int i=0; i < flags.length; i++) |
129 |
{ |
130 |
|
131 |
solver.setSourceFlag(flags[i]); |
132 |
long t1 = System.currentTimeMillis(); |
133 |
solver.calculate(); |
134 |
long t2 = System.currentTimeMillis(); |
135 |
System.out.println("Punto " + i + " de " + flags.length + ". " + (t2-t1) + " msecs."); |
136 |
// Escribe el resultado
|
137 |
// idNodo1 idNodo2 tiempo distancia
|
138 |
|
139 |
for (int j=0; j < flags.length; j++) |
140 |
{ |
141 |
// String aux2 = NumberFormat.getInstance().format(flags[j].getCost());
|
142 |
long secs = Math.round(flags[j].getCost()); |
143 |
long meters = Math.round(flags[j].getAccumulatedLength()); |
144 |
String strAux = i + " " + j + " " + secs + " " + meters; |
145 |
output.write(strAux); |
146 |
output.newLine(); |
147 |
|
148 |
} |
149 |
} |
150 |
output.flush(); |
151 |
output.close(); |
152 |
} catch (IOException e) { |
153 |
// TODO Auto-generated catch block
|
154 |
e.printStackTrace(); |
155 |
} catch (GraphException e) {
|
156 |
// TODO Auto-generated catch block
|
157 |
e.printStackTrace(); |
158 |
} |
159 |
} // if
|
160 |
|
161 |
return;
|
162 |
} |
163 |
} |
164 |
} |
165 |
|
166 |
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Asignamos el coste en funci?n de un array de velocidades y el tipo de via. Las velocidades, en metros/seg
|
171 |
* @param net
|
172 |
*/
|
173 |
private void setVelocities(Network net) { |
174 |
double[] veloKm = {120, 110, 90, 80, 70, 60, 50, 40}; |
175 |
ArrayList veloMeters = new ArrayList(); |
176 |
for (int i=0; i<veloKm.length; i++) |
177 |
{ |
178 |
veloMeters.add(new Double(veloKm[i] / 3.6)); |
179 |
} |
180 |
|
181 |
for (int i=0; i < net.getGraph().numEdges(); i++) |
182 |
{ |
183 |
GvEdge edge = net.getGraph().getEdgeByID(i); |
184 |
Double vel = (Double) veloMeters.get(edge.getType()); |
185 |
edge.setWeight(edge.getDistance() / vel.doubleValue()); // segundos
|
186 |
} |
187 |
|
188 |
} |
189 |
|
190 |
private void createGraphicsFrom(Collection featureList, MapControl mapControl) { |
191 |
Iterator it = featureList.iterator();
|
192 |
GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
193 |
if (idSymbolLine == -1) |
194 |
{ |
195 |
Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f); |
196 |
FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
197 |
lineSymbol.setStroke(new BasicStroke(3.0f)); |
198 |
idSymbolLine = graphicLayer.addSymbol(lineSymbol); |
199 |
} |
200 |
while (it.hasNext()) {
|
201 |
IFeature feat = (IFeature) it.next(); |
202 |
IGeometry gAux = feat.getGeometry(); |
203 |
FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
204 |
graphicLayer.insertGraphic(0, graphic);
|
205 |
} |
206 |
mapControl.drawGraphics(); |
207 |
|
208 |
} |
209 |
|
210 |
private void createGraphicsFrom(ListIterator it, MapControl mapControl) throws DriverException { |
211 |
GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
212 |
Color color = new Color(0.5f, 0.8f, 0.0f, 0.5f); |
213 |
FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
214 |
lineSymbol.setStroke(new BasicStroke(7.0f)); |
215 |
int idSymbol = graphicLayer.addSymbol(lineSymbol);
|
216 |
VectorialAdapter va = (VectorialAdapter) solver.getNetwork().getLayer().getSource(); |
217 |
while (it.hasNext()) {
|
218 |
FEdge edge = (FEdge) it.next(); |
219 |
IFeature feat = va.getFeature(edge.getArcID()); |
220 |
IGeometry gAux = feat.getGeometry(); |
221 |
FGraphic graphic = new FGraphic(gAux, idSymbol);
|
222 |
graphicLayer.insertGraphic(0, graphic);
|
223 |
} |
224 |
mapControl.drawGraphics(); |
225 |
|
226 |
} |
227 |
|
228 |
|
229 |
public boolean isEnabled() { |
230 |
// TODO Auto-generated method stub
|
231 |
return true; |
232 |
} |
233 |
|
234 |
public boolean isVisible() { |
235 |
// TODO Auto-generated method stub
|
236 |
return true; |
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
|