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 / AbstractSextanteGeoProcess.java @ 1259
History | View | Annotate | Download (18 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; |
25 |
|
26 |
import java.io.File; |
27 |
import java.util.HashMap; |
28 |
import java.util.Iterator; |
29 |
|
30 |
import org.gvsig.fmap.dal.DataTypes; |
31 |
import org.gvsig.fmap.dal.exception.DataException; |
32 |
import org.gvsig.fmap.dal.exception.ReadException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
34 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.GeometryLocator; |
39 |
import org.gvsig.fmap.geom.GeometryManager; |
40 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
41 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
42 |
import org.gvsig.fmap.geom.primitive.Envelope; |
43 |
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsEnvelopeEvaluator; |
44 |
import org.gvsig.geoprocess.lib.api.GeoProcess; |
45 |
import org.gvsig.geoprocess.lib.api.GeoProcessLocator; |
46 |
import org.gvsig.geoprocess.lib.api.GeoProcessManager; |
47 |
import org.gvsig.geoprocess.lib.sextante.core.DefaultOutputFactory; |
48 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
49 |
|
50 |
import es.unex.sextante.core.AnalysisExtent; |
51 |
import es.unex.sextante.core.GeoAlgorithm; |
52 |
import es.unex.sextante.core.ITaskMonitor; |
53 |
import es.unex.sextante.core.OutputFactory; |
54 |
import es.unex.sextante.core.OutputObjectsSet; |
55 |
import es.unex.sextante.core.Sextante; |
56 |
import es.unex.sextante.dataObjects.IRasterLayer; |
57 |
import es.unex.sextante.dataObjects.IVectorLayer; |
58 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
59 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
60 |
import es.unex.sextante.exceptions.WrongOutputIDException; |
61 |
import es.unex.sextante.gui.algorithm.GeoAlgorithmParametersPanel; |
62 |
import es.unex.sextante.outputs.IOutputChannel; |
63 |
import es.unex.sextante.outputs.Output; |
64 |
import java.util.ArrayList; |
65 |
import java.util.List; |
66 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
67 |
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
68 |
import org.gvsig.tools.evaluator.Evaluator; |
69 |
import org.gvsig.tools.namestranslator.NamesTranslator; |
70 |
|
71 |
/**
|
72 |
* Base implementation for Sextante based {@link GeoProcess} objects.
|
73 |
*
|
74 |
* @author gvSIG Team
|
75 |
* @version $Id$
|
76 |
*/
|
77 |
public abstract class AbstractSextanteGeoProcess extends GeoAlgorithm implements GeoProcess { |
78 |
protected String[] attrNames = null; |
79 |
private SimpleTaskStatusDelegated status = null; |
80 |
protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
81 |
|
82 |
@Override
|
83 |
public boolean execute(ITaskMonitor task, OutputFactory outputFactory, |
84 |
HashMap<String, String> outputMap) |
85 |
throws GeoAlgorithmExecutionException {
|
86 |
status = new SimpleTaskStatusDelegated(m_Task, getGeoProcessName());
|
87 |
status.add(); |
88 |
boolean result = false; |
89 |
try {
|
90 |
result = super.execute(task, outputFactory, outputMap);
|
91 |
} finally {
|
92 |
status.remove(); |
93 |
} |
94 |
return result;
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* This method creates a new raster layer and adds it to the set of output objects of the algorithm. Use this when your
|
99 |
* algorithm generates a new raster layer and you have to create it. The grid extent is taken from the algorithm analysis
|
100 |
* extent (@see {@link #getAnalysisExtent()}
|
101 |
*
|
102 |
* @param sName
|
103 |
* The name of the layer. Has to be the same that you used to define this output in the
|
104 |
* @see {@link #defineCharacteristics()} method.
|
105 |
* @param sDescription
|
106 |
* the long description of the output. This is the one usually used to describe the layer when added to a GIS
|
107 |
* GUI
|
108 |
* @param iDataType
|
109 |
* The data type. See
|
110 |
* @see {@link IRasterLayer} for more info about valid values
|
111 |
* @param iBands
|
112 |
* the number of bands of the new layer
|
113 |
* @return a new raster layer
|
114 |
* @throws UnsupportedOutputChannelException
|
115 |
*/
|
116 |
protected IRasterLayer getNewRORasterLayer(final String sName, |
117 |
final String sDescription, |
118 |
final int iDataType, |
119 |
final int iBands) throws UnsupportedOutputChannelException { |
120 |
|
121 |
final IOutputChannel channel = getOutputChannel(sName);
|
122 |
|
123 |
final IRasterLayer newLayer = ((DefaultOutputFactory)m_OutputFactory).getNewEmptyRORasterLayer(sName, iDataType,
|
124 |
m_AnalysisExtent, iBands, channel, m_CRS); |
125 |
|
126 |
addOutputRasterLayer(sName, sDescription, iBands, channel, newLayer); |
127 |
|
128 |
return newLayer;
|
129 |
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* Builds the output FeatureStore
|
134 |
*
|
135 |
* @param featureType
|
136 |
* @return FeatureStore
|
137 |
*/
|
138 |
protected FeatureStore buildOutPutStore(FeatureType featureType,
|
139 |
int shapeType, String sextanteLayerName, String sextanteLayerLabel) { |
140 |
|
141 |
// Class<?>[] types = null;
|
142 |
// if (featureType.getDefaultGeometryAttribute() != null) {
|
143 |
// // Tiene campo GEOMETRY.
|
144 |
// // Hay que quitarlo
|
145 |
// attrNames = new String[featureType.size() - 1];
|
146 |
// types = new Class[featureType.size() - 1];
|
147 |
// } else {
|
148 |
// attrNames = new String[featureType.size()];
|
149 |
// types = new Class[featureType.size()];
|
150 |
// }
|
151 |
List<Class> types = new ArrayList(); |
152 |
List<String> theAttrNames = new ArrayList(); |
153 |
|
154 |
List<FeatureAttributeDescriptor> emulateds = new ArrayList(); |
155 |
@SuppressWarnings("unchecked") |
156 |
Iterator<FeatureAttributeDescriptor> it = featureType.iterator();
|
157 |
while (it.hasNext()) {
|
158 |
FeatureAttributeDescriptor attribute = it.next(); |
159 |
if(attribute.isComputed()){
|
160 |
emulateds.add(attribute); |
161 |
} else {
|
162 |
String attrName = attribute.getName();
|
163 |
if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
|
164 |
theAttrNames.add(attrName); |
165 |
types.add(attribute.getObjectClass()); |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
try {
|
171 |
FlyrVectIVectorLayer output |
172 |
= (FlyrVectIVectorLayer) getNewVectorLayer( |
173 |
sextanteLayerLabel, |
174 |
sextanteLayerName, |
175 |
shapeType, |
176 |
types.toArray(new Class[types.size()]), |
177 |
theAttrNames.toArray(new String[theAttrNames.size()]) |
178 |
); |
179 |
if (output != null) { |
180 |
FeatureStore store = output.getFeatureStore(); |
181 |
if (!emulateds.isEmpty()) {
|
182 |
try {
|
183 |
boolean needStartEditingMode = !(store.isEditing() || store.isAppending());
|
184 |
if (needStartEditingMode) {
|
185 |
store.edit(); |
186 |
} |
187 |
EditableFeatureType ft = store.getDefaultFeatureType().getEditable(); |
188 |
|
189 |
for (FeatureAttributeDescriptor emulated : emulateds) {
|
190 |
theAttrNames.add(emulated.getName()); |
191 |
ft.add(emulated.getName(), emulated.getType(), emulated.getFeatureAttributeEmulator()); |
192 |
} |
193 |
store.update(ft); |
194 |
if (needStartEditingMode) {
|
195 |
store.finishEditing(); |
196 |
} |
197 |
|
198 |
} catch (Exception e) { |
199 |
Sextante.addErrorToLog(e); |
200 |
} |
201 |
} |
202 |
this.attrNames = theAttrNames.toArray(new String[theAttrNames.size()]); |
203 |
return output.getFeatureStore();
|
204 |
} |
205 |
} catch (UnsupportedOutputChannelException e) {
|
206 |
Sextante.addErrorToLog(e); |
207 |
} catch (GeoAlgorithmExecutionException e) {
|
208 |
Sextante.addErrorToLog(e); |
209 |
} |
210 |
return null; |
211 |
} |
212 |
|
213 |
/**
|
214 |
* Builds the output FeatureStore
|
215 |
*
|
216 |
* @param featureType
|
217 |
* @return FeatureStore
|
218 |
*/
|
219 |
protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
|
220 |
FeatureType featureType2, int shapeType, String sextanteLayerName, |
221 |
String sextanteLayerLabel, NamesTranslator nameTranslator) {
|
222 |
return buildOutPutStoreFromUnion(featureType1, featureType2, shapeType,
|
223 |
sextanteLayerName, sextanteLayerLabel, null, null, nameTranslator); |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Builds the output FeatureStore
|
228 |
*
|
229 |
* @param featureType
|
230 |
* @return FeatureStore
|
231 |
*/
|
232 |
protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
|
233 |
FeatureType featureType2, int shapeType, String sextanteLayerName, |
234 |
String sextanteLayerLabel, String newField, Class<?> newFieldType, NamesTranslator nameTranslator) { |
235 |
|
236 |
nameTranslator.clear(); |
237 |
|
238 |
int sizeAux = 0; |
239 |
if (newField != null && newFieldType != null) |
240 |
sizeAux = 1;
|
241 |
|
242 |
Class<?>[] types = null; |
243 |
// Tiene campo GEOMETRY. Hay que quitarlo
|
244 |
if (featureType1.getDefaultGeometryAttribute() != null) { |
245 |
attrNames = |
246 |
new String[featureType1.size() + featureType2.size() - 2 |
247 |
+ sizeAux]; |
248 |
types = |
249 |
new Class[featureType1.size() + featureType2.size() - 2 |
250 |
+ sizeAux]; |
251 |
} else {
|
252 |
attrNames = |
253 |
new String[featureType1.size() + featureType2.size() + sizeAux]; |
254 |
types = |
255 |
new Class[featureType1.size() + featureType2.size() + sizeAux]; |
256 |
} |
257 |
|
258 |
int i = 0; |
259 |
@SuppressWarnings("unchecked") |
260 |
Iterator<FeatureAttributeDescriptor> it = featureType1.iterator();
|
261 |
while (it.hasNext()) {
|
262 |
FeatureAttributeDescriptor attribute = |
263 |
(FeatureAttributeDescriptor) it.next(); |
264 |
if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
|
265 |
attrNames[i] = attribute.getName(); |
266 |
nameTranslator.addSource(attribute.getName()); |
267 |
types[i] = attribute.getObjectClass(); |
268 |
i++; |
269 |
} |
270 |
} |
271 |
|
272 |
@SuppressWarnings("unchecked") |
273 |
Iterator<FeatureAttributeDescriptor> it2 = featureType2.iterator();
|
274 |
while (it2.hasNext()) {
|
275 |
FeatureAttributeDescriptor attribute = it2.next(); |
276 |
if(attribute.getDataType().getType() != DataTypes.GEOMETRY) {
|
277 |
String attrName =
|
278 |
checkAttrName(attribute.getName(), featureType1.size() - 1);
|
279 |
attrNames[i] = attrName; |
280 |
nameTranslator.addSource(attribute.getName()); |
281 |
types[i] = attribute.getObjectClass(); |
282 |
i++; |
283 |
} |
284 |
} |
285 |
|
286 |
if (newField != null && newFieldType != null) { |
287 |
attrNames[attrNames.length - 1] = newField;
|
288 |
nameTranslator.addSource(newField); |
289 |
types[types.length - 1] = newFieldType;
|
290 |
} |
291 |
|
292 |
try {
|
293 |
IVectorLayer output = |
294 |
getNewVectorLayer(sextanteLayerLabel, sextanteLayerName, |
295 |
shapeType, types, nameTranslator.getTranslatedNamesAsArray()); |
296 |
if (output != null) { |
297 |
return ((FlyrVectIVectorLayer) output).getFeatureStore();
|
298 |
} |
299 |
} catch (UnsupportedOutputChannelException e) {
|
300 |
Sextante.addErrorToLog(e); |
301 |
} catch (GeoAlgorithmExecutionException e) {
|
302 |
Sextante.addErrorToLog(e); |
303 |
} |
304 |
return null; |
305 |
} |
306 |
|
307 |
/**
|
308 |
* Changes the attribute name if this name is in the list.
|
309 |
*
|
310 |
* @param name
|
311 |
* @return
|
312 |
*/
|
313 |
protected String checkAttrName(String name, int size) { |
314 |
return checkAttrName(name, size, attrNames);
|
315 |
} |
316 |
|
317 |
/**
|
318 |
* Gets the shape type of the selected feature store
|
319 |
*
|
320 |
* @param FeatureStore
|
321 |
* source
|
322 |
* @return shape type
|
323 |
* @throws ReadException
|
324 |
*/
|
325 |
protected int getShapeType(FeatureStore storeLayer1) throws ReadException { |
326 |
FeatureType featureType; |
327 |
try {
|
328 |
featureType = storeLayer1.getDefaultFeatureType(); |
329 |
} catch (DataException e) {
|
330 |
throw new ReadException(storeLayer1.getName(), e); |
331 |
} |
332 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
333 |
return featureType.getAttributeDescriptor(indexGeom).getGeomType()
|
334 |
.getType(); |
335 |
} |
336 |
|
337 |
/**
|
338 |
* Returns true if it is a point layer
|
339 |
*
|
340 |
* @param store
|
341 |
* @return
|
342 |
* @throws ReadException
|
343 |
*/
|
344 |
protected boolean isPoint(FeatureStore store) throws ReadException { |
345 |
return (getShapeType(store) == Geometry.TYPES.POINT || getShapeType(store) == Geometry.TYPES.MULTIPOINT);
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* Returns true if it is a polygon layer
|
350 |
*
|
351 |
* @param store
|
352 |
* @return
|
353 |
* @throws ReadException
|
354 |
*/
|
355 |
protected boolean isPolygon(FeatureStore store) throws ReadException { |
356 |
int type = getShapeType(store);
|
357 |
return geomManager.isSubtype(Geometry.TYPES.SURFACE, type) || geomManager.isSubtype(Geometry.TYPES.MULTISURFACE, type);
|
358 |
} |
359 |
|
360 |
/**
|
361 |
* Returns true if it is a line layer
|
362 |
*
|
363 |
* @param store
|
364 |
* @return
|
365 |
* @throws ReadException
|
366 |
*/
|
367 |
protected boolean isLine(FeatureStore store) throws ReadException { |
368 |
int type = getShapeType(store);
|
369 |
return geomManager.isSubtype(Geometry.TYPES.CURVE, type) || geomManager.isSubtype(Geometry.TYPES.MULTICURVE, type);
|
370 |
} |
371 |
|
372 |
/**
|
373 |
* Returns true if it is a line layer
|
374 |
*
|
375 |
* @param store
|
376 |
* @return
|
377 |
* @throws ReadException
|
378 |
*/
|
379 |
protected boolean isUndefined(FeatureStore store) throws ReadException { |
380 |
return (getShapeType(store) == Geometry.TYPES.GEOMETRY);
|
381 |
} |
382 |
|
383 |
public String getGeoProcessName() { |
384 |
return getName();
|
385 |
} |
386 |
|
387 |
@Override
|
388 |
protected void setProgressText(String sText) { |
389 |
super.setProgressText(sText);
|
390 |
getStatus().message(sText); |
391 |
} |
392 |
|
393 |
@Override
|
394 |
public boolean setProgress(int iStep, int iTotalNumberOfSteps) { |
395 |
boolean cancelled = super.setProgress(iStep, iTotalNumberOfSteps); |
396 |
SimpleTaskStatusDelegated status = getStatus(); |
397 |
status.setRangeOfValues(0, iTotalNumberOfSteps);
|
398 |
status.setCurValue(iStep); |
399 |
if(cancelled || status.isCancelled())
|
400 |
return true; |
401 |
return false; |
402 |
} |
403 |
|
404 |
protected SimpleTaskStatusDelegated getStatus() {
|
405 |
return status;
|
406 |
} |
407 |
|
408 |
@Override
|
409 |
public String getCommandLineName() { |
410 |
// Override sextante command line value, as it might be the same as
|
411 |
// other sextante algorithms and some information is not shown as
|
412 |
// expected.
|
413 |
return "gvSIG-".concat(super.getCommandLineName()); |
414 |
} |
415 |
|
416 |
/**
|
417 |
* Returns the {@link GeoProcessManager}.
|
418 |
*
|
419 |
* @return the {@link GeoProcessManager}
|
420 |
*/
|
421 |
public GeoProcessManager getGeoProcessManager() {
|
422 |
return GeoProcessLocator.getGeoProcessManager();
|
423 |
} |
424 |
|
425 |
/**
|
426 |
* Returns the translation for a label depending on the current locale.
|
427 |
*
|
428 |
* @param label
|
429 |
* to translate
|
430 |
* @return text in the current locale
|
431 |
*/
|
432 |
protected String getTranslation(String label) { |
433 |
return getGeoProcessManager().getTranslation(label);
|
434 |
} |
435 |
|
436 |
public Class<? extends GeoAlgorithmParametersPanel> getCustomParametersPanelClass() { |
437 |
return null; |
438 |
} |
439 |
|
440 |
/**
|
441 |
* Gets a feature query using the analysis bounding box
|
442 |
* @param e
|
443 |
* @param store
|
444 |
* @return
|
445 |
* @throws CreateEnvelopeException
|
446 |
* @throws DataException
|
447 |
*/
|
448 |
public FeatureQuery getQueryFromAnalysisExtent(AnalysisExtent e, FeatureStore store) throws CreateEnvelopeException, DataException { |
449 |
Envelope analysisEnvelope = geomManager.createEnvelope(e.getXMin(), e.getYMin(), e.getXMax(), e.getYMax(), SUBTYPES.GEOM2D); |
450 |
FeatureQuery query = store.createFeatureQuery(); |
451 |
Evaluator filter = SpatialEvaluatorsFactory.getInstance().intersects( |
452 |
analysisEnvelope, |
453 |
store.getDefaultFeatureType().getDefaultSRS(), |
454 |
store |
455 |
); |
456 |
query.setFilter(filter); |
457 |
return query;
|
458 |
} |
459 |
|
460 |
/**
|
461 |
* Returns true if exists the output file and false i
|
462 |
* @param resultLabel
|
463 |
* @param type zero shape and one tif
|
464 |
* @return
|
465 |
*/
|
466 |
public boolean existsOutPutFile(String resultLabel, int type) { |
467 |
OutputObjectsSet ooSet = getOutputObjects(); |
468 |
Output out; |
469 |
try {
|
470 |
out = ooSet.getOutput(resultLabel); |
471 |
} catch (WrongOutputIDException e) {
|
472 |
return false; |
473 |
} |
474 |
String s = out.getOutputChannel()!= null ? out.getOutputChannel().getAsCommandLineParameter() : ""; |
475 |
if(new File(s).exists()) |
476 |
return true; |
477 |
if(type == 0) |
478 |
return (new File(s + ".shp").exists()); |
479 |
if(type == 1) |
480 |
return (new File(s + ".tif").exists()); |
481 |
return false; |
482 |
} |
483 |
|
484 |
public String getOutPutFile(String resultLabel) { |
485 |
OutputObjectsSet ooSet = getOutputObjects(); |
486 |
Output out; |
487 |
try {
|
488 |
out = ooSet.getOutput(resultLabel); |
489 |
} catch (WrongOutputIDException e) {
|
490 |
return null; |
491 |
} |
492 |
return out.getOutputChannel().getAsCommandLineParameter();
|
493 |
} |
494 |
|
495 |
public ITaskMonitor getTaskMonitor() {
|
496 |
return m_Task;
|
497 |
} |
498 |
|
499 |
public void setTaskMonitor(ITaskMonitor task) { |
500 |
this.m_Task = task;
|
501 |
} |
502 |
} |