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