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 @ 223
History | View | Annotate | Download (9.69 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 |
*/
|
22 |
package org.gvsig.geoprocess.lib.sextante; |
23 |
|
24 |
import java.util.HashMap; |
25 |
import java.util.Iterator; |
26 |
|
27 |
import es.unex.sextante.core.GeoAlgorithm; |
28 |
import es.unex.sextante.core.ITaskMonitor; |
29 |
import es.unex.sextante.core.OutputFactory; |
30 |
import es.unex.sextante.core.Sextante; |
31 |
import es.unex.sextante.dataObjects.IVectorLayer; |
32 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
33 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
34 |
|
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.ReadException; |
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.dal.feature.FeatureType; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.geoprocess.lib.api.GeoProcess; |
42 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.gvVectorLayer; |
43 |
|
44 |
/**
|
45 |
* Base implementation for Sextante based {@link GeoProcess} objects.
|
46 |
*
|
47 |
* @author gvSIG Team
|
48 |
* @version $Id$
|
49 |
*/
|
50 |
public abstract class AbstractSextanteGeoProcess extends GeoAlgorithm implements |
51 |
GeoProcess { |
52 |
|
53 |
protected String[] attrNames = null; |
54 |
private SimpleTaskStatusDelegated status;
|
55 |
|
56 |
@Override
|
57 |
public boolean execute(ITaskMonitor task, OutputFactory outputFactory, |
58 |
HashMap<String, String> outputMap) |
59 |
throws GeoAlgorithmExecutionException {
|
60 |
status = new SimpleTaskStatusDelegated(m_Task, getGeoProcessName());
|
61 |
status.add(); |
62 |
boolean result = false; |
63 |
try {
|
64 |
result = super.execute(task, outputFactory, outputMap);
|
65 |
} finally {
|
66 |
status.remove(); |
67 |
} |
68 |
return result;
|
69 |
} |
70 |
|
71 |
/**
|
72 |
* Builds the output FeatureStore
|
73 |
*
|
74 |
* @param featureType
|
75 |
* @return FeatureStore
|
76 |
*/
|
77 |
protected FeatureStore buildOutPutStore(FeatureType featureType,
|
78 |
int shapeType, String sextanteLayerName, String sextanteLayerLabel) { |
79 |
|
80 |
Class<?>[] types = null; |
81 |
if (featureType.getDefaultGeometryAttribute() != null) { |
82 |
// Tiene campo GEOMETRY.
|
83 |
// Hay que quitarlo
|
84 |
attrNames = new String[featureType.size() - 1]; |
85 |
types = new Class[featureType.size() - 1]; |
86 |
} else {
|
87 |
attrNames = new String[featureType.size()]; |
88 |
types = new Class[featureType.size()]; |
89 |
} |
90 |
|
91 |
int i = 0; |
92 |
@SuppressWarnings("unchecked") |
93 |
Iterator<FeatureAttributeDescriptor> it = featureType.iterator();
|
94 |
while (it.hasNext()) {
|
95 |
FeatureAttributeDescriptor attribute = it.next(); |
96 |
if (attribute.getName().compareTo("GEOMETRY") != 0) { |
97 |
attrNames[i] = attribute.getName(); |
98 |
types[i] = attribute.getObjectClass(); |
99 |
i++; |
100 |
} |
101 |
} |
102 |
|
103 |
try {
|
104 |
IVectorLayer output = |
105 |
getNewVectorLayer(sextanteLayerLabel, sextanteLayerName, |
106 |
shapeType, types, attrNames); |
107 |
return ((gvVectorLayer) output).getFeatureStore();
|
108 |
} catch (UnsupportedOutputChannelException e) {
|
109 |
Sextante.addErrorToLog(e); |
110 |
} catch (GeoAlgorithmExecutionException e) {
|
111 |
Sextante.addErrorToLog(e); |
112 |
} |
113 |
return null; |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Builds the output FeatureStore
|
118 |
*
|
119 |
* @param featureType
|
120 |
* @return FeatureStore
|
121 |
*/
|
122 |
protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
|
123 |
FeatureType featureType2, int shapeType, String sextanteLayerName, |
124 |
String sextanteLayerLabel) {
|
125 |
return buildOutPutStoreFromUnion(featureType1, featureType2, shapeType,
|
126 |
sextanteLayerName, sextanteLayerLabel, null, null); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Builds the output FeatureStore
|
131 |
*
|
132 |
* @param featureType
|
133 |
* @return FeatureStore
|
134 |
*/
|
135 |
protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
|
136 |
FeatureType featureType2, int shapeType, String sextanteLayerName, |
137 |
String sextanteLayerLabel, String newField, Class<?> newFieldType) { |
138 |
int sizeAux = 0; |
139 |
if (newField != null && newFieldType != null) |
140 |
sizeAux = 1;
|
141 |
|
142 |
Class<?>[] types = null; |
143 |
// Tiene campo GEOMETRY. Hay que quitarlo
|
144 |
if (featureType1.getDefaultGeometryAttribute() != null) { |
145 |
attrNames = |
146 |
new String[featureType1.size() + featureType2.size() - 2 |
147 |
+ sizeAux]; |
148 |
types = |
149 |
new Class[featureType1.size() + featureType2.size() - 2 |
150 |
+ sizeAux]; |
151 |
} else {
|
152 |
attrNames = |
153 |
new String[featureType1.size() + featureType2.size() + sizeAux]; |
154 |
types = |
155 |
new Class[featureType1.size() + featureType2.size() + sizeAux]; |
156 |
} |
157 |
|
158 |
int i = 0; |
159 |
@SuppressWarnings("unchecked") |
160 |
Iterator<FeatureAttributeDescriptor> it = featureType1.iterator();
|
161 |
while (it.hasNext()) {
|
162 |
FeatureAttributeDescriptor attribute = |
163 |
(FeatureAttributeDescriptor) it.next(); |
164 |
if (attribute.getName().compareTo("GEOMETRY") != 0) { |
165 |
attrNames[i] = attribute.getName(); |
166 |
types[i] = attribute.getObjectClass(); |
167 |
i++; |
168 |
} |
169 |
} |
170 |
|
171 |
@SuppressWarnings("unchecked") |
172 |
Iterator<FeatureAttributeDescriptor> it2 = featureType2.iterator();
|
173 |
while (it2.hasNext()) {
|
174 |
FeatureAttributeDescriptor attribute = it2.next(); |
175 |
if (attribute.getName().compareTo("GEOMETRY") != 0) { |
176 |
String attrName =
|
177 |
checkAttrName(attribute.getName(), featureType1.size() - 1);
|
178 |
attrNames[i] = attrName; |
179 |
types[i] = attribute.getObjectClass(); |
180 |
i++; |
181 |
} |
182 |
} |
183 |
|
184 |
if (newField != null && newFieldType != null) { |
185 |
attrNames[attrNames.length - 1] = newField;
|
186 |
types[types.length - 1] = newFieldType;
|
187 |
} |
188 |
|
189 |
try {
|
190 |
IVectorLayer output = |
191 |
getNewVectorLayer(sextanteLayerLabel, sextanteLayerName, |
192 |
shapeType, types, attrNames); |
193 |
return ((gvVectorLayer) output).getFeatureStore();
|
194 |
} catch (UnsupportedOutputChannelException e) {
|
195 |
Sextante.addErrorToLog(e); |
196 |
} catch (GeoAlgorithmExecutionException e) {
|
197 |
Sextante.addErrorToLog(e); |
198 |
} |
199 |
return null; |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Changes the attribute name if this name is in the list.
|
204 |
*
|
205 |
* @param name
|
206 |
* @return
|
207 |
*/
|
208 |
protected String checkAttrName(String name, int size) { |
209 |
String newName = name;
|
210 |
for (int i = 0; i < size; i++) |
211 |
if (attrNames[i].compareTo(newName) == 0) |
212 |
newName = newName + "I";
|
213 |
return newName;
|
214 |
} |
215 |
|
216 |
/**
|
217 |
* Gets the shape type of the selected feature store
|
218 |
*
|
219 |
* @param FeatureStore
|
220 |
* source
|
221 |
* @return shape type
|
222 |
* @throws ReadException
|
223 |
*/
|
224 |
protected int getShapeType(FeatureStore storeLayer1) throws ReadException { |
225 |
FeatureType featureType; |
226 |
try {
|
227 |
featureType = storeLayer1.getDefaultFeatureType(); |
228 |
} catch (DataException e) {
|
229 |
throw new ReadException(storeLayer1.getName(), e); |
230 |
} |
231 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
232 |
return featureType.getAttributeDescriptor(indexGeom).getGeomType()
|
233 |
.getType(); |
234 |
} |
235 |
|
236 |
/**
|
237 |
* Returns true if it is a point layer
|
238 |
*
|
239 |
* @param store
|
240 |
* @return
|
241 |
* @throws ReadException
|
242 |
*/
|
243 |
protected boolean isPoint(FeatureStore store) throws ReadException { |
244 |
return (getShapeType(store) == Geometry.TYPES.POINT || getShapeType(store) == Geometry.TYPES.MULTIPOINT);
|
245 |
} |
246 |
|
247 |
/**
|
248 |
* Returns true if it is a polygon layer
|
249 |
*
|
250 |
* @param store
|
251 |
* @return
|
252 |
* @throws ReadException
|
253 |
*/
|
254 |
protected boolean isPolygon(FeatureStore store) throws ReadException { |
255 |
return (getShapeType(store) == Geometry.TYPES.SURFACE || getShapeType(store) == Geometry.TYPES.MULTISURFACE);
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* Returns true if it is a line layer
|
260 |
*
|
261 |
* @param store
|
262 |
* @return
|
263 |
* @throws ReadException
|
264 |
*/
|
265 |
protected boolean isLine(FeatureStore store) throws ReadException { |
266 |
return (getShapeType(store) == Geometry.TYPES.CURVE || getShapeType(store) == Geometry.TYPES.MULTICURVE);
|
267 |
} |
268 |
|
269 |
public String getGeoProcessName() { |
270 |
return getName();
|
271 |
} |
272 |
|
273 |
@Override
|
274 |
protected void setProgressText(String sText) { |
275 |
getStatus().message(sText); |
276 |
} |
277 |
|
278 |
@Override
|
279 |
protected boolean setProgress(int iStep, int iTotalNumberOfSteps) { |
280 |
SimpleTaskStatusDelegated status = getStatus(); |
281 |
status.setRangeOfValues(0, iTotalNumberOfSteps);
|
282 |
status.setCurValue(iStep); |
283 |
return status.isCancelled();
|
284 |
} |
285 |
|
286 |
protected SimpleTaskStatusDelegated getStatus() {
|
287 |
return status;
|
288 |
} |
289 |
|
290 |
} |