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