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 @ 2329
History | View | Annotate | Download (11.6 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("applying_mask"));
|
86 |
try {
|
87 |
if (store == null) |
88 |
throw new ROIMaskException("Store not found"); |
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 |
query.setReadOnly(false);
|
103 |
query.forceRGBRequest(); |
104 |
query.setDrawableBands(store.getRender().getRenderColorInterpretation().buildRenderBands()); |
105 |
} else {
|
106 |
query.setAllDrawableBands(); |
107 |
query.setSupersamplingOption(false);
|
108 |
query.setReadOnly(true);
|
109 |
} |
110 |
|
111 |
|
112 |
query.setAreaOfInterest(windowExtent, w, h); |
113 |
Buffer sourceBuffer = null; |
114 |
try {
|
115 |
sourceBuffer = store.query(query); |
116 |
sourceBuffer.setDataExtent(windowExtent.toRectangle2D()); |
117 |
} catch (QueryException e) {
|
118 |
throw new ROIMaskException("Error reading data", e); |
119 |
} |
120 |
|
121 |
bufferForTest = sourceBuffer; |
122 |
ColorInterpretation ci = store.getColorInterpretation(); |
123 |
int ouputAlphaBandNumber = -1; |
124 |
|
125 |
if(rois != null && rois.length > 0) { |
126 |
if(ci.isRGBA()) {
|
127 |
bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, ci.getAlphaBand()); |
128 |
ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
|
129 |
nodata.setNoDataTransparent(false);
|
130 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB); |
131 |
} else if(ci.isRGB()) { |
132 |
bufferForTest = processRGB(windowExtent, sourceBuffer, w, h, cellSize, -1);
|
133 |
ouputAlphaBandNumber = bufferForTest.getBandCount() - 1;
|
134 |
nodata.setNoDataTransparent(false);
|
135 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.ARGB); |
136 |
} else {
|
137 |
bufferForTest = processMDT(windowExtent, sourceBuffer, cellSize); |
138 |
nodata.setNoDataTransparent(true);
|
139 |
ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(ColorInterpretation.GRAYSCALE); |
140 |
} |
141 |
} |
142 |
|
143 |
if(export) { |
144 |
super.exportRaster(filename,
|
145 |
bufferForTest, |
146 |
ci, |
147 |
windowExtent, |
148 |
nodata, |
149 |
store.getProjection()); |
150 |
} |
151 |
|
152 |
addOutputValue(FILENAME, filename); |
153 |
addOutputValue(BUFFER, bufferForTest); |
154 |
addOutputValue(ALPHA_BAND, ouputAlphaBandNumber); |
155 |
} catch (ROIMaskException e) {
|
156 |
if (incrementableTask != null) |
157 |
incrementableTask.processFinalize(); |
158 |
messageBoxError("Error in mask process", this, e); |
159 |
} |
160 |
} |
161 |
|
162 |
private Buffer processRGB(Extent extent, Buffer sourceBuffer, int w, int h, double cellsize, int inputAlphaBandNumber) throws ProcessInterruptedException, ProcessException { |
163 |
BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams( |
164 |
w, h, 4, store.getDataType()[0], true); |
165 |
Buffer outputBuffer = null; |
166 |
try {
|
167 |
outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params); |
168 |
} catch (Exception e) { |
169 |
throw new ProcessException("Error creating buffer", e); |
170 |
} |
171 |
|
172 |
for (int row = 0; row < h; row++) { |
173 |
for (int col = 0; col < w; col++) { |
174 |
double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w)); |
175 |
double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h)); |
176 |
|
177 |
for (int iBand = 0; iBand < 3; iBand++) { |
178 |
outputBuffer.setElem(row, col, iBand, sourceBuffer.getElemByte(row, col, iBand)); |
179 |
} |
180 |
|
181 |
boolean insideRoi = false; |
182 |
for (int i = 0; i < rois.length; i++) { |
183 |
if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
|
184 |
if (inverse) {
|
185 |
if(inputAlphaBandNumber != -1) |
186 |
outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
|
187 |
else
|
188 |
outputBuffer.setElem(row, col, 3, (byte) 255); |
189 |
} else {
|
190 |
outputBuffer.setElem(row, col, 3, (byte) (255 - alpha)); |
191 |
} |
192 |
insideRoi = true;
|
193 |
} |
194 |
} |
195 |
|
196 |
if(!insideRoi) {
|
197 |
if (inverse) {
|
198 |
outputBuffer.setElem(row, col, 3, (byte) (255 - alpha)); |
199 |
} else {
|
200 |
if(inputAlphaBandNumber != -1) |
201 |
outputBuffer.setElem(row, col, 3, sourceBuffer.getElemByte(row, col, inputAlphaBandNumber));
|
202 |
else
|
203 |
outputBuffer.setElem(row, col, 3, (byte) 255); |
204 |
} |
205 |
} |
206 |
} |
207 |
updatePercent(row, h); |
208 |
} |
209 |
return outputBuffer;
|
210 |
} |
211 |
|
212 |
private Buffer processMDT(Extent extent, Buffer inputBuffer, double cellsize) throws ProcessInterruptedException, ProcessException { |
213 |
int w = inputBuffer.getWidth();
|
214 |
int h = inputBuffer.getHeight();
|
215 |
Buffer outputBuffer = null; |
216 |
|
217 |
BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams( |
218 |
w, h, inputBuffer.getBandCount(), inputBuffer.getDataType(), true);
|
219 |
try {
|
220 |
outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params); |
221 |
} catch (Exception e) { |
222 |
throw new ProcessException("Error creating buffer", e); |
223 |
} |
224 |
|
225 |
for (int row = 0; row < h; row++) { |
226 |
for (int col = 0; col < w; col++) { |
227 |
for (int nband = 0; nband < inputBuffer.getBandCount(); nband++) { |
228 |
double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w)); |
229 |
double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h)); |
230 |
boolean insideRoi = false; |
231 |
for (int i = 0; i < rois.length; i++) { |
232 |
if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
|
233 |
if (inverse)
|
234 |
writePixel(inputBuffer, outputBuffer, col, row, nband); |
235 |
else
|
236 |
writeNoData(outputBuffer, col, row, nband); |
237 |
insideRoi = true;
|
238 |
} |
239 |
} |
240 |
|
241 |
if(!insideRoi) {
|
242 |
if (inverse)
|
243 |
writeNoData(outputBuffer, col, row, nband); |
244 |
else {
|
245 |
writePixel(inputBuffer, outputBuffer, col, row, nband); |
246 |
} |
247 |
} |
248 |
} |
249 |
} |
250 |
updatePercent(row, h); |
251 |
} |
252 |
return outputBuffer;
|
253 |
} |
254 |
|
255 |
private void writePixel(Buffer inputBuffer, Buffer outputBuffer, int col, int row, int nband) { |
256 |
switch (inputBuffer.getDataType()) {
|
257 |
case Buffer.TYPE_BYTE: |
258 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemByte(row, col, nband)); |
259 |
break;
|
260 |
case Buffer.TYPE_SHORT: |
261 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemShort(row, col, nband)); |
262 |
break;
|
263 |
case Buffer.TYPE_INT: |
264 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemInt(row, col, nband)); |
265 |
break;
|
266 |
case Buffer.TYPE_FLOAT: |
267 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemFloat(row, col, nband)); |
268 |
break;
|
269 |
case Buffer.TYPE_DOUBLE: |
270 |
outputBuffer.setElem(row, col, nband, inputBuffer.getElemDouble(row, col, nband)); |
271 |
break;
|
272 |
} |
273 |
} |
274 |
|
275 |
private void writeNoData(Buffer outputBuffer, int col, int row, int nband) { |
276 |
switch (outputBuffer.getDataType()) {
|
277 |
case Buffer.TYPE_BYTE: |
278 |
outputBuffer.setElem(row, col, nband, nodata.getValue().byteValue()); |
279 |
break;
|
280 |
case Buffer.TYPE_SHORT: |
281 |
outputBuffer.setElem(row, col, nband, nodata.getValue().shortValue()); |
282 |
break;
|
283 |
case Buffer.TYPE_INT: |
284 |
outputBuffer.setElem(row, col, nband, nodata.getValue().intValue()); |
285 |
break;
|
286 |
case Buffer.TYPE_FLOAT: |
287 |
outputBuffer.setElem(row, col, nband, nodata.getValue().floatValue()); |
288 |
break;
|
289 |
case Buffer.TYPE_DOUBLE: |
290 |
outputBuffer.setElem(row, col, nband, nodata.getValue().doubleValue()); |
291 |
break;
|
292 |
} |
293 |
} |
294 |
|
295 |
public String getTitle() { |
296 |
return Messages.getText("mask_process"); |
297 |
} |
298 |
} |