Statistics
| Revision:

root / branches / v10 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / ODMatrixExtension.java @ 13000

History | View | Annotate | Download (5.48 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.Component;
44
import java.io.BufferedWriter;
45
import java.io.FileWriter;
46
import java.io.IOException;
47

    
48
import javax.swing.JFileChooser;
49
import javax.swing.JOptionPane;
50

    
51
import com.iver.andami.PluginServices;
52
import com.iver.andami.plugins.Extension;
53
import com.iver.andami.ui.mdiManager.IWindow;
54
import com.iver.cit.gvsig.fmap.MapContext;
55
import com.iver.cit.gvsig.fmap.MapControl;
56
import com.iver.cit.gvsig.fmap.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
58
import com.iver.cit.gvsig.graph.core.GvFlag;
59
import com.iver.cit.gvsig.graph.core.Network;
60
import com.iver.cit.gvsig.graph.solvers.OneToManySolver;
61
import com.iver.cit.gvsig.project.documents.view.gui.View;
62

    
63
public class ODMatrixExtension extends Extension {
64

    
65

    
66

    
67
        public void initialize() {
68
        }
69

    
70
        public void execute(String actionCommand) {
71
                
72
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
73
                MapContext map = v.getMapControl().getMapContext();
74
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
75
                
76
                if (actionCommand.equals("ODMATRIX")) {
77
                        while (it.hasNext())
78
                        {
79
                                FLayer aux = it.next();
80
                                if (!aux.isActive())
81
                                        continue;
82
                                Network net = (Network) aux.getProperty("network");
83

    
84
                                if ( net != null)
85
                                {
86
                                        GvFlag[] flags = net.getFlags();
87
                                        if(flags.length == 0)
88
                                        {
89
                                                JOptionPane.showMessageDialog(null, "Primero carga las paradas.");
90
                                                return;
91
                                        }
92

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

    
157
        }
158

    
159
        
160
        public boolean isEnabled() {
161
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
162
                if (window instanceof View)
163
                {
164
                        View v = (View) window;
165
                MapControl mapCtrl = v.getMapControl();
166
                        MapContext map = mapCtrl.getMapContext();
167
                        
168
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
169
                        while (it.hasNext())
170
                        {
171
                                FLayer aux = it.next();
172
                                if (!aux.isActive())
173
                                        continue;
174
                                Network net = (Network) aux.getProperty("network");
175
                                
176
                                if ( net != null)
177
                                {
178
                                        return true;
179
                                }
180
                        }
181
                }
182
                return false;
183
        }
184

    
185
        public boolean isVisible() {
186
                IWindow f = PluginServices.getMDIManager()
187
                 .getActiveWindow();
188
                if (f == null) {
189
                    return false;
190
                }
191
                if (f instanceof View) {
192
                        return true;
193
                }
194
                return false;
195

    
196
        }
197

    
198

    
199
}
200

    
201