root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / tableBasicStats / TableBasicStatsAlgorithm.java @ 59
History | View | Annotate | Download (5.24 KB)
1 |
package es.unex.sextante.tables.tableBasicStats; |
---|---|
2 |
|
3 |
import java.text.DecimalFormat; |
4 |
|
5 |
import es.unex.sextante.core.GeoAlgorithm; |
6 |
import es.unex.sextante.core.Sextante; |
7 |
import es.unex.sextante.dataObjects.IRecord; |
8 |
import es.unex.sextante.dataObjects.IRecordsetIterator; |
9 |
import es.unex.sextante.dataObjects.ITable; |
10 |
import es.unex.sextante.docEngines.html.HTMLDoc; |
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.math.simpleStats.SimpleStats; |
16 |
|
17 |
public class TableBasicStatsAlgorithm |
18 |
extends
|
19 |
GeoAlgorithm { |
20 |
|
21 |
public static final String TABLE = "TABLE"; |
22 |
public static final String FIELD = "FIELD"; |
23 |
public static final String STATS = "STATS"; |
24 |
public static final String MEAN = "MEAN"; |
25 |
public static final String MEAN_SQUARED = "MEAN_SQUARED"; |
26 |
public static final String MIN = "MIN"; |
27 |
public static final String MAX = "MAX"; |
28 |
public static final String VARIANCE = "VARIANCE"; |
29 |
public static final String SUM = "SUM"; |
30 |
public static final String COEF_OF_VARIATION = "COEF_OF_VARIATION"; |
31 |
|
32 |
|
33 |
@Override
|
34 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
35 |
|
36 |
int i;
|
37 |
int iField;
|
38 |
long iCount;
|
39 |
double dValue;
|
40 |
ITable table; |
41 |
final SimpleStats stats = new SimpleStats(); |
42 |
|
43 |
table = m_Parameters.getParameterValueAsTable(TABLE); |
44 |
iField = m_Parameters.getParameterValueAsInt(FIELD); |
45 |
|
46 |
i = 0;
|
47 |
iCount = table.getRecordCount(); |
48 |
final IRecordsetIterator iter = table.iterator();
|
49 |
while (iter.hasNext() && setProgress(i, (int) iCount)) { |
50 |
final IRecord record = iter.next();
|
51 |
try {
|
52 |
dValue = Double.parseDouble(record.getValue(iField).toString());
|
53 |
stats.addValue(dValue); |
54 |
} |
55 |
catch (final NumberFormatException e) {} |
56 |
i++; |
57 |
} |
58 |
iter.close(); |
59 |
|
60 |
if (m_Task.isCanceled()) {
|
61 |
return false; |
62 |
} |
63 |
else {
|
64 |
final DecimalFormat df = new DecimalFormat("##.###"); |
65 |
final HTMLDoc doc = new HTMLDoc(); |
66 |
doc.open(Sextante.getText("Statistics"));
|
67 |
doc.addHeader(Sextante.getText("Basic_statistics"), 2); |
68 |
doc.startUnorderedList(); |
69 |
doc.addListElement(Sextante.getText("Mean_value") + ": " + df.format(stats.getMean())); |
70 |
doc.addListElement(Sextante.getText("Mean_squared_value") + ": " + df.format(stats.getRMS())); |
71 |
doc.addListElement(Sextante.getText("Min_value") + ": " + df.format(stats.getMin())); |
72 |
doc.addListElement(Sextante.getText("Max_value") + ": " + df.format(stats.getMax())); |
73 |
doc.addListElement(Sextante.getText("Variance") + ": " + df.format(stats.getVariance())); |
74 |
doc.addListElement(Sextante.getText("Total_sum") + ": " + df.format(stats.getSum())); |
75 |
doc.addListElement(Sextante.getText("Coefficient_of_variation") + ": " + df.format(stats.getCoeffOfVar())); |
76 |
doc.closeUnorderedList(); |
77 |
doc.close(); |
78 |
|
79 |
addOutputText("STATS", Sextante.getText("Statistics") + "[" + table.getName() + "]", doc.getHTMLCode()); |
80 |
|
81 |
addOutputNumericalValue(MEAN, Sextante.getText("Mean_value"));
|
82 |
addOutputNumericalValue(MEAN_SQUARED, Sextante.getText("Mean_squared_value"));
|
83 |
addOutputNumericalValue(MIN, Sextante.getText("Minimum_value"));
|
84 |
addOutputNumericalValue(MAX, Sextante.getText("Maximum_value"));
|
85 |
addOutputNumericalValue(VARIANCE, Sextante.getText("Variance"));
|
86 |
addOutputNumericalValue(SUM, Sextante.getText("Total_sum"));
|
87 |
addOutputNumericalValue(COEF_OF_VARIATION, Sextante.getText("Coefficient_of_variation"));
|
88 |
|
89 |
return true; |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
|
95 |
@Override
|
96 |
public void defineCharacteristics() { |
97 |
|
98 |
setName(Sextante.getText("Basic_statistics"));
|
99 |
setGroup(Sextante.getText("Table_tools"));
|
100 |
|
101 |
try {
|
102 |
m_Parameters.addInputTable(TABLE, Sextante.getText("Table"), true); |
103 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), TABLE);
|
104 |
addOutputText(STATS, Sextante.getText("Statistics"));
|
105 |
addOutputNumericalValue(MEAN, Sextante.getText("Mean_value"));
|
106 |
addOutputNumericalValue(MEAN_SQUARED, Sextante.getText("Mean_squared_value"));
|
107 |
addOutputNumericalValue(MIN, Sextante.getText("Minimum_value"));
|
108 |
addOutputNumericalValue(MAX, Sextante.getText("Maximum_value"));
|
109 |
addOutputNumericalValue(VARIANCE, Sextante.getText("Variance"));
|
110 |
addOutputNumericalValue(SUM, Sextante.getText("Total_sum"));
|
111 |
addOutputNumericalValue(COEF_OF_VARIATION, Sextante.getText("Coefficient_of_variation"));
|
112 |
} |
113 |
catch (final RepeatedParameterNameException e) { |
114 |
Sextante.addErrorToLog(e); |
115 |
} |
116 |
catch (final UndefinedParentParameterNameException e) { |
117 |
Sextante.addErrorToLog(e); |
118 |
} |
119 |
catch (final OptionalParentParameterException e) { |
120 |
Sextante.addErrorToLog(e); |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
} |