Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / deprecated / buffer / impl / io / BufferMultiDataSourceImpl.java @ 1939

History | View | Annotate | Download (11.1 KB)

1
package org.gvsig.raster.cache.buffer.impl.io;
2

    
3
import java.io.IOException;
4

    
5
import org.gvsig.jgdal.GdalException;
6
import org.gvsig.raster.cache.buffer.Buffer;
7
import org.gvsig.raster.cache.buffer.BufferDataSource;
8
import org.gvsig.raster.cache.buffer.PxTile;
9
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
10
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
11
import org.gvsig.raster.cache.buffer.impl.PxTileImpl;
12

    
13
/**
14
 * Data Server for read only buffers. 
15
 * 
16
 * @author Nacho Brodin nachobrodin@gmail.com
17
 */
18
public class BufferMultiDataSourceImpl extends BufferDataSourceImpl implements BufferDataSource {
19
        private GdalRead[]      reader        = null;
20
        private String[]        path          = null;
21
        private PxTile          window        = null;  
22
        private int             selectedBand  = -1;
23
                
24
        /**
25
         * This constructor is useful to use this class with a image reference.
26
         * Typically ReadOnlyCache   
27
         * @param  path
28
         *         Path to the file
29
         * @throws IOException
30
         */
31
        public BufferMultiDataSourceImpl(String[] path) throws IOException {
32
                this.path = path;
33
                reader = new GdalRead[path.length];
34
                int bandCount = 0;
35
                
36
                try {
37
                        for (int i = 0; i < path.length; i++) {
38
                                reader[i] = new GdalRead(path[i]);
39
                                bandCount += reader[i].getBandCount();
40
                        }
41
                } catch (GdalException e) {
42
                        throw new IOException();
43
                }
44
                super.setWidth(reader[0].getWidth());
45
                super.setHeight(reader[0].getHeight());
46
                super.setDataType(reader[0].getRasterBufTypeFromGdalType(reader[0].getDataType()));
47
                super.setBandCount(bandCount);
48
                window = new PxTileImpl(0, 0, reader[0].getWidth(), reader[0].getHeight());
49
        }
50
        
51
        /**
52
         * This constructor is useful to use this class with a image reference
53
         * .Typically ReadOnlyCache. 
54
         * @param  path
55
         *         Path to the file
56
         * @throws IOException
57
         */
58
        public BufferMultiDataSourceImpl(String[] path, PxTileImpl window) throws IOException {
59
                this.path = path;
60
                this.window = window;
61
                reader = new GdalRead[path.length];
62
                int bandCount = 0;
63
                
64
                try {
65
                        for (int i = 0; i < path.length; i++) {
66
                                reader[i] = new GdalRead(path[i]);
67
                                bandCount += reader[i].getBandCount();
68
                        }
69
                } catch (GdalException e) {
70
                        throw new IOException();
71
                }
72
                super.setWidth(window.getWidth());
73
                super.setHeight(window.getHeight());
74
                super.setDataType(reader[0].getRasterBufTypeFromGdalType(reader[0].getDataType()));
75
                super.setBandCount(bandCount);
76
        }
77
        
78
        /*
79
         * (non-Javadoc)
80
         * @see org.cachete.buffer.IBufferDataSource#close()
81
         */
82
        public void close() throws IOException {
83
                for (int i = 0; i < reader.length; i++) {
84
                        if(reader[i] != null)
85
                                try {
86
                                        reader[i].close();
87
                                        reader[i] = null;
88
                                } catch (GdalException e) {
89
                                        throw new IOException();
90
                                }                        
91
                }
92
        }
93
                
94
        /*
95
         * (non-Javadoc)
96
         * @see org.gvsig.raster.cache.buffer.BufferDataSource#getPath()
97
         */
98
        public String[] getPath() {
99
                return path;
100
        }
101
        
102
        /*
103
         * (non-Javadoc)
104
         * @see org.gvsig.raster.cache.buffer.BufferDataSource#setPath(java.lang.String[])
105
         */
106
        public void setPath(String[] path) {
107
                this.path = path;
108
        }
109
        
110
        /*
111
         * (non-Javadoc)
112
         * @see org.gvsig.raster.cache.buffer.BufferDataSource#setPath(java.lang.String)
113
         */
114
        public void setPath(String path) {
115
                this.path = new String[]{path};
116
        }
117
        
118
        /*
119
         * (non-Javadoc)
120
         * @see org.gvsig.raster.buffer.rocache.ICacheDataSource#loadSelectedBand(org.gvsig.raster.buffer.IRasterBuffer, org.gvsig.raster.buffer.rocache.ReadOnlyStripe)
121
         */
122
        public void loadSelectedBand(Buffer rb, PxTile stripe) throws IOException, ProcessInterruptedException, OperationNotSupportedException {
123
                loadPage(rb, stripe, selectedBand);
124
        }
125

    
126
        /*
127
         * (non-Javadoc)
128
         * @see org.gvsig.raster.buffer.rocache.ICacheDataSource#loadPage(org.gvsig.raster.buffer.IRasterBuffer, org.gvsig.raster.buffer.rocache.ReadOnlyStripe)
129
         */
130
        public void loadPage(Buffer rb, PxTile stripe) throws IOException, ProcessInterruptedException, OperationNotSupportedException {
131
                try {
132
                        int dataType = reader[0].getDataType();
133
                        
134
                        if (dataType == GdalRead.GDT_Byte) {
135
                                
136
                                int last = 0;                                
137
                                byte[][][] buf = new byte[rb.getBandCount()][][];
138
                                for (int i = 0; i < reader.length; i++) {
139
                                        Object dataObj = reader[0].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
140
                                        byte[][][] b = (byte[][][])dataObj;
141
                                        int k = 0;
142
                                        for (int j = last; j < b.length; j++) {
143
                                                buf[j] = b[k];
144
                                                k ++;
145
                                        }
146
                                        last += b.length; 
147
                                }
148
                                
149
                                for (int iBand = 0; iBand < buf.length; iBand++) 
150
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
151
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
152
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
153
                                                                                
154
                        } else if (dataType == GdalRead.GDT_CInt16 || dataType == GdalRead.GDT_Int16  || dataType == GdalRead.GDT_UInt16) {
155

    
156
                                int last = 0;                                
157
                                short[][][] buf = new short[rb.getBandCount()][][];
158
                                for (int i = 0; i < reader.length; i++) {
159
                                        Object dataObj = reader[0].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
160
                                        short[][][] b = (short[][][])dataObj;
161
                                        int k = 0;
162
                                        for (int j = last; j < b.length; j++) {
163
                                                buf[j] = b[k];
164
                                                k ++;
165
                                        }
166
                                        last += b.length; 
167
                                }
168
                                
169
                                for (int iBand = 0; iBand < buf.length; iBand++) 
170
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
171
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
172
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
173
                                                        
174
                        } else if (dataType == GdalRead.GDT_CInt32 || dataType == GdalRead.GDT_Int32  || dataType == GdalRead.GDT_UInt32) {
175

    
176
                                int last = 0;                                
177
                                int[][][] buf = new int[rb.getBandCount()][][];
178
                                for (int i = 0; i < reader.length; i++) {
179
                                        Object dataObj = reader[0].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
180
                                        int[][][] b = (int[][][])dataObj;
181
                                        int k = 0;
182
                                        for (int j = last; j < b.length; j++) {
183
                                                buf[j] = b[k];
184
                                                k ++;
185
                                        }
186
                                        last += b.length; 
187
                                }
188
                                
189
                                for (int iBand = 0; iBand < buf.length; iBand++) 
190
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
191
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
192
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
193
                                                        
194
                        } else if(dataType == GdalRead.GDT_Float32 || dataType == GdalRead.GDT_CFloat32) {
195

    
196
                                int last = 0;                                
197
                                float[][][] buf = new float[rb.getBandCount()][][];
198
                                for (int i = 0; i < reader.length; i++) {
199
                                        Object dataObj = reader[0].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
200
                                        float[][][] b = (float[][][])dataObj;
201
                                        int k = 0;
202
                                        for (int j = last; j < b.length; j++) {
203
                                                buf[j] = b[k];
204
                                                k ++;
205
                                        }
206
                                        last += b.length; 
207
                                }
208
                                
209
                                for (int iBand = 0; iBand < buf.length; iBand++) 
210
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
211
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
212
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
213
                                                        
214
                        } else if(dataType == GdalRead.GDT_Float64 || dataType == GdalRead.GDT_CFloat64) {
215

    
216
                                int last = 0;                                
217
                                double[][][] buf = new double[rb.getBandCount()][][];
218
                                for (int i = 0; i < reader.length; i++) {
219
                                        Object dataObj = reader[0].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
220
                                        double[][][] b = (double[][][])dataObj;
221
                                        int k = 0;
222
                                        for (int j = last; j < b.length; j++) {
223
                                                buf[j] = b[k];
224
                                                k ++;
225
                                        }
226
                                        last += b.length; 
227
                                }
228
                                
229
                                for (int iBand = 0; iBand < buf.length; iBand++) 
230
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
231
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
232
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
233
                                                        
234
                        }
235
                } catch (GdalException e) {
236
                        throw new IOException("Error reading block.");
237
                }
238
        }
239
        
240
        /*
241
         * (non-Javadoc)
242
         * @see org.fv.raster.buffer.rocache.ICacheDataSource#loadPage(org.fv.raster.buffer.IRasterBuffer, org.fv.raster.buffer.rocache.ReadOnlyStripe, int)
243
         */
244
        public void loadPage(Buffer rb, PxTile stripe, int iBand) throws IOException, ProcessInterruptedException, OperationNotSupportedException {
245
                if(rb.getBandCount() != 1)
246
                        throw new IOException("Wrong buffer");
247
                
248
                int dataType = reader[0].getDataType();
249
                int readerNum = 0;
250
                int bandInsideReader = 0;
251
                int accum = 0;
252
                for (int i = 0; i < reader.length; i++) {
253
                        bandInsideReader = iBand - accum;
254
                        accum += reader[i].getBandCount();
255
                        if(accum > iBand) {
256
                                readerNum = i;
257
                                break;
258
                        }
259
                }
260
                
261
                try {
262
                        Object dataObj = reader[readerNum].readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight(), bandInsideReader);
263
                        if (dataType == GdalRead.GDT_Byte) {
264
                                byte[][] buf = (byte[][])dataObj;
265
                                for (int iLine = 0; iLine < buf.length; iLine++) 
266
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
267
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
268

    
269
                        } else if (dataType == GdalRead.GDT_CInt16 || dataType == GdalRead.GDT_Int16  || dataType == GdalRead.GDT_UInt16) {
270
                                short[][] buf = (short[][])dataObj;
271
                                for (int iLine = 0; iLine < buf.length; iLine++) 
272
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
273
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
274

    
275
                        } else if (dataType == GdalRead.GDT_CInt32 || dataType == GdalRead.GDT_Int32  || dataType == GdalRead.GDT_UInt32) {
276
                                int[][] buf = (int[][])dataObj;
277
                                for (int iLine = 0; iLine < buf.length; iLine++) 
278
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
279
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
280

    
281
                        } else if(dataType == GdalRead.GDT_Float32 || dataType == GdalRead.GDT_CFloat32) {
282
                                float[][] buf = (float[][])dataObj;
283
                                for (int iLine = 0; iLine < buf.length; iLine++) 
284
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
285
                                                rb.setElem(iLine, iColumn, iBand, buf[iLine][iColumn]);
286

    
287
                        } else if(dataType == GdalRead.GDT_Float64 || dataType == GdalRead.GDT_CFloat64) {
288
                                double[][] buf = (double[][])dataObj;
289
                                for (int iLine = 0; iLine < buf.length; iLine++) 
290
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
291
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
292

    
293
                        }
294
                } catch (GdalException e) {
295
                        throw new IOException("Error reading block.");
296
                }
297
        }
298
        
299
        /* (non-Javadoc)
300
         * @see org.gvsig.fmap.dataaccess.cache.ICacheDataSource#loadPage(int, org.gvsig.fmap.dataaccess.cache.PageBuffer)
301
         */
302
        public void loadPage(Buffer buf, String inFileName) {
303
                for (int i = 0; i < buf.getBandCount(); i++) 
304
                        loadBand(buf.getBand(i), inFileName + i);
305
        }
306
        
307
        /*
308
         * (non-Javadoc)
309
         * @see java.lang.Object#clone()
310
         */
311
        public BufferDataSource clone() {
312
                try {
313
                        BufferMultiDataSourceImpl result = new BufferMultiDataSourceImpl(path, (PxTileImpl)window);
314
                        result.selectedBand = selectedBand;
315
                        return result;
316
                } catch (IOException e) {
317
                        return null;
318
                }
319
        }
320
}