root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / gridTools / clipBBoxGrid / clipBBoxGridAlgorithm.java @ 59
History | View | Annotate | Download (5.38 KB)
1 |
package es.unex.sextante.gridTools.clipBBoxGrid; |
---|---|
2 |
|
3 |
import java.awt.geom.Rectangle2D; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Envelope; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
7 |
|
8 |
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer; |
9 |
import es.unex.sextante.core.AnalysisExtent; |
10 |
import es.unex.sextante.core.GeoAlgorithm; |
11 |
import es.unex.sextante.core.Sextante; |
12 |
import es.unex.sextante.dataObjects.IFeature; |
13 |
import es.unex.sextante.dataObjects.IFeatureIterator; |
14 |
import es.unex.sextante.dataObjects.IRasterLayer; |
15 |
import es.unex.sextante.dataObjects.IVectorLayer; |
16 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
17 |
import es.unex.sextante.exceptions.IteratorException; |
18 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
19 |
import es.unex.sextante.exceptions.UnsupportedOutputChannelException; |
20 |
|
21 |
public class clipBBoxGridAlgorithm |
22 |
extends
|
23 |
GeoAlgorithm { |
24 |
|
25 |
public static final String INPUT = "INPUT"; |
26 |
public static final String POLYGONS = "POLYGONS"; |
27 |
public static final String RESULT = "RESULT"; |
28 |
|
29 |
private AnalysisExtent m_Extent;
|
30 |
private int m_iMinX, m_iMinY; |
31 |
private IRasterLayer m_Output;
|
32 |
private IRasterLayer m_Raster;
|
33 |
private IVectorLayer m_Polygons;
|
34 |
private int m_iNX; |
35 |
private int m_iNY; |
36 |
|
37 |
|
38 |
@Override
|
39 |
public void defineCharacteristics() { |
40 |
|
41 |
setName(Sextante.getText("Clip_grid_with_bbox_of_polygons"));
|
42 |
setGroup(Sextante.getText("Basic_tools_for_raster_layers"));
|
43 |
setUserCanDefineAnalysisExtent(false);
|
44 |
try {
|
45 |
m_Parameters.addInputRasterLayer(INPUT, Sextante.getText("Layer_to_clip"), true); |
46 |
m_Parameters.addInputVectorLayer(POLYGONS, Sextante.getText("Polygons"), AdditionalInfoVectorLayer.SHAPE_TYPE_POLYGON,
|
47 |
true);
|
48 |
addOutputRasterLayer(RESULT, Sextante.getText("Clipped_layer"));
|
49 |
} |
50 |
catch (final RepeatedParameterNameException e) { |
51 |
Sextante.addErrorToLog(e); |
52 |
} |
53 |
|
54 |
} |
55 |
|
56 |
|
57 |
@Override
|
58 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
59 |
|
60 |
m_Raster = m_Parameters.getParameterValueAsRasterLayer("INPUT");
|
61 |
m_Polygons = m_Parameters.getParameterValueAsVectorLayer("POLYGONS");
|
62 |
|
63 |
clip(); |
64 |
|
65 |
return !m_Task.isCanceled();
|
66 |
|
67 |
} |
68 |
|
69 |
|
70 |
private void clip() throws UnsupportedOutputChannelException, IteratorException { |
71 |
|
72 |
int i;
|
73 |
m_Extent = getAdjustedGridExtent(); |
74 |
|
75 |
if (m_Extent != null) { |
76 |
m_Raster.setWindowExtent(m_Extent); |
77 |
m_Output = getNewRasterLayer(RESULT, Sextante.getText("Result"), m_Raster.getDataType(), m_Extent,
|
78 |
m_Raster.getBandsCount()); |
79 |
m_Output.setNoDataValue(m_Raster.getNoDataValue()); |
80 |
m_Output.assignNoData(); |
81 |
|
82 |
m_iNX = m_Extent.getNX(); |
83 |
m_iNY = m_Extent.getNY(); |
84 |
|
85 |
i = 0;
|
86 |
final IFeatureIterator iter = m_Polygons.iterator();
|
87 |
final int iShapeCount = m_Polygons.getShapesCount(); |
88 |
while (iter.hasNext() && setProgress(i, iShapeCount)) {
|
89 |
final IFeature feature = iter.next();
|
90 |
final Geometry geom = feature.getGeometry();
|
91 |
doPolygon(geom); |
92 |
if (m_Task.isCanceled()) {
|
93 |
return;
|
94 |
} |
95 |
i++; |
96 |
} |
97 |
iter.close(); |
98 |
|
99 |
} |
100 |
|
101 |
} |
102 |
|
103 |
|
104 |
private void doPolygon(final Geometry geom) { |
105 |
|
106 |
for (int i = 0; i < geom.getNumGeometries(); i++) { |
107 |
final Geometry part = geom.getGeometryN(i);
|
108 |
doPolygonPart(part); |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
|
114 |
private void doPolygonPart(final Geometry geom) { |
115 |
|
116 |
int x, y, xStart, xStop;
|
117 |
|
118 |
final Envelope extent = geom.getEnvelopeInternal();
|
119 |
|
120 |
xStart = (int) ((extent.getMinX() - m_Extent.getXMin()) / m_Extent.getCellSize()) - 1; |
121 |
if (xStart < 0) { |
122 |
xStart = 0;
|
123 |
} |
124 |
|
125 |
xStop = (int) ((extent.getMaxX() - m_Extent.getXMin()) / m_Extent.getCellSize()) + 1; |
126 |
if (xStop >= m_iNX) {
|
127 |
xStop = m_iNX - 1;
|
128 |
} |
129 |
|
130 |
for (y = 0; y < m_iNY; y++) { |
131 |
for (x = xStart; x <= xStop; x++) {
|
132 |
for (int i = 0; i < m_Raster.getBandsCount(); i++) { |
133 |
m_Output.setCellValue(x, y, i, m_Raster.getCellValueAsDouble(x, y, i)); |
134 |
} |
135 |
|
136 |
} |
137 |
} |
138 |
|
139 |
} |
140 |
|
141 |
|
142 |
private AnalysisExtent getAdjustedGridExtent() {
|
143 |
|
144 |
double iMaxX, iMaxY;
|
145 |
double dMinX, dMinY;
|
146 |
double dMinX2, dMinY2, dMaxX2, dMaxY2;
|
147 |
double dCellSize;
|
148 |
final AnalysisExtent ge = new AnalysisExtent(); |
149 |
|
150 |
final Rectangle2D rect = m_Polygons.getFullExtent(); |
151 |
dMinX = m_Raster.getLayerGridExtent().getXMin(); |
152 |
dMinY = m_Raster.getLayerGridExtent().getYMin(); |
153 |
dCellSize = m_Raster.getLayerGridExtent().getCellSize(); |
154 |
|
155 |
m_iMinX = (int) Math.floor((rect.getMinX() - dMinX) / dCellSize); |
156 |
iMaxX = Math.ceil((rect.getMaxX() - dMinX) / dCellSize);
|
157 |
m_iMinY = (int) Math.floor((rect.getMinY() - dMinY) / dCellSize); |
158 |
iMaxY = Math.ceil((rect.getMaxY() - dMinY) / dCellSize);
|
159 |
|
160 |
dMinX2 = dMinX + m_iMinX * dCellSize; |
161 |
dMinY2 = dMinY + m_iMinY * dCellSize; |
162 |
dMaxX2 = dMinX + iMaxX * dCellSize; |
163 |
dMaxY2 = dMinY + iMaxY * dCellSize; |
164 |
|
165 |
ge.setCellSize(dCellSize); |
166 |
ge.setXRange(dMinX2, dMaxX2, true);
|
167 |
ge.setYRange(dMinY2, dMaxY2, true);
|
168 |
|
169 |
return ge;
|
170 |
|
171 |
} |
172 |
|
173 |
} |