gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.extension / src / main / java / org / gvsig / sextante / app / extension / core / gvGeoAlgorithm.java @ 50
History | View | Annotate | Download (7.78 KB)
1 |
package org.gvsig.sextante.app.extension.core; |
---|---|
2 |
|
3 |
import java.util.Iterator; |
4 |
|
5 |
import org.gvsig.fmap.dal.exception.DataException; |
6 |
import org.gvsig.fmap.dal.exception.ReadException; |
7 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
8 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
9 |
import org.gvsig.fmap.dal.feature.FeatureType; |
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
|
12 |
import es.unex.sextante.core.GeoAlgorithm; |
13 |
import es.unex.sextante.core.Sextante; |
14 |
import es.unex.sextante.dataObjects.IVectorLayer; |
15 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
16 |
|
17 |
/**
|
18 |
* Base class for gvSIG algorithms
|
19 |
*
|
20 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
21 |
*/
|
22 |
public abstract class gvGeoAlgorithm extends GeoAlgorithm implements IProgressModel { |
23 |
protected String[] attrNames = null; |
24 |
private int nSteps = 0; |
25 |
|
26 |
/**
|
27 |
* Builds the output FeatureStore
|
28 |
* @param featureType
|
29 |
* @return FeatureStore
|
30 |
*/
|
31 |
@SuppressWarnings("unchecked") |
32 |
protected FeatureStore buildOutPutStore(FeatureType featureType,
|
33 |
int shapeType,
|
34 |
String sextanteLayerName,
|
35 |
String sextanteLayerLabel) {
|
36 |
|
37 |
Class [] types = null; |
38 |
if(featureType.getDefaultGeometryAttribute() != null) { //Tiene campo GEOMETRY. Hay que quitarlo |
39 |
attrNames = new String[featureType.size() - 1]; |
40 |
types = new Class[featureType.size() - 1]; |
41 |
} else {
|
42 |
attrNames = new String[featureType.size()]; |
43 |
types = new Class[featureType.size()]; |
44 |
} |
45 |
|
46 |
int i = 0; |
47 |
Iterator it = featureType.iterator();
|
48 |
while( it.hasNext() ) {
|
49 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next(); |
50 |
if(attribute.getName().compareTo("GEOMETRY") != 0) { |
51 |
attrNames[i] = attribute.getName(); |
52 |
types[i] = attribute.getObjectClass(); |
53 |
i++; |
54 |
} |
55 |
} |
56 |
|
57 |
try {
|
58 |
IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
59 |
sextanteLayerName, |
60 |
shapeType, types, attrNames); |
61 |
return ((gvVectorLayer)output).getFeatureStore();
|
62 |
} catch (UnsupportedOutputChannelException e) {
|
63 |
Sextante.addErrorToLog(e); |
64 |
} |
65 |
return null; |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Builds the output FeatureStore
|
70 |
* @param featureType
|
71 |
* @return FeatureStore
|
72 |
*/
|
73 |
@SuppressWarnings("unchecked") |
74 |
protected FeatureStore buildOutPutStoreFromUnion(FeatureType featureType1,
|
75 |
FeatureType featureType2, |
76 |
int shapeType,
|
77 |
String sextanteLayerName,
|
78 |
String sextanteLayerLabel) {
|
79 |
Class [] types = null; |
80 |
if(featureType1.getDefaultGeometryAttribute() != null) { //Tiene campo GEOMETRY. Hay que quitarlo |
81 |
attrNames = new String[featureType1.size() + featureType2.size() - 2]; |
82 |
types = new Class[featureType1.size() + featureType2.size() - 2]; |
83 |
} else {
|
84 |
attrNames = new String[featureType1.size() + featureType2.size()]; |
85 |
types = new Class[featureType1.size() + featureType2.size()]; |
86 |
} |
87 |
|
88 |
int i = 0; |
89 |
Iterator it = featureType1.iterator();
|
90 |
while( it.hasNext() ) {
|
91 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next(); |
92 |
if(attribute.getName().compareTo("GEOMETRY") != 0) { |
93 |
attrNames[i] = attribute.getName(); |
94 |
types[i] = attribute.getObjectClass(); |
95 |
i++; |
96 |
} |
97 |
} |
98 |
|
99 |
it = featureType2.iterator(); |
100 |
while( it.hasNext() ) {
|
101 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor)it.next(); |
102 |
if(attribute.getName().compareTo("GEOMETRY") != 0) { |
103 |
String attrName = checkAttrName(attribute.getName(), featureType1.size() - 1); |
104 |
attrNames[i] = attrName; |
105 |
types[i] = attribute.getObjectClass(); |
106 |
i++; |
107 |
} |
108 |
} |
109 |
|
110 |
try {
|
111 |
IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
112 |
sextanteLayerName, |
113 |
shapeType, types, attrNames); |
114 |
return ((gvVectorLayer)output).getFeatureStore();
|
115 |
} catch (UnsupportedOutputChannelException e) {
|
116 |
Sextante.addErrorToLog(e); |
117 |
} |
118 |
return null; |
119 |
} |
120 |
|
121 |
/**
|
122 |
* Changes the attribute name if this name is in the list.
|
123 |
* @param name
|
124 |
* @return
|
125 |
*/
|
126 |
private String checkAttrName(String name, int size) { |
127 |
String newName = name;
|
128 |
for (int i = 0; i < size; i++) |
129 |
if(attrNames[i].compareTo(newName) == 0) |
130 |
newName = newName + "I";
|
131 |
return newName;
|
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
* @see es.unex.sextante.core.GeoAlgorithm#getOutputFilename(es.unex.sextante.outputs.Output)
|
137 |
*/
|
138 |
/*protected String getOutputFilename(Output out) {
|
139 |
|
140 |
IOutputChannel channel = out.getOutputChannel();
|
141 |
|
142 |
String sFilename = null;
|
143 |
if(channel.getParameters() instanceof String)
|
144 |
sFilename = (String)((IOutputChannel)channel).getParameters();
|
145 |
else
|
146 |
if(channel.getParameters() instanceof FilesystemStoreParameters)
|
147 |
sFilename = ((FilesystemStoreParameters)channel.getParameters()).getFile().getAbsolutePath();
|
148 |
if (sFilename == null){
|
149 |
return null;
|
150 |
}
|
151 |
String[] exts = null;
|
152 |
if (out instanceof OutputRasterLayer){
|
153 |
exts = m_OutputFactory.getRasterLayerOutputExtensions();
|
154 |
}
|
155 |
else if (out instanceof OutputVectorLayer){
|
156 |
exts = m_OutputFactory.getVectorLayerOutputExtensions();
|
157 |
}
|
158 |
else if (out instanceof OutputTable){
|
159 |
exts = m_OutputFactory.getTableOutputExtensions();
|
160 |
}
|
161 |
else{
|
162 |
return null;
|
163 |
}
|
164 |
|
165 |
for (int i = 0; i < exts.length; i++) {
|
166 |
if (sFilename.endsWith(exts[i])){
|
167 |
return sFilename;
|
168 |
}
|
169 |
}
|
170 |
return sFilename + "." + exts[0];
|
171 |
}*/
|
172 |
|
173 |
/*
|
174 |
* (non-Javadoc)
|
175 |
* @see es.unex.sextante.core.GeoAlgorithm#getOutputChannel(java.lang.String)
|
176 |
*/
|
177 |
/*protected IOutputChannel getOutputChannel(String sName) {
|
178 |
Output out = null;
|
179 |
IOutputChannel channel = null;
|
180 |
try {
|
181 |
out = m_OutputObjects.getOutput(sName);
|
182 |
channel = out.getOutputChannel();
|
183 |
if (channel == null)
|
184 |
return new FileOutputChannel(m_OutputFactory.getTempFilename(out));
|
185 |
else {
|
186 |
String sFilename = getOutputFilename(out);
|
187 |
if (sFilename == null) {
|
188 |
if(channel instanceof CompositeSourceOutputChannel)
|
189 |
return channel;
|
190 |
return new FileOutputChannel(m_OutputFactory.getTempFilename(out));
|
191 |
} else {
|
192 |
if(channel instanceof FileOutputChannel)
|
193 |
((FileOutputChannel) channel).setFilename(sFilename);
|
194 |
return channel;
|
195 |
}
|
196 |
}
|
197 |
} catch (WrongOutputIDException e) {
|
198 |
return null;
|
199 |
}
|
200 |
}*/
|
201 |
|
202 |
/**
|
203 |
* Gets the shape type of the selected feature store
|
204 |
* @param FeatureStore source
|
205 |
* @return shape type
|
206 |
* @throws ReadException
|
207 |
*/
|
208 |
protected int getShapeType(FeatureStore storeLayer1) throws ReadException { |
209 |
FeatureType featureType; |
210 |
try {
|
211 |
featureType = storeLayer1.getDefaultFeatureType(); |
212 |
} catch (DataException e) {
|
213 |
throw new ReadException(storeLayer1.getName(), e); |
214 |
} |
215 |
int indexGeom = featureType.getDefaultGeometryAttributeIndex();
|
216 |
return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* Returns true if it is a point layer
|
221 |
* @param store
|
222 |
* @return
|
223 |
* @throws ReadException
|
224 |
*/
|
225 |
protected boolean isPoint(FeatureStore store) throws ReadException { |
226 |
return (getShapeType(store) == Geometry.TYPES.POINT || getShapeType(store) == Geometry.TYPES.MULTIPOINT);
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Returns true if it is a polygon layer
|
231 |
* @param store
|
232 |
* @return
|
233 |
* @throws ReadException
|
234 |
*/
|
235 |
protected boolean isPolygon(FeatureStore store) throws ReadException { |
236 |
return (getShapeType(store) == Geometry.TYPES.SURFACE || getShapeType(store) == Geometry.TYPES.MULTISURFACE);
|
237 |
} |
238 |
|
239 |
/**
|
240 |
* Returns true if it is a line layer
|
241 |
* @param store
|
242 |
* @return
|
243 |
* @throws ReadException
|
244 |
*/
|
245 |
protected boolean isLine(FeatureStore store) throws ReadException { |
246 |
return (getShapeType(store) == Geometry.TYPES.CURVE || getShapeType(store) == Geometry.TYPES.MULTICURVE);
|
247 |
} |
248 |
|
249 |
|
250 |
/*
|
251 |
* (non-Javadoc)
|
252 |
* @see org.gvsig.sextante.app.algorithm.base.core.IProgressAdapter#setProgress(int)
|
253 |
*/
|
254 |
public void setProgress(int iStep) { |
255 |
super.setProgress(iStep, nSteps);
|
256 |
} |
257 |
|
258 |
/*
|
259 |
* (non-Javadoc)
|
260 |
* @see org.gvsig.sextante.app.algorithm.base.core.IProgressAdapter#setTotalNumberOfSteps(int)
|
261 |
*/
|
262 |
public void setTotalNumberOfSteps(int nSteps) { |
263 |
this.nSteps = nSteps;
|
264 |
} |
265 |
} |