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