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