root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / rasterize / interpolationBase / BaseInterpolationAlgorithm.java @ 513
History | View | Annotate | Download (6.93 KB)
1 |
|
---|---|
2 |
|
3 |
package es.unex.sextante.rasterize.interpolationBase; |
4 |
|
5 |
import java.awt.geom.Point2D; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Coordinate; |
8 |
import com.vividsolutions.jts.geom.Geometry; |
9 |
|
10 |
import es.unex.sextante.additionalInfo.AdditionalInfoNumericalValue; |
11 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
12 |
import es.unex.sextante.closestpts.NearestNeighbourFinder; |
13 |
import es.unex.sextante.closestpts.PtAndDistance; |
14 |
import es.unex.sextante.core.GeoAlgorithm; |
15 |
import es.unex.sextante.core.Sextante; |
16 |
import es.unex.sextante.dataObjects.IFeature; |
17 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
18 |
import es.unex.sextante.dataObjects.IRasterLayer; |
19 |
import es.unex.sextante.dataObjects.ITable; |
20 |
import es.unex.sextante.dataObjects.IVectorLayer; |
21 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
22 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
23 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
24 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
25 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
26 |
import es.unex.sextante.rasterWrappers.GridCell; |
27 |
|
28 |
|
29 |
public abstract class BaseInterpolationAlgorithm |
30 |
extends
|
31 |
GeoAlgorithm { |
32 |
|
33 |
protected double NO_DATA; |
34 |
|
35 |
public static final String LAYER = "LAYER"; |
36 |
public static final String DIST = "DIST"; |
37 |
public static final String FIELD = "FIELD"; |
38 |
public static final String RESULT = "RESULT"; |
39 |
|
40 |
protected double m_dDistance; |
41 |
protected int m_iField; |
42 |
protected NearestNeighbourFinder m_SearchEngine;
|
43 |
protected PtAndDistance[] m_NearestPoints; |
44 |
|
45 |
protected IVectorLayer m_Layer;
|
46 |
|
47 |
|
48 |
@Override
|
49 |
public void defineCharacteristics() { |
50 |
|
51 |
setUserCanDefineAnalysisExtent(true);
|
52 |
|
53 |
try {
|
54 |
m_Parameters.addInputVectorLayer(LAYER, Sextante.getText("Point_layer"), AdditionalInfoVectorLayer.SHAPE_TYPE_POINT,
|
55 |
true);
|
56 |
m_Parameters.addTableField(FIELD, Sextante.getText("Field"), LAYER);
|
57 |
m_Parameters.addNumericalValue(DIST, Sextante.getText("Search_radius"),
|
58 |
AdditionalInfoNumericalValue.NUMERICAL_VALUE_DOUBLE, 100, 0, Double.MAX_VALUE); |
59 |
addOutputRasterLayer(RESULT, Sextante.getText("Result"));
|
60 |
} |
61 |
catch (final UndefinedParentParameterNameException e) { |
62 |
Sextante.addErrorToLog(e); |
63 |
} |
64 |
catch (final OptionalParentParameterException e) { |
65 |
Sextante.addErrorToLog(e); |
66 |
} |
67 |
catch (final RepeatedParameterNameException e) { |
68 |
Sextante.addErrorToLog(e); |
69 |
} |
70 |
|
71 |
} |
72 |
|
73 |
|
74 |
@Override
|
75 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
76 |
|
77 |
int x, y;
|
78 |
int iNX, iNY;
|
79 |
|
80 |
NO_DATA = m_OutputFactory.getDefaultNoDataValue(); |
81 |
|
82 |
setValues(); |
83 |
|
84 |
m_SearchEngine = new NearestNeighbourFinder(m_Layer, m_iField, m_Task);
|
85 |
final IRasterLayer result = getNewRasterLayer(RESULT, m_Layer.getName() + Sextante.getText("[interpolated]"), |
86 |
IRasterLayer.RASTER_DATA_TYPE_DOUBLE); |
87 |
|
88 |
iNX = m_AnalysisExtent.getNX(); |
89 |
iNY = m_AnalysisExtent.getNY(); |
90 |
|
91 |
setProgressText(Sextante.getText("Interpolating"));
|
92 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
93 |
for (x = 0; x < iNX; x++) { |
94 |
result.setCellValue(x, y, getValueAt(x, y)); |
95 |
} |
96 |
} |
97 |
|
98 |
createCrossValidationTable(); |
99 |
|
100 |
return !m_Task.isCanceled();
|
101 |
|
102 |
} |
103 |
|
104 |
|
105 |
protected void createCrossValidationTable() throws UnsupportedOutputChannelException { |
106 |
|
107 |
int i;
|
108 |
int iPoints;
|
109 |
double x, y, z;
|
110 |
double dValue;
|
111 |
final Object[] values = new Object[5]; |
112 |
final String sFields[] = { "X", "Y", "Real_value", "Esti_value", |
113 |
Sextante.getText("Diference") };
|
114 |
final Class types[] = { Double.class, Double.class, Double.class, Double.class, Double.class }; |
115 |
final String sTableName = Sextante.getText("Cross_validation_[") + m_Layer.getName() + "]"; |
116 |
final ITable table = getNewTable("CROSSVALIDATION", sTableName, types, sFields); |
117 |
|
118 |
try {
|
119 |
setProgressText(Sextante.getText("Creating_cross_validation"));
|
120 |
iPoints = m_Layer.getShapesCount(); |
121 |
final IFeatureIterator iter = m_Layer.iterator();
|
122 |
i = 0;
|
123 |
while (iter.hasNext() && setProgress(i, iPoints)) {
|
124 |
final IFeature feature = iter.next();
|
125 |
final Geometry geom = feature.getGeometry();
|
126 |
final Coordinate coord = geom.getCoordinate();
|
127 |
x = coord.x; |
128 |
y = coord.y; |
129 |
values[0] = new Double(x); |
130 |
values[1] = new Double(y); |
131 |
try {
|
132 |
z = Double.parseDouble(feature.getRecord().getValue(m_iField).toString());
|
133 |
} |
134 |
catch (final NumberFormatException e) { |
135 |
z = 0;
|
136 |
} |
137 |
values[2] = new Double(z); |
138 |
dValue = getValueAt(x, y); |
139 |
if (dValue != NO_DATA) {
|
140 |
values[3] = new Double(dValue); |
141 |
values[4] = new Double(dValue - z); |
142 |
table.addRecord(values); |
143 |
} |
144 |
i++; |
145 |
} |
146 |
iter.close(); |
147 |
} |
148 |
catch (final Exception e) { |
149 |
Sextante.addErrorToLog(e); |
150 |
return;
|
151 |
} |
152 |
|
153 |
} |
154 |
|
155 |
|
156 |
protected void setValues() throws GeoAlgorithmExecutionException { |
157 |
|
158 |
m_Layer = m_Parameters.getParameterValueAsVectorLayer(LAYER); |
159 |
m_dDistance = m_Parameters.getParameterValueAsDouble(DIST); |
160 |
if (m_dDistance == 0) { |
161 |
m_dDistance = Double.MAX_VALUE;
|
162 |
} |
163 |
m_iField = m_Parameters.getParameterValueAsInt(FIELD); |
164 |
|
165 |
} |
166 |
|
167 |
|
168 |
protected double getValueAt(final int x, |
169 |
final int y) { |
170 |
|
171 |
final Point2D pt = m_AnalysisExtent.getWorldCoordsFromGridCoords(new GridCell(x, y, 0)); |
172 |
m_NearestPoints = m_SearchEngine.getClosestPoints(pt.getX(), pt.getY(), m_dDistance); |
173 |
|
174 |
final double dValue = interpolate(pt.getX(), pt.getY()); |
175 |
|
176 |
return dValue;
|
177 |
|
178 |
} |
179 |
|
180 |
|
181 |
protected double getValueAt(final double x, |
182 |
final double y) { |
183 |
|
184 |
try {
|
185 |
final PtAndDistance[] nearestPoints = m_SearchEngine.getClosestPoints(x, y, m_dDistance); |
186 |
|
187 |
m_NearestPoints = new PtAndDistance[nearestPoints.length - 1]; |
188 |
int iIndex = 0; |
189 |
for (final PtAndDistance element : nearestPoints) { |
190 |
try {
|
191 |
if (element.getDist() != 0) { |
192 |
m_NearestPoints[iIndex] = element; |
193 |
iIndex++; |
194 |
} |
195 |
} |
196 |
catch (final Exception e) { |
197 |
} |
198 |
} |
199 |
return interpolate(x, y);
|
200 |
} |
201 |
catch (final Exception e) { |
202 |
return NO_DATA;
|
203 |
} |
204 |
|
205 |
} |
206 |
|
207 |
|
208 |
protected abstract double interpolate(double x, |
209 |
double y);
|
210 |
|
211 |
|
212 |
} |