root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / normalityTest / NormalityTestAlgorithm.java @ 59
History | View | Annotate | Download (4.39 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.tables.normalityTest; |
4 |
|
5 |
import java.text.DecimalFormat; |
6 |
import java.util.ArrayList; |
7 |
|
8 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
9 |
import es.unex.sextante.core.GeoAlgorithm; |
10 |
import es.unex.sextante.core.Sextante; |
11 |
import es.unex.sextante.dataObjects.IFeature; |
12 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
13 |
import es.unex.sextante.dataObjects.IVectorLayer; |
14 |
import es.unex.sextante.dataObjects.vectorFilters.BoundingBoxFilter; |
15 |
import es.unex.sextante.docEngines.html.HTMLDoc; |
16 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
17 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
18 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
19 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
20 |
|
21 |
|
22 |
public class NormalityTestAlgorithm |
23 |
extends
|
24 |
GeoAlgorithm { |
25 |
|
26 |
public static final String RESULT = "RESULT"; |
27 |
public static final String FIELD = "FIELD"; |
28 |
public static final String LAYER = "LAYER"; |
29 |
|
30 |
|
31 |
@Override
|
32 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
33 |
|
34 |
int i;
|
35 |
int iField;
|
36 |
int iCount;
|
37 |
double dValue;
|
38 |
IVectorLayer layer; |
39 |
final ArrayList<Double> array = new ArrayList<Double>(); |
40 |
|
41 |
layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
42 |
if (!m_bIsAutoExtent) {
|
43 |
layer.addFilter(new BoundingBoxFilter(m_AnalysisExtent));
|
44 |
} |
45 |
iField = m_Parameters.getParameterValueAsInt(FIELD); |
46 |
|
47 |
i = 0;
|
48 |
iCount = layer.getShapesCount(); |
49 |
final IFeatureIterator iter = layer.iterator();
|
50 |
while (iter.hasNext() && setProgress(i, iCount)) {
|
51 |
final IFeature feature = iter.next();
|
52 |
try {
|
53 |
dValue = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
54 |
array.add(new Double(dValue)); |
55 |
} |
56 |
catch (final Exception e) { |
57 |
} |
58 |
i++; |
59 |
} |
60 |
iter.close(); |
61 |
|
62 |
if (array.size() < 3) { |
63 |
throw new GeoAlgorithmExecutionException(Sextante.getText("At_least_three_features_are_needed_for_using_this_algorithm")); |
64 |
} |
65 |
|
66 |
if (array.size() > 5000) { |
67 |
Sextante.addWarningToLog("Mas_de_5000puntos_shapiro");
|
68 |
} |
69 |
|
70 |
final double[] values = new double[array.size() + 1]; |
71 |
for (int j = 0; j < array.size(); j++) { |
72 |
values[j + 1] = array.get(j).doubleValue();
|
73 |
} |
74 |
|
75 |
final double[] pw = new double[1]; |
76 |
final double[] w = new double[1]; |
77 |
final double[] a = new double[array.size() / 2]; |
78 |
final int n = Math.min(5000, array.size()); |
79 |
final boolean[] init = new boolean[1]; |
80 |
init[0] = false; |
81 |
final int[] ifault = new int[] { -1 }; |
82 |
|
83 |
SWilk.swilk(init, values, n, n, n / 2, a, w, pw, ifault);
|
84 |
|
85 |
if ((ifault[0] != 0) && (ifault[0] != 2)) { |
86 |
throw new GeoAlgorithmExecutionException("Error_calculando_shapiro"); |
87 |
} |
88 |
|
89 |
|
90 |
if (m_Task.isCanceled()) {
|
91 |
return false; |
92 |
} |
93 |
else {
|
94 |
final DecimalFormat df = new DecimalFormat("##.###"); |
95 |
final HTMLDoc doc = new HTMLDoc(); |
96 |
doc.open(Sextante.getText("Normality_test"));
|
97 |
doc.addHeader(Sextante.getText("Normality_test"), 2); |
98 |
doc.startUnorderedList(); |
99 |
doc.addListElement(Sextante.getText("Shapiro-Wilk_W") + df.format(w[0])); |
100 |
doc.closeUnorderedList(); |
101 |
doc.close(); |
102 |
|
103 |
addOutputText(RESULT, Sextante.getText("Statistics") + "[" + layer.getName() + "]", doc.getHTMLCode()); |
104 |
return true; |
105 |
} |
106 |
|
107 |
} |
108 |
|
109 |
|
110 |
@Override
|
111 |
public void defineCharacteristics() { |
112 |
|
113 |
setName(Sextante.getText("Normality_test"));
|
114 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
115 |
setUserCanDefineAnalysisExtent(true);
|
116 |
try {
|
117 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
118 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), "LAYER"); |
119 |
addOutputText(RESULT, Sextante.getText("Result"));
|
120 |
} |
121 |
catch (final RepeatedParameterNameException e) { |
122 |
Sextante.addErrorToLog(e); |
123 |
} |
124 |
catch (final UndefinedParentParameterNameException e) { |
125 |
Sextante.addErrorToLog(e); |
126 |
} |
127 |
catch (final OptionalParentParameterException e) { |
128 |
Sextante.addErrorToLog(e); |
129 |
} |
130 |
|
131 |
} |
132 |
|
133 |
} |