root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / createEquivalentNumericalClass / CreateEquivalentNumericalClassAlgorithm.java @ 59
History | View | Annotate | Download (3.81 KB)
1 |
package es.unex.sextante.tables.createEquivalentNumericalClass; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
6 |
import es.unex.sextante.core.GeoAlgorithm; |
7 |
import es.unex.sextante.core.Sextante; |
8 |
import es.unex.sextante.dataObjects.IFeature; |
9 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
10 |
import es.unex.sextante.dataObjects.IRecord; |
11 |
import es.unex.sextante.dataObjects.IVectorLayer; |
12 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
13 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
14 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
15 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
16 |
import es.unex.sextante.outputs.OutputVectorLayer; |
17 |
|
18 |
public class CreateEquivalentNumericalClassAlgorithm |
19 |
extends
|
20 |
GeoAlgorithm { |
21 |
|
22 |
public static final String LAYER = "LAYER"; |
23 |
public static final String NEW_FIELD_NAME = "NEW_FIELD_NAME"; |
24 |
public static final String CLASS_FIELD = "CLASS_FIELD"; |
25 |
public static final String RESULT = "RESULT"; |
26 |
|
27 |
|
28 |
@Override
|
29 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
30 |
|
31 |
final ArrayList<String> list = new ArrayList<String>(); |
32 |
|
33 |
final IVectorLayer layerIn = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
34 |
final int iField = m_Parameters.getParameterValueAsInt(CLASS_FIELD); |
35 |
final Class[] types = new Class[layerIn.getFieldCount() + 1]; |
36 |
System.arraycopy(layerIn.getFieldTypes(), 0, types, 0, layerIn.getFieldCount()); |
37 |
types[types.length - 1] = Integer.class; |
38 |
final String[] sFields = new String[layerIn.getFieldCount() + 1]; |
39 |
System.arraycopy(layerIn.getFieldNames(), 0, sFields, 0, layerIn.getFieldCount()); |
40 |
sFields[types.length - 1] = m_Parameters.getParameterValueAsString(NEW_FIELD_NAME);
|
41 |
|
42 |
final IVectorLayer driver = getNewVectorLayer(RESULT, layerIn.getName(), layerIn.getShapeType(), types, sFields);
|
43 |
|
44 |
final IFeatureIterator iter = layerIn.iterator();
|
45 |
final int iTotal = layerIn.getShapesCount(); |
46 |
int i = 0; |
47 |
while (iter.hasNext() && setProgress(i, iTotal)) {
|
48 |
final IFeature feature = iter.next();
|
49 |
final IRecord record = feature.getRecord();
|
50 |
final Object[] values = record.getValues(); |
51 |
final Object[] newValues = new Object[values.length + 1]; |
52 |
System.arraycopy(values, 0, newValues, 0, values.length); |
53 |
int iClass = list.indexOf(values[iField].toString());
|
54 |
if (iClass == -1) { |
55 |
iClass = list.size(); |
56 |
list.add(values[iField].toString()); |
57 |
} |
58 |
newValues[values.length] = new Integer(iClass); |
59 |
driver.addFeature(feature.getGeometry(), newValues); |
60 |
i++; |
61 |
} |
62 |
iter.close(); |
63 |
|
64 |
return !m_Task.isCanceled();
|
65 |
} |
66 |
|
67 |
|
68 |
@Override
|
69 |
public void defineCharacteristics() { |
70 |
|
71 |
setName(Sextante.getText("Create_equivalent_numerical_class"));
|
72 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
73 |
|
74 |
try {
|
75 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Input_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
76 |
m_Parameters.addString(NEW_FIELD_NAME, Sextante.getText("name_for_new_field"), "CLASS_ID"); |
77 |
m_Parameters.addTableField(CLASS_FIELD, Sextante.getText("Class_field"), LAYER);
|
78 |
|
79 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_UNDEFINED);
|
80 |
} |
81 |
catch (final RepeatedParameterNameException e) { |
82 |
Sextante.addErrorToLog(e); |
83 |
} |
84 |
catch (final UndefinedParentParameterNameException e) { |
85 |
Sextante.addErrorToLog(e); |
86 |
} |
87 |
catch (final OptionalParentParameterException e) { |
88 |
Sextante.addErrorToLog(e); |
89 |
} |
90 |
} |
91 |
|
92 |
} |