svn-gvsig-desktop / trunk / extensions / extGraph / src / com / iver / cit / gvsig / graph / LoadNetworkExtension.java @ 20103
History | View | Annotate | Download (8.09 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.File; |
45 |
import java.sql.Types; |
46 |
import java.util.ArrayList; |
47 |
|
48 |
import javax.swing.JOptionPane; |
49 |
|
50 |
import org.gvsig.exceptions.BaseException; |
51 |
|
52 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
53 |
import com.iver.andami.PluginServices; |
54 |
import com.iver.andami.messages.NotificationManager; |
55 |
import com.iver.andami.plugins.Extension; |
56 |
import com.iver.andami.ui.mdiManager.IWindow; |
57 |
import com.iver.cit.gvsig.ProjectExtension; |
58 |
import com.iver.cit.gvsig.fmap.MapContext; |
59 |
import com.iver.cit.gvsig.fmap.MapControl; |
60 |
import com.iver.cit.gvsig.fmap.core.FShape; |
61 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
62 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
63 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
64 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
65 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
66 |
import com.iver.cit.gvsig.graph.core.IGraph; |
67 |
import com.iver.cit.gvsig.graph.core.Network; |
68 |
import com.iver.cit.gvsig.graph.core.NetworkUtils; |
69 |
import com.iver.cit.gvsig.graph.core.loaders.NetworkLoader; |
70 |
import com.iver.cit.gvsig.graph.core.loaders.NetworkRedLoader; |
71 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
72 |
import com.iver.cit.gvsig.project.documents.view.IProjectView; |
73 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
74 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
75 |
|
76 |
public class LoadNetworkExtension extends Extension { |
77 |
|
78 |
public void initialize() { |
79 |
PluginServices.getIconTheme().registerDefault( |
80 |
"network",
|
81 |
this.getClass().getClassLoader().getResource("images/network.png") |
82 |
); |
83 |
|
84 |
} |
85 |
|
86 |
public void execute(String actionCommand) { |
87 |
IView view = (View) PluginServices.getMDIManager().getActiveWindow();
|
88 |
MapControl mapControl = view.getMapControl(); |
89 |
MapContext map = mapControl.getMapContext(); |
90 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(map
|
91 |
.getLayers()); |
92 |
while (lyrIterator.hasNext()) {
|
93 |
FLayer lyr = lyrIterator.next(); |
94 |
if ((lyr.isActive()) && (lyr instanceof FLyrVect)) |
95 |
{ |
96 |
FLyrVect lyrVect = (FLyrVect) lyr; |
97 |
int shapeType;
|
98 |
try {
|
99 |
shapeType = lyrVect.getShapeType(); |
100 |
if (shapeType == FShape.LINE)
|
101 |
{ |
102 |
if (actionCommand.equalsIgnoreCase("LOAD_RED")) { |
103 |
loadNetwork(lyrVect); |
104 |
return;
|
105 |
} |
106 |
} |
107 |
} catch (BaseException e) {
|
108 |
e.printStackTrace(); |
109 |
NotificationManager.addError(e); |
110 |
} |
111 |
|
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Suponemos que en el proyecto hay 2 tablas, una con los nodos
|
120 |
* y otro con los edges.
|
121 |
* Cargamos la red a partir de esas tablas y se la
|
122 |
* asociamos a la capa. A partir de ah?, nuestras
|
123 |
* herramientas pueden ver si la capa activa tiene
|
124 |
* asociada o no una red y ponerse visibles / invisibles
|
125 |
* Otra posible soluci?n es llevar nuestra propia lista de capas
|
126 |
* con red (que ser? peque?ita), y as?, en lugar de recorrer
|
127 |
* el MapContext, recorremos nuestra lista para ver la
|
128 |
* capa que est? activa y con red. Me empieza a preocupar
|
129 |
* que todas las herramientas iteren por la colecci?n de
|
130 |
* capas para habilitarse/deshabilitarse:
|
131 |
* 100 herramientas * 100 capas = 10.000 comprobaciones
|
132 |
* Si comprobar algo cuesta 1 mseg => 10 segundos!!!
|
133 |
* @param lyrVect
|
134 |
* @throws ReadDriverException
|
135 |
*/
|
136 |
private void loadNetworkFromTables(FLyrVect lyrVect) throws ReadDriverException { |
137 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
138 |
// de nodos y edges
|
139 |
// y hacer un mapping (si es necesario) entre los
|
140 |
// nombres de campos
|
141 |
String tableNodes = "Nodes"; |
142 |
String tableEdges = "Edges"; |
143 |
|
144 |
ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
145 |
|
146 |
ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes); |
147 |
ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges); |
148 |
|
149 |
SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset(); |
150 |
|
151 |
|
152 |
SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset(); |
153 |
|
154 |
NetworkLoader netLoader = new NetworkLoader(true); |
155 |
|
156 |
netLoader.setNodeReader(sdsNodes); |
157 |
netLoader.setEdgeReader(sdsEdges); |
158 |
|
159 |
IGraph g = netLoader.loadNetwork(); |
160 |
|
161 |
System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
162 |
|
163 |
lyrVect.setProperty("network", g);
|
164 |
|
165 |
} |
166 |
private void loadNetwork(FLyrVect lyrVect) throws BaseException { |
167 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
168 |
// de nodos y edges
|
169 |
// y hacer un mapping (si es necesario) entre los
|
170 |
// nombres de campos
|
171 |
|
172 |
// TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
|
173 |
ArrayList aux = new ArrayList(); |
174 |
FieldDescription[] fields = lyrVect.getRecordset().getFieldsDescription();
|
175 |
for (int i=0; i<fields.length; i++) |
176 |
{ |
177 |
if (fields[i].getFieldType() == Types.VARCHAR) |
178 |
{ |
179 |
aux.add(fields[i].getFieldName()); |
180 |
} |
181 |
} |
182 |
String fieldStreetName = (String) JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(), |
183 |
PluginServices.getText(this, "select_street_route_field_name"), |
184 |
"gvSIG",
|
185 |
JOptionPane.QUESTION_MESSAGE,
|
186 |
null,
|
187 |
(Object[]) aux.toArray(new String[0]), |
188 |
"NOMBRE");
|
189 |
|
190 |
if (fieldStreetName == null) |
191 |
return;
|
192 |
|
193 |
|
194 |
NetworkRedLoader netLoader = new NetworkRedLoader();
|
195 |
File redFile = NetworkUtils.getNetworkFile(lyrVect);
|
196 |
netLoader.setNetFile(redFile); |
197 |
|
198 |
IGraph g = netLoader.loadNetwork(); |
199 |
|
200 |
System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
201 |
|
202 |
Network net = new Network();
|
203 |
// lyrVect.createSpatialIndex();
|
204 |
net.setGraph(g); |
205 |
net.setLayer(lyrVect); |
206 |
ShortestPathExtension.solver.setNetwork(net); |
207 |
ShortestPathExtension.solver.setFielStreetName(fieldStreetName); |
208 |
|
209 |
lyrVect.setProperty("network", net);
|
210 |
|
211 |
} |
212 |
|
213 |
public boolean isEnabled() { |
214 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
215 |
|
216 |
if (f == null) { |
217 |
return false; |
218 |
} |
219 |
|
220 |
if (f instanceof View) { |
221 |
View vista = (View) f; |
222 |
IProjectView model = vista.getModel(); |
223 |
MapContext mapa = model.getMapContext(); |
224 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
225 |
if (activeLayers.length > 0) |
226 |
if (activeLayers[0] instanceof FLyrVect){ |
227 |
FLyrVect lyrVect = (FLyrVect) activeLayers[0];
|
228 |
File netFile = NetworkUtils.getNetworkFile(lyrVect);
|
229 |
if (netFile.exists())
|
230 |
return true; |
231 |
|
232 |
} |
233 |
} |
234 |
return false; |
235 |
} |
236 |
|
237 |
public boolean isVisible() { |
238 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
239 |
|
240 |
if (f == null) { |
241 |
return false; |
242 |
} |
243 |
|
244 |
if (f instanceof View) { |
245 |
View vista = (View) f; |
246 |
IProjectView model = vista.getModel(); |
247 |
MapContext mapa = model.getMapContext(); |
248 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
249 |
if (activeLayers.length > 0) |
250 |
if (activeLayers[0] instanceof FLyrVect){ |
251 |
FLyrVect lyrVect = (FLyrVect) activeLayers[0];
|
252 |
int shapeType ;
|
253 |
try {
|
254 |
shapeType = lyrVect.getShapeType(); |
255 |
if (shapeType == FShape.LINE)
|
256 |
return true; |
257 |
} catch (ReadDriverException e) {
|
258 |
// TODO Auto-generated catch block
|
259 |
e.printStackTrace(); |
260 |
} |
261 |
} |
262 |
} |
263 |
return false; |
264 |
|
265 |
} |
266 |
|
267 |
|
268 |
} |