root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / tables / vectorFieldCorrelation / VectorFieldCorrelationAlgorithm.java @ 59
History | View | Annotate | Download (4.39 KB)
1 |
package es.unex.sextante.tables.vectorFieldCorrelation; |
---|---|
2 |
|
3 |
import java.text.DecimalFormat; |
4 |
|
5 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
6 |
import es.unex.sextante.additionalResults.CorrelationGraphCreator; |
7 |
import es.unex.sextante.core.GeoAlgorithm; |
8 |
import es.unex.sextante.core.Sextante; |
9 |
import es.unex.sextante.dataObjects.IFeature; |
10 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
11 |
import es.unex.sextante.dataObjects.IVectorLayer; |
12 |
import es.unex.sextante.docEngines.html.HTMLDoc; |
13 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
14 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
15 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
16 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
17 |
import es.unex.sextante.math.regression.Regression; |
18 |
|
19 |
public class VectorFieldCorrelationAlgorithm |
20 |
extends
|
21 |
GeoAlgorithm { |
22 |
|
23 |
public static final String FIELD2 = "FIELD2"; |
24 |
public static final String FIELD = "FIELD"; |
25 |
public static final String LAYER = "LAYER"; |
26 |
public static final String METHOD = "METHOD"; |
27 |
public static final String REGRESSION = "REGRESSION"; |
28 |
public static final String REGRESSION_DATA = "REGRESSION_DATA"; |
29 |
|
30 |
|
31 |
@Override
|
32 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
33 |
|
34 |
int i;
|
35 |
int iCount;
|
36 |
int iField, iField2;
|
37 |
double dValue, dValue2;
|
38 |
IVectorLayer layer; |
39 |
final Regression regression = new Regression(); |
40 |
|
41 |
final int iType = m_Parameters.getParameterValueAsInt(METHOD); |
42 |
layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
43 |
iField = m_Parameters.getParameterValueAsInt(FIELD); |
44 |
iField2 = m_Parameters.getParameterValueAsInt(FIELD2); |
45 |
|
46 |
i = 0;
|
47 |
iCount = layer.getShapesCount(); |
48 |
final IFeatureIterator iter = layer.iterator();
|
49 |
while (iter.hasNext() && setProgress(i, iCount)) {
|
50 |
final IFeature feature = iter.next();
|
51 |
try {
|
52 |
dValue = Double.parseDouble(feature.getRecord().getValue(iField).toString());
|
53 |
dValue2 = Double.parseDouble(feature.getRecord().getValue(iField2).toString());
|
54 |
regression.addValue(dValue, dValue2); |
55 |
} |
56 |
catch (final Exception e) {} |
57 |
i++; |
58 |
} |
59 |
iter.close(); |
60 |
|
61 |
regression.calculate(iType); |
62 |
final CorrelationGraphCreator panel = new CorrelationGraphCreator(regression); |
63 |
|
64 |
addOutputChart(REGRESSION, Sextante.getText("Regression"), panel.getChartPanel());
|
65 |
|
66 |
final DecimalFormat df = new DecimalFormat("####.###"); |
67 |
final HTMLDoc doc = new HTMLDoc(); |
68 |
doc.open(Sextante.getText("Regression"));
|
69 |
doc.addHeader(Sextante.getText("Regression"), 2); |
70 |
doc.startUnorderedList(); |
71 |
doc.addListElement(regression.getExpression()); |
72 |
doc.addListElement("R� = " + df.format(regression.getR2()));
|
73 |
doc.closeUnorderedList(); |
74 |
doc.close(); |
75 |
|
76 |
addOutputText(REGRESSION_DATA, Sextante.getText("Regression_values"), doc.getHTMLCode());
|
77 |
|
78 |
return !m_Task.isCanceled();
|
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
@Override
|
84 |
public void defineCharacteristics() { |
85 |
|
86 |
final String sMethod[] = { Sextante.getText("Best_fit"), "y = a + b * x", "y = a + b / x", "y = a / (b - x)", |
87 |
"y = a * x^b", "y = a e^(b * x)", "y = a + b * ln(x)" }; |
88 |
|
89 |
setName(Sextante.getText("Correlation_between_fields"));
|
90 |
setGroup(Sextante.getText("Tools_for_vector_layers"));
|
91 |
setUserCanDefineAnalysisExtent(false);
|
92 |
try {
|
93 |
m_Parameters.addInputVectorLayer("LAYER", Sextante.getText("Layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_ANY, true); |
94 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), LAYER);
|
95 |
m_Parameters.addTableField(FIELD2, Sextante.getText("Field") + "2", LAYER); |
96 |
m_Parameters.addSelection(METHOD, Sextante.getText("Equation"), sMethod);
|
97 |
addOutputChart(REGRESSION, Sextante.getText("Regression"));
|
98 |
addOutputText(REGRESSION_DATA, Sextante.getText("Regression_values"));
|
99 |
} |
100 |
catch (final RepeatedParameterNameException e) { |
101 |
Sextante.addErrorToLog(e); |
102 |
} |
103 |
catch (final UndefinedParentParameterNameException e) { |
104 |
Sextante.addErrorToLog(e); |
105 |
} |
106 |
catch (final OptionalParentParameterException e) { |
107 |
Sextante.addErrorToLog(e); |
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
} |