svn-gvsig-desktop / tags / PilotoRedes_Build_4 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / TestExtension.java @ 12191
History | View | Annotate | Download (7.62 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 | package com.iver.cit.gvsig.graph; |
||
42 | |||
43 | import java.awt.BasicStroke; |
||
44 | import java.awt.Color; |
||
45 | 11631 | fjp | 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 | 8063 | fjp | import java.util.Collection; |
51 | import java.util.Iterator; |
||
52 | 8133 | fjp | import java.util.ListIterator; |
53 | 8063 | fjp | |
54 | 11631 | fjp | import javax.swing.JFileChooser; |
55 | import javax.swing.JOptionPane; |
||
56 | |||
57 | 8063 | fjp | import com.iver.andami.PluginServices; |
58 | import com.iver.andami.plugins.Extension; |
||
59 | 8133 | fjp | import com.iver.cit.gvsig.fmap.DriverException; |
60 | 8063 | fjp | 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 | 8133 | fjp | import com.iver.cit.gvsig.fmap.layers.VectorialAdapter; |
70 | 8063 | fjp | import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
71 | 8133 | fjp | import com.iver.cit.gvsig.graph.core.FEdge; |
72 | 11631 | fjp | import com.iver.cit.gvsig.graph.core.GvEdge; |
73 | 8513 | azabala | import com.iver.cit.gvsig.graph.core.GvFlag; |
74 | 8063 | fjp | import com.iver.cit.gvsig.graph.core.Network; |
75 | 11631 | fjp | import com.iver.cit.gvsig.graph.solvers.OneToManySolver; |
76 | 8499 | fjp | import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverDijkstra; |
77 | 8063 | fjp | import com.iver.cit.gvsig.project.documents.view.gui.View; |
78 | |||
79 | public class TestExtension extends Extension { |
||
80 | |||
81 | 8499 | fjp | public static ShortestPathSolverDijkstra solver = new ShortestPathSolverDijkstra(); |
82 | 8063 | fjp | private int idSymbolLine = -1; |
83 | 8188 | jaume | |
84 | 8063 | fjp | public void initialize() { |
85 | } |
||
86 | |||
87 | public void execute(String actionCommand) { |
||
88 | 8513 | azabala | |
89 | 8063 | fjp | View v = (View) PluginServices.getMDIManager().getActiveWindow(); |
90 | MapContext map = v.getMapControl().getMapContext(); |
||
91 | SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
|
||
92 | 8513 | azabala | |
93 | 11631 | fjp | if (actionCommand.equals("ODMATRIX")) { |
94 | 8513 | azabala | 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 | 11631 | fjp | 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 | 12157 | fjp | solver.putDestinationsOnNetwork(); |
129 | 11631 | fjp | for (int i=0; i < flags.length; i++) |
130 | { |
||
131 | |||
132 | solver.setSourceFlag(flags[i]); |
||
133 | long t1 = System.currentTimeMillis(); |
||
134 | 12157 | fjp | |
135 | 11631 | fjp | solver.calculate(); |
136 | long t2 = System.currentTimeMillis(); |
||
137 | System.out.println("Punto " + i + " de " + flags.length + ". " + (t2-t1) + " msecs."); |
||
138 | // Escribe el resultado
|
||
139 | // idNodo1 idNodo2 tiempo distancia
|
||
140 | |||
141 | for (int j=0; j < flags.length; j++) |
||
142 | { |
||
143 | // String aux2 = NumberFormat.getInstance().format(flags[j].getCost());
|
||
144 | 12157 | fjp | // long secs = Math.round(flags[j].getCost());
|
145 | 11631 | fjp | long meters = Math.round(flags[j].getAccumulatedLength()); |
146 | 12157 | fjp | String strAux = i + "\t" + j + "\t" + meters; |
147 | 11631 | fjp | output.write(strAux); |
148 | output.newLine(); |
||
149 | |||
150 | } |
||
151 | 12157 | fjp | long t3 = System.currentTimeMillis(); |
152 | System.out.println("T. de escritura: " + (t3-t2) + " msecs."); |
||
153 | |||
154 | 11631 | fjp | } |
155 | 12157 | fjp | solver.removeDestinationsFromNetwork(); |
156 | 11631 | fjp | output.flush(); |
157 | output.close(); |
||
158 | } catch (IOException e) { |
||
159 | // TODO Auto-generated catch block
|
||
160 | e.printStackTrace(); |
||
161 | } catch (GraphException e) {
|
||
162 | // TODO Auto-generated catch block
|
||
163 | e.printStackTrace(); |
||
164 | } |
||
165 | } // if
|
||
166 | |||
167 | 8513 | azabala | return;
|
168 | } |
||
169 | } |
||
170 | } |
||
171 | |||
172 | 8188 | jaume | |
173 | 11631 | fjp | } |
174 | 8188 | jaume | |
175 | 11631 | fjp | /**
|
176 | * Asignamos el coste en funci?n de un array de velocidades y el tipo de via. Las velocidades, en metros/seg
|
||
177 | * @param net
|
||
178 | */
|
||
179 | private void setVelocities(Network net) { |
||
180 | double[] veloKm = {120, 110, 90, 80, 70, 60, 50, 40}; |
||
181 | ArrayList veloMeters = new ArrayList(); |
||
182 | for (int i=0; i<veloKm.length; i++) |
||
183 | { |
||
184 | veloMeters.add(new Double(veloKm[i] / 3.6)); |
||
185 | 8063 | fjp | } |
186 | 11631 | fjp | |
187 | for (int i=0; i < net.getGraph().numEdges(); i++) |
||
188 | { |
||
189 | GvEdge edge = net.getGraph().getEdgeByID(i); |
||
190 | Double vel = (Double) veloMeters.get(edge.getType()); |
||
191 | edge.setWeight(edge.getDistance() / vel.doubleValue()); // segundos
|
||
192 | } |
||
193 | |||
194 | 8063 | fjp | } |
195 | |||
196 | private void createGraphicsFrom(Collection featureList, MapControl mapControl) { |
||
197 | Iterator it = featureList.iterator();
|
||
198 | GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
||
199 | if (idSymbolLine == -1) |
||
200 | { |
||
201 | 8133 | fjp | Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f); |
202 | FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
||
203 | 8063 | fjp | lineSymbol.setStroke(new BasicStroke(3.0f)); |
204 | idSymbolLine = graphicLayer.addSymbol(lineSymbol); |
||
205 | } |
||
206 | while (it.hasNext()) {
|
||
207 | IFeature feat = (IFeature) it.next(); |
||
208 | IGeometry gAux = feat.getGeometry(); |
||
209 | FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
||
210 | 8207 | fjp | graphicLayer.insertGraphic(0, graphic);
|
211 | 8063 | fjp | } |
212 | mapControl.drawGraphics(); |
||
213 | 8188 | jaume | |
214 | 8063 | fjp | } |
215 | 8188 | jaume | |
216 | 8133 | fjp | private void createGraphicsFrom(ListIterator it, MapControl mapControl) throws DriverException { |
217 | GraphicLayer graphicLayer = mapControl.getMapContext().getGraphicsLayer(); |
||
218 | Color color = new Color(0.5f, 0.8f, 0.0f, 0.5f); |
||
219 | FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
|
||
220 | 8162 | fjp | lineSymbol.setStroke(new BasicStroke(7.0f)); |
221 | 8133 | fjp | int idSymbol = graphicLayer.addSymbol(lineSymbol);
|
222 | VectorialAdapter va = (VectorialAdapter) solver.getNetwork().getLayer().getSource(); |
||
223 | while (it.hasNext()) {
|
||
224 | FEdge edge = (FEdge) it.next(); |
||
225 | IFeature feat = va.getFeature(edge.getArcID()); |
||
226 | IGeometry gAux = feat.getGeometry(); |
||
227 | FGraphic graphic = new FGraphic(gAux, idSymbol);
|
||
228 | 8207 | fjp | graphicLayer.insertGraphic(0, graphic);
|
229 | 8133 | fjp | } |
230 | mapControl.drawGraphics(); |
||
231 | 8188 | jaume | |
232 | 8133 | fjp | } |
233 | 8063 | fjp | |
234 | 8188 | jaume | |
235 | 8063 | fjp | public boolean isEnabled() { |
236 | // TODO Auto-generated method stub
|
||
237 | return true; |
||
238 | } |
||
239 | |||
240 | public boolean isVisible() { |
||
241 | // TODO Auto-generated method stub
|
||
242 | 11631 | fjp | return true; |
243 | 8063 | fjp | } |
244 | |||
245 | } |
||
246 |