Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / Supersampling.java @ 2328

History | View | Annotate | Download (7.15 KB)

1
package org.gvsig.raster.impl.store;
2

    
3
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
4
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
5
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
6
import org.gvsig.fmap.dal.coverage.exception.QueryException;
7
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
8
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
9
import org.gvsig.raster.impl.DefaultRasterManager;
10
import org.gvsig.raster.impl.buffer.DefaultRasterQuery;
11
import org.gvsig.raster.impl.buffer.SpiRasterQuery;
12
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
13
import org.gvsig.raster.impl.provider.RasterProvider;
14

    
15
/**
16
 * The requests to a <code>RasterDataStore</code> adjust the bounding box and the size in pixels 
17
 * to the source limits. The user can need a buffer that exceeds the raster dimension. 
18
 * In this case, the area that is out of the raster limits should be set to nodata values or 
19
 * transparent in RGB cases. 
20
 * 
21
 * @author Nacho Brodin (nachobrodin@gmail.com)
22
 * @deprecated Is being used the method getAdjustedWindow of <code>Buffer</code> 
23
 */
24
public class Supersampling {
25
        private DefaultRasterQuery           query                   = null;
26
        private DefaultRasterQuery           originalQuery           = null;
27
        private RasterDataStore              store                   = null; 
28
        private FramedBufferResampling       framedBufferResampling  = null;
29
        protected byte                       byteNoData              = (byte)0x00;
30
        protected short                      shortNoData             = Short.MIN_VALUE;
31
        protected int                        intNoData               = Integer.MIN_VALUE;
32
        protected float                      floatNoData             = -99999;
33
        protected double                     doubleNoData            = -99999;
34
        
35
        public Supersampling(DefaultRasterQuery q, RasterDataStore store) {
36
                this.query = q;
37
                this.store = store;
38
                setNoDataValue(store.getNoDataValue());
39
                this.originalQuery = (DefaultRasterQuery)query.clone();
40
        }
41
        
42
        public Buffer query(RasterProvider provider) throws QueryException, ProcessInterruptedException {
43
                query.calculateParameters(store);
44
                Buffer bufResult = null;
45
                Buffer buf = null;
46
                
47
                try {
48
                        if(query.requestHasShift()) {
49
                                framedBufferResampling = new FramedBufferResampling(originalQuery, store);
50
                                buf = framedBufferResampling.query(provider);
51
                        } else 
52
                                buf = ((AbstractRasterProvider)provider).getDataSet((SpiRasterQuery)query);
53
                        
54
                        bufResult = supersampleBuffer(buf);
55
                        buf.dispose();
56
                } catch (RasterDriverException e) {
57
                        throw new QueryException("Error getting data", e);
58
                }
59
                return bufResult;
60
        }
61
        
62
        private Buffer supersampleBuffer(Buffer buf) {
63
                int width = query.getBufWidth();
64
                int height = query.getBufHeight();
65
                double[] step = query.getStep();
66
                
67
                Buffer result = buildBuffer(buf.getBandCount(), width, height, buf.getDataType());
68
                int[] r = new int[height];
69
                int[] c = new int[width];
70
                calcSupersamplingStepsArrays(r, c, step);
71

    
72
                if(buf.getDataType() == Buffer.TYPE_BYTE) {
73
                        byte[] data = new byte[buf.getBandCount()];
74
                        for (int row = 0; row < height; row++) {
75
                                for (int col = 0; col < width; col++) {
76
                                        buf.getElemByte(r[row], c[col], data);
77
                                        result.setElemByte(row, col, data);
78
                                }
79
                        }
80
                }
81
                
82
                if(buf.getDataType() == Buffer.TYPE_SHORT) {
83
                        short[] data = new short[buf.getBandCount()];
84
                        for (int row = 0; row < height; row++) {
85
                                for (int col = 0; col < width; col++) {
86
                                        buf.getElemShort(r[row], c[col], data);
87
                                        result.setElemShort(row, col, data);
88
                                }
89
                        }
90
                }
91
                
92
                if(buf.getDataType() == Buffer.TYPE_INT) {
93
                        int[] data = new int[buf.getBandCount()];
94
                        for (int row = 0; row < height; row++) {
95
                                for (int col = 0; col < width; col++) {
96
                                        buf.getElemInt(r[row], c[col], data);
97
                                        result.setElemInt(row, col, data);
98
                                }
99
                        }
100
                }
101
                
102
                if(buf.getDataType() == Buffer.TYPE_FLOAT) {
103
                        float[] data = new float[buf.getBandCount()];
104
                        for (int row = 0; row < height; row++) {
105
                                for (int col = 0; col < width; col++) {
106
                                        buf.getElemFloat(r[row], c[col], data);
107
                                        result.setElemFloat(row, col, data);
108
                                }
109
                        }
110
                }
111
                
112
                if(buf.getDataType() == Buffer.TYPE_DOUBLE) {
113
                        double[] data = new double[buf.getBandCount()];
114
                        for (int row = 0; row < height; row++) {
115
                                for (int col = 0; col < width; col++) {
116
                                        buf.getElemDouble(r[row], c[col], data);
117
                                        result.setElemDouble(row, col, data);
118
                                }
119
                        }
120
                }
121
                return result;
122
        }
123
        
124
        /**
125
         * Calcula los vectores de desplazamiento en pixels en X e Y cuando se supersamplea.
126
         * @param r Array de desplazamientos para las filas. Debe tener espacio reservado
127
         * @param c Array de desplazamientos para las columnas. Debe tener espacio reservado
128
         * cargados.
129
         */
130
        private void calcSupersamplingStepsArrays(int[] r, int[] c, double[] step) {
131
                double pos = step[1];
132
                for(int row = 0; row < r.length; row ++) {
133
                        r[row] = (int)(pos / step[3]);
134
                        pos ++;
135
                }
136
                pos = step[0];
137
                for(int col = 0; col < c.length; col ++) {
138
                        c[col] = (int)(pos / step[2]);
139
                        pos ++;
140
                }
141
        }
142

    
143
        
144
        /**
145
         * Sets the nodata value
146
         * @param noData
147
         */
148
        private void setNoDataValue(NoData noData) {
149
                if(noData == null)
150
                        return;
151
                switch (store.getDataType()[0]) {
152
                case Buffer.TYPE_BYTE:
153
                        byteNoData = noData.getValue().byteValue();
154
                        break;
155
                case Buffer.TYPE_SHORT:
156
                        shortNoData = noData.getValue().shortValue();
157
                        break;
158
                case Buffer.TYPE_INT:
159
                        intNoData = noData.getValue().intValue();
160
                        break;
161
                case Buffer.TYPE_FLOAT:
162
                        floatNoData = noData.getValue().floatValue();
163
                        break;
164
                case Buffer.TYPE_DOUBLE:
165
                        doubleNoData = ((NoData)noData).getValue().doubleValue();
166
                        break;
167
                }
168
        }
169
        
170
        /**
171
         * Builds a buffer using the specified number of bands and initialize it
172
         * @param nbands
173
         * @return
174
         */
175
        private Buffer buildBuffer(int nbands, int w, int h, int datatype) {
176
                Buffer bufResult = DefaultRasterManager.getInstance().createBuffer(datatype, w, h, nbands, true);
177
                for (int i = 0; i < bufResult.getBandCount(); i++) {
178
                        clearMaskBuffer(bufResult, i);
179
                }
180
                return bufResult;
181
        }
182
        
183
        /**
184
         * Sets a band to NODATA value 
185
         * @param bufResult
186
         * @param tileExtent
187
         * @param buf
188
         * @param ex
189
         * @param pixelSize
190
         */
191
        protected void clearMaskBuffer(Buffer buf, int nBand) {
192
                if(buf.getDataType() == Buffer.TYPE_BYTE) {
193
                        for (int i = 0; i < buf.getHeight(); i++) {
194
                                for (int j = 0; j < buf.getWidth(); j++) {
195
                                        buf.setElem(i, j, nBand, byteNoData);
196
                                }
197
                        }
198
                        return;
199
                }
200
                if(buf.getDataType() == Buffer.TYPE_SHORT) {
201
                        for (int i = 0; i < buf.getHeight(); i++) {
202
                                for (int j = 0; j < buf.getWidth(); j++) {
203
                                        buf.setElem(i, j, nBand, shortNoData);
204
                                }
205
                        }
206
                        return;
207
                }
208
                if(buf.getDataType() == Buffer.TYPE_INT) {
209
                        for (int i = 0; i < buf.getHeight(); i++) {
210
                                for (int j = 0; j < buf.getWidth(); j++) {
211
                                        buf.setElem(i, j, nBand, intNoData);
212
                                }
213
                        }
214
                        return;
215
                }
216
                if(buf.getDataType() == Buffer.TYPE_FLOAT) {
217
                        for (int i = 0; i < buf.getHeight(); i++) {
218
                                for (int j = 0; j < buf.getWidth(); j++) {
219
                                        buf.setElem(i, j, nBand, floatNoData);
220
                                }
221
                        }
222
                        return;
223
                }
224
                if(buf.getDataType() == Buffer.TYPE_DOUBLE) {
225
                        for (int i = 0; i < buf.getHeight(); i++) {
226
                                for (int j = 0; j < buf.getWidth(); j++) {
227
                                        buf.setElem(i, j, nBand, doubleNoData);
228
                                }
229
                        }
230
                        return;
231
                }
232
        }
233
}