svn-gvsig-desktop / tags / PilotoRedes_Build_4 / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / GenerateNetworkExtension.java @ 12191
History | View | Annotate | Download (15.5 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.util.HashMap; |
46 |
|
47 |
import javax.swing.ImageIcon; |
48 |
import javax.swing.JFileChooser; |
49 |
import javax.swing.JOptionPane; |
50 |
|
51 |
import com.iver.andami.PluginServices; |
52 |
import com.iver.andami.messages.NotificationManager; |
53 |
import com.iver.andami.plugins.Extension; |
54 |
import com.iver.andami.preferences.IPreference; |
55 |
import com.iver.andami.preferences.IPreferenceExtension; |
56 |
import com.iver.andami.ui.mdiManager.IWindow; |
57 |
import com.iver.cit.gvsig.fmap.DriverException; |
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.DriverIOException; |
62 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
63 |
import com.iver.cit.gvsig.fmap.edition.EditionException; |
64 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
65 |
import com.iver.cit.gvsig.fmap.edition.ShpSchemaManager; |
66 |
import com.iver.cit.gvsig.fmap.edition.writers.dbf.DbfWriter; |
67 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.MultiShpWriter; |
68 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
69 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
70 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
71 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
72 |
import com.iver.cit.gvsig.fmap.layers.SingleLayerIterator; |
73 |
import com.iver.cit.gvsig.geoprocess.core.fmap.GeoprocessException; |
74 |
import com.iver.cit.gvsig.geoprocess.core.fmap.XTypes; |
75 |
import com.iver.cit.gvsig.graph.core.writers.NetworkFileRedWriter; |
76 |
import com.iver.cit.gvsig.graph.core.writers.NetworkGvTableWriter; |
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.view.IProjectView; |
80 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
81 |
import com.iver.cit.gvsig.project.documents.view.gui.View; |
82 |
import com.iver.cit.gvsig.project.documents.view.toc.FLyrVectEditPropertiesTocMenuEntry2; |
83 |
import com.iver.cit.gvsig.topology.lineclean.fmap.LineCleanGeoprocess; |
84 |
import com.iver.utiles.SimpleFileFilter; |
85 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
86 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
87 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
88 |
import com.iver.utiles.swing.threads.AbstractMonitorableTask; |
89 |
import com.iver.utiles.swing.threads.IPipedTask; |
90 |
import com.iver.utiles.swing.threads.PipeTask; |
91 |
|
92 |
|
93 |
public class GenerateNetworkExtension extends Extension implements |
94 |
IPreferenceExtension { |
95 |
private static final IPreference thePreferencePage = new RoutePage(); |
96 |
public void initialize() { |
97 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
98 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).remove("FLyrVectEditProperties"); |
99 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).remove("FLyrVectEditProperties2"); |
100 |
((ExtensionPoint) extensionPoints.get("View_TocActions")).put("FLyrVectEditProperties",new FLyrVectEditPropertiesTocMenuEntry2()); |
101 |
//extensionPoints.add("View_TocActions","FLyrVectEditProperties2",new FLyrVectEditPropertiesTocMenuEntry2());
|
102 |
|
103 |
} |
104 |
|
105 |
public void execute(String actionCommand) { |
106 |
IView view = (View) PluginServices.getMDIManager().getActiveWindow();
|
107 |
MapControl mapControl = view.getMapControl(); |
108 |
MapContext map = mapControl.getMapContext(); |
109 |
FLayers tocLyrs = map.getLayers(); |
110 |
SingleLayerIterator lyrIterator = new SingleLayerIterator(tocLyrs);
|
111 |
while (lyrIterator.hasNext()) {
|
112 |
FLayer lyr = lyrIterator.next(); |
113 |
if ((lyr.isActive()) && (lyr instanceof FLyrVect)) { |
114 |
FLyrVect lyrVect = (FLyrVect) lyr; |
115 |
int shapeType;
|
116 |
try {
|
117 |
shapeType = lyrVect.getShapeType(); |
118 |
if (shapeType == FShape.LINE) {
|
119 |
if (actionCommand.equalsIgnoreCase("GENERATE_RED")) { |
120 |
generateRedNetwork(lyrVect, tocLyrs); |
121 |
return;
|
122 |
} |
123 |
} |
124 |
} catch (DriverException e) {
|
125 |
e.printStackTrace(); |
126 |
NotificationManager.addError(e); |
127 |
} |
128 |
|
129 |
} |
130 |
} |
131 |
|
132 |
} |
133 |
|
134 |
private void generateNetwork(FLyrVect lyr) { |
135 |
NetworkGvTableWriter netBuilder = new NetworkGvTableWriter();
|
136 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
137 |
// de di?logo para hecer el mapping.
|
138 |
// Tambi?n un cuadro de di?logo para seleccionar
|
139 |
// en qu? tablas quiere escribir, y su formato
|
140 |
// (dbf, postgres, etc)
|
141 |
String fieldType = "tipored"; |
142 |
String fieldDist = "length"; |
143 |
String fieldSense = "sen"; |
144 |
String fieldCost = "cost"; |
145 |
try {
|
146 |
netBuilder.setLayer(lyr); |
147 |
netBuilder.setFieldType(fieldType); |
148 |
netBuilder.setFieldDist(fieldDist); |
149 |
netBuilder.setFieldSense(fieldSense); |
150 |
netBuilder.setFieldCost(fieldCost); |
151 |
DbfWriter nodeWriter = new DbfWriter();
|
152 |
nodeWriter.setFile(new File("c:/nodes.dbf")); |
153 |
|
154 |
DbfWriter edgeWriter = new DbfWriter();
|
155 |
edgeWriter.setFile(new File("c:/edges.dbf")); |
156 |
|
157 |
netBuilder.setEdgeWriter(edgeWriter); |
158 |
netBuilder.setNodeWriter(nodeWriter); |
159 |
|
160 |
netBuilder.writeNetwork(); |
161 |
} catch (DriverException e1) {
|
162 |
// TODO Auto-generated catch block
|
163 |
e1.printStackTrace(); |
164 |
} catch (EditionException e1) {
|
165 |
// TODO Auto-generated catch block
|
166 |
e1.printStackTrace(); |
167 |
} |
168 |
JOptionPane.showMessageDialog(null, PluginServices |
169 |
.getText(this, "done")); |
170 |
} |
171 |
|
172 |
class GenerateRedNetworkAfterCleanTask |
173 |
extends AbstractMonitorableTask implements IPipedTask{ |
174 |
|
175 |
File redFile;
|
176 |
NetworkFileRedWriter netBuilder; |
177 |
|
178 |
FLyrVect inputLayer; |
179 |
FLyrVect pseudonodes; |
180 |
FLayers tocLyrs; |
181 |
|
182 |
/**
|
183 |
* Constructor
|
184 |
* @param tocLyrs
|
185 |
*/
|
186 |
GenerateRedNetworkAfterCleanTask(NetworkFileRedWriter netBuilder, FLayers tocLyrs) { |
187 |
this.netBuilder = netBuilder;
|
188 |
this.tocLyrs = tocLyrs;
|
189 |
setInitialStep(0);
|
190 |
setDeterminatedProcess(true);
|
191 |
setStatusMessage(PluginServices.getText(this,
|
192 |
"Generando_red_a_partir_de_capa_lineal"));
|
193 |
} |
194 |
|
195 |
public void run() throws Exception { |
196 |
int numShapes;
|
197 |
try {
|
198 |
numShapes = inputLayer.getSource().getShapeCount(); |
199 |
// lo del 10 es para que termine despu?s de
|
200 |
// escribir los puntos
|
201 |
setFinalStep(numShapes + 10);
|
202 |
|
203 |
|
204 |
} catch (DriverIOException e) {
|
205 |
// TODO Auto-generated catch block
|
206 |
e.printStackTrace(); |
207 |
} |
208 |
netBuilder.setLayer(inputLayer); |
209 |
netBuilder.setCancellableMonitorable(this);
|
210 |
netBuilder.setRedFile(redFile); |
211 |
netBuilder.writeNetwork(); |
212 |
tocLyrs.addLayer(inputLayer); |
213 |
tocLyrs.addLayer(pseudonodes); |
214 |
enableControls(); |
215 |
} |
216 |
|
217 |
public String getNote() { |
218 |
String processText = PluginServices.getText(this, "Procesando_linea"); |
219 |
String of = PluginServices.getText(this, "de"); |
220 |
return processText + " " + getCurrentStep() + " " + of |
221 |
+ " " + getFinishStep();
|
222 |
} |
223 |
|
224 |
public void cancel() { |
225 |
setCanceled(true);
|
226 |
} |
227 |
|
228 |
public boolean isFinished() { |
229 |
return (getCurrentStep() >= getFinalStep());
|
230 |
} |
231 |
|
232 |
/* (non-Javadoc)
|
233 |
* @see com.iver.utiles.swing.threads.IPipedTask#getResult()
|
234 |
*/
|
235 |
public Object getResult() { |
236 |
// TODO Auto-generated method stub
|
237 |
return null; |
238 |
} |
239 |
|
240 |
/**
|
241 |
* Implementation of PipeTask interface
|
242 |
* */
|
243 |
public void setEntry(Object object) { |
244 |
//The previous task of this piped task is clean geoprocess
|
245 |
//whose result es FLayers with two layers
|
246 |
//first layer has cleaned layer
|
247 |
//and second layer has pseudonodes layer
|
248 |
FLayers layers = (FLayers) object; |
249 |
this.inputLayer = (FLyrVect) layers.getLayer(0); |
250 |
inputLayer.createSpatialIndex(); |
251 |
this.redFile = NetworkUtils.getNetworkFile(inputLayer);
|
252 |
this.pseudonodes = (FLyrVect) layers.getLayer(1); |
253 |
} |
254 |
} |
255 |
|
256 |
public void enableControls(){ |
257 |
PluginServices.backgroundExecution(new Runnable(){ |
258 |
public void run() { |
259 |
PluginServices.getMainFrame().enableControls(); |
260 |
|
261 |
}}); |
262 |
} |
263 |
|
264 |
|
265 |
class GenerateRedNetworkTask extends AbstractMonitorableTask { |
266 |
FLyrVect layer; |
267 |
|
268 |
File redFile;
|
269 |
|
270 |
NetworkFileRedWriter netBuilder; |
271 |
|
272 |
|
273 |
|
274 |
/**
|
275 |
* Constructor
|
276 |
*/
|
277 |
GenerateRedNetworkTask(FLyrVect layer, File redFile,
|
278 |
NetworkFileRedWriter netBuilder) { |
279 |
this.layer = layer;
|
280 |
if(! layer.isSpatiallyIndexed())
|
281 |
layer.createSpatialIndex(); |
282 |
|
283 |
this.redFile = redFile;
|
284 |
this.netBuilder = netBuilder;
|
285 |
setInitialStep(0);
|
286 |
int numShapes;
|
287 |
try {
|
288 |
numShapes = layer.getSource().getShapeCount(); |
289 |
// lo del 10 es porque escribimos los nodos despu?s de
|
290 |
// los tramos.
|
291 |
setFinalStep(numShapes + 10);
|
292 |
setDeterminatedProcess(true);
|
293 |
setStatusMessage(PluginServices.getText(this,
|
294 |
"Generando_red_a_partir_de_capa_lineal"));
|
295 |
} catch (DriverIOException e) {
|
296 |
// TODO Auto-generated catch block
|
297 |
e.printStackTrace(); |
298 |
} |
299 |
|
300 |
} |
301 |
|
302 |
public void run() throws Exception { |
303 |
netBuilder.setLayer(layer); |
304 |
netBuilder.setCancellableMonitorable(this);
|
305 |
netBuilder.setRedFile(redFile); |
306 |
netBuilder.writeNetwork(); |
307 |
enableControls(); |
308 |
} |
309 |
|
310 |
public String getNote() { |
311 |
String processText = PluginServices.getText(this, "Procesando_linea"); |
312 |
String of = PluginServices.getText(this, "de"); |
313 |
return processText + " " + getCurrentStep() + " " + of |
314 |
+ " " + getFinishStep();
|
315 |
} |
316 |
|
317 |
public void cancel() { |
318 |
setCanceled(true);
|
319 |
} |
320 |
|
321 |
public Object getResult(){ |
322 |
return null; |
323 |
|
324 |
} |
325 |
public void setEntry(Object object){ |
326 |
this.layer = (FLyrVect) object;
|
327 |
} |
328 |
} |
329 |
|
330 |
/**
|
331 |
* It returns a geoprocess to make a CLEAN of the input layer
|
332 |
* */
|
333 |
private LineCleanGeoprocess createCleanGeoprocess(FLyrVect lineLyr) {
|
334 |
File outputFile = null; |
335 |
JOptionPane.showMessageDialog(null, |
336 |
PluginServices.getText(null, "Especifique_fichero_shp_resultante"), |
337 |
PluginServices.getText(null, "Fichero_para_capa_corregida"), |
338 |
JOptionPane.INFORMATION_MESSAGE);
|
339 |
JFileChooser jfc = new JFileChooser(); |
340 |
SimpleFileFilter filterShp = new SimpleFileFilter("shp", PluginServices |
341 |
.getText(this, "shp_files")); |
342 |
jfc.setFileFilter(filterShp); |
343 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
344 |
File newFile = jfc.getSelectedFile();
|
345 |
String path = newFile.getAbsolutePath();
|
346 |
if (newFile.exists()) {
|
347 |
int resp = JOptionPane.showConfirmDialog( |
348 |
(Component) PluginServices.getMainFrame(),
|
349 |
PluginServices.getText(this,
|
350 |
"fichero_ya_existe_seguro_desea_guardarlo"),
|
351 |
PluginServices.getText(this, "guardar"), |
352 |
JOptionPane.YES_NO_OPTION);
|
353 |
if (resp != JOptionPane.YES_OPTION) { |
354 |
return null; |
355 |
} |
356 |
}// if
|
357 |
if (!(path.toLowerCase().endsWith(".shp"))) { |
358 |
path = path + ".shp";
|
359 |
} |
360 |
outputFile = new File(path); |
361 |
} else {
|
362 |
return null; |
363 |
} |
364 |
LineCleanGeoprocess geoprocess = new LineCleanGeoprocess(lineLyr);
|
365 |
SHPLayerDefinition definition = (SHPLayerDefinition) geoprocess |
366 |
.createLayerDefinition(); |
367 |
definition.setFile(outputFile); |
368 |
ShpSchemaManager schemaManager = new ShpSchemaManager(outputFile
|
369 |
.getAbsolutePath()); |
370 |
IWriter writer = null;
|
371 |
try {
|
372 |
int shapeType = definition.getShapeType();
|
373 |
if (shapeType != XTypes.MULTI) {
|
374 |
writer = new ShpWriter();
|
375 |
((ShpWriter) writer).setFile(definition.getFile()); |
376 |
writer.initialize(definition); |
377 |
} else {
|
378 |
writer = new MultiShpWriter();
|
379 |
((MultiShpWriter) writer).setFile(definition.getFile()); |
380 |
writer.initialize(definition); |
381 |
} |
382 |
} catch (Exception e1) { |
383 |
String error = PluginServices.getText(this, |
384 |
"Error_escritura_resultados");
|
385 |
String errorDescription = PluginServices.getText(this, |
386 |
"Error_preparar_escritura_resultados");
|
387 |
return null; |
388 |
} |
389 |
geoprocess.setResultLayerProperties(writer, schemaManager); |
390 |
HashMap params = new HashMap(); |
391 |
params.put("layer_selection", new Boolean(false)); |
392 |
try {
|
393 |
geoprocess.setParameters(params); |
394 |
geoprocess.checkPreconditions(); |
395 |
return geoprocess;
|
396 |
|
397 |
} catch (GeoprocessException e) {
|
398 |
String error = PluginServices.getText(this, "Error_ejecucion"); |
399 |
String errorDescription = PluginServices.getText(this, |
400 |
"Error_fallo_geoproceso");
|
401 |
return null; |
402 |
} |
403 |
|
404 |
} |
405 |
|
406 |
private void generateRedNetwork(FLyrVect lyr, FLayers tocLyrs) { |
407 |
lyr.createSpatialIndex(); |
408 |
NetworkFileRedWriter netBuilder = new NetworkFileRedWriter();
|
409 |
// Por ahora, a pelo, pero hay que sacar un cuadro
|
410 |
// de di?logo para hecer el mapping.
|
411 |
// Tambi?n un cuadro de di?logo para seleccionar
|
412 |
// en qu? tablas quiere escribir, y su formato
|
413 |
// (dbf, postgres, etc)
|
414 |
|
415 |
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader() |
416 |
.getResource("images/net-wizard-logo.jpg"));
|
417 |
|
418 |
NetWizard wiz = new NetWizard(icon, lyr);
|
419 |
PluginServices.getMDIManager().addWindow(wiz); |
420 |
if (!wiz.wasFinishPressed())
|
421 |
return;
|
422 |
// try {
|
423 |
String fieldType = wiz.getFieldType();
|
424 |
String fieldLength = wiz.getFieldLength();
|
425 |
String fieldCost = wiz.getFieldCost();
|
426 |
String fieldSense = wiz.getFieldSense();
|
427 |
netBuilder.setLayer(lyr); |
428 |
netBuilder.setFieldType(fieldType); |
429 |
netBuilder.setFieldDist(fieldLength); |
430 |
netBuilder.setFieldSense(fieldSense); |
431 |
netBuilder.setFieldCost(fieldCost); |
432 |
File redFile = NetworkUtils.getNetworkFile(lyr);
|
433 |
|
434 |
boolean cleanOrigLyr = wiz.getCleanOriginalLayer();
|
435 |
LineCleanGeoprocess clean = null;
|
436 |
if(cleanOrigLyr)
|
437 |
clean = createCleanGeoprocess(lyr); |
438 |
boolean applySnap = wiz.getApplySnapTolerance();
|
439 |
if(applySnap){
|
440 |
double snapTolerance = wiz.getSnapTolerance();
|
441 |
netBuilder.setSnapTolerance(snapTolerance); |
442 |
} |
443 |
if(clean != null){ |
444 |
//we wont start the process of network creation
|
445 |
//until clean geoprocess will be finished
|
446 |
IPipedTask cleanTask = (IPipedTask) clean.createTask(); |
447 |
GenerateRedNetworkAfterCleanTask task = |
448 |
new GenerateRedNetworkAfterCleanTask(netBuilder, tocLyrs);
|
449 |
|
450 |
PipeTask pipe = new PipeTask(cleanTask, (IPipedTask)task);
|
451 |
|
452 |
PluginServices.cancelableBackgroundExecution(pipe); |
453 |
// PluginServices.cancelableBackgroundExecution(task);
|
454 |
|
455 |
}else{
|
456 |
GenerateRedNetworkTask task = new GenerateRedNetworkTask(lyr, redFile,
|
457 |
netBuilder); |
458 |
PluginServices.cancelableBackgroundExecution(task); |
459 |
} |
460 |
} |
461 |
|
462 |
public boolean isEnabled() { |
463 |
return true; |
464 |
} |
465 |
|
466 |
public boolean isVisible() { |
467 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
468 |
|
469 |
if (f == null) { |
470 |
return false; |
471 |
} |
472 |
|
473 |
if (f instanceof View) { |
474 |
View vista = (View) f; |
475 |
IProjectView model = vista.getModel(); |
476 |
MapContext mapa = model.getMapContext(); |
477 |
FLayer[] activeLayers = mapa.getLayers().getActives();
|
478 |
if (activeLayers.length > 0) |
479 |
if (activeLayers[0] instanceof FLyrVect) { |
480 |
FLyrVect lyrVect = (FLyrVect) activeLayers[0];
|
481 |
int shapeType;
|
482 |
try {
|
483 |
shapeType = lyrVect.getShapeType(); |
484 |
if (shapeType == FShape.LINE)
|
485 |
return true; |
486 |
} catch (DriverException e) {
|
487 |
// TODO Auto-generated catch block
|
488 |
e.printStackTrace(); |
489 |
} |
490 |
} |
491 |
} |
492 |
return false; |
493 |
|
494 |
} |
495 |
|
496 |
public IPreference getPreferencesPage() {
|
497 |
return thePreferencePage;
|
498 |
} |
499 |
|
500 |
} |