root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / it / falciano / sextante / vectorRenameField / VectorRenameFieldAlgorithm.java @ 59
History | View | Annotate | Download (4.3 KB)
1 |
package it.falciano.sextante.vectorRenameField; |
---|---|
2 |
|
3 |
import com.vividsolutions.jts.geom.Geometry; |
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.IVectorLayer; |
11 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
12 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
13 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
14 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
15 |
import es.unex.sextante.outputs.OutputVectorLayer; |
16 |
|
17 |
public class VectorRenameFieldAlgorithm |
18 |
extends
|
19 |
GeoAlgorithm { |
20 |
|
21 |
public static final String INPUT = "INPUT"; |
22 |
public static final String FIELD_TO_RENAME = "FIELD_TO_RENAME"; |
23 |
public static final String NEW_FIELD_NAME = "NEW_FIELD_NAME"; |
24 |
public static final String RESULT = "RESULT"; |
25 |
|
26 |
|
27 |
@Override
|
28 |
public void defineCharacteristics() { |
29 |
|
30 |
this.setName(Sextante.getText("Rename_field")); |
31 |
this.setGroup(Sextante.getText("Tools_for_vector_layers")); |
32 |
this.setUserCanDefineAnalysisExtent(false); |
33 |
|
34 |
try {
|
35 |
m_Parameters.addInputVectorLayer(INPUT, Sextante.getText("Vector layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
36 |
m_Parameters.addTableField(FIELD_TO_RENAME, Sextante.getText("field_to_rename"), "INPUT"); |
37 |
m_Parameters.addString(NEW_FIELD_NAME, Sextante.getText("name_for_new_field"), ""); |
38 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"), OutputVectorLayer.SHAPE_TYPE_UNDEFINED, INPUT);
|
39 |
} |
40 |
catch (final RepeatedParameterNameException e) { |
41 |
e.printStackTrace(); |
42 |
} |
43 |
catch (final UndefinedParentParameterNameException e) { |
44 |
e.printStackTrace(); |
45 |
} |
46 |
catch (final OptionalParentParameterException e) { |
47 |
e.printStackTrace(); |
48 |
} |
49 |
|
50 |
} |
51 |
|
52 |
|
53 |
@Override
|
54 |
@SuppressWarnings("unchecked") |
55 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
56 |
|
57 |
final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(INPUT);
|
58 |
final int iField = m_Parameters.getParameterValueAsInt(FIELD_TO_RENAME); |
59 |
final String sNewFieldName = m_Parameters.getParameterValueAsString(NEW_FIELD_NAME); |
60 |
|
61 |
if (sNewFieldName.length() == 0) { |
62 |
final String s = Sextante.getText("New_field_name_is_empty_Processing_aborted!"); |
63 |
Sextante.addErrorToLog(s); |
64 |
throw new GeoAlgorithmExecutionException(s); |
65 |
} |
66 |
else if (sNewFieldName.length() > 10) { |
67 |
final String s = Sextante.getText("The_length_of_new_field_name_is_over_10_characters_Processing_aborted!"); |
68 |
Sextante.addErrorToLog(s); |
69 |
throw new GeoAlgorithmExecutionException(s); |
70 |
} |
71 |
|
72 |
final Class[] inputFieldTypes = layer.getFieldTypes(); |
73 |
final String[] inputFieldNames = layer.getFieldNames(); |
74 |
final Class[] outputFieldTypes = new Class[inputFieldTypes.length]; |
75 |
final String[] outputFieldNames = new String[inputFieldTypes.length]; |
76 |
|
77 |
for (int i = 0; i < inputFieldTypes.length; i++) { |
78 |
outputFieldTypes[i] = inputFieldTypes[i]; |
79 |
if (i != iField) {
|
80 |
outputFieldNames[i] = inputFieldNames[i]; |
81 |
} |
82 |
else {
|
83 |
outputFieldNames[i] = sNewFieldName; |
84 |
} |
85 |
} |
86 |
|
87 |
final IVectorLayer output = getNewVectorLayer("RESULT", layer.getName(), layer.getShapeType(), outputFieldTypes, |
88 |
outputFieldNames); |
89 |
|
90 |
final int j = 0; |
91 |
final int iShapeCount = layer.getShapesCount(); |
92 |
final IFeatureIterator iter = layer.iterator();
|
93 |
while (iter.hasNext() && setProgress(j, iShapeCount)) {
|
94 |
final IFeature feature = iter.next();
|
95 |
final Object[] inputValues = feature.getRecord().getValues(); |
96 |
final Object[] outputValues = new Object[inputValues.length]; |
97 |
for (int i = 0; i < inputFieldTypes.length; i++) { |
98 |
outputValues[i] = inputValues[i]; |
99 |
} |
100 |
final Geometry geom = feature.getGeometry();
|
101 |
output.addFeature(geom, outputValues); |
102 |
} |
103 |
iter.close(); |
104 |
|
105 |
return !m_Task.isCanceled();
|
106 |
} |
107 |
|
108 |
} |