Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / templates / examples / org.gvsig.raster.roimask_v2_1 / org.gvsig.raster.roimask.algorithm / src / main / java / org / gvsig / raster / roimask / algorithm / ROIMaskProcess.java @ 2139

History | View | Annotate | Download (10 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.InvalidSetViewException;
9
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
10
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
11
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
12
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
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
        private Buffer            alphaBand         = null;
54
        
55
        public static void registerParameters() {
56
                registerInputParameter(RASTER_STORE1, RasterDataStore.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
57
                registerInputParameter(PATH, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
58
                registerInputParameter(TEST_EXTENT, Extent.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
59
                registerInputParameter(TEST_WIDTH, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
60
                registerInputParameter(TEST_HEIGHT, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
61
                registerInputParameter(ROIS, ROI[].class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
62
                registerInputParameter(INVERSE, Boolean.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
63
                registerInputParameter(ALPHA, Integer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
64
                registerInputParameter(NODATA, NoData.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
65
                registerInputParameter(EXPORT, Boolean.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
66
                
67
                registerOutputParameter(FILENAME, String.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
68
                registerOutputParameter(ALPHA_BAND, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
69
                registerOutputParameter(BUFFER, Buffer.class, ROIMaskAlgorithmLibrary.PROCESS_LABEL);
70
        }
71
        
72
        public void init() {
73
                store = getParam(RASTER_STORE1) != null ? (RasterDataStore)getParam(RASTER_STORE1) : null;
74
                filename = getStringParam(PATH);
75
                inverse = getBooleanParam(INVERSE);
76
                export = getBooleanParam(EXPORT);
77
                alpha = getIntParam(ALPHA);
78
                testExtent = getParam(TEST_EXTENT) != null ? (Extent)getParam(TEST_EXTENT) : null;
79
                testWidth = getIntParam(TEST_WIDTH);
80
                testHeight = getIntParam(TEST_HEIGHT);
81
                nodata = getParam(NODATA) != null ? (NoData)getParam(NODATA) : null;
82
                rois = getParam(ROIS) != null ? (ROI[])getParam(ROIS) : null;
83
        }
84

    
85
        public void process() throws ProcessInterruptedException, ProcessException {
86
                insertLineLog(Messages.getText("..."));
87
                try {
88
                        if (store == null)
89
                                throw new ROIMaskException("...");
90
                        
91
                        Extent windowExtent = store.getExtent();
92
                        int w = (int)store.getWidth();
93
                        int h = (int)store.getHeight();
94
                        double cellSize = store.getCellSize();
95
                        
96
                        if(testExtent != null) {
97
                                windowExtent = testExtent;
98
                                w = testWidth;
99
                                h = testHeight;
100
                                cellSize = testExtent.width() / w;
101
                        }
102

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

    
116
                        bufferForTest = sourceBuffer;
117
                        
118
                        if(rois != null && rois.length > 0) {
119
                                if(store.getDataType()[0] == Buffer.TYPE_BYTE) {
120
                                        processRGB(windowExtent, w, h, cellSize);
121
                                } else {
122
                                        bufferForTest = processMDT(windowExtent, sourceBuffer, cellSize);
123
                                }
124
                        } 
125
                        
126
                        if(export) {
127
                                super.exportRaster(filename, 
128
                                                bufferForTest, 
129
                                                alphaBand,
130
                                                cellSize, 
131
                                                windowExtent.getULX(), 
132
                                                windowExtent.getULY());
133
                        }
134

    
135
                        addOutputValue(FILENAME, filename);
136
                        addOutputValue(BUFFER, bufferForTest);
137
                        addOutputValue(ALPHA_BAND, alphaBand);
138
                } catch (ROIMaskException e) {
139
                        if (incrementableTask != null)
140
                                incrementableTask.processFinalize();
141
                        messageBoxError("...", this, e);
142
                }
143
        }
144
        
145
        private void processRGB(Extent extent, int w, int h, double cellsize) throws ProcessInterruptedException, ProcessException {
146
                BufferParam params = RasterLocator.getManager().getBufferFactory().createBufferParams(
147
                                w, h, 4, store.getDataType()[0], true);
148
                try {
149
                        alphaBand = RasterLocator.getManager().getBufferFactory().createBuffer(params);
150
                } catch (Exception e) {
151
                        throw new ProcessException("Error creating buffer", e);
152
                } 
153

    
154
                for (int row = 0; row < h; row++) {
155
                        for (int col = 0; col < w; col++) {
156
                                double wcX = extent.minX() + ((((double) col) * extent.width()) / ((double) w));
157
                                double wcY = extent.minY() + ((((double) (h - (row))) * extent.height()) / ((double) h));
158
                                boolean insideRoi = false;
159
                                for (int i = 0; i < rois.length; i++) {
160
                                        if (((ROI) rois[i]).isInside(wcX, wcY, cellsize, cellsize)) {
161
                                                if (inverse)
162
                                                        alphaBand.setElem(row, col, 0, (byte) 255);
163
                                                else
164
                                                        alphaBand.setElem(row, col, 0, (byte) (255 - alpha));
165
                                                insideRoi = true;
166
                                        }
167
                                }
168

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

    
209
                                        if(!insideRoi) {
210
                                                if (inverse)
211
                                                        writeNoData(outputBuffer, col, row, nband);
212
                                                else {
213
                                                        writePixel(inputBuffer, outputBuffer, col, row, nband);
214
                                                }
215
                                        }
216
                                }
217
                        }
218
                        updatePercent(row, h);
219
                }
220
                return outputBuffer;
221
        }
222
        
223
        private void writePixel(Buffer inputBuffer, Buffer outputBuffer, int col, int row, int nband) {
224
                switch (inputBuffer.getDataType()) {
225
                case Buffer.TYPE_BYTE:
226
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemByte(row, col, nband));
227
                        break;
228
                case Buffer.TYPE_SHORT:
229
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemShort(row, col, nband));
230
                        break;
231
                case Buffer.TYPE_INT:
232
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemInt(row, col, nband));
233
                        break;
234
                case Buffer.TYPE_FLOAT:
235
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemFloat(row, col, nband));
236
                        break;
237
                case Buffer.TYPE_DOUBLE:
238
                        outputBuffer.setElem(row, col, nband, inputBuffer.getElemDouble(row, col, nband));
239
                        break;
240
                }
241
        }
242
        
243
        private void writeNoData(Buffer outputBuffer, int col, int row, int nband) {
244
                switch (outputBuffer.getDataType()) {
245
                case Buffer.TYPE_BYTE:
246
                        outputBuffer.setElem(row, col, nband, nodata.getValue().byteValue());
247
                        break;
248
                case Buffer.TYPE_SHORT:
249
                        outputBuffer.setElem(row, col, nband, nodata.getValue().shortValue());
250
                        break;
251
                case Buffer.TYPE_INT:
252
                        outputBuffer.setElem(row, col, nband, nodata.getValue().intValue());
253
                        break;
254
                case Buffer.TYPE_FLOAT:
255
                        outputBuffer.setElem(row, col, nband, nodata.getValue().floatValue());
256
                        break;
257
                case Buffer.TYPE_DOUBLE:
258
                        outputBuffer.setElem(row, col, nband, nodata.getValue().doubleValue());
259
                        break;
260
                }
261
        }
262
        
263
        public String getTitle() {
264
                return Messages.getText("...");
265
        }
266
}