Statistics
| Revision:

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 @ 1861

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 = testExtent != null ? testExtent : store.getExtent();
101
                        int w = testExtent != null ? testWidth : (int)store.getWidth();
102
                        int h = testExtent != null ? testHeight : (int)store.getHeight();
103

    
104
                        RasterQuery query = RasterLocator.getManager().createQuery();
105
                        query.setAllDrawableBands();
106
                        query.setAreaOfInterest(windowExtent, w, h);
107
                        Buffer sourceBuffer = null;
108
                        try {
109
                                sourceBuffer = store.query(query);
110
                                sourceBuffer.setDataExtent(windowExtent.toRectangle2D());
111
                        } catch (RasterDriverException e) {
112
                                throw new ROIMaskException("");
113
                        } catch (InvalidSetViewException e) {
114
                                throw new ROIMaskException("");
115
                        }
116

    
117
                        bufferForTest = sourceBuffer;
118
                        
119
                        double cellsize = testExtent != null ? testExtent.width() / testWidth : store.getCellSize();
120
                        
121
                        if(rois != null && rois.length > 0) {
122
                                if(store.getDataType()[0] == Buffer.TYPE_BYTE) {
123
                                        processRGB(windowExtent, w, h, cellsize);
124
                                } else {
125
                                        bufferForTest = processMDT(windowExtent, sourceBuffer, cellsize);
126
                                }
127
                        } 
128
                        
129
                        if(export) {
130
                                super.exportRaster(filename, 
131
                                                bufferForTest, 
132
                                                alphaBand,
133
                                                store.getCellSize(), 
134
                                                windowExtent.getULX(), 
135
                                                windowExtent.getULY());
136
                        }
137

    
138
                        long t2 = new java.util.Date().getTime();
139
                        millis = t2 - t1;
140

    
141
                        SwingUtilities.invokeLater(new Runnable() {
142
                                public void run() {
143
                                        if (externalActions != null) {
144
                                                externalActions.end(getResult());
145
                                        }
146
                                }
147
                        });
148
                } catch (ROIMaskException e) {
149
                        if (incrementableTask != null)
150
                                incrementableTask.processFinalize();
151
                        messageBoxError("...", this, e);
152
                }
153
        }
154
        
155
        private void processRGB(Extent extent, int w, int h, double cellsize) throws ProcessInterruptedException, ProcessException {
156
                BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams(
157
                                w, h, 4, store.getDataType()[0], true);
158
                try {
159
                        alphaBand = RasterLocator.getManager().getBufferFactory().createBuffer(params);
160
                } catch (Exception e) {
161
                        throw new ProcessException("Error creating buffer", e);
162
                } 
163

    
164
                for (int row = 0; row < h; row++) {
165
                        for (int col = 0; col < w; col++) {
166
                                double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w));
167
                                double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h));
168
                                boolean insideRoi = false;
169
                                for (int i = 0; i < rois.length; i++) {
170
                                        if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
171
                                                if (inverse)
172
                                                        alphaBand.setElem(row, col, 0, (byte) 255);
173
                                                else
174
                                                        alphaBand.setElem(row, col, 0, (byte) (255 - alpha));
175
                                                insideRoi = true;
176
                                        }
177
                                }
178

    
179
                                if(!insideRoi) {
180
                                        if (inverse)
181
                                                alphaBand.setElem(row, col, 0, (byte) (255 - alpha));
182
                                        else
183
                                                alphaBand.setElem(row, col, 0, (byte) 255);
184
                                }
185
                        }
186
                        updatePercent(row, h);
187
                }
188
        }
189
        
190
        private Buffer processMDT(Extent extent, Buffer inputBuffer, double cellsize) throws ProcessInterruptedException, ProcessException {
191
                int w = inputBuffer.getWidth();
192
                int h = inputBuffer.getHeight();
193
                Buffer outputBuffer = null;
194
                
195
                BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams(
196
                                w, h, inputBuffer.getBandCount(), inputBuffer.getDataType(), true);
197
                try {
198
                        outputBuffer = RasterLocator.getManager().getBufferFactory().createBuffer(params);
199
                } catch (Exception e) {
200
                        throw new ProcessException("Error creating buffer", e);
201
                } 
202
                
203
                for (int row = 0; row < h; row++) {
204
                        for (int col = 0; col < w; col++) {
205
                                for (int nband = 0; nband < inputBuffer.getBandCount(); nband++) {
206
                                        double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w));
207
                                        double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h));
208
                                        boolean insideRoi = false;
209
                                        for (int i = 0; i < rois.length; i++) {
210
                                                if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
211
                                                        if (inverse)
212
                                                                 writePixel(inputBuffer, outputBuffer, col, row, nband);
213
                                                        else
214
                                                                writeNoData(outputBuffer, col, row, nband);
215
                                                        insideRoi = true;
216
                                                }
217
                                        }
218

    
219
                                        if(!insideRoi) {
220
                                                if (inverse)
221
                                                        writeNoData(outputBuffer, col, row, nband);
222
                                                else {
223
                                                        writePixel(inputBuffer, outputBuffer, col, row, nband);
224
                                                }
225
                                        }
226
                                }
227
                        }
228
                        updatePercent(row, h);
229
                }
230
                return outputBuffer;
231
        }
232
        
233
        private void writePixel(Buffer inputBuffer, Buffer outputBuffer, int col, int row, int nband) {
234
                switch (inputBuffer.getDataType()) {
235
                case Buffer.TYPE_BYTE:
236
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemByte(row, col, nband));
237
                        break;
238
                case Buffer.TYPE_SHORT:
239
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemShort(row, col, nband));
240
                        break;
241
                case Buffer.TYPE_INT:
242
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemInt(row, col, nband));
243
                        break;
244
                case Buffer.TYPE_FLOAT:
245
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemFloat(row, col, nband));
246
                        break;
247
                case Buffer.TYPE_DOUBLE:
248
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemDouble(row, col, nband));
249
                        break;
250
                }
251
        }
252
        
253
        private void writeNoData(Buffer outputBuffer, int col, int row, int nband) {
254
                switch (outputBuffer.getDataType()) {
255
                case Buffer.TYPE_BYTE:
256
                        outputBuffer.setElem(row, col, nband, nodata.getValue().byteValue());
257
                        break;
258
                case Buffer.TYPE_SHORT:
259
                        outputBuffer.setElem(row, col, nband, nodata.getValue().shortValue());
260
                        break;
261
                case Buffer.TYPE_INT:
262
                        outputBuffer.setElem(row, col, nband, nodata.getValue().intValue());
263
                        break;
264
                case Buffer.TYPE_FLOAT:
265
                        outputBuffer.setElem(row, col, nband, nodata.getValue().floatValue());
266
                        break;
267
                case Buffer.TYPE_DOUBLE:
268
                        outputBuffer.setElem(row, col, nband, nodata.getValue().doubleValue());
269
                        break;
270
                }
271
        }
272
        
273
        
274
        public Object getResult() {
275
                HashMap<String, Object> map = new HashMap<String, Object>();
276
                map.put(FILENAME, filename);
277
                map.put(TIME, new Long(millis));
278
                map.put(BUFFER, bufferForTest);
279
                map.put(ALPHA_BAND, alphaBand);
280
                return map;
281
        }
282

    
283
        public int getPercent() {
284
                return percent;
285
        }
286

    
287
        public String getTitle() {
288
                return Messages.getText("...");
289
        }
290
}