gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / core / PostProcessTaskRunnable.java @ 466
History | View | Annotate | Download (16.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.geoprocess.lib.sextante.core; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.util.List; |
28 |
|
29 |
import javax.swing.BorderFactory; |
30 |
import javax.swing.JOptionPane; |
31 |
import javax.swing.JScrollPane; |
32 |
import javax.swing.JTextPane; |
33 |
import javax.swing.ScrollPaneConstants; |
34 |
import javax.swing.SwingUtilities; |
35 |
import javax.swing.border.BevelBorder; |
36 |
|
37 |
import org.cresques.cts.IProjection; |
38 |
import org.gvsig.andami.PluginServices; |
39 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
40 |
import org.gvsig.app.ApplicationLocator; |
41 |
import org.gvsig.app.extension.ProjectExtension; |
42 |
import org.gvsig.app.project.Project; |
43 |
import org.gvsig.app.project.ProjectManager; |
44 |
import org.gvsig.app.project.documents.Document; |
45 |
import org.gvsig.app.project.documents.table.TableDocument; |
46 |
import org.gvsig.app.project.documents.view.ViewDocument; |
47 |
import org.gvsig.app.project.documents.view.ViewManager; |
48 |
import org.gvsig.app.project.documents.view.gui.IView; |
49 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
50 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
51 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
52 |
import org.gvsig.fmap.mapcontext.MapContext; |
53 |
import org.gvsig.fmap.mapcontext.exceptions.ReloadLayerException; |
54 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
55 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
56 |
import org.gvsig.fmap.mapcontext.layers.LayersIterator; |
57 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
58 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FileTools; |
59 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
60 |
|
61 |
import es.unex.sextante.core.GeoAlgorithm; |
62 |
import es.unex.sextante.core.ObjectAndDescription; |
63 |
import es.unex.sextante.core.OutputObjectsSet; |
64 |
import es.unex.sextante.core.ParametersSet; |
65 |
import es.unex.sextante.core.Sextante; |
66 |
import es.unex.sextante.dataObjects.ILayer; |
67 |
import es.unex.sextante.dataObjects.IRasterLayer; |
68 |
import es.unex.sextante.dataObjects.IVectorLayer; |
69 |
import es.unex.sextante.gui.additionalResults.AdditionalResults; |
70 |
import es.unex.sextante.gui.algorithm.iterative.SingleFeatureVectorLayer; |
71 |
import es.unex.sextante.gui.core.SextanteGUI; |
72 |
import es.unex.sextante.gui.settings.SextanteGeneralSettings; |
73 |
import es.unex.sextante.outputs.FileOutputChannel; |
74 |
import es.unex.sextante.outputs.IOutputChannel; |
75 |
import es.unex.sextante.outputs.NullOutputChannel; |
76 |
import es.unex.sextante.outputs.Output; |
77 |
import es.unex.sextante.outputs.Output3DRasterLayer; |
78 |
import es.unex.sextante.outputs.OutputRasterLayer; |
79 |
import es.unex.sextante.outputs.OutputTable; |
80 |
import es.unex.sextante.outputs.OutputText; |
81 |
import es.unex.sextante.outputs.OutputVectorLayer; |
82 |
import es.unex.sextante.outputs.OverwriteOutputChannel; |
83 |
import es.unex.sextante.parameters.Parameter; |
84 |
import es.unex.sextante.parameters.RasterLayerAndBand; |
85 |
|
86 |
public class PostProcessTaskRunnable implements Runnable { |
87 |
|
88 |
private MapContext m_MapContext;
|
89 |
private final GeoAlgorithm m_Algorithm; |
90 |
private final OutputObjectsSet m_Output; |
91 |
private final boolean m_bShowResultsWindows; |
92 |
|
93 |
public PostProcessTaskRunnable(final GeoAlgorithm algorithm, |
94 |
final boolean bShowResultsWindow) { |
95 |
|
96 |
m_Output = algorithm.getOutputObjects(); |
97 |
m_Algorithm = algorithm; |
98 |
m_bShowResultsWindows = bShowResultsWindow; |
99 |
|
100 |
} |
101 |
|
102 |
public void run() { |
103 |
|
104 |
if (m_Output.hasLayers()) {
|
105 |
setOutputView(); |
106 |
} |
107 |
|
108 |
addResults(); |
109 |
|
110 |
} |
111 |
|
112 |
private void setOutputView() { |
113 |
|
114 |
ViewDocument view = null;
|
115 |
final ParametersSet parameters = m_Algorithm.getParameters();
|
116 |
for (int i = 0; i < parameters.getNumberOfParameters(); i++) { |
117 |
final Parameter param = parameters.getParameter(i);
|
118 |
final Object object = param.getParameterValueAsObject(); |
119 |
if (object instanceof ILayer) { |
120 |
view = getViewFromLayer((ILayer) object); |
121 |
m_MapContext = view.getMapContext(); |
122 |
return;
|
123 |
} else
|
124 |
if (object instanceof List) { |
125 |
final List list = (List) object; |
126 |
for (int j = 0; j < list.size(); j++) { |
127 |
final Object obj = list.get(j); |
128 |
if (obj instanceof ILayer) { |
129 |
view = getViewFromLayer((ILayer) obj); |
130 |
m_MapContext = view.getMapContext(); |
131 |
return;
|
132 |
} else
|
133 |
if (obj instanceof RasterLayerAndBand) { |
134 |
final RasterLayerAndBand rlab =
|
135 |
(RasterLayerAndBand) obj; |
136 |
view = getViewFromLayer(rlab.getRasterLayer()); |
137 |
m_MapContext = view.getMapContext(); |
138 |
return;
|
139 |
} |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
// if there is no input view, ask the user
|
145 |
final Project project =
|
146 |
((ProjectExtension) PluginServices |
147 |
.getExtension(ProjectExtension.class)).getProject(); |
148 |
final List<Document> views = project.getDocuments(ViewManager.TYPENAME); |
149 |
final Object[] options = new Object[views.size() + 1]; |
150 |
options[0] = Sextante.getText("Create_new_view"); |
151 |
for (int i = 0; i < views.size(); i++) { |
152 |
options[i + 1] = views.get(i);
|
153 |
} |
154 |
|
155 |
final Object selectedObject = |
156 |
JOptionPane.showInputDialog(null, |
157 |
Sextante.getText("Select_output_view"),
|
158 |
Sextante.getText("Output_view"), JOptionPane.PLAIN_MESSAGE, |
159 |
null, options, null); |
160 |
|
161 |
if (selectedObject instanceof IView) { |
162 |
m_MapContext = |
163 |
((IView) selectedObject).getMapControl().getMapContext(); |
164 |
} else {
|
165 |
final Document newView = |
166 |
ProjectManager.getInstance().createDocument( |
167 |
ViewManager.TYPENAME, |
168 |
"SEXTANTE (" + m_Algorithm.getName() + ")"); |
169 |
((ProjectExtension) PluginServices |
170 |
.getExtension(ProjectExtension.class)).getProject() |
171 |
.add(newView); |
172 |
final IWindow window = newView.getFactory().getMainWindow(newView);
|
173 |
final Runnable doWorkRunnable = new Runnable() { |
174 |
|
175 |
public void run() { |
176 |
PluginServices.getMDIManager().addWindow(window); |
177 |
m_MapContext = ((ViewDocument) newView).getMapContext(); |
178 |
} |
179 |
}; |
180 |
try {
|
181 |
SwingUtilities.invokeAndWait(doWorkRunnable);
|
182 |
} catch (final Exception e) { |
183 |
Sextante.addErrorToLog(e); |
184 |
} |
185 |
} |
186 |
|
187 |
} |
188 |
|
189 |
private ViewDocument getViewFromLayer(ILayer layer) {
|
190 |
|
191 |
if (layer instanceof SingleFeatureVectorLayer) { |
192 |
layer = ((SingleFeatureVectorLayer) layer).getOriginalLayer(); |
193 |
} |
194 |
|
195 |
final FLayer gvSIGBaseLayer = (FLayer) layer.getBaseDataObject();
|
196 |
final Project project =
|
197 |
((ProjectExtension) PluginServices |
198 |
.getExtension(ProjectExtension.class)).getProject(); |
199 |
final List<Document> docs = project.getDocuments(ViewManager.TYPENAME); |
200 |
|
201 |
for (int i = 0; i < docs.size(); i++) { |
202 |
final ViewDocument viewDoc = (ViewDocument) docs.get(i);
|
203 |
final FLayers layers = viewDoc.getMapContext().getLayers();
|
204 |
final LayersIterator iter = new LayersIterator(layers); |
205 |
while (iter.hasNext()) {
|
206 |
final FLayer gvSIGLayer = iter.nextLayer();
|
207 |
if (gvSIGLayer.equals(gvSIGBaseLayer)) {
|
208 |
return viewDoc;
|
209 |
} |
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
return null; |
215 |
|
216 |
} |
217 |
|
218 |
private void addResults() { |
219 |
|
220 |
final boolean bUseInternalNames = |
221 |
new Boolean( |
222 |
SextanteGUI |
223 |
.getSettingParameterValue(SextanteGeneralSettings.USE_INTERNAL_NAMES)) |
224 |
.booleanValue(); |
225 |
final boolean bModiFyResultsNames = |
226 |
new Boolean( |
227 |
SextanteGUI |
228 |
.getSettingParameterValue(SextanteGeneralSettings.MODIFY_NAMES)) |
229 |
.booleanValue(); |
230 |
|
231 |
String sDescription;
|
232 |
boolean bInvalidate = false; |
233 |
boolean bShowAdditionalPanel = false; |
234 |
|
235 |
if (m_MapContext != null) { |
236 |
m_MapContext.beginAtomicEvent(); |
237 |
} |
238 |
|
239 |
for (int i = 0; i < m_Output.getOutputObjectsCount(); i++) { |
240 |
|
241 |
final Output out = m_Output.getOutput(i);
|
242 |
sDescription = out.getDescription(); |
243 |
final IOutputChannel channel = out.getOutputChannel();
|
244 |
final Object object = out.getOutputObject(); |
245 |
|
246 |
if ((out instanceof OutputRasterLayer) |
247 |
|| (out instanceof Output3DRasterLayer)
|
248 |
|| (out instanceof OutputTable)
|
249 |
|| (out instanceof OutputVectorLayer)) {
|
250 |
if (bUseInternalNames) {
|
251 |
sDescription = out.getName(); |
252 |
} else
|
253 |
if (bModiFyResultsNames) {
|
254 |
sDescription = |
255 |
SextanteGUI.modifyResultName(sDescription); |
256 |
} |
257 |
if ((channel instanceof NullOutputChannel) || (channel == null)) { |
258 |
continue;
|
259 |
} |
260 |
} |
261 |
if (out instanceof OutputVectorLayer) { |
262 |
String sFilename = null; |
263 |
if (channel instanceof FileOutputChannel) { |
264 |
sFilename = ((FileOutputChannel) channel).getFilename(); |
265 |
final FLyrVect flayer =
|
266 |
(FLyrVect) FileTools.openLayer(sFilename, sDescription, |
267 |
(IProjection) m_Algorithm.getOutputCRS()); |
268 |
if (flayer != null) { |
269 |
flayer.setName(sDescription); |
270 |
m_MapContext.getLayers().addLayer(flayer); |
271 |
bInvalidate = true;
|
272 |
} |
273 |
|
274 |
} else
|
275 |
if (channel instanceof OverwriteOutputChannel) { |
276 |
// TODO:add support for non file based layer
|
277 |
final FLyrVect flayer =
|
278 |
(FLyrVect) ((OverwriteOutputChannel) channel) |
279 |
.getLayer().getBaseDataObject(); |
280 |
try {
|
281 |
flayer.reload(); |
282 |
bInvalidate = true;
|
283 |
} catch (final ReloadLayerException e) { |
284 |
|
285 |
} |
286 |
} |
287 |
|
288 |
if (object != null) { |
289 |
((IVectorLayer) object).close(); |
290 |
} |
291 |
|
292 |
} else
|
293 |
if (out instanceof OutputTable) { |
294 |
try {
|
295 |
final String sFilename = |
296 |
((FileOutputChannel) channel).getFilename(); |
297 |
final TableDocument table =
|
298 |
FileTools.openTable(sFilename, sDescription); |
299 |
if (table != null) { |
300 |
table.setName(sDescription); |
301 |
ApplicationLocator.getManager().getProjectManager() |
302 |
.getCurrentProject().add(table); |
303 |
|
304 |
// final JScrollPane jScrollPane =
|
305 |
// TableTools
|
306 |
// .getScrollableTablePanelFromITable((ITable)
|
307 |
// object);
|
308 |
// AdditionalResults
|
309 |
// .addComponent(new ObjectAndDescription(
|
310 |
// sDescription, jScrollPane));
|
311 |
// bShowAdditionalPanel = true;
|
312 |
} |
313 |
} catch (final Exception e) { |
314 |
Sextante.addErrorToLog(e); |
315 |
} |
316 |
} else
|
317 |
if (out instanceof OutputRasterLayer) { |
318 |
final IRasterLayer rasterLayer = (IRasterLayer) object;
|
319 |
if (channel instanceof FileOutputChannel) { |
320 |
final String sFilename = |
321 |
((FileOutputChannel) channel).getFilename(); |
322 |
final FLyrRaster flayer =
|
323 |
(FLyrRaster) FileTools.openLayer( |
324 |
sFilename, |
325 |
sDescription, |
326 |
(IProjection) m_Algorithm.getOutputCRS()); |
327 |
if (flayer != null) { |
328 |
if (rasterLayer != null) { |
329 |
try {
|
330 |
NoData nodata = RasterLocator.getManager().getDataStructFactory().createNoData( |
331 |
rasterLayer.getNoDataValue(), |
332 |
null,
|
333 |
sFilename); |
334 |
if(flayer.getDataStore().getDataType()[0] == Buffer.TYPE_BYTE) |
335 |
nodata.setNoDataTransparent(false);
|
336 |
flayer.setNoDataValue(nodata); |
337 |
nodata.save(); |
338 |
|
339 |
rasterLayer.close(); |
340 |
} catch (final Exception e) { |
341 |
Sextante.addErrorToLog(e); |
342 |
} |
343 |
} |
344 |
flayer.setName(sDescription); |
345 |
m_MapContext.getLayers().addLayer(flayer); |
346 |
bInvalidate = true;
|
347 |
|
348 |
} |
349 |
} |
350 |
} else
|
351 |
if (out instanceof OutputText) { |
352 |
JTextPane jTextPane;
|
353 |
JScrollPane jScrollPane;
|
354 |
jTextPane = new JTextPane(); |
355 |
jTextPane.setEditable(false);
|
356 |
jTextPane.setContentType("text/html");
|
357 |
jTextPane.setText((String) object);
|
358 |
jScrollPane = new JScrollPane(); |
359 |
jScrollPane.setViewportView(jTextPane); |
360 |
jScrollPane |
361 |
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
|
362 |
jTextPane.setBorder(BorderFactory
|
363 |
.createEtchedBorder(BevelBorder.LOWERED));
|
364 |
AdditionalResults |
365 |
.addComponent(new ObjectAndDescription(
|
366 |
sDescription, jScrollPane)); |
367 |
bShowAdditionalPanel = true;
|
368 |
} else
|
369 |
if (object instanceof Component) { |
370 |
AdditionalResults |
371 |
.addComponent(new ObjectAndDescription(
|
372 |
sDescription, object)); |
373 |
bShowAdditionalPanel = true;
|
374 |
} else
|
375 |
if (out instanceof Output3DRasterLayer) { |
376 |
JOptionPane.showMessageDialog(
|
377 |
SextanteGUI.getMainFrame(), |
378 |
Sextante.getText("3d_not_supported"),
|
379 |
Sextante.getText("Warning"),
|
380 |
JOptionPane.WARNING_MESSAGE);
|
381 |
} |
382 |
|
383 |
} |
384 |
|
385 |
if (m_MapContext != null) { |
386 |
m_MapContext.endAtomicEvent(); |
387 |
} |
388 |
|
389 |
if (bInvalidate) {
|
390 |
m_MapContext.invalidate(); |
391 |
} |
392 |
|
393 |
if (bShowAdditionalPanel && m_bShowResultsWindows) {
|
394 |
AdditionalResults.showPanel(); |
395 |
} |
396 |
|
397 |
} |
398 |
} |