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