Revision 35 org.gvsig.sextante/trunk/org.gvsig.sextante.app/org.gvsig.sextante.app.algorithm/org.gvsig.sextante.app.algorithm.dissolve/src/main/java/org/gvsig/sextante/app/algorithm/dissolve/DissolveAlgorithm.java

View differences:

DissolveAlgorithm.java
20 20
 */
21 21
package org.gvsig.sextante.app.algorithm.dissolve;
22 22

  
23
import java.util.ArrayList;
24
import java.util.Arrays;
25
import java.util.Iterator;
26

  
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
29
import org.gvsig.fmap.dal.feature.FeatureSet;
30
import org.gvsig.fmap.dal.feature.FeatureStore;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.sextante.app.extension.core.gvGeoAlgorithm;
33
import org.gvsig.sextante.app.extension.core.gvVectorLayer;
34

  
23 35
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue;
24
import es.unex.sextante.core.GeoAlgorithm;
25 36
import es.unex.sextante.core.Sextante;
26 37
import es.unex.sextante.dataObjects.IVectorLayer;
27 38
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException;
28 39
import es.unex.sextante.exceptions.RepeatedParameterNameException;
40
import es.unex.sextante.exceptions.UnsupportedOutputChannelException;
29 41
import es.unex.sextante.outputs.OutputVectorLayer;
30 42

  
31 43
/**
32 44
 * Dissolve algorithm
33 45
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
34 46
 */
35
public class DissolveAlgorithm extends GeoAlgorithm {
36
	public static final String RESULT            = "RESULT";
37
	public static final String LAYER             = "LAYER";
38
	public static final String FIELD             = "FIELD";
39
	public static final String FUNCTIONS         = "FUNCTIONS";
40
	public static final String SELECTED_GEOM     = "SELECTED_GEOM";
41
	public static final String DISSOLV_ADJ       = "DISSOLV_ADJ";
42
	public static final String FUNCTION_LIST     = "FUNCTION_LIST";
43
	public static final String Summary[]         = {"Min", "Max", "Sum", "Avg"};
44
	
47
public class DissolveAlgorithm extends gvGeoAlgorithm {
48
	public static final String        RESULT            = "RESULT";
49
	public static final String        LAYER             = "LAYER";
50
	public static final String        FIELD             = "FIELD";
51
	public static final String        FUNCTIONS         = "FUNCTIONS";
52
	public static final String        SELECTED_GEOM     = "SELECTED_GEOM";
53
	public static final String        DISSOLV_ADJ       = "DISSOLV_ADJ";
54
	public static final String        FUNCTION_LIST     = "FUNCTION_LIST";
55
	public static final String        Summary[]         = {"Min", "Max", "Sum", "Avg"};
56
	private boolean                   funcList[]        = new boolean[Summary.length];
57

  
45 58
	/*
46 59
	 * (non-Javadoc)
47 60
	 * @see es.unex.sextante.core.GeoAlgorithm#defineCharacteristics()
......
65 78
											Sextante.getText("Field"),
66 79
											0,
67 80
											AdditionalInfoNumericalValue.NUMERICAL_VALUE_INTEGER);
68
			m_Parameters.addString(FUNCTION_LIST, 
69
									Sextante.getText("Function_list"));
81
			m_Parameters.addString(FUNCTION_LIST, Sextante.getText("Function_list"));
70 82
			addOutputVectorLayer(RESULT,
71 83
								Sextante.getText( "Dissolve"),
72 84
								OutputVectorLayer.SHAPE_TYPE_POLYGON);
......
81 93
	 * @see es.unex.sextante.core.GeoAlgorithm#processAlgorithm()
82 94
	 */
83 95
	public boolean processAlgorithm() throws GeoAlgorithmExecutionException {
84
		IVectorLayer clip = m_Parameters.getParameterValueAsVectorLayer(LAYER);
96
		IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
85 97
		int indexField = m_Parameters.getParameterValueAsInt(FIELD);
86 98
		boolean selectedGeom = m_Parameters.getParameterValueAsBoolean(SELECTED_GEOM);
87 99
		boolean dissolvAdj = m_Parameters.getParameterValueAsBoolean(DISSOLV_ADJ);
88 100
		String functionList = m_Parameters.getParameterValueAsString(FUNCTION_LIST);
101
		loadSummary(functionList);
89 102
		
90
		System.out.println(functionList);
103
		FeatureStore storeLayer = null;
104
		if(layer instanceof gvVectorLayer)
105
			storeLayer = ((gvVectorLayer)layer).getFeatureStore();
106
		else
107
			return false;
91 108
		
109
		FeatureSet features = null;
110
		FeatureType featureType = null;
111
		try {
112
			features = storeLayer.getFeatureSet();
113
			featureType = features.getDefaultFeatureType();
114
		} catch (DataException e) {
115
			Sextante.addErrorToLog(e);
116
			return false;
117
		}
118
		FeatureStore outFeatStore = buildDissolvedOutPutStore(featureType, indexField, layer.getShapeType(),
119
														Sextante.getText("Dissolve"), RESULT);
120
		DissolveOperation op = new DissolveOperation();
121
		try {
122
			op.computesOperation(storeLayer, outFeatStore, attrNames, selectedGeom, true);
123
		} catch (DataException e) {
124
			Sextante.addErrorToLog(e);
125
			return false;
126
		}
92 127
		return true;
93 128
	}
94

  
129
	
130
	/**
131
	 * Checks if the parameter is in Summary list
132
	 * @param it
133
	 * @return the position in the list
134
	 */
135
	private int isInList(String it) {
136
		for (int i = 0; i < Summary.length; i++) {
137
			if(Summary[i].compareTo(it) == 0)
138
				return i;
139
		}
140
		return -1;
141
	}
142
	
143
	/**
144
	 * Loads the list of functions to use
145
	 * @param functionList
146
	 */
147
	private void loadSummary(String functionList) {
148
		String[] attrList = functionList.split(";");
149
		for (int i = 0; i < attrList.length; i++) {
150
			String[] func = attrList[i].split(",");
151
			for (int j = 1; j < func.length; j++) {
152
				int pos = isInList(func[j]);
153
				if(pos != -1)
154
					funcList[pos] = true;
155
			}
156
		}
157
	}
158
	
159
	/**
160
	 * Builds the output FeatureStore 
161
	 * @param featureType
162
	 * @return FeatureStore
163
	 */
164
	@SuppressWarnings("unchecked")
165
	protected FeatureStore buildDissolvedOutPutStore(FeatureType featureType1,
166
											int indexField,
167
											int shapeType,
168
											String sextanteLayerName, 
169
											String sextanteLayerLabel) {
170
		ArrayList<Class> typesList = new ArrayList<Class>();
171
		ArrayList<String> attr = new ArrayList<String>();
172
		attr.add("FID");
173
		typesList.add(Integer.class);
174
		FeatureAttributeDescriptor desc = featureType1.getAttributeDescriptor(indexField);
175
		attr.add(desc.getName());
176
		typesList.add(desc.getObjectClass());
177
		for (int i = 0; i < funcList.length; i++) 
178
			if(funcList[i])
179
				attr.add(Summary[i]);
180
		
181
		attrNames = new String[attr.size()];
182
		attr.toArray(attrNames);
183
		Class[] types = new Class[typesList.size()];
184
		typesList.toArray(types);
185
		
186
		try {
187
			IVectorLayer output = getNewVectorLayer(sextanteLayerLabel,
188
													sextanteLayerName,
189
													shapeType, types, attrNames);
190
			return ((gvVectorLayer)output).getFeatureStore();
191
		} catch (UnsupportedOutputChannelException e) {
192
			Sextante.addErrorToLog(e);
193
		}
194
		return null;
195
	}
95 196
}

Also available in: Unified diff