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