root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / vectorSplitField / VectorSplitFieldAlgorithm.java @ 59
History | View | Annotate | Download (4.09 KB)
1 |
package es.unex.sextante.tables.vectorSplitField; |
---|---|
2 |
|
3 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
4 |
import es.unex.sextante.core.GeoAlgorithm; |
5 |
import es.unex.sextante.core.Sextante; |
6 |
import es.unex.sextante.dataObjects.IFeature; |
7 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
8 |
import es.unex.sextante.dataObjects.IVectorLayer; |
9 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
10 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
11 |
import es.unex.sextante.outputs.IOutputChannel; |
12 |
import es.unex.sextante.outputs.OutputVectorLayer; |
13 |
import es.unex.sextante.shapesTools.ShapesTools; |
14 |
|
15 |
|
16 |
public class VectorSplitFieldAlgorithm |
17 |
extends
|
18 |
GeoAlgorithm { |
19 |
|
20 |
private static final Object NO_DATA = new Double(-99999.); |
21 |
|
22 |
public static final String FIELD = "FIELD"; |
23 |
public static final String BREAKPOINTS = "BREAKPOINTS"; |
24 |
public static final String LAYER = "LAYER"; |
25 |
public static final String RESULT = "RESULT"; |
26 |
|
27 |
|
28 |
@Override
|
29 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
30 |
|
31 |
final int i, j; |
32 |
final double dValue; |
33 |
final boolean bOK; |
34 |
final String sVariable; |
35 |
|
36 |
final IVectorLayer layer = m_Parameters.getParameterValueAsVectorLayer(LAYER);
|
37 |
if (!m_bIsAutoExtent) {
|
38 |
layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
39 |
} |
40 |
|
41 |
final int iField = m_Parameters.getParameterValueAsInt(FIELD); |
42 |
final String sBreakpoints = m_Parameters.getParameterValueAsString(BREAKPOINTS); |
43 |
final String[] sTokens = sBreakpoints.split(","); |
44 |
|
45 |
final int[] breakpoints = new int[sTokens.length + 2]; |
46 |
breakpoints[0] = 0; |
47 |
breakpoints[breakpoints.length - 1] = 1000; |
48 |
for (int k = 0; k < sTokens.length; k++) { |
49 |
breakpoints[k + 1] = Integer.parseInt(sTokens[k]); |
50 |
} |
51 |
|
52 |
final Class[] types = new Class[sTokens.length + 1]; |
53 |
for (int k = 0; k < types.length; k++) { |
54 |
types[k] = String.class;
|
55 |
} |
56 |
|
57 |
final String sBaseName = layer.getFieldName(iField); |
58 |
final String[] sFields = new String[sTokens.length + 1]; |
59 |
for (int k = 0; k < sFields.length; k++) { |
60 |
sFields[k] = sBaseName + "_" + Integer.toString(k); |
61 |
} |
62 |
|
63 |
|
64 |
final int iTotalCount = layer.getShapesCount(); |
65 |
final String[][] values = new String[breakpoints.length][iTotalCount]; |
66 |
final IFeatureIterator iter = layer.iterator();
|
67 |
IFeature feat; |
68 |
int iCount = 0; |
69 |
while (iter.hasNext() && setProgress(iCount, iTotalCount)) {
|
70 |
feat = iter.next(); |
71 |
final String s = feat.getRecord().getValue(iField).toString(); |
72 |
System.out.println(s);
|
73 |
for (int k = 1; k < breakpoints.length; k++) { |
74 |
final int iMin = Math.min(breakpoints[k - 1], s.length()); |
75 |
final int iMin2 = Math.min(breakpoints[k], s.length()); |
76 |
values[k - 1][iCount] = s.substring(iMin, iMin2);
|
77 |
} |
78 |
iCount++; |
79 |
} |
80 |
|
81 |
|
82 |
final IOutputChannel channel = getOutputChannel(RESULT);
|
83 |
final OutputVectorLayer out = new OutputVectorLayer(); |
84 |
out.setName(RESULT); |
85 |
out.setOutputChannel(channel); |
86 |
out.setDescription(Sextante.getText("Result"));
|
87 |
out.setOutputObject(ShapesTools.addFields(m_OutputFactory, layer, channel, sFields, values, types)); |
88 |
addOutputObject(out); |
89 |
|
90 |
|
91 |
return !m_Task.isCanceled();
|
92 |
|
93 |
} |
94 |
|
95 |
|
96 |
@Override
|
97 |
public void defineCharacteristics() { |
98 |
|
99 |
setName(Sextante.getText("Split_field"));
|
100 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
101 |
setUserCanDefineAnalysisExtent(true);
|
102 |
|
103 |
try {
|
104 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
105 |
m_Parameters.addString(BREAKPOINTS, Sextante.getText("Breakpoints"));
|
106 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), LAYER);
|
107 |
addOutputVectorLayer(RESULT, Sextante.getText("Result"));
|
108 |
} |
109 |
catch (final Exception e) { |
110 |
Sextante.addErrorToLog(e); |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
} |