Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / MinimumSpanningTreeExtension.java @ 30840

History | View | Annotate | Download (5.71 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 org.gvsig.graph;
42

    
43
import javax.swing.JOptionPane;
44

    
45
import org.gvsig.exceptions.BaseException;
46
import org.gvsig.graph.core.GraphException;
47
import org.gvsig.graph.core.GvFlag;
48
import org.gvsig.graph.core.Network;
49
import org.gvsig.graph.solvers.MinimumSpanningTreeExtractor;
50
import org.gvsig.graph.solvers.OneToManySolver;
51

    
52
import com.iver.andami.PluginServices;
53
import com.iver.andami.plugins.Extension;
54
import com.iver.andami.ui.mdiManager.IWindow;
55
import com.iver.cit.gvsig.fmap.MapContext;
56
import com.iver.cit.gvsig.fmap.MapControl;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
59
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
60
import com.iver.cit.gvsig.project.documents.view.gui.View;
61

    
62
/**
63
 * @author fjp
64
 * 
65
 * Extension to perform ServiceArea calculations. Here you will find code to:
66
 * 1.- See the distances to every node on the network to one or many point
67
 * sources. 2.- TODO: Calculate a polyline layer with costs and length
68
 * calculated to nearest source point. 3.- TODO: Calculate polygons covering
69
 * those service areas.
70
 */
71
public class MinimumSpanningTreeExtension extends Extension {
72

    
73
        private int idSymbolLine = -1;
74

    
75
        public void initialize() {
76
                PluginServices.getIconTheme().registerDefault(
77
                                "mst",
78
                                this.getClass().getClassLoader().getResource("images/mst.png")
79
                        );                
80
                
81
        }
82

    
83
        public void execute(String actionCommand) {
84

    
85
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
86
                MapControl mapCtrl = v.getMapControl();
87
                MapContext map = mapCtrl.getMapContext();
88
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());                
89
                while (it.hasNext()) {
90
                        FLayer aux = it.next();
91
                        if (!aux.isActive())
92
                                continue;
93
                        Network net = (Network) aux.getProperty("network");
94

    
95
                        if (net != null) {
96
                                GvFlag[] flags = net.getFlags();
97
                                if (flags.length == 0) {
98
                                        JOptionPane.showMessageDialog(null,
99
                                                        "Primero carga las paradas.");
100
                                        return;
101
                                }
102
//                                        setVelocities(net);
103
                                try {
104
                                        OneToManySolver solver = new OneToManySolver();
105
                                        solver.setNetwork(net);
106
                                        solver.putDestinationsOnNetwork(net.getFlags());
107
                                        if (actionCommand.equals("MST")) {
108
                                                calculateMST(map, net, flags, solver);
109
                                        }                                        
110
                                        solver.removeDestinationsFromNetwork(net.getFlags());
111
                                } catch (BaseException e) {
112
                                        // TODO Auto-generated catch block
113
                                        e.printStackTrace();
114
                                }
115

    
116
                                return;
117
                        }
118
                }
119

    
120
        }
121

    
122
        /**
123
         * @param mapCtrl
124
         * @param map
125
         * @param net
126
         * @param flags
127
         * @param solver
128
         * @return
129
         * @throws GraphException
130
         */
131
        private void calculateMST(MapContext map, Network net, GvFlag[] flags, OneToManySolver solver) throws BaseException {
132
                MinimumSpanningTreeExtractor extractor = new MinimumSpanningTreeExtractor(net);
133
                String aux = JOptionPane.showInputDialog(PluginServices.getText(this, "Please_enter_max_cost_MST") + ":");
134
                if (aux == null)
135
                        return;
136
                double cost = Double.parseDouble(aux);
137
                solver.addListener(extractor);
138
                for (int i = 0; i < flags.length; i++) {
139
                        solver.setSourceFlag(flags[i]);
140
                        long t1 = System.currentTimeMillis();                        
141
                        solver.setExploreAllNetwork(true);
142
                        solver.setMaxCost(cost);
143
                        extractor.setIdFlag(i);
144
                        solver.calculate();
145
                        long t2 = System.currentTimeMillis();
146
                        System.out.println("Punto " + i + " de "
147
                                        + flags.length + ". " + (t2 - t1)
148
                                        + " msecs.");                                        
149
                }
150
                extractor.endExtraction();
151
                
152
                FLyrVect lyrLine = extractor.getLineLayer();
153
                lyrLine.setProjection(map.getProjection());
154
                map.beginAtomicEvent();
155
                map.getLayers().addLayer(lyrLine);
156
                map.endAtomicEvent();
157

    
158
        
159
        }
160
        
161

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

    
189

    
190
        }
191

    
192
        public boolean isVisible() {
193
                IWindow f = PluginServices.getMDIManager()
194
                 .getActiveWindow();
195
                if (f == null) {
196
                    return false;
197
                }
198
                if (f instanceof View) {
199
                        return true;
200
                }
201
                return false;
202

    
203
        }
204

    
205
}