gvsig-raster / org.gvsig.raster.roimask / trunk / org.gvsig.raster.roimask / org.gvsig.raster.roimask.algorithm / src / main / java / org / gvsig / raster / roimask / algorithm / ROIMaskProcess.java @ 2328
History | View | Annotate | Download (11.4 KB)
1 |
package org.gvsig.raster.roimask.algorithm; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
4 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
5 |
import org.gvsig.fmap.dal.coverage.dataset.BufferParam; |
6 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
7 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
8 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
9 |
import org.gvsig.fmap.dal.coverage.exception.QueryException; |
10 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
11 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
12 |
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
13 |
import org.gvsig.i18n.Messages; |
14 |
import org.gvsig.raster.algorithm.process.DataProcess; |
15 |
import org.gvsig.raster.algorithm.process.ProcessException; |
16 |
import org.gvsig.raster.roi.ROI; |
17 |
|
18 |
/**
|
19 |
* Process
|
20 |
*/
|
21 |
public class ROIMaskProcess extends DataProcess { |
22 |
public static String RASTER_STORE1 = "RasterStore1"; |
23 |
public static String BUFFER = "Buffer"; |
24 |
public static String PATH = "Path"; |
25 |
public static String FILENAME = "FileName"; |
26 |
public static String ROIS = "Rois"; |
27 |
public static String INVERSE = "Inverse"; |
28 |
public static String ALPHA = "Alpha"; |
29 |
public static String ALPHA_BAND = "AlphaBand"; |
30 |
public static String NODATA = "NoData"; |
31 |
public static String EXPORT = "Export"; |
32 |
|
33 |
public static String TEST_EXTENT = "TestExtent"; |
34 |
public static String TEST_WIDTH = "TestWidth"; |
35 |
public static String TEST_HEIGHT = "TestHeight"; |
36 |
|
37 |
private RasterDataStore store = null; |
38 |
private String filename = null; |
39 |
|
40 |
private Extent testExtent = null; |
41 |
private int testWidth = 0; |
42 |
private int testHeight = 0; |
43 |
private ROI[] rois = null; |
44 |
private boolean inverse = false; |
45 |
private boolean export = true; |
46 |
private int alpha = 0; |
47 |
private NoData nodata = null; |
48 |
|
49 |
/**
|
50 |
* This buffer is just to test
|
51 |
*/
|
52 |
private Buffer bufferForTest = null; |
53 |
|
54 |
public static void registerParameters() { |
55 |
registerInputParameter(RASTER_STORE1, RasterDataStore.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL); |
56 |
registerInputParameter(PATH, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
57 |
registerInputParameter(TEST_EXTENT, Extent.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL); |
58 |
registerInputParameter(TEST_WIDTH, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
59 |
registerInputParameter(TEST_HEIGHT, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
60 |
registerInputParameter(ROIS, ROI[].class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
61 |
registerInputParameter(INVERSE, Boolean.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
62 |
registerInputParameter(ALPHA, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
63 |
registerInputParameter(NODATA, NoData.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL); |
64 |
registerInputParameter(EXPORT, Boolean.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
65 |
|
66 |
registerOutputParameter(FILENAME, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
67 |
registerOutputParameter(ALPHA_BAND, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
68 |
registerOutputParameter(BUFFER, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
|
69 |
} |
70 |
|
71 |
public void init() { |
72 |
store = getParam(RASTER_STORE1) != null ? (RasterDataStore)getParam(RASTER_STORE1) : null; |
73 |
filename = getStringParam(PATH); |
74 |
inverse = getBooleanParam(INVERSE); |
75 |
export = getBooleanParam(EXPORT);
|
76 |
alpha = getIntParam(ALPHA); |
77 |
testExtent = getParam(TEST_EXTENT) != null ? (Extent)getParam(TEST_EXTENT) : null; |
78 |
testWidth = getIntParam(TEST_WIDTH); |
79 |
testHeight = getIntParam(TEST_HEIGHT); |
80 |
nodata = getParam(NODATA) != null ? (NoData)getParam(NODATA) : null; |
81 |
rois = getParam(ROIS) != null ? (ROI[])getParam(ROIS) : null; |
82 |
} |
83 |
|
84 |
public void process() throws ProcessInterruptedException, ProcessException { |
85 |
insertLineLog(Messages.getText("..."));
|
86 |
try {
|
87 |
if (store == null) |
88 |
throw new ROIMaskException("..."); |
89 |
|
90 |
Extent windowExtent = store.getExtent(); |
91 |
int w = (int)store.getWidth(); |
92 |
int h = (int)store.getHeight(); |
93 |
double cellSize = store.getCellSize();
|
94 |
RasterQuery query = RasterLocator.getManager().createQuery(); |
95 |
|
96 |
if(testExtent != null) { |
97 |
windowExtent = testExtent; |
98 |
w = testWidth; |
99 |
h = testHeight; |
100 |
cellSize = testExtent.width() / w; |
101 |
query.setSupersamplingOption(true);
|
102 |
} else {
|
103 |
query.setSupersamplingOption(false);
|
104 |
} |
105 |
|
106 |
query.setAllDrawableBands(); |
107 |
query.setAreaOfInterest(windowExtent, w, h); |
108 |
Buffer sourceBuffer = null; |
109 |
try {
|
110 |
sourceBuffer = store.query(query); |
111 |
sourceBuffer.setDataExtent(windowExtent.toRectangle2D()); |
112 |
} catch (QueryException e) {
|
113 |
throw new ROIMaskException("Error reading data", e); |
114 |
} |
115 |
|
116 |
bufferForTest = sourceBuffer; |
117 |
ColorInterpretation ci = store.getColorInterpretation(); |
118 |
int ouputAlphaBandNumber = -1; |
119 |
|
120 |
if(rois != null && rois.length > 0) { |
121 |
if(ci.isRGBA()) {
|
122 |
bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, ci.getAlphaBand()); |
123 |
ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
|
124 |
nodata.setNoDataTransparent(false);
|
125 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB); |
126 |
} else if(ci.isRGB()) { |
127 |
bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, -1);
|
128 |
ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
|
129 |
nodata.setNoDataTransparent(false);
|
130 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB); |
131 |
} else {
|
132 |
bufferForTest = processMDT(windowExtent, sourceBuffer, cellSize); |
133 |
nodata.setNoDataTransparent(true);
|
134 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.GRAYSCALE); |
135 |
} |
136 |
} |
137 |
|
138 |
if(export) { |
139 |
super.exportRaster(filename,
|
140 |
bufferForTest, |
141 |
ci, |
142 |
windowExtent, |
143 |
nodata, |
144 |
store.getProjection()); |
145 |
} |
146 |
|
147 |
addOutputValue(FILENAME, filename); |
148 |
addOutputValue(BUFFER, bufferForTest); |
149 |
addOutputValue(ALPHA_BAND, ouputAlphaBandNumber); |
150 |
} catch (ROIMaskException e) {
|
151 |
if (incrementableTask != null) |
152 |
incrementableTask.processFinalize(); |
153 |
messageBoxError("...", this, e); |
154 |
} |
155 |
} |
156 |
|
157 |
private Buffer processRGB(Extent extent, Buffer sourceBuffer, int w, int h, double cellsize, int inputAlphaBandNumber) throws ProcessInterruptedException, ProcessException { |
158 |
BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams( |
159 |
w, h, 4, store.getDataType()[0], true); |
160 |
Buffer outputBuffer = null; |
161 |
try {
|
162 |
outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params); |
163 |
} catch (Exception e) { |
164 |
throw new ProcessException("Error creating buffer", e); |
165 |
} |
166 |
|
167 |
for (int row = 0; row < h; row++) { |
168 |
for (int col = 0; col < w; col++) { |
169 |
double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w)); |
170 |
double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h)); |
171 |
|
172 |
for (int iBand = 0; iBand < 3; iBand++) { |
173 |
outputBuffer.setElem(row, col, iBand, sourceBuffer.getElemByte(row, col, iBand)); |
174 |
} |
175 |
|
176 |
boolean insideRoi = false; |
177 |
for (int i = 0; i < rois.length; i++) { |
178 |
if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
|
179 |
if (inverse) {
|
180 |
if(inputAlphaBandNumber != -1) |
181 |
outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
|
182 |
else
|
183 |
outputBuffer.setElem(row, col, 3, (byte) 255); |
184 |
} else {
|
185 |
outputBuffer.setElem(row, col, 3, (byte) (255 - alpha)); |
186 |
} |
187 |
insideRoi = true;
|
188 |
} |
189 |
} |
190 |
|
191 |
if(!insideRoi) {
|
192 |
if (inverse) {
|
193 |
outputBuffer.setElem(row, col, 3, (byte) (255 - alpha)); |
194 |
} else {
|
195 |
if(inputAlphaBandNumber != -1) |
196 |
outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
|
197 |
else
|
198 |
outputBuffer.setElem(row, col, 3, (byte) 255); |
199 |
} |
200 |
} |
201 |
} |
202 |
updatePercent(row, h); |
203 |
} |
204 |
return outputBuffer;
|
205 |
} |
206 |
|
207 |
private Buffer processMDT(Extent extent, Buffer inputBuffer, double cellsize) throws ProcessInterruptedException, ProcessException { |
208 |
int w = inputBuffer.getWidth();
|
209 |
int h = inputBuffer.getHeight();
|
210 |
Buffer outputBuffer = null; |
211 |
|
212 |
BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams( |
213 |
w, h, inputBuffer.getBandCount(), inputBuffer.getDataType(), true);
|
214 |
try {
|
215 |
outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params); |
216 |
} catch (Exception e) { |
217 |
throw new ProcessException("Error creating buffer", e); |
218 |
} |
219 |
|
220 |
for (int row = 0; row < h; row++) { |
221 |
for (int col = 0; col < w; col++) { |
222 |
for (int nband = 0; nband < inputBuffer.getBandCount(); nband++) { |
223 |
double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w)); |
224 |
double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h)); |
225 |
boolean insideRoi = false; |
226 |
for (int i = 0; i < rois.length; i++) { |
227 |
if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
|
228 |
if (inverse)
|
229 |
writePixel(inputBuffer, outputBuffer, col, row, nband); |
230 |
else
|
231 |
writeNoData(outputBuffer, col, row, nband); |
232 |
insideRoi = true;
|
233 |
} |
234 |
} |
235 |
|
236 |
if(!insideRoi) {
|
237 |
if (inverse)
|
238 |
writeNoData(outputBuffer, col, row, nband); |
239 |
else {
|
240 |
writePixel(inputBuffer, outputBuffer, col, row, nband); |
241 |
} |
242 |
} |
243 |
} |
244 |
} |
245 |
updatePercent(row, h); |
246 |
} |
247 |
return outputBuffer;
|
248 |
} |
249 |
|
250 |
private void writePixel(Buffer inputBuffer, Buffer outputBuffer, int col, int row, int nband) { |
251 |
switch (inputBuffer.getDataType()) {
|
252 |
case Buffer.TYPE_BYTE: |
253 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemByte(row, col, nband)); |
254 |
break;
|
255 |
case Buffer.TYPE_SHORT: |
256 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemShort(row, col, nband)); |
257 |
break;
|
258 |
case Buffer.TYPE_INT: |
259 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemInt(row, col, nband)); |
260 |
break;
|
261 |
case Buffer.TYPE_FLOAT: |
262 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemFloat(row, col, nband)); |
263 |
break;
|
264 |
case Buffer.TYPE_DOUBLE: |
265 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemDouble(row, col, nband)); |
266 |
break;
|
267 |
} |
268 |
} |
269 |
|
270 |
private void writeNoData(Buffer outputBuffer, int col, int row, int nband) { |
271 |
switch (outputBuffer.getDataType()) {
|
272 |
case Buffer.TYPE_BYTE: |
273 |
outputBuffer.setElem(row, col, nband, nodata.getValue().byteValue()); |
274 |
break;
|
275 |
case Buffer.TYPE_SHORT: |
276 |
outputBuffer.setElem(row, col, nband, nodata.getValue().shortValue()); |
277 |
break;
|
278 |
case Buffer.TYPE_INT: |
279 |
outputBuffer.setElem(row, col, nband, nodata.getValue().intValue()); |
280 |
break;
|
281 |
case Buffer.TYPE_FLOAT: |
282 |
outputBuffer.setElem(row, col, nband, nodata.getValue().floatValue()); |
283 |
break;
|
284 |
case Buffer.TYPE_DOUBLE: |
285 |
outputBuffer.setElem(row, col, nband, nodata.getValue().doubleValue()); |
286 |
break;
|
287 |
} |
288 |
} |
289 |
|
290 |
public String getTitle() { |
291 |
return Messages.getText("..."); |
292 |
} |
293 |
} |