gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / GeoProcessBuilerBase.java @ 1059
History | View | Annotate | Download (18.5 KB)
1 |
package org.gvsig.geoprocess.lib.sextante; |
---|---|
2 |
|
3 |
import es.unex.sextante.additionalInfo.AdditionalInfo; |
4 |
import es.unex.sextante.core.AnalysisExtent; |
5 |
import es.unex.sextante.core.GeoAlgorithm; |
6 |
import es.unex.sextante.core.OutputFactory; |
7 |
import es.unex.sextante.core.OutputObjectsSet; |
8 |
import es.unex.sextante.core.ParametersSet; |
9 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
10 |
import es.unex.sextante.exceptions.WrongOutputIDException; |
11 |
import es.unex.sextante.exceptions.WrongParameterIDException; |
12 |
import es.unex.sextante.outputs.FileOutputChannel; |
13 |
import es.unex.sextante.outputs.Output; |
14 |
import es.unex.sextante.outputs.OutputRasterLayer; |
15 |
import es.unex.sextante.outputs.OutputTable; |
16 |
import es.unex.sextante.outputs.OutputVectorLayer; |
17 |
import es.unex.sextante.parameters.Parameter; |
18 |
import es.unex.sextante.parameters.ParameterNumericalValue; |
19 |
import es.unex.sextante.parameters.ParameterRasterLayer; |
20 |
import es.unex.sextante.parameters.ParameterString; |
21 |
import es.unex.sextante.parameters.ParameterVectorLayer; |
22 |
import java.io.File; |
23 |
import java.text.MessageFormat; |
24 |
import java.util.ArrayList; |
25 |
import java.util.Iterator; |
26 |
import java.util.List; |
27 |
import org.gvsig.fmap.dal.DALLocator; |
28 |
import org.gvsig.fmap.dal.DataManager; |
29 |
import org.gvsig.fmap.dal.DataStoreParameters; |
30 |
import org.gvsig.fmap.dal.exception.ReadException; |
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
32 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
33 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
34 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
35 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
36 |
import org.gvsig.fmap.geom.primitive.Envelope; |
37 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
38 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
39 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
40 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
41 |
import org.gvsig.geoprocess.lib.api.GeoProcessBuiler; |
42 |
import org.gvsig.geoprocess.lib.api.GeoProcessLocator; |
43 |
import org.gvsig.geoprocess.lib.sextante.core.DefaultOutputFactory; |
44 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FLyrRasterIRasterLayer; |
45 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
46 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.TableDocumentITable; |
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
public class GeoProcessBuilerBase implements GeoProcessBuiler { |
51 |
|
52 |
private static final Logger logger = LoggerFactory.getLogger(GeoProcessBuilerBase.class); |
53 |
|
54 |
private class ParameterInfoBase implements ParameterInfo { |
55 |
|
56 |
private final Parameter param; |
57 |
|
58 |
public ParameterInfoBase(Parameter param) {
|
59 |
this.param = param;
|
60 |
} |
61 |
|
62 |
@Override
|
63 |
public String getName() { |
64 |
return this.param.getParameterName(); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
public String getTypeName() { |
69 |
return this.param.getParameterTypeName(); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public String getDescription() { |
74 |
return this.param.getParameterDescription(); |
75 |
} |
76 |
|
77 |
@Override
|
78 |
public String getTooltip() { |
79 |
return this.param.getParameterTooltip(); |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Class getParameterClass() { |
84 |
return this.param.getParameterClass(); |
85 |
} |
86 |
|
87 |
public String getDetailedDescription() { |
88 |
try {
|
89 |
AdditionalInfo x = this.param.getParameterAdditionalInfo();
|
90 |
if( x == null ) { |
91 |
return null; |
92 |
} |
93 |
return x.getTextDescription();
|
94 |
} catch (Exception ex) { |
95 |
return null; |
96 |
} |
97 |
} |
98 |
|
99 |
|
100 |
} |
101 |
|
102 |
private class OutputAccesorBase implements OutputAccesor { |
103 |
|
104 |
private Output getOutput(String name) { |
105 |
OutputObjectsSet oos = algorithm.getOutputObjects(); |
106 |
Output oo; |
107 |
try {
|
108 |
oo = oos.getOutput(name); |
109 |
} catch (WrongOutputIDException ex) {
|
110 |
throw new IllegalArgumentException( |
111 |
MessageFormat.format(
|
112 |
"Can't access to output data ''{0}'' of the geoprocess ''{1}''.",
|
113 |
name, |
114 |
getName() |
115 |
) |
116 |
); |
117 |
} |
118 |
return oo;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public Object get(String name) { |
123 |
Output oo = this.getOutput(name);
|
124 |
Object value;
|
125 |
if (oo instanceof OutputRasterLayer) { |
126 |
value = this.getRasterLayer(name);
|
127 |
|
128 |
} else if (oo instanceof OutputVectorLayer) { |
129 |
value = this.getVectorLayer(name);
|
130 |
|
131 |
} else if (oo instanceof OutputTable) { |
132 |
value = this.getFeatureStore(name);
|
133 |
|
134 |
} else {
|
135 |
value = oo.getOutputObject(); |
136 |
} |
137 |
return value;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public File getFile(String name) { |
142 |
Output oo = this.getOutput(name);
|
143 |
FileOutputChannel channel = (FileOutputChannel) oo.getOutputChannel(); |
144 |
File f = new File(channel.getFilename()); |
145 |
return f;
|
146 |
} |
147 |
|
148 |
@Override
|
149 |
public boolean getBoolean(String name) { |
150 |
Object o = this.get(name); |
151 |
return (Boolean) o; |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public int getInt(String name) { |
156 |
Object o = this.get(name); |
157 |
return ((Number) o).intValue(); |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public double getDouble(String name) { |
162 |
Object o = this.get(name); |
163 |
return ((Number) o).doubleValue(); |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public String getString(String name) { |
168 |
Object o = this.get(name); |
169 |
return (String) o; |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public FeatureStore getFeatureStore(String name) { |
174 |
Output oo = this.getOutput(name);
|
175 |
if (oo instanceof OutputTable) { |
176 |
TableDocumentITable table = (TableDocumentITable) oo.getOutputObject(); |
177 |
table.postProcess(); |
178 |
FeatureStore store = table.getFeatureStore(); |
179 |
return store;
|
180 |
|
181 |
} else if (oo instanceof OutputVectorLayer) { |
182 |
FlyrVectIVectorLayer slayer = (FlyrVectIVectorLayer) oo.getOutputObject(); |
183 |
slayer.postProcess(); |
184 |
FeatureStore store = slayer.getFeatureStore(); |
185 |
return store;
|
186 |
} |
187 |
return null; |
188 |
} |
189 |
|
190 |
@Override
|
191 |
public FLyrVect getVectorLayer(String name) { |
192 |
try {
|
193 |
FeatureStore store = this.getFeatureStore(name);
|
194 |
FLyrVect layer = (FLyrVect) MapContextLocator.getMapContextManager().createLayer( |
195 |
store.getName(), |
196 |
store |
197 |
); |
198 |
return layer;
|
199 |
} catch (LoadLayerException ex) {
|
200 |
String s = MessageFormat.format( |
201 |
"Can't get vector layer parameter ''{0}'' of the geoprocess ''{2}''",
|
202 |
name, |
203 |
getName() |
204 |
); |
205 |
logger.warn(s); |
206 |
throw new RuntimeException(s, ex); |
207 |
} |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public RasterLayer getRasterLayer(String name) { |
212 |
try {
|
213 |
RasterStore store = this.getRasterStore(name);
|
214 |
RasterLayer layer = (RasterLayer) MapContextLocator.getMapContextManager().createLayer( |
215 |
store.getName(), |
216 |
store |
217 |
); |
218 |
return layer;
|
219 |
} catch (LoadLayerException ex) {
|
220 |
String s = MessageFormat.format( |
221 |
"Can't get raster layer parameter ''{0}'' of the geoprocess ''{2}''",
|
222 |
name, |
223 |
getName() |
224 |
); |
225 |
logger.warn(s); |
226 |
throw new RuntimeException(s, ex); |
227 |
} |
228 |
} |
229 |
|
230 |
@Override
|
231 |
public RasterStore getRasterStore(String name) { |
232 |
Output oo = this.getOutput(name);
|
233 |
|
234 |
// Obtenemos el fichero generado
|
235 |
FileOutputChannel channel = (FileOutputChannel) oo.getOutputChannel(); |
236 |
File rasterFile = new File(channel.getFilename()); |
237 |
|
238 |
// Buscamos entre los store de fichero, el que pueda abrir el fichero raster
|
239 |
String storeProviderName = null; |
240 |
FilesystemServerExplorer explorer = (FilesystemServerExplorer) DALLocator.getDataManager() |
241 |
.getServerExplorerRegister() |
242 |
.getFactory(FilesystemServerExplorer.NAME); |
243 |
Iterator<FilesystemServerExplorerProvider> it = explorer.getFilters();
|
244 |
while (it.hasNext()) {
|
245 |
FilesystemServerExplorerProvider provider = it.next(); |
246 |
if (provider.accept(rasterFile)) {
|
247 |
storeProviderName = provider.getDataStoreProviderName(); |
248 |
break;
|
249 |
} |
250 |
} |
251 |
if (storeProviderName == null) { |
252 |
String s = MessageFormat.format( |
253 |
"Can't get raster store ''{0}'', type ''{1}'', of the geoprocess ''{2}''. Can't locate store for open file ''{3}''.",
|
254 |
name, |
255 |
oo.getClass().getSimpleName(), |
256 |
getName(), |
257 |
rasterFile.getAbsolutePath() |
258 |
); |
259 |
logger.warn(s); |
260 |
throw new RuntimeException(s); |
261 |
} |
262 |
|
263 |
try {
|
264 |
// Abrimos el store raster
|
265 |
DataManager dataManager = DALLocator.getDataManager(); |
266 |
DataStoreParameters params; |
267 |
params = dataManager.createStoreParameters(storeProviderName); |
268 |
((FilesystemStoreParameters) params).setFile(rasterFile); |
269 |
RasterStore store = (RasterStore) dataManager.openStore(storeProviderName, params); |
270 |
return store;
|
271 |
} catch (Exception ex) { |
272 |
String s = MessageFormat.format( |
273 |
"Can't get output raster store ''{0}'', type ''{1}'', of the geoprocess ''{2}''. Can't open store for file ''{3}''.",
|
274 |
name, |
275 |
oo.getClass().getSimpleName(), |
276 |
getName(), |
277 |
rasterFile.getAbsolutePath() |
278 |
); |
279 |
logger.warn(s); |
280 |
throw new RuntimeException(s, ex); |
281 |
} |
282 |
} |
283 |
|
284 |
} |
285 |
|
286 |
private GeoAlgorithm algorithm;
|
287 |
private AnalysisExtent extent;
|
288 |
private double cellSizeZ; |
289 |
private double cellSize; |
290 |
|
291 |
public GeoProcessBuilerBase() {
|
292 |
this.cellSize = 1; |
293 |
this.cellSizeZ = 1; |
294 |
} |
295 |
|
296 |
@Override
|
297 |
public String getName() { |
298 |
if (this.algorithm == null) { |
299 |
return "unknown"; |
300 |
} |
301 |
return this.algorithm.getCommandLineName(); |
302 |
} |
303 |
|
304 |
@Override
|
305 |
public GeoProcessBuiler algorithm(String name) { |
306 |
SextanteGeoProcessManager manager = (SextanteGeoProcessManager) GeoProcessLocator.getGeoProcessManager(); |
307 |
GeoAlgorithm alg = manager.getAlgorithms().get(name); |
308 |
if (alg == null) { |
309 |
throw new IllegalArgumentException( |
310 |
MessageFormat.format(
|
311 |
"Can't access to geoprocess ''{0}''.",
|
312 |
name |
313 |
) |
314 |
); |
315 |
} |
316 |
this.algorithm = alg;
|
317 |
return this; |
318 |
} |
319 |
|
320 |
protected Parameter getParam(String name, Class expectedType, Object value) { |
321 |
Parameter param; |
322 |
try {
|
323 |
param = this.algorithm.getParameters().getParameter(name);
|
324 |
} catch (WrongParameterIDException ex) {
|
325 |
throw new IllegalArgumentException( |
326 |
MessageFormat.format(
|
327 |
"Can't access parameter ''{0}'' of the geoprocess ''{1}''.",
|
328 |
name, |
329 |
this.getName()
|
330 |
), |
331 |
ex |
332 |
); |
333 |
} |
334 |
if (!expectedType.isInstance(param)) {
|
335 |
throw new IllegalArgumentException( |
336 |
MessageFormat.format(
|
337 |
"Incorrect type in parameter ''{0}'' of the geoprocess ''{1}'', expected a ''{2}'', and received a ''{3}''.",
|
338 |
name, |
339 |
this.getName(),
|
340 |
param.getParameterTypeName(), |
341 |
(value == null) ? "null" : value.getClass().getSimpleName() |
342 |
) |
343 |
); |
344 |
} |
345 |
return param;
|
346 |
} |
347 |
|
348 |
@Override
|
349 |
public GeoProcessBuiler parameter(String name, String value) { |
350 |
Parameter param = this.getParam(name, ParameterString.class, value);
|
351 |
param.setParameterValue(value); |
352 |
return this; |
353 |
} |
354 |
|
355 |
@Override
|
356 |
public GeoProcessBuiler parameter(String name, int value) { |
357 |
Parameter param = this.getParam(name, ParameterNumericalValue.class, value);
|
358 |
param.setParameterValue(value); |
359 |
return this; |
360 |
} |
361 |
|
362 |
@Override
|
363 |
public GeoProcessBuiler parameter(String name, double value) { |
364 |
Parameter param = this.getParam(name, ParameterNumericalValue.class, value);
|
365 |
param.setParameterValue(value); |
366 |
return this; |
367 |
} |
368 |
|
369 |
@Override
|
370 |
public GeoProcessBuiler parameter(String name, FLyrVect value) { |
371 |
Parameter param = this.getParam(name, ParameterVectorLayer.class, value);
|
372 |
FlyrVectIVectorLayer layer = new FlyrVectIVectorLayer();
|
373 |
layer.create(value); |
374 |
param.setParameterValue(value); |
375 |
if (this.extent == null) { |
376 |
try {
|
377 |
this.extent(value.getFullEnvelope());
|
378 |
} catch (ReadException ex) {
|
379 |
logger.warn( |
380 |
MessageFormat.format(
|
381 |
"Can't set default extent of analysis from parameter ''{0}'' of the geoprocess ''{1}''",
|
382 |
name, |
383 |
this.getName()
|
384 |
), |
385 |
ex |
386 |
); |
387 |
} |
388 |
} |
389 |
return this; |
390 |
} |
391 |
|
392 |
@Override
|
393 |
public GeoProcessBuiler parameter(String name, RasterLayer value) { |
394 |
Parameter param = this.getParam(name, ParameterRasterLayer.class, value);
|
395 |
FLyrRasterIRasterLayer layer; |
396 |
layer = new FLyrRasterIRasterLayer();
|
397 |
layer.create(value); |
398 |
param.setParameterValue(value); |
399 |
if (this.extent == null) { |
400 |
try {
|
401 |
this.extent(value.getFullEnvelope());
|
402 |
} catch (ReadException ex) {
|
403 |
logger.warn( |
404 |
MessageFormat.format(
|
405 |
"Can't get Full Envelope ''{0}'' of the geoprocess ''{1}''",
|
406 |
name, |
407 |
this.getName()
|
408 |
), |
409 |
ex |
410 |
); |
411 |
} |
412 |
} |
413 |
return this; |
414 |
} |
415 |
|
416 |
// @Override
|
417 |
// public GeoProcessBuiler parameter(String name, TableDocument value) {
|
418 |
// Parameter param = this.getParam(name, ParameterTable.class, value);
|
419 |
// TableDocumentITable layer = new TableDocumentITable();
|
420 |
// layer.create(value);
|
421 |
// param.setParameterValue(value);
|
422 |
// return this;
|
423 |
// }
|
424 |
|
425 |
@Override
|
426 |
public GeoProcessBuiler extent(Envelope envelope) {
|
427 |
double xlow = envelope.getLowerCorner().getX();
|
428 |
double ylow = envelope.getLowerCorner().getY();
|
429 |
double xup = envelope.getUpperCorner().getX();
|
430 |
double yup = envelope.getUpperCorner().getY();
|
431 |
AnalysisExtent extent = new AnalysisExtent();
|
432 |
extent.setXRange(xlow, xup, false);
|
433 |
extent.setYRange(ylow, yup, false);
|
434 |
extent.setZRange(0, 0, false); |
435 |
this.extent = extent;
|
436 |
this.algorithm.setAnalysisExtent(extent);
|
437 |
return this; |
438 |
} |
439 |
|
440 |
@Override
|
441 |
public GeoProcessBuiler cellSize(double cellSize) { |
442 |
this.cellSize = cellSize;
|
443 |
return this; |
444 |
} |
445 |
|
446 |
@Override
|
447 |
public GeoProcessBuiler cellSizeZ(double cellSizeZ) { |
448 |
this.cellSizeZ = cellSizeZ;
|
449 |
return this; |
450 |
} |
451 |
|
452 |
@Override
|
453 |
public GeoProcessBuiler execute() {
|
454 |
if (this.algorithm == null) { |
455 |
throw new IllegalStateException("Algorithm can't be set"); |
456 |
} |
457 |
if (!this.algorithm.hasCorrectParameterValues()) { |
458 |
throw new IllegalArgumentException( |
459 |
MessageFormat.format(
|
460 |
"Some parameters of the process ''{0}'' has incorrect values",
|
461 |
this.getName()
|
462 |
) |
463 |
); |
464 |
} |
465 |
if (this.extent != null) { |
466 |
this.extent.setCellSize(this.cellSize); |
467 |
this.extent.setCellSizeZ(this.cellSizeZ); |
468 |
} |
469 |
OutputFactory outputFactory = new DefaultOutputFactory();
|
470 |
try {
|
471 |
this.algorithm.execute(null, outputFactory); |
472 |
} catch (GeoAlgorithmExecutionException ex) {
|
473 |
logger.warn( |
474 |
MessageFormat.format(
|
475 |
"Can't execute the process ''{0}''",
|
476 |
this.getName()
|
477 |
), |
478 |
ex |
479 |
); |
480 |
} |
481 |
return this; |
482 |
} |
483 |
|
484 |
@Override
|
485 |
public OutputAccesor output() {
|
486 |
return new OutputAccesorBase(); |
487 |
} |
488 |
|
489 |
@Override
|
490 |
public List<String> getAlgorithmNames() { |
491 |
SextanteGeoProcessManager manager = (SextanteGeoProcessManager) GeoProcessLocator.getGeoProcessManager(); |
492 |
List<String> algorithms = new ArrayList(manager.getAlgorithms().keySet()); |
493 |
return algorithms;
|
494 |
} |
495 |
|
496 |
@Override
|
497 |
public List<String> getParameterNames() { |
498 |
List<String> parameterNames = new ArrayList<String>(); |
499 |
ParametersSet params = this.algorithm.getParameters();
|
500 |
for( int i=0; i<params.getNumberOfParameters(); i++ ) { |
501 |
Parameter param = params.getParameter(i); |
502 |
parameterNames.add(param.getParameterName()); |
503 |
} |
504 |
return parameterNames;
|
505 |
} |
506 |
|
507 |
@Override
|
508 |
public ParameterInfo getParameterInfo(String name) { |
509 |
Parameter param; |
510 |
try {
|
511 |
param = this.algorithm.getParameters().getParameter(name);
|
512 |
} catch (WrongParameterIDException ex) {
|
513 |
throw new IllegalArgumentException( |
514 |
MessageFormat.format(
|
515 |
"Can't access parameter ''{0}'' of the geoprocess ''{1}''.",
|
516 |
name, |
517 |
this.getName()
|
518 |
), |
519 |
ex |
520 |
); |
521 |
} |
522 |
return new ParameterInfoBase(param); |
523 |
} |
524 |
|
525 |
|
526 |
|
527 |
} |