Statistics
| Revision:

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

History | View | Annotate | Download (6.96 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.Color;
44
import java.text.NumberFormat;
45
import java.util.ArrayList;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.plugins.Extension;
51
import com.iver.andami.ui.mdiManager.IWindow;
52
import com.iver.cit.gvsig.fmap.MapContext;
53
import com.iver.cit.gvsig.fmap.MapControl;
54
import com.iver.cit.gvsig.fmap.core.IGeometry;
55
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
56
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
57
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
58
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
61
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
62
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator;
63
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
64
import com.iver.cit.gvsig.fmap.rendering.FGraphicLabel;
65
import com.iver.cit.gvsig.graph.core.GraphException;
66
import com.iver.cit.gvsig.graph.core.GvEdge;
67
import com.iver.cit.gvsig.graph.core.GvFlag;
68
import com.iver.cit.gvsig.graph.core.GvNode;
69
import com.iver.cit.gvsig.graph.core.IGraph;
70
import com.iver.cit.gvsig.graph.core.Network;
71
import com.iver.cit.gvsig.graph.solvers.EdgesMemoryDriver;
72
import com.iver.cit.gvsig.graph.solvers.OneToManySolver;
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
        }
90

    
91
        public void execute(String actionCommand) {
92

    
93
                View v = (View) PluginServices.getMDIManager().getActiveWindow();
94
                MapControl mapCtrl = v.getMapControl();
95
                MapContext map = mapCtrl.getMapContext();
96
                SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
97

    
98
                if (actionCommand.equals("LABEL_NODE_DISTANCES")) {
99
                        while (it.hasNext()) {
100
                                FLayer aux = it.next();
101
                                if (!aux.isActive())
102
                                        continue;
103
                                Network net = (Network) aux.getProperty("network");
104

    
105
                                if (net != null) {
106
                                        GvFlag[] flags = net.getFlags();
107
                                        if (flags.length == 0) {
108
                                                JOptionPane.showMessageDialog(null,
109
                                                                "Primero carga las paradas.");
110
                                                return;
111
                                        }
112
//                                        setVelocities(net);
113
                                        try {
114
                                                OneToManySolver solver = new OneToManySolver();
115
                                                solver.setNetwork(net);
116
                                                solver.putDestinationsOnNetwork();
117
                                                GraphicLayer graphicLayer = mapCtrl.getMapContext()
118
                                                                .getGraphicsLayer();
119
                                                removeOldLabels(graphicLayer);
120
                                                for (int i = 0; i < flags.length; i++) {
121

    
122
                                                        solver.setSourceFlag(flags[i]);
123
                                                        long t1 = System.currentTimeMillis();
124
                                                        solver.setExploreAllNetwork(true);
125
                                                        solver.calculate();
126
                                                        long t2 = System.currentTimeMillis();
127
                                                        System.out.println("Punto " + i + " de "
128
                                                                        + flags.length + ". " + (t2 - t1)
129
                                                                        + " msecs.");
130
                                                        // Despu?s de esto, los nodos de la red est?n
131
                                                        // etiquetados con los costes al nodo or?gen
132
                                                        EdgesMemoryDriver driver = new EdgesMemoryDriver(net);
133
                                                        FLayer lyr = LayerFactory.createLayer("Edges", driver, null);
134
                                                        map.getLayers().addLayer(lyr);
135
                                                        // doLabeling(mapCtrl, net, flags[i]);
136

    
137
                                                }
138

    
139
                                                solver.removeDestinationsFromNetwork();
140
                                        } catch (GraphException e) {
141
                                                // TODO Auto-generated catch block
142
                                                e.printStackTrace();
143
                                        }
144

    
145
                                        return;
146
                                }
147
                        }
148
                }
149

    
150
        }
151

    
152
        private FSymbol getTextSymbol() {
153
                FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
154
                theSymbol.setColor(Color.RED);
155
                theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_CIRCLE);
156
                theSymbol.setFontColor(Color.BLACK);
157
                theSymbol.setSizeInPixels(true);
158
                theSymbol.setSize(9);
159
                return theSymbol;
160
        }
161

    
162
        private void removeOldLabels(GraphicLayer gLyr) {
163
                for (int i = gLyr.getNumGraphics() - 1; i >= 0; i--) {
164
                        FGraphic gr = gLyr.getGraphic(i);
165
                        if (gr.equals("N"))
166
                                gLyr.removeGraphic(i);
167

    
168
                }
169
        }
170

    
171
        private void doLabeling(MapControl mapControl, Network net, GvFlag flag) {
172
                GraphicLayer graphicLayer = mapControl.getMapContext()
173
                                .getGraphicsLayer();
174
                IGraph g = net.getGraph();
175
                int idSymbol = graphicLayer.addSymbol(getTextSymbol());
176
                String tag = "N";
177
                for (int i = 0; i < g.numVertices(); i++) {
178
                        GvNode node = g.getNodeByID(i);
179
                        IGeometry geom = ShapeFactory.createPoint2D(node.getX(), node
180
                                        .getY());
181
                        NumberFormat nf = NumberFormat.getInstance();
182
                        nf.setMaximumFractionDigits(1);
183
                        String aux = "\u221E"; // infinito
184
                        if (node.getBestCost() < Double.MAX_VALUE)
185
                                aux = nf.format(node.getBestCost()) + " - " + nf.format(node.getAccumulatedLength());
186
                        FGraphicLabel theGLabel = new FGraphicLabel(geom, idSymbol, aux);
187
                        theGLabel.setObjectTag(tag);
188
                        theGLabel.getLabel().setJustification(FLabel.CENTER_TOP);
189
                        graphicLayer.addGraphic(theGLabel);
190
                }
191
                mapControl.drawGraphics();
192

    
193
        }
194

    
195
        public boolean isEnabled() {
196
                IWindow window = PluginServices.getMDIManager().getActiveWindow();
197
                if (window instanceof View)
198
                {
199
                        View v = (View) window;
200
                MapControl mapCtrl = v.getMapControl();
201
                        MapContext map = mapCtrl.getMapContext();
202
                        
203
                        SingleLayerIterator it = new SingleLayerIterator(map.getLayers());
204
                        while (it.hasNext())
205
                        {
206
                                FLayer aux = it.next();
207
                                if (!aux.isActive())
208
                                        continue;
209
                                Network net = (Network) aux.getProperty("network");
210
                                
211
                                if ( net != null)
212
                                {
213
                                        return true;
214
                                }
215
                        }
216
                }
217
                return false;
218
        }
219

    
220
        public boolean isVisible() {
221
                IWindow f = PluginServices.getMDIManager()
222
                 .getActiveWindow();
223
                if (f == null) {
224
                    return false;
225
                }
226
                if (f instanceof View) {
227
                        return true;
228
                }
229
                return false;
230

    
231
        }
232

    
233
}