root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / cropToValidData / CropToValidDataAlgorithm.java @ 59
History | View | Annotate | Download (3.69 KB)
1 |
package es.unex.sextante.gridTools.cropToValidData; |
---|---|
2 |
|
3 |
import es.unex.sextante.core.AnalysisExtent; |
4 |
import es.unex.sextante.core.GeoAlgorithm; |
5 |
import es.unex.sextante.core.Sextante; |
6 |
import es.unex.sextante.dataObjects.IRasterLayer; |
7 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
8 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
9 |
|
10 |
public class CropToValidDataAlgorithm |
11 |
extends
|
12 |
GeoAlgorithm { |
13 |
|
14 |
public static final String RESULT = "RESULT"; |
15 |
public static final String INPUT = "INPUT"; |
16 |
|
17 |
private IRasterLayer m_Window;
|
18 |
|
19 |
|
20 |
@Override
|
21 |
public void defineCharacteristics() { |
22 |
|
23 |
setName(Sextante.getText("Crop_to_valid_data_cells"));
|
24 |
setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
25 |
setUserCanDefineAnalysisExtent(false);
|
26 |
|
27 |
try {
|
28 |
m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer_to_crop"), true); |
29 |
addOutputRasterLayer(RESULT, Sextante.getText("Cropped_layer"));
|
30 |
} |
31 |
catch (final RepeatedParameterNameException e) { |
32 |
Sextante.addErrorToLog(e); |
33 |
} |
34 |
|
35 |
} |
36 |
|
37 |
|
38 |
@Override
|
39 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
40 |
|
41 |
int x, y;
|
42 |
int iNX, iNY;
|
43 |
|
44 |
m_Window = m_Parameters.getParameterValueAsRasterLayer(INPUT); |
45 |
|
46 |
m_Window.setFullExtent(); |
47 |
|
48 |
final AnalysisExtent extent = getAdjustedGridExtent(m_Window);
|
49 |
|
50 |
|
51 |
m_Window.setWindowExtent(extent); |
52 |
final IRasterLayer output = this.getNewRasterLayer(RESULT, m_Window.getName() + Sextante.getText("[cropped]"), |
53 |
m_Window.getDataType(), extent); |
54 |
output.setNoDataValue(m_Window.getNoDataValue()); |
55 |
|
56 |
iNX = extent.getNX(); |
57 |
iNY = extent.getNY(); |
58 |
|
59 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
60 |
for (x = 0; x < iNX; x++) { |
61 |
output.setCellValue(x, y, m_Window.getCellValueAsDouble(x, y)); |
62 |
} |
63 |
} |
64 |
return !m_Task.isCanceled();
|
65 |
|
66 |
} |
67 |
|
68 |
|
69 |
private AnalysisExtent getAdjustedGridExtent(final IRasterLayer input) { |
70 |
|
71 |
int x, y;
|
72 |
int iNX, iNY;
|
73 |
double iMaxX, iMaxY;
|
74 |
double iMinX, iMinY;
|
75 |
double dMinX, dMaxY;
|
76 |
double dMinX2, dMinY2, dMaxX2, dMaxY2;
|
77 |
double dCellSize;
|
78 |
double dValue;
|
79 |
final AnalysisExtent ge = new AnalysisExtent(); |
80 |
|
81 |
dMinX = input.getWindowGridExtent().getXMin(); |
82 |
dMaxY = input.getWindowGridExtent().getYMax(); |
83 |
dCellSize = input.getWindowGridExtent().getCellSize(); |
84 |
|
85 |
iNX = input.getWindowGridExtent().getNX(); |
86 |
iNY = input.getWindowGridExtent().getNY(); |
87 |
|
88 |
iMinX = iNX; |
89 |
iMinY = iNY; |
90 |
iMaxX = 0;
|
91 |
iMaxY = 0;
|
92 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
93 |
for (x = 0; x < iNX; x++) { |
94 |
dValue = m_Window.getCellValueAsDouble(x, y); |
95 |
if (!m_Window.isNoDataValue(dValue)) {
|
96 |
if (x < iMinX) {
|
97 |
iMinX = x; |
98 |
} |
99 |
if (x > iMaxX) {
|
100 |
iMaxX = x; |
101 |
} |
102 |
if (y < iMinY) {
|
103 |
iMinY = y; |
104 |
} |
105 |
if (y > iMaxY) {
|
106 |
iMaxY = y; |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
// if ((iMinX == 0) && (iMinY == 0) && (iMaxX == iNX - 1) && (iMaxY == iNY - 1)) {
|
113 |
// return null;
|
114 |
// }
|
115 |
|
116 |
dMinX2 = dMinX + iMinX * dCellSize; |
117 |
dMinY2 = dMaxY - iMaxY * dCellSize; |
118 |
dMaxX2 = dMinX + iMaxX * dCellSize; |
119 |
dMaxY2 = dMaxY - iMinY * dCellSize; |
120 |
|
121 |
ge.setCellSize(dCellSize); |
122 |
ge.setXRange(dMinX2, dMaxX2, true);
|
123 |
ge.setYRange(dMinY2, dMaxY2, true);
|
124 |
|
125 |
return ge;
|
126 |
|
127 |
} |
128 |
|
129 |
} |