gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.fusespatially / src / main / java / org / gvsig / geoprocess / algorithm / fusespatially / FuseSpatiallyAlgorithm.java @ 897
History | View | Annotate | Download (9.46 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.algorithm.fusespatially; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.List; |
28 |
|
29 |
import org.gvsig.fmap.dal.exception.DataException; |
30 |
import org.gvsig.fmap.dal.feature.Feature; |
31 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
import org.gvsig.geoprocess.algorithm.dissolve.DissolveAlgorithm; |
35 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
36 |
import org.gvsig.geoprocess.lib.sextante.dataObjects.FlyrVectIVectorLayer; |
37 |
import org.gvsig.tools.task.SimpleTaskStatus; |
38 |
|
39 |
import es.unex.sextante.core.Sextante; |
40 |
import es.unex.sextante.dataObjects.IVectorLayer; |
41 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
42 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
43 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
44 |
import es.unex.sextante.outputs.OutputVectorLayer; |
45 |
import java.util.Iterator; |
46 |
|
47 |
/**
|
48 |
* Fuse spatially algorithm
|
49 |
* @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
50 |
*/
|
51 |
public class FuseSpatiallyAlgorithm extends AbstractSextanteGeoProcess { |
52 |
|
53 |
public static final String RESULT = "RESULT"; |
54 |
public static final String RESULT_TABLE = "RESULT_TABLE"; |
55 |
public static final String LAYER = "LAYER"; |
56 |
public static final String SELECTED_GEOM = "SELECTED_GEOM"; |
57 |
private AbstractSextanteGeoProcess process = null; |
58 |
private String fid = "FID"; |
59 |
|
60 |
/*
|
61 |
* (non-Javadoc)
|
62 |
* @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
|
63 |
*/
|
64 |
public void defineCharacteristics(){ |
65 |
setName(getTranslation("fusespatially"));
|
66 |
setGroup(getTranslation("basic_vect_algorithms"));
|
67 |
// setGeneratesUserDefinedRasterOutput(false);
|
68 |
try {
|
69 |
m_Parameters.addInputVectorLayer(LAYER, getTranslation("Input_layer"), IVectorLayer.SHAPE_TYPE_WRONG, true); |
70 |
m_Parameters.addBoolean(SELECTED_GEOM, getTranslation("Selected_geometries_fuse"), false); |
71 |
addOutputVectorLayer(RESULT, getTranslation("fuse_spatially") + " ", OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
72 |
addOutputVectorLayer(RESULT_TABLE, getTranslation("fuse_spatially") + "_" + getTranslation("Table") + " ", OutputVectorLayer.SHAPE_TYPE_UNDEFINED); |
73 |
} catch (RepeatedParameterNameException e) {
|
74 |
Sextante.addErrorToLog(e); |
75 |
} |
76 |
} |
77 |
|
78 |
public void setParentProcess(AbstractSextanteGeoProcess process) { |
79 |
this.process = process;
|
80 |
} |
81 |
|
82 |
/*
|
83 |
* (non-Javadoc)
|
84 |
* @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
|
85 |
*/
|
86 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
87 |
if(existsOutPutFile(DissolveAlgorithm.RESULT, 0)) { |
88 |
throw new GeoAlgorithmExecutionException(getTranslation("file_exists")); |
89 |
} |
90 |
IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
91 |
boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
|
92 |
|
93 |
FeatureStore storeLayer = null;
|
94 |
if(layer instanceof FlyrVectIVectorLayer) |
95 |
storeLayer = ((FlyrVectIVectorLayer)layer).getFeatureStore(); |
96 |
else
|
97 |
return false; |
98 |
|
99 |
try {
|
100 |
String[] attrNames = new String[]{"FID"}; |
101 |
Class[] types = new Class[] {Integer.class}; |
102 |
|
103 |
FeatureStore outFeatStore = buildFuseSpatiallyOutPutStore(attrNames, types, |
104 |
layer.getShapeType(), getTranslation("fusespatially"), RESULT);
|
105 |
|
106 |
return execute(storeLayer, outFeatStore, layer.getShapeType(), selectedGeom, getStatus(), "FID", true); |
107 |
} catch (DataException e) {
|
108 |
Sextante.addErrorToLog(e); |
109 |
return false; |
110 |
} |
111 |
} |
112 |
|
113 |
/**
|
114 |
* @param inputStoreLayer
|
115 |
* @param outFeatStore
|
116 |
* @param shapeType
|
117 |
* @param selectedGeom
|
118 |
* @param status
|
119 |
* @param idField
|
120 |
* @param createTable
|
121 |
* @return boolean
|
122 |
* @throws DataException
|
123 |
*/
|
124 |
public boolean execute(FeatureStore inputStoreLayer, |
125 |
FeatureStore outFeatStore, |
126 |
int shapeType,
|
127 |
boolean selectedGeom,
|
128 |
SimpleTaskStatus status, |
129 |
String idField,
|
130 |
boolean createTable) throws DataException { |
131 |
FeatureStore outFeatStoreTable = null;
|
132 |
String[] attrNamesTable = null; |
133 |
if(createTable) {
|
134 |
attrNamesTable = new String[inputStoreLayer.getDefaultFeatureType().size() + 1]; |
135 |
Class[] typesTable = new Class[inputStoreLayer.getDefaultFeatureType().size() + 1]; |
136 |
attrNamesTable[0] = fid;
|
137 |
typesTable[0] = Integer.class; |
138 |
for (int i = 0; i < inputStoreLayer.getDefaultFeatureType().size(); i++) { |
139 |
FeatureAttributeDescriptor attrDesc = inputStoreLayer.getDefaultFeatureType().getAttributeDescriptor(i); |
140 |
attrNamesTable[i + 1] = attrDesc.getName();
|
141 |
typesTable[i + 1] = attrDesc.getDataType().getDefaultClass();
|
142 |
} |
143 |
|
144 |
attrNamesTable = checkFields(attrNamesTable); |
145 |
outFeatStoreTable = buildFuseSpatiallyOutPutStore(attrNamesTable, typesTable, |
146 |
shapeType, getTranslation("fusespatially") + "_Table", RESULT_TABLE); |
147 |
} |
148 |
|
149 |
FuseSpatiallyOperationFast2 operation = new FuseSpatiallyOperationFast2(this); |
150 |
operation.setTaskStatus(getStatus()); |
151 |
operation.computesGeometryOperation(inputStoreLayer, |
152 |
outFeatStore, |
153 |
outFeatStoreTable, |
154 |
new String[]{fid}, |
155 |
attrNamesTable, |
156 |
selectedGeom, |
157 |
false,
|
158 |
idField); |
159 |
|
160 |
if(getTaskMonitor().isCanceled())
|
161 |
return false; |
162 |
/*computesGeometryOperation(inputStoreLayer, outFeatStore, outFeatStoreTable,
|
163 |
attrNames, attrNamesTable, selectedGeom, status, idField);*/
|
164 |
return true; |
165 |
} |
166 |
|
167 |
/**
|
168 |
* Removes duplicate fields
|
169 |
* @param names
|
170 |
* @return
|
171 |
*/
|
172 |
public String[] checkFields(String[] names) { |
173 |
if(names.length <= 1) |
174 |
return names;
|
175 |
int cont = 0; |
176 |
|
177 |
int i = 1; |
178 |
while(i < names.length) {
|
179 |
if(names[0].compareTo(names[i]) == 0) { |
180 |
names[0] = "FID_" + cont; |
181 |
i = 0;
|
182 |
cont ++; |
183 |
} |
184 |
i ++; |
185 |
} |
186 |
return names;
|
187 |
} |
188 |
|
189 |
|
190 |
/**
|
191 |
* Computes a complete operation over the input FeatureStore. The result of this operation
|
192 |
* is stored in the output FeatureStore. This method will call once for each geometry.
|
193 |
* @param inFeatStore
|
194 |
* Input FeatureStore
|
195 |
* @param outFeatStore
|
196 |
* Output FeatureStore
|
197 |
* @param attrNames
|
198 |
* List of attributes to build the output feature store
|
199 |
* @param selectedGeom
|
200 |
* If it is true only the selected geometries will be processed
|
201 |
* @deprecated This method uses FuseSpatiallyOperation which is deprecated
|
202 |
* @throws DataException
|
203 |
*/
|
204 |
@SuppressWarnings("deprecation") |
205 |
public void computesGeometryOperation(FeatureStore inFeatStore, |
206 |
FeatureStore outFeatStore, |
207 |
FeatureStore outFeatStoreTable, |
208 |
String[] attrNames, |
209 |
String[] attrNamesTable, |
210 |
boolean selectedGeom,
|
211 |
SimpleTaskStatus status, |
212 |
String idField) throws DataException { |
213 |
FeatureSet featuresSet = null;
|
214 |
Iterator it = null; |
215 |
|
216 |
if(selectedGeom) {
|
217 |
featuresSet = (FeatureSet)inFeatStore.getSelection(); |
218 |
} else {
|
219 |
featuresSet = inFeatStore.getFeatureSet(); |
220 |
} |
221 |
|
222 |
it = featuresSet.iterator(); |
223 |
int numberOfFeatures = (int)featuresSet.getSize(); |
224 |
if (status != null) { |
225 |
status.setRangeOfValues(0, numberOfFeatures);
|
226 |
} |
227 |
|
228 |
//Stack<Feature> featList = new Stack<Feature>();
|
229 |
List<Feature> featList = new ArrayList<Feature>(); |
230 |
while( it.hasNext() ) {
|
231 |
Feature feature = (Feature)it.next(); |
232 |
featList.add(feature); |
233 |
} |
234 |
|
235 |
|
236 |
FuseSpatiallyOperation operation = new FuseSpatiallyOperation(featList,
|
237 |
outFeatStoreTable, |
238 |
attrNamesTable, |
239 |
idField, |
240 |
this);
|
241 |
operation.setFeatureStore(outFeatStore, attrNames); |
242 |
int size = featList.size();
|
243 |
operation.setGeoProcess(this, size);
|
244 |
|
245 |
while (featList.size() > 0 && !m_Task.isCanceled()) { |
246 |
Feature f = featList.remove(featList.size() - 1);
|
247 |
operation.invoke(f.getDefaultGeometry(), f); |
248 |
setProgress(size - featList.size(), size); |
249 |
} |
250 |
|
251 |
if(operation.getWriter() != null) |
252 |
operation.getWriter().end(); |
253 |
} |
254 |
|
255 |
public boolean setProgress(int step, int size) { |
256 |
if(process != null) |
257 |
return process.setProgress(step, size);
|
258 |
else
|
259 |
return super.setProgress(step, size); |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Builds the output FeatureStore
|
264 |
* @param featureType
|
265 |
* @return FeatureStore
|
266 |
*/
|
267 |
@SuppressWarnings("unchecked") |
268 |
protected FeatureStore buildFuseSpatiallyOutPutStore(String[] attrNames, |
269 |
Class[] types, |
270 |
int shapeType,
|
271 |
String sextanteLayerName,
|
272 |
String sextanteLayerLabel) {
|
273 |
|
274 |
|
275 |
try {
|
276 |
IVectorLayer output = getNewVectorLayer(sextanteLayerLabel, |
277 |
sextanteLayerName, |
278 |
shapeType, types, attrNames); |
279 |
return ((FlyrVectIVectorLayer)output).getFeatureStore();
|
280 |
} catch (UnsupportedOutputChannelException e) {
|
281 |
Sextante.addErrorToLog(e); |
282 |
} catch (GeoAlgorithmExecutionException e) {
|
283 |
Sextante.addErrorToLog(e); |
284 |
} |
285 |
return null; |
286 |
} |
287 |
|
288 |
} |