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