root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / NetworkExtension.java @ 8513
History | View | Annotate | Download (11.9 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 |
|
46 |
import javax.swing.ImageIcon; |
47 |
import javax.swing.JOptionPane; |
48 |
|
49 |
import com.iver.andami.PluginServices; |
50 |
import com.iver.andami.plugins.Extension; |
51 |
import com.iver.andami.preferences.IPreference; |
52 |
import com.iver.andami.preferences.IPreferenceExtension; |
53 |
import com.iver.andami.ui.mdiManager.IWindow; |
54 |
import com.iver.cit.gvsig.ProjectExtension; |
55 |
import com.iver.cit.gvsig.fmap.DriverException; |
56 |
import com.iver.cit.gvsig.fmap.MapContext; |
57 |
import com.iver.cit.gvsig.fmap.MapControl; |
58 |
import com.iver.cit.gvsig.fmap.core.FShape; |
59 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
60 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
61 |
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter; |
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.fmap.operations.strategies.Strategy; |
67 |
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager; |
68 |
import com.iver.cit.gvsig.geoprocess.core.fmap.FeaturePersisterProcessor2; |
69 |
import com.iver.cit.gvsig.graph.core.GvFlag; |
70 |
import com.iver.cit.gvsig.graph.core.IGraph; |
71 |
import com.iver.cit.gvsig.graph.core.Network; |
72 |
import com.iver.cit.gvsig.graph.core.loaders.NetworkLoader; |
73 |
import com.iver.cit.gvsig.graph.core.loaders.NetworkRedLoader; |
74 |
import com.iver.cit.gvsig.graph.core.writers.NetworkFileRedWriter; |
75 |
import com.iver.cit.gvsig.graph.core.writers.NetworkGvTableWriter; |
76 |
import com.iver.cit.gvsig.graph.gui.RouteControlPanel; |
77 |
import com.iver.cit.gvsig.graph.gui.wizard.NetWizard; |
78 |
import com.iver.cit.gvsig.graph.preferences.RoutePage; |
79 |
import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
80 |
import com.iver.cit.gvsig.project.documents.view.IProjectView; |
81 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
82 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
83 |
import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
84 |
|
85 |
public class NetworkExtension extends Extension implements IPreferenceExtension { |
86 |
private static final IPreference thePreferencePage = new RoutePage(); |
87 |
// private String fieldType;
|
88 |
// private String fieldDist;
|
89 |
// private String fieldSense;
|
90 |
|
91 |
public void initialize() { |
92 |
} |
93 |
|
94 |
public void execute(String actionCommand) { |
95 |
IView view = (View) PluginServices.getMDIManager().getActiveWindow();
|
96 |
MapControl mapControl = view.getMapControl(); |
97 |
MapContext map = mapControl.getMapContext(); |
98 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(map
|
99 |
.getLayers()); |
100 |
while (lyrIterator.hasNext()) {
|
101 |
FLayer lyr = lyrIterator.next(); |
102 |
if ((lyr.isActive()) && (lyr instanceof FLyrVect)) |
103 |
{ |
104 |
FLyrVect lyrVect = (FLyrVect) lyr; |
105 |
int shapeType;
|
106 |
try {
|
107 |
shapeType = lyrVect.getShapeType(); |
108 |
if (shapeType == FShape.LINE)
|
109 |
{ |
110 |
if (actionCommand.equalsIgnoreCase("GENERATE_RED")) { |
111 |
generateRedNetwork(lyrVect); |
112 |
} |
113 |
if (actionCommand.equalsIgnoreCase("LOAD_RED")) { |
114 |
loadNetwork(lyrVect); |
115 |
} |
116 |
if (actionCommand.equalsIgnoreCase("MANAGE_FLAGS")) { |
117 |
manageFlags(lyrVect); |
118 |
} |
119 |
} |
120 |
} catch (DriverException e) {
|
121 |
// TODO Auto-generated catch block
|
122 |
e.printStackTrace(); |
123 |
} |
124 |
|
125 |
} |
126 |
} |
127 |
|
128 |
|
129 |
} |
130 |
|
131 |
private void manageFlags(FLyrVect lyrVect) { |
132 |
Network net = (Network) lyrVect.getProperty("network");
|
133 |
|
134 |
if ( net != null) |
135 |
{ |
136 |
GvFlag[] flags = net.getFlags();
|
137 |
if(flags.length > 0) |
138 |
PluginServices.getMDIManager().addWindow(new RouteControlPanel(flags));
|
139 |
else
|
140 |
PluginServices.getMDIManager().addWindow(new RouteControlPanel());
|
141 |
return;
|
142 |
} |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Suponemos que en el proyecto hay 2 tablas, una con los nodos
|
147 |
* y otro con los edges.
|
148 |
* Cargamos la red a partir de esas tablas y se la
|
149 |
* asociamos a la capa. A partir de ah?, nuestras
|
150 |
* herramientas pueden ver si la capa activa tiene
|
151 |
* asociada o no una red y ponerse visibles / invisibles
|
152 |
* Otra posible soluci?n es llevar nuestra propia lista de capas
|
153 |
* con red (que ser? peque?ita), y as?, en lugar de recorrer
|
154 |
* el MapContext, recorremos nuestra lista para ver la
|
155 |
* capa que est? activa y con red. Me empieza a preocupar
|
156 |
* que todas las herramientas iteren por la colecci?n de
|
157 |
* capas para habilitarse/deshabilitarse:
|
158 |
* 100 herramientas * 100 capas = 10.000 comprobaciones
|
159 |
* Si comprobar algo cuesta 1 mseg => 10 segundos!!!
|
160 |
* @param lyrVect
|
161 |
*/
|
162 |
private void loadNetworkFromTables(FLyrVect lyrVect) { |
163 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
164 |
// de nodos y edges
|
165 |
// y hacer un mapping (si es necesario) entre los
|
166 |
// nombres de campos
|
167 |
String tableNodes = "Nodes"; |
168 |
String tableEdges = "Edges"; |
169 |
|
170 |
ProjectExtension projectExt = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
171 |
|
172 |
ProjectTable ptNodes = projectExt.getProject().getTable(tableNodes); |
173 |
ProjectTable ptEdges = projectExt.getProject().getTable(tableEdges); |
174 |
|
175 |
SelectableDataSource sdsNodes = ptNodes.getModelo().getRecordset(); |
176 |
|
177 |
|
178 |
SelectableDataSource sdsEdges = ptEdges.getModelo().getRecordset(); |
179 |
|
180 |
NetworkLoader netLoader = new NetworkLoader(true); |
181 |
|
182 |
netLoader.setNodeReader(sdsNodes); |
183 |
netLoader.setEdgeReader(sdsEdges); |
184 |
|
185 |
IGraph g = netLoader.loadNetwork(); |
186 |
|
187 |
System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
188 |
|
189 |
lyrVect.setProperty("network", g);
|
190 |
|
191 |
} |
192 |
private void loadNetwork(FLyrVect lyrVect) { |
193 |
// Aqu? mostrar un di?lgo para seleccionar las tablas
|
194 |
// de nodos y edges
|
195 |
// y hacer un mapping (si es necesario) entre los
|
196 |
// nombres de campos
|
197 |
|
198 |
NetworkRedLoader netLoader = new NetworkRedLoader();
|
199 |
File redFile = new File("c:/" + lyrVect.getName().replaceAll("\\Q.shp\\E", ".red")); |
200 |
netLoader.setNetFile(redFile); |
201 |
|
202 |
IGraph g = netLoader.loadNetwork(); |
203 |
|
204 |
System.out.println("Num nodos=" + g.numVertices() + " numEdges = " + g.numEdges()); |
205 |
|
206 |
// TODO: MOSTRAR UN CUADRO DE DI?LOGO CON UN COMBOBOX PARA QUE ESCOJA EL CAMPO DE NOMBRE DE CALLE.
|
207 |
String fieldStreetName = JOptionPane.showInputDialog((Component) PluginServices.getMainFrame(), |
208 |
"Street-Route field name?", "NOMBRE"); |
209 |
|
210 |
Network net = new Network();
|
211 |
// lyrVect.createSpatialIndex();
|
212 |
net.setGraph(g); |
213 |
net.setLayer(lyrVect); |
214 |
ShortestPathExtension.solver.setNetwork(net); |
215 |
ShortestPathExtension.solver.setFielStreetName(fieldStreetName); |
216 |
|
217 |
lyrVect.setProperty("network", net);
|
218 |
|
219 |
} |
220 |
|
221 |
|
222 |
private void generateNetwork(FLyrVect lyr) { |
223 |
NetworkGvTableWriter netBuilder = new NetworkGvTableWriter();
|
224 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
225 |
// de di?logo para hecer el mapping.
|
226 |
// Tambi?n un cuadro de di?logo para seleccionar
|
227 |
// en qu? tablas quiere escribir, y su formato
|
228 |
// (dbf, postgres, etc)
|
229 |
String fieldType = "tipored"; String fieldDist = "length"; String fieldSense = "sen"; |
230 |
String fieldCost = "cost"; |
231 |
try {
|
232 |
netBuilder.setLayer(lyr); |
233 |
netBuilder.setFieldType(fieldType); |
234 |
netBuilder.setFieldDist(fieldDist); |
235 |
netBuilder.setFieldSense(fieldSense); |
236 |
netBuilder.setFieldCost(fieldCost); |
237 |
DbfWriter nodeWriter = new DbfWriter();
|
238 |
nodeWriter.setFile(new File("c:/nodes.dbf")); |
239 |
|
240 |
DbfWriter edgeWriter = new DbfWriter();
|
241 |
edgeWriter.setFile(new File("c:/edges.dbf")); |
242 |
|
243 |
netBuilder.setEdgeWriter(edgeWriter); |
244 |
netBuilder.setNodeWriter(nodeWriter); |
245 |
|
246 |
netBuilder.writeNetwork(); |
247 |
} catch (DriverException e1) {
|
248 |
// TODO Auto-generated catch block
|
249 |
e1.printStackTrace(); |
250 |
} catch (EditionException e1) {
|
251 |
// TODO Auto-generated catch block
|
252 |
e1.printStackTrace(); |
253 |
} |
254 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "done")); |
255 |
} |
256 |
|
257 |
|
258 |
class GenerateRedNetworkTask extends AbstractMonitorableTask{ |
259 |
FLyrVect layer; |
260 |
File redFile;
|
261 |
NetworkFileRedWriter netBuilder; |
262 |
/**
|
263 |
* Constructor
|
264 |
* */
|
265 |
GenerateRedNetworkTask(FLyrVect layer, File redFile, NetworkFileRedWriter netBuilder){
|
266 |
this.layer = layer;
|
267 |
this.redFile = redFile;
|
268 |
this.netBuilder = netBuilder;
|
269 |
setInitialStep(0);
|
270 |
int numShapes;
|
271 |
try {
|
272 |
numShapes = layer.getSource().getShapeCount(); |
273 |
setFinalStep(numShapes); |
274 |
setDeterminatedProcess(true);
|
275 |
setStatusMessage(PluginServices. |
276 |
getText(this, "Generando_red_a_partir_de_capa_lineal")); |
277 |
} catch (DriverIOException e) {
|
278 |
// TODO Auto-generated catch block
|
279 |
e.printStackTrace(); |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
public void run() throws Exception { |
285 |
netBuilder.setLayer(layer); |
286 |
netBuilder.setCancellableMonitorable(this);
|
287 |
netBuilder.setRedFile(redFile); |
288 |
netBuilder.writeNetwork(); |
289 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "done")); |
290 |
} |
291 |
|
292 |
public String getNote() { |
293 |
return "Procesando linea..." + |
294 |
" " +
|
295 |
getCurrentStep()+ |
296 |
" "+
|
297 |
"de"+
|
298 |
" "+
|
299 |
getFinishStep(); |
300 |
} |
301 |
|
302 |
public void cancel() { |
303 |
setCanceled(true);
|
304 |
} |
305 |
} |
306 |
|
307 |
|
308 |
private void generateRedNetwork(FLyrVect lyr) { |
309 |
lyr.createSpatialIndex(); |
310 |
NetworkFileRedWriter netBuilder = new NetworkFileRedWriter();
|
311 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
312 |
// de di?logo para hecer el mapping.
|
313 |
// Tambi?n un cuadro de di?logo para seleccionar
|
314 |
// en qu? tablas quiere escribir, y su formato
|
315 |
// (dbf, postgres, etc)
|
316 |
|
317 |
ImageIcon icon = new ImageIcon(this. |
318 |
getClass(). |
319 |
getClassLoader(). |
320 |
getResource("images/net-wizard-logo.jpg"));
|
321 |
|
322 |
NetWizard wiz = new NetWizard(icon, lyr);
|
323 |
PluginServices.getMDIManager().addWindow(wiz); |
324 |
if(!wiz.wasFinishPressed())
|
325 |
return;
|
326 |
// try {
|
327 |
String fieldType = wiz.getFieldType();
|
328 |
String fieldLength = wiz.getFieldLength();
|
329 |
String fieldCost = wiz.getFieldCost();
|
330 |
String fieldSense = wiz.getFieldSense();
|
331 |
netBuilder.setLayer(lyr); |
332 |
netBuilder.setFieldType(fieldType); |
333 |
netBuilder.setFieldDist(fieldLength); |
334 |
netBuilder.setFieldSense(fieldSense); |
335 |
netBuilder.setFieldCost(fieldCost); |
336 |
File redFile = new File("c:/" + lyr.getName().replaceAll("\\Q.shp\\E", ".red")); |
337 |
|
338 |
|
339 |
// netBuilder.createRedFile(lyr, redFile);
|
340 |
|
341 |
GenerateRedNetworkTask task = new GenerateRedNetworkTask(lyr, redFile, netBuilder);
|
342 |
PluginServices.cancelableBackgroundExecution(task); |
343 |
|
344 |
|
345 |
// } catch (DriverException e1) {
|
346 |
// // TODO Auto-generated catch block
|
347 |
// e1.printStackTrace();
|
348 |
//
|
349 |
// }
|
350 |
// JOptionPane.showMessageDialog(null, PluginServices.getText(this, "done"));
|
351 |
|
352 |
} |
353 |
|
354 |
|
355 |
public boolean isEnabled() { |
356 |
return true; |
357 |
} |
358 |
|
359 |
public boolean isVisible() { |
360 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
361 |
|
362 |
if (f == null) { |
363 |
return false; |
364 |
} |
365 |
|
366 |
if (f instanceof View) { |
367 |
View vista = (View) f; |
368 |
IProjectView model = vista.getModel(); |
369 |
MapContext mapa = model.getMapContext(); |
370 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
371 |
if (activeLayers.length > 0) |
372 |
if (activeLayers[0] instanceof FLyrVect) |
373 |
return true; |
374 |
} |
375 |
return false; |
376 |
|
377 |
} |
378 |
|
379 |
public IPreference getPreferencesPage() {
|
380 |
return thePreferencePage;
|
381 |
} |
382 |
|
383 |
} |