Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph / src / org / gvsig / graph / ServiceAreaExtension.java @ 39203

History | View | Annotate | Download (9.38 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 java.awt.Color;
44
import java.text.NumberFormat;
45

    
46
import org.gvsig.exceptions.BaseException;
47
import org.gvsig.graph.core.GraphException;
48
import org.gvsig.graph.core.GvFlag;
49
import org.gvsig.graph.core.GvNode;
50
import org.gvsig.graph.core.IGraph;
51
import org.gvsig.graph.core.Network;
52
import org.gvsig.graph.gui.ServiceAreaControlPanel;
53
import org.gvsig.graph.solvers.EdgesMemoryDriver;
54
import org.gvsig.graph.solvers.OneToManySolver;
55
import org.gvsig.graph.solvers.ServiceAreaExtractor2;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.andami.plugins.Extension;
59
import com.iver.andami.ui.mdiManager.IWindow;
60
import com.iver.cit.gvsig.fmap.MapContext;
61
import com.iver.cit.gvsig.fmap.MapControl;
62
import com.iver.cit.gvsig.fmap.core.IGeometry;
63
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
64
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
65
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
66
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
67
import com.iver.cit.gvsig.fmap.layers.FLayer;
68
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
69
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
71
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
72
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
73
import com.iver.cit.gvsig.project.documents.view.gui.View;
74

    
75
/**
76
 * @author fjp
77
 * 
78
 * Extension to perform ServiceArea calculations. Here you will find code to:
79
 * 1.- See the distances to every node on the network to one or many point
80
 * sources. 2.- TODO: Calculate a polyline layer with costs and length
81
 * calculated to nearest source point. 3.- TODO: Calculate polygons covering
82
 * those service areas.
83
 */
84
public class ServiceAreaExtension extends Extension {
85

    
86
        private int idSymbolLine = -1;
87

    
88
        public void initialize() {
89
                PluginServices.getIconTheme().registerDefault(
90
                                "service_area",
91
                                this.getClass().getClassLoader().getResource("images/service_area.png")
92
                        );
93
                
94
                PluginServices.getIconTheme().registerDefault(
95
                                "service_area_wrong_costs",
96
                                this.getClass().getClassLoader().getResource("images/service_area_wrong_costs.png"));
97
                
98
                PluginServices.getIconTheme().registerDefault(
99
                                "service_area_compact",
100
                                this.getClass().getClassLoader().getResource("images/service_area_compact.png"));
101
                
102
                PluginServices.getIconTheme().registerDefault(
103
                                "service_area_convex",
104
                                this.getClass().getClassLoader().getResource("images/service_area_convex.png"));
105
                
106
                PluginServices.getIconTheme().registerDefault(
107
                                "service_area_fusion",
108
                                this.getClass().getClassLoader().getResource("images/service_area_fusion.png"));
109
                
110
                PluginServices.getIconTheme().registerDefault(
111
                                "service_area_non_fusion",
112
                                this.getClass().getClassLoader().getResource("images/service_area_non_fusion.png"));
113
                
114
                PluginServices.getIconTheme().registerDefault(
115
                                "service_area_disks",
116
                                this.getClass().getClassLoader().getResource("images/service_area_disks.png"));
117
                
118
                PluginServices.getIconTheme().registerDefault(
119
                                "service_area_rings",
120
                                this.getClass().getClassLoader().getResource("images/service_area_rings.png"));
121
        }
122

    
123
        public void execute(String actionCommand) {
124

    
125
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
126
                MapControl mapCtrl = v.getMapControl();
127
                MapContext map = mapCtrl.getMapContext();
128
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());                
129
                while (it.hasNext()) {
130
                        FLayer aux = it.next();
131
                        if (!aux.isActive())
132
                                continue;
133
                        Network net = (Network) aux.getProperty("network");
134

    
135
                        if (net != null) {
136
                                GvFlag[] flags = net.getFlags();
137
//                                if (flags.length == 0) {
138
//                                        JOptionPane.showMessageDialog(null,
139
//                                                        "Primero carga las paradas.");
140
//                                        return;
141
//                                }
142
//                                        setVelocities(net);
143
                                try {
144
//                                        OneToManySolver solver = new OneToManySolver();
145
//                                        solver.setNetwork(net);
146
//                                        solver.putDestinationsOnNetwork(net.getFlags());
147
//                                        if (actionCommand.equals("LABEL_NODE_DISTANCES")) {
148
//                                                calculateLabels(mapCtrl, map, net, flags, solver);
149
//                                        }
150
                                        if (actionCommand.equals("SERVICE_AREA")) {
151
                                                calculateServiceArea(mapCtrl, net, flags);
152
                                        }
153
//                                        if(actionCommand.equals("PRUEBA_WIZARD_SERVICE_AREA")){
154
//                                                ImageIcon icon = new ImageIcon(this.getClass().getClassLoader()
155
//                                                                .getResource("images/service_area-wizard-logo.jpg"));
156
//                                                ServiceAreaWizard wiz=new ServiceAreaWizard(icon, null);
157
//                                                PluginServices.getMDIManager().addWindow(wiz);
158
//                                        }
159
//                                        solver.removeDestinationsFromNetwork(net.getFlags());
160
                                } catch (BaseException e) {
161
                                        // TODO Auto-generated catch block
162
                                        e.printStackTrace();
163
                                }
164

    
165
                                return;
166
                        }
167
                }
168

    
169
        }
170

    
171
        /**
172
         * @param mapCtrl
173
         * @param map
174
         * @param net
175
         * @param flags
176
         * @param solver
177
         * @return
178
         * @throws GraphException
179
         */
180
        private void calculateLabels(MapControl mapCtrl, MapContext map, Network net, GvFlag[] flags, OneToManySolver solver) throws GraphException {
181
                GraphicLayer graphicLayer = mapCtrl.getMapContext()
182
                                .getGraphicsLayer();
183
                removeOldLabels(graphicLayer);
184
                for (int i = 0; i < flags.length; i++) {
185

    
186
                        solver.setSourceFlag(flags[i]);
187
                        long t1 = System.currentTimeMillis();
188
                        solver.setExploreAllNetwork(true);
189
                        solver.calculate();
190
                        long t2 = System.currentTimeMillis();
191
                        System.out.println("Punto " + i + " de "
192
                                        + flags.length + ". " + (t2 - t1)
193
                                        + " msecs.");
194
                        // Despu?s de esto, los nodos de la red est?n
195
                        // etiquetados con los costes al nodo or?gen
196
                        EdgesMemoryDriver driver = new EdgesMemoryDriver(net);
197
                        FLayer lyr = LayerFactory.createLayer("Edges", driver, null);
198
                        map.getLayers().addLayer(lyr);
199
                        // doLabeling(mapCtrl, net, flags[i]);
200

    
201
                }
202
        
203
        }
204

    
205
        private void calculateServiceArea(MapControl mapCtrl, Network net, GvFlag[] flags) throws BaseException {
206
                ServiceAreaExtractor2 extractor = new ServiceAreaExtractor2(net);
207
                
208
                ServiceAreaControlPanel controlPanel = new ServiceAreaControlPanel(net);
209
                controlPanel.setMapControl(mapCtrl, net);
210
                controlPanel = (ServiceAreaControlPanel) PluginServices.getMDIManager().addWindow(controlPanel);
211
                
212
                
213
        }
214
        
215
        private FSymbol getTextSymbol() {
216
                FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
217
                theSymbol.setColor(Color.RED);
218
                theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_CIRCLE);
219
                theSymbol.setFontColor(Color.BLACK);
220
                theSymbol.setSizeInPixels(true);
221
                theSymbol.setSize(9);
222
                return theSymbol;
223
        }
224

    
225
        private void removeOldLabels(GraphicLayer gLyr) {
226
                for (int i = gLyr.getNumGraphics() - 1; i >= 0; i--) {
227
                        FGraphic gr = gLyr.getGraphic(i);
228
                        if (gr.equals("N"))
229
                                gLyr.removeGraphic(i);
230

    
231
                }
232
        }
233

    
234
        private void doLabeling(MapControl mapControl, Network net, GvFlag flag) {
235
                GraphicLayer graphicLayer = mapControl.getMapContext()
236
                                .getGraphicsLayer();
237
                IGraph g = net.getGraph();
238
                int idSymbol = graphicLayer.addSymbol(getTextSymbol());
239
                String tag = "N";
240
                for (int i = 0; i < g.numVertices(); i++) {
241
                        GvNode node = g.getNodeByID(i);
242
                        IGeometry geom = ShapeFactory.createPoint2D(node.getX(), node
243
                                        .getY());
244
                        NumberFormat nf = NumberFormat.getInstance();
245
                        nf.setMaximumFractionDigits(1);
246
                        String aux = "\u221E"; // infinito
247
                        if (node.getBestCost() < Double.MAX_VALUE)
248
                                aux = nf.format(node.getBestCost()) + " - " + nf.format(node.getAccumulatedLength());
249
                        FGraphicLabel theGLabel = new FGraphicLabel(geom, idSymbol, aux);
250
                        theGLabel.setObjectTag(tag);
251
                        theGLabel.getLabel().setJustification(FLabel.CENTER_TOP);
252
                        graphicLayer.addGraphic(theGLabel);
253
                }
254
                mapControl.drawGraphics();
255

    
256
        }
257

    
258
        public boolean isEnabled() {
259
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
260
                if (window instanceof View)
261
                {
262
                        View v = (View) window;
263
                MapControl mapCtrl = v.getMapControl();
264
                        MapContext map = mapCtrl.getMapContext();
265
                        
266
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
267
                        while (it.hasNext())
268
                        {
269
                                FLayer aux = it.next();
270
                                if (!aux.isActive())
271
                                        continue;
272
                                Network net = (Network) aux.getProperty("network");
273
                                
274
                                if ( net != null)
275
                                {
276
//                                        if (net.getFlags().length > 0)
277
//                                        {
278
                                                return true;
279
//                                        }
280
                                }
281
                        }
282
                }
283
                return false;
284
        }
285

    
286
        public boolean isVisible() {
287
                IWindow f = PluginServices.getMDIManager()
288
                 .getActiveWindow();
289
                if (f == null) {
290
                    return false;
291
                }
292
                if (f instanceof View) {
293
                        return true;
294
                }
295
                return false;
296

    
297
        }
298

    
299
}