Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / buffer / impl / io / BufferDataSourceImpl.java @ 991

History | View | Annotate | Download (15.3 KB)

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

    
3
import java.io.BufferedInputStream;
4
import java.io.BufferedOutputStream;
5
import java.io.DataInputStream;
6
import java.io.DataOutputStream;
7
import java.io.File;
8
import java.io.FileInputStream;
9
import java.io.FileNotFoundException;
10
import java.io.FileOutputStream;
11
import java.io.IOException;
12

    
13
import org.gvsig.jgdal.GdalException;
14
import org.gvsig.raster.cache.buffer.Band;
15
import org.gvsig.raster.cache.buffer.BufferDataSource;
16
import org.gvsig.raster.cache.buffer.Buffer;
17
import org.gvsig.raster.cache.buffer.PxTile;
18
import org.gvsig.raster.cache.buffer.exception.OperationNotSupportedException;
19
import org.gvsig.raster.cache.buffer.impl.PxTileImpl;
20
import org.gvsig.raster.cache.buffer.impl.RasterBase;
21

    
22
/**
23
 * Data Server for read only buffers. This server only read tiff data.
24
 * 
25
 * 02/11/2008
26
 * @author Nacho Brodin nachobrodin@gmail.com
27
 */
28
public class BufferDataSourceImpl extends RasterBase implements BufferDataSource {
29
        private GdalRead        reader        = null;
30
        private String          path          = null;
31
        private PxTile          window        = null;  
32
        private int             selectedBand  = -1;
33
                
34
        /**
35
         * This constructor is useful to use this class with a image reference.
36
         * Typically ReadOnlyCache   
37
         * @param  path
38
         *         Path to the file
39
         * @throws IOException
40
         */
41
        public BufferDataSourceImpl(String path) throws IOException {
42
                super(0, 0, 0, 0); //Se asignan los par?metros despues de construir el tiff
43
                this.path = path;
44
                
45
                try {
46
                        reader = new GdalRead(path);
47
                } catch (GdalException e) {
48
                        throw new IOException();
49
                }
50
                super.setWidth(reader.getWidth());
51
                super.setHeight(reader.getHeight());
52
                super.setDataType(reader.getDataType());
53
                super.setBandCount(reader.getBandCount());
54
                window = new PxTileImpl(0, 0, reader.getWidth(), reader.getHeight());
55
        }
56
        
57
        /**
58
         * This constructor is useful to use this class with a image reference
59
         * .Typically ReadOnlyCache. 
60
         * @param  path
61
         *         Path to the file
62
         * @throws IOException
63
         */
64
        public BufferDataSourceImpl(String path, PxTileImpl window) throws IOException {
65
                super(window.getX(), window.getY(), 0, 0); //Se asignan los par?metros despues de construir el tiff
66
                this.path = path;
67
                this.window = window;
68
                
69
                try {
70
                        reader = new GdalRead(path);
71
                } catch (GdalException e) {
72
                        throw new IOException();
73
                }
74
                super.setWidth(window.getWidth());
75
                super.setHeight(window.getHeight());
76
                super.setDataType(reader.getRasterBufTypeFromGdalType(reader.getDataType()));
77
                super.setBandCount(reader.getBandCount());
78
        }
79
        
80
        /*
81
         * (non-Javadoc)
82
         * @see org.cachete.buffer.IBufferDataSource#close()
83
         */
84
        public void close() throws IOException {
85
                if(reader != null)
86
                        try {
87
                                reader.close();
88
                                reader = null;
89
                        } catch (GdalException e) {
90
                                throw new IOException();
91
                        }
92
        }
93
        
94
        /**
95
         * This constructor is useful to use this class without a image reference. 
96
         * Typically StripeCache    
97
         * @throws IOException
98
         */
99
        public BufferDataSourceImpl() {
100
                super(0, 0, 0, 0);
101
        }
102
                
103
        /*
104
         *  (non-Javadoc)
105
         * @see org.gvsig.raster.dataaccess.cache.ICacheDataSource#getPath()
106
         */
107
        public String getPath() {
108
                return path;
109
        }
110
        
111
        /*
112
         *  (non-Javadoc)
113
         * @see org.gvsig.raster.dataaccess.cache.ICacheDataSource#setPath(java.lang.String)
114
         */
115
        public void setPath(String path) {
116
                this.path = path;
117
        }
118
        
119
        /*
120
         * (non-Javadoc)
121
         * @see org.fv.raster.buffer.rocache.ICacheDataSource#getBand()
122
         */
123
        public int getSelectedBand() {
124
                return this.selectedBand;
125
        }
126
        
127
        /*
128
         * (non-Javadoc)
129
         * @see org.fv.raster.buffer.rocache.ICacheDataSource#setBand(java.lang.String)
130
         */
131
        public void setSelectedBand(int selectedBand) {
132
                this.selectedBand = selectedBand;
133
        }
134
        
135
        /*
136
         * (non-Javadoc)
137
         * @see org.gvsig.raster.buffer.rocache.ICacheDataSource#loadSelectedBand(org.gvsig.raster.buffer.IRasterBuffer, org.gvsig.raster.buffer.rocache.ReadOnlyStripe)
138
         */
139
        public void loadSelectedBand(Buffer rb, PxTile stripe) throws IOException, InterruptedException, OperationNotSupportedException {
140
                loadPage(rb, stripe, selectedBand);
141
        }
142

    
143
        /*
144
         * (non-Javadoc)
145
         * @see org.gvsig.raster.buffer.rocache.ICacheDataSource#loadPage(org.gvsig.raster.buffer.IRasterBuffer, org.gvsig.raster.buffer.rocache.ReadOnlyStripe)
146
         */
147
        public void loadPage(Buffer rb, PxTile stripe) throws IOException, InterruptedException, OperationNotSupportedException {
148
                try {
149
                        Object dataObj = reader.readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight());
150
                        if (reader.getDataType() == GdalRead.GDT_Byte) {
151
                                byte[][][] buf = (byte[][][])dataObj;
152
                                for (int iBand = 0; iBand < buf.length; iBand++) 
153
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
154
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
155
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
156
                                                                                
157
                        } else if (reader.getDataType() == GdalRead.GDT_CInt16 || reader.getDataType() == GdalRead.GDT_Int16  || reader.getDataType() == GdalRead.GDT_UInt16) {
158
                                short[][][] buf = (short[][][])dataObj;
159
                                for (int iBand = 0; iBand < buf.length; iBand++) 
160
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
161
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
162
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
163
                                                        
164
                        } else if (reader.getDataType() == GdalRead.GDT_CInt32 || reader.getDataType() == GdalRead.GDT_Int32  || reader.getDataType() == GdalRead.GDT_UInt32) {
165
                                int[][][] buf = (int[][][])dataObj;
166
                                for (int iBand = 0; iBand < buf.length; iBand++) 
167
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
168
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
169
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
170
                                                        
171
                        } else if(reader.getDataType() == GdalRead.GDT_Float32 || reader.getDataType() == GdalRead.GDT_CFloat32) {
172
                                float[][][] buf = (float[][][])dataObj;
173
                                for (int iBand = 0; iBand < buf.length; iBand++) 
174
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
175
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
176
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
177
                                                        
178
                        } else if(reader.getDataType() == GdalRead.GDT_Float64 || reader.getDataType() == GdalRead.GDT_CFloat64) {
179
                                double[][][] buf = (double[][][])dataObj;
180
                                for (int iBand = 0; iBand < buf.length; iBand++) 
181
                                        for (int iLine = 0; iLine < buf[iBand].length; iLine++) 
182
                                                for (int iColumn = 0; iColumn < buf[iBand][iLine].length; iColumn++) 
183
                                                        rb.setElem(iLine, iColumn, iBand, buf[iBand][iLine][iColumn]);
184
                                                        
185
                        }
186
                } catch (GdalException e) {
187
                        throw new IOException("Error reading block.");
188
                }
189
        }
190
        
191
        /*
192
         * (non-Javadoc)
193
         * @see org.fv.raster.buffer.rocache.ICacheDataSource#loadPage(org.fv.raster.buffer.IRasterBuffer, org.fv.raster.buffer.rocache.ReadOnlyStripe, int)
194
         */
195
        public void loadPage(Buffer rb, PxTile stripe, int iBand) throws IOException, InterruptedException, OperationNotSupportedException {
196
                if(rb.getBandCount() != 1)
197
                        throw new IOException("Wrong buffer");
198
                
199
                try {
200
                        Object dataObj = reader.readBlock((int)stripe.getX() + window.getX(), (int)stripe.getY() + window.getY(), (int)stripe.getWidth(), (int)stripe.getHeight(), iBand);
201
                        if (reader.getDataType() == GdalRead.GDT_Byte) {
202
                                byte[][] buf = (byte[][])dataObj;
203
                                for (int iLine = 0; iLine < buf.length; iLine++) 
204
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
205
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
206

    
207
                        } else if (reader.getDataType() == GdalRead.GDT_CInt16 || reader.getDataType() == GdalRead.GDT_Int16  || reader.getDataType() == GdalRead.GDT_UInt16) {
208
                                short[][] buf = (short[][])dataObj;
209
                                for (int iLine = 0; iLine < buf.length; iLine++) 
210
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
211
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
212

    
213
                        } else if (reader.getDataType() == GdalRead.GDT_CInt32 || reader.getDataType() == GdalRead.GDT_Int32  || reader.getDataType() == GdalRead.GDT_UInt32) {
214
                                int[][] buf = (int[][])dataObj;
215
                                for (int iLine = 0; iLine < buf.length; iLine++) 
216
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
217
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
218

    
219
                        } else if(reader.getDataType() == GdalRead.GDT_Float32 || reader.getDataType() == GdalRead.GDT_CFloat32) {
220
                                float[][] buf = (float[][])dataObj;
221
                                for (int iLine = 0; iLine < buf.length; iLine++) 
222
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
223
                                                rb.setElem(iLine, iColumn, iBand, buf[iLine][iColumn]);
224

    
225
                        } else if(reader.getDataType() == GdalRead.GDT_Float64 || reader.getDataType() == GdalRead.GDT_CFloat64) {
226
                                double[][] buf = (double[][])dataObj;
227
                                for (int iLine = 0; iLine < buf.length; iLine++) 
228
                                        for (int iColumn = 0; iColumn < buf[iLine].length; iColumn++) 
229
                                                rb.setElem(iLine, iColumn, 0, buf[iLine][iColumn]);
230

    
231
                        }
232
                } catch (GdalException e) {
233
                        throw new IOException("Error reading block.");
234
                }
235
        }
236
        
237
        /* (non-Javadoc)
238
         * @see org.gvsig.fmap.dataaccess.cache.ICacheDataSource#loadPage(int, org.gvsig.fmap.dataaccess.cache.PageBuffer)
239
         */
240
        public void loadPage(Buffer buf, String inFileName) {
241
                for (int i = 0; i < buf.getBandCount(); i++) 
242
                        loadBand(buf.getBand(i), inFileName + i);
243
        }
244
        
245
        /**
246
         * Load a band from hard disk
247
         * @param pageBuffer
248
         * @param inFileName
249
         */
250
        public void loadBand(Band pageBuffer, String inFileName) {
251
                File file = new File(inFileName);
252
                if (!file.exists())
253
                        return;
254

    
255
                try {
256
                        FileInputStream fis = new FileInputStream(file);
257
                        BufferedInputStream bis = new BufferedInputStream(fis);
258
                        DataInputStream dis = new DataInputStream(bis);
259
                        read(dis, pageBuffer);
260
                        fis.close();
261
                        bis.close();
262
                        dis.close();
263
                } catch (FileNotFoundException e) {
264
                        return;
265
                } catch (IOException ex) {
266
                        // TODO: EXCEPTION: Lanzar IO y FileNotFound
267
                        ex.printStackTrace();
268
                        return;
269
                }  catch (OperationNotSupportedException ex) {
270
                        ex.printStackTrace();
271
                        return;
272
                }
273
        }
274

    
275
        /* (non-Javadoc)
276
         * @see org.gvsig.fmap.dataaccess.cache.ICacheDataSource#savePage(int, org.gvsig.fmap.dataaccess.cache.PageBuffer)
277
         */
278
        public void savePage(Buffer buf, String fileName) throws IOException {
279
                for (int i = 0; i < buf.getBandCount(); i++)
280
                        saveBand(buf.getBand(i), fileName + i);
281
        }
282

    
283
        /**
284
         * Save a band to hard disk
285
         * @param pageBuffer
286
         * @param fileName
287
         * @throws IOException
288
         */
289
        public void saveBand(Band pageBuffer, String fileName) throws IOException {
290
                File f = new File(fileName);
291
                FileOutputStream fos = new FileOutputStream(f);
292
                BufferedOutputStream bos = new BufferedOutputStream(fos);
293
                DataOutputStream dos = new DataOutputStream(bos);
294
                save(dos, pageBuffer);
295
                dos.close();
296
                bos.close();
297
                fos.close();
298
        }
299
                
300
        /*
301
         * (non-Javadoc)
302
         * @see org.gvsig.raster.dataaccess.cache.ICacheDataSource#delete()
303
         */
304
        public void delete(String fileName) {
305
                File f = new File(fileName);
306
                if (f.exists()) {
307
                        f.delete();
308
                }
309
        }
310
                
311
        /**
312
         * Salva un PageBuffer sobre un stream DataOutpuStream dependiendo del tipo de dato del 
313
         * buffer.
314
         * @param dos DataOutputStream 
315
         * @param pageBuffer PageBuffer
316
         * @throws IOException
317
         */
318
        private void save(DataOutputStream dos, Band pageBuffer) throws IOException {
319
                switch (pageBuffer.getDataType()) {
320
                        case Buffer.TYPE_BYTE:
321
                                try {
322
                                        for (int line = 0; line < pageBuffer.getHeight(); line++)
323
                                                dos.write(pageBuffer.getByteLine(line));
324
                                } catch (OperationNotSupportedException e) {
325
                                        for (int line = 0; line < pageBuffer.getHeight(); line++)
326
                                                for (int col = 0; col < pageBuffer.getWidth(); col++)
327
                                                        dos.writeByte(pageBuffer.getElemByte(line, col));
328
                                }
329
                                break;
330
                        case Buffer.TYPE_SHORT:
331
                                for (int line = 0; line < pageBuffer.getHeight(); line++)
332
                                        for (int col = 0; col < pageBuffer.getWidth(); col++)
333
                                                dos.writeShort(pageBuffer.getElemShort(line, col));
334
                                break;
335
                        case Buffer.TYPE_INT:
336
                                for (int line = 0; line < pageBuffer.getHeight(); line++)
337
                                        for (int col = 0; col < pageBuffer.getWidth(); col++)
338
                                                dos.writeInt(pageBuffer.getElemInt(line, col));
339
                                break;
340
                        case Buffer.TYPE_FLOAT:
341
                                for (int line = 0; line < pageBuffer.getHeight(); line++)
342
                                        for (int col = 0; col < pageBuffer.getWidth(); col++)
343
                                                dos.writeFloat(pageBuffer.getElemFloat(line, col));
344
                                break;
345
                        case Buffer.TYPE_DOUBLE:
346
                                for (int line = 0; line < pageBuffer.getHeight(); line++)
347
                                        for (int col = 0; col < pageBuffer.getWidth(); col++)
348
                                                dos.writeDouble(pageBuffer.getElemDouble(line, col));
349
                                break;
350
                }
351
        }
352
        
353
        /**
354
         * Carga un PageBuffer desde un stream DataInputStream dependiendo del tipo de dato del 
355
         * buffer.
356
         * @param dis DataInputStream
357
         * @param pageBuffer PageBuffer
358
         * @throws IOException
359
         * @throws OperationNotSupportedException 
360
         */
361
        private void read(DataInputStream dis, Band pageBuffer) throws IOException, OperationNotSupportedException {
362
                int w = pageBuffer.getWidth();
363
                int j = 0;
364
                switch (pageBuffer.getDataType()) {
365
                case Buffer.TYPE_BYTE:
366
                        byte[] b = new byte[pageBuffer.getWidth() * pageBuffer.getHeight()];
367
                        dis.readFully(b);
368
                        for (int line = 0; line < pageBuffer.getHeight(); line++) {
369
                                byte[] linea = new byte[w];
370
                                for (int d = 0; d < linea.length; d++)
371
                                        linea[d] = b[(line * w) + d];
372
                                pageBuffer.setByteLine(linea, line);
373
                        }
374
                        break;
375
                case Buffer.TYPE_SHORT:
376
                        short[] s = new short[pageBuffer.getWidth() * pageBuffer.getHeight()];
377
                        while (j < s.length) {
378
                                s[j] = dis.readShort();
379
                                j++;
380
                        }
381
                        for (int line = 0; line < pageBuffer.getHeight(); line++) {
382
                                short[] linea = new short[w];
383
                                for (int x = 0; x < linea.length; x++)
384
                                        linea[x] = s[(line * w) + x];
385
                                pageBuffer.setShortLine(linea, line);
386
                        }
387
                        break;
388
                case Buffer.TYPE_INT:
389
                        int[] i = new int[pageBuffer.getWidth() * pageBuffer.getHeight()];
390
                        while (j < i.length) {
391
                                i[j] = dis.readInt();
392
                                j++;
393
                        }
394
                        for (int line = 0; line < pageBuffer.getHeight(); line++) {
395
                                int[] linea = new int[w];
396
                                for (int x = 0; x < linea.length; x++)
397
                                        linea[x] = i[(line * w) + x];
398
                                pageBuffer.setIntLine(linea, line);
399
                        }
400
                        break;
401
                case Buffer.TYPE_FLOAT:
402
                        float[] f = new float[pageBuffer.getWidth() * pageBuffer.getHeight()];
403
                        while (j < f.length) {
404
                                f[j] = dis.readFloat();
405
                                j++;
406
                        }
407
                        for (int line = 0; line < pageBuffer.getHeight(); line++) {
408
                                float[] linea = new float[w];
409
                                for (int x = 0; x < linea.length; x++)
410
                                        linea[x] = f[(line * w) + x];
411
                                pageBuffer.setFloatLine(linea, line);
412
                        }
413
                        break;
414
                case Buffer.TYPE_DOUBLE:
415
                        double[] d = new double[pageBuffer.getWidth() * pageBuffer.getHeight()];
416
                        while (j < d.length) {
417
                                d[j] = dis.readDouble();
418
                                j++;
419
                        }
420
                        for (int line = 0; line < pageBuffer.getHeight(); line++) {
421
                                double[] linea = new double[w];
422
                                for (int x = 0; x < linea.length; x++)
423
                                        linea[x] = d[(line * w) + x];
424
                                pageBuffer.setDoubleLine(linea, line);
425
                        }
426
                        break;
427
                }
428
        }
429
        
430
        /*
431
         * (non-Javadoc)
432
         * @see org.gvsig.raster.buffer.stripecache.ICacheDataSource#changeFileName(java.lang.String, java.lang.String)
433
         */
434
        public void changeFileName(String oldName, String newName) {
435
                File f1 = new File(oldName);
436
                f1.renameTo(new File(newName));
437
        }
438
        
439
        /*
440
         * (non-Javadoc)
441
         * @see java.lang.Object#clone()
442
         */
443
        public BufferDataSource clone() {
444
                try {
445
                        BufferDataSourceImpl result = new BufferDataSourceImpl(path, (PxTileImpl)window);
446
                        result.selectedBand = selectedBand;
447
                        return result;
448
                } catch (IOException e) {
449
                        return null;
450
                }
451
        }
452
}