Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_2_Build_1044 / prototypes / VectorialAvanzado / extensions / extGraph / src / com / iver / cit / gvsig / graph / TestExtension.java @ 20099

History | View | Annotate | Download (7.6 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.GraphException;
73
import com.iver.cit.gvsig.graph.core.GvEdge;
74
import com.iver.cit.gvsig.graph.core.GvFlag;
75
import com.iver.cit.gvsig.graph.core.Network;
76
import com.iver.cit.gvsig.graph.solvers.OneToManySolver;
77
import com.iver.cit.gvsig.graph.solvers.ShortestPathSolverDijkstra;
78
import com.iver.cit.gvsig.project.documents.view.gui.View;
79

    
80
public class TestExtension extends Extension {
81

    
82
        public static ShortestPathSolverDijkstra solver = new ShortestPathSolverDijkstra();
83
        private int idSymbolLine = -1;
84

    
85
        public void initialize() {
86
        }
87

    
88
        public void execute(String actionCommand) {
89
                
90
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
91
                MapContext map = v.getMapControl().getMapContext();
92
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
93
                
94
                if (actionCommand.equals("ODMATRIX")) {
95
                        while (it.hasNext())
96
                        {
97
                                FLayer aux = it.next();
98
                                if (!aux.isActive())
99
                                        continue;
100
                                Network net = (Network) aux.getProperty("network");
101

    
102
                                if ( net != null)
103
                                {
104
                                        GvFlag[] flags = net.getFlags();
105
                                        if(flags.length == 0)
106
                                        {
107
                                                JOptionPane.showMessageDialog(null, "Primero carga las paradas.");
108
                                                return;
109
                                        }
110
                                        setVelocities(net);
111
//                                        PluginServices.getMDIManager().addWindow(new RouteControlPanel(net));
112
                                        JFileChooser dlg = new JFileChooser();
113
                                        if (dlg.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
114
                                        {
115
//                                                RandomAccessFile file = null;
116
//                                                try {
117
//                                                        file = new RandomAccessFile(dlg.getSelectedFile(), "rw");
118
//                                                } catch (FileNotFoundException e1) {
119
//                                                        // TODO Auto-generated catch block
120
//                                                        e1.printStackTrace();
121
//                                                }
122
                                                BufferedWriter output;
123
                                                try {
124
                                                        output = new BufferedWriter(new FileWriter(dlg.getSelectedFile()));
125
        //                                                output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
126
        
127
                                                        OneToManySolver solver = new OneToManySolver();
128
                                                        solver.setNetwork(net);
129
                                                        solver.putDestinationsOnNetwork();
130
                                                        for (int i=0; i < flags.length; i++)
131
                                                        {
132
                                                                
133
                                                                solver.setSourceFlag(flags[i]);
134
                                                                long t1 = System.currentTimeMillis();
135
                                                                
136
                                                                solver.calculate();
137
                                                                long t2 = System.currentTimeMillis();
138
                                                                System.out.println("Punto " + i + " de " + flags.length + ". " + (t2-t1) + " msecs.");
139
                                                                // Escribe el resultado
140
                                                                // idNodo1 idNodo2 tiempo distancia
141
                                                                
142
                                                                for (int j=0; j < flags.length; j++)
143
                                                                {
144
                                                                        long secs = Math.round(flags[j].getCost());
145
                                                                        long meters = Math.round(flags[j].getAccumulatedLength());
146
                                                                        String strAux = i + "\t" + j + "\t" + secs + "\t" + meters;
147
                                                                        output.write(strAux);
148
                                                                        output.newLine();
149
                                                                        
150
                                                                }
151
                                                                long t3 = System.currentTimeMillis();
152
                                                                System.out.println("T. de escritura: " + (t3-t2) + " msecs.");
153
                                                                
154
                                                        }
155
                                                        solver.removeDestinationsFromNetwork();
156
                                                        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
                                        return;
168
                                }
169
                        }
170
                }
171
                
172

    
173
        }
174

    
175
        /**
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
                }
186
                
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
        }
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
                        Color color = new Color(0.9f, 0.0f, 0.0f, 0.3f);
202
                        FSymbol lineSymbol = new FSymbol(FShape.LINE, color);
203
                        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
                        graphicLayer.insertGraphic(0, graphic);
211
                }
212
                mapControl.drawGraphics();
213

    
214
        }
215

    
216
        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
                lineSymbol.setStroke(new BasicStroke(7.0f));
221
                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
                        graphicLayer.insertGraphic(0, graphic);
229
                }
230
                mapControl.drawGraphics();
231

    
232
        }
233

    
234

    
235
        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
                return true;
243
        }
244

    
245
}
246

    
247