Statistics
| Revision:

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

History | View | Annotate | Download (14.5 KB)

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

    
3

    
4
import java.awt.geom.AffineTransform;
5
import java.io.IOException;
6

    
7
import org.gvsig.jgdal.Gdal;
8
import org.gvsig.jgdal.GdalBuffer;
9
import org.gvsig.jgdal.GdalException;
10
import org.gvsig.jgdal.GdalRasterBand;
11
import org.gvsig.jgdal.GeoTransform;
12
import org.gvsig.raster.cache.buffer.Buffer;
13
import org.gvsig.raster.cache.buffer.exception.ProcessInterruptedException;
14
/**
15
 * Lectura de datos de un TIFF de disco
16
 * @author Nacho Brodin (nachobrodin@gmail.com)
17
 */
18
public class GdalRead extends Gdal {
19
        public         GeoTransform                 trans = null;
20
        public int                           width = 0, height = 0;
21
        public double                        originX = 0D, originY = 0D;
22
        protected int                        rBandNr = 1, gBandNr = 2, bBandNr = 3, aBandNr = 4;
23
        private int                          dataType = 0;
24
        protected GdalRasterBand[]           gdalBands = null;
25

    
26
        /**
27
         * Estado de transparencia del raster.
28
         */
29
        protected AffineTransform            ownTransformation = null;
30
        private int                          nBands            = 0;
31
        
32
        
33
        public GdalRead(String fName) throws GdalException, IOException {
34
                super();
35
                init(fName);
36
        }
37
        
38
        /**
39
         * Acciones de inicializaci?n
40
         * @param fName
41
         * @throws GdalException
42
         * @throws IOException
43
         */
44
        private void init(String fName) throws GdalException, IOException {
45
                open(fName, GA_ReadOnly);
46
                if (getPtro() == -1)
47
                        throw new GdalException("Error en la apertura del fichero. El fichero no tiene un formato v?lido.");
48

    
49
                width = getRasterXSize();
50
                height = getRasterYSize();
51
                int[] dt = new int[getRasterCount()];
52
                for (int i = 0; i < getRasterCount(); i++)
53
                        dt[i] = this.getRasterBand(i + 1).getRasterDataType();
54
                nBands = getRasterCount();
55
                dataType = dt[0];
56
                
57

    
58
                try {
59
                        trans = getGeoTransform();
60

    
61
                        boolean isCorrect = false;
62
                        for (int i = 0; i < trans.adfgeotransform.length; i++)
63
                                if (trans.adfgeotransform[i] != 0)
64
                                        isCorrect = true;
65
                        if (!isCorrect)
66
                                throw new GdalException("");
67

    
68
                        ownTransformation = new AffineTransform(trans.adfgeotransform[1], trans.adfgeotransform[4], trans.adfgeotransform[2], trans.adfgeotransform[5], trans.adfgeotransform[0], trans.adfgeotransform[3]);
69
                } catch (GdalException exc) {
70
                        // Transformaci?n para ficheros sin georreferenciaci?n. Se invierte la Y
71
                        // ya que las WC decrecen de
72
                        // arriba a abajo y los pixeles crecen de arriba a abajo
73
                        ownTransformation = new AffineTransform(1, 0, 0, -1, 0, height);
74
                }
75
        }
76
        
77
        /**
78
         * Conversi?n de los tipos de datos de gdal a los tipos de datos de RasterBuf
79
         * @param gdalType Tipo de dato de gdal
80
         * @return Tipo de dato de RasterBuf
81
         */
82
        public int getRasterBufTypeFromGdalType(int gdalType) {
83
                switch (gdalType) {
84
                        case 1:// Eight bit unsigned integer GDT_Byte = 1
85
                                return Buffer.TYPE_BYTE;
86

    
87
                        case 3:// Sixteen bit signed integer GDT_Int16 = 3,
88
                                return Buffer.TYPE_SHORT;
89

    
90
                        case 2:// Sixteen bit unsigned integer GDT_UInt16 = 2
91
                                //return RasterBuffer.TYPE_USHORT;
92
                                return Buffer.TYPE_SHORT; //Apa?o para usar los tipos de datos que soportamos
93

    
94
                        case 5:// Thirty two bit signed integer GDT_Int32 = 5
95
                                return Buffer.TYPE_INT;
96

    
97
                        case 6:// Thirty two bit floating point GDT_Float32 = 6
98
                                return Buffer.TYPE_FLOAT;
99

    
100
                        case 7:// Sixty four bit floating point GDT_Float64 = 7
101
                                return Buffer.TYPE_DOUBLE;
102

    
103
                                // TODO:Estos tipos de datos no podemos gestionarlos. Habria que definir
104
                                // el tipo complejo y usar el tipo long que de momento no se gasta.
105
                        case 4:// Thirty two bit unsigned integer GDT_UInt32 = 4,
106
                                return Buffer.TYPE_INT;
107
                                //return RasterBuffer.TYPE_UNDEFINED; // Deberia devolver un Long
108

    
109
                        case 8:// Complex Int16 GDT_CInt16 = 8
110
                        case 9:// Complex Int32 GDT_CInt32 = 9
111
                        case 10:// Complex Float32 GDT_CFloat32 = 10
112
                        case 11:// Complex Float64 GDT_CFloat64 = 11
113
                                return Buffer.TYPE_UNDEFINED;
114
                }
115
                return Buffer.TYPE_UNDEFINED;
116
        }
117
        
118
        /**
119
         * Conversi?n de los tipos de datos de gdal a los tipos de datos de RasterBuf
120
         * @param gdalType Tipo de dato de gdal
121
         * @return Tipo de dato de RasterBuf
122
         */
123
        public int getGdalTypeFromRasterBufType(int rasterBufType) {
124
                switch (rasterBufType) {
125
                        case Buffer.TYPE_BYTE: return Gdal.GDT_Byte;
126
                        case Buffer.TYPE_USHORT: return Gdal.GDT_UInt16;
127
                        case Buffer.TYPE_SHORT: return Gdal.GDT_Int16;
128
                        case Buffer.TYPE_INT: return Gdal.GDT_Int32;
129
                        case Buffer.TYPE_FLOAT: return Gdal.GDT_Float32;
130
                        case Buffer.TYPE_DOUBLE: return Gdal.GDT_Float64;
131
                        case Buffer.TYPE_UNDEFINED: return Gdal.GDT_Unknown;
132
                }
133
                return Gdal.GDT_Unknown;
134
        }
135
        
136
        /**
137
         * Obtiene el anchura del raster
138
         * @return
139
         */
140
        public int getWidth() { 
141
                return this.width;
142
        }
143
        
144
        /**
145
         * Obtiene la altura del raster
146
         * @return 
147
         */
148
        public int getHeight() { 
149
                return this.height;
150
        }
151
        
152
        /**
153
         * Obtiene el flag que informa de si el raster tiene valor no data o no.
154
         * Consultar? todas las bandas del mismo y si alguna tiene valor no data
155
         * devuelve true sino devolver? false.
156
         * @return true si tiene valor no data y false si no lo tiene
157
         * @throws GdalException
158
         */
159
        public boolean existsNoDataValue() throws GdalException {
160
                for (int i = 0; i < getRasterCount(); i++) {
161
                        GdalRasterBand rb = getRasterBand(i + 1);
162
                        if (rb.existsNoDataValue())
163
                                return true;
164
                }
165
                return false;
166
        }
167
        
168
        /**
169
         * Obtiene el flag que informa de si el raster tiene valor no data o no
170
         * en una banda concreta.
171
         * @return true si tiene valor no data en esa banda y false si no lo tiene
172
         * @param band Posici?n de la banda a consultar (0..n)
173
         * @throws GdalException
174
         */
175
        public boolean existsNoDataValue(int band) throws GdalException {
176
                GdalRasterBand rb = getRasterBand(band + 1);
177
                return rb.existsNoDataValue();
178
        }
179
        
180
        /**
181
         * Obtiene el tipo de dato
182
         * @return entero que representa el tipo de dato
183
         */
184
        public int getDataType() { 
185
                return dataType; 
186
        }
187
        
188
        /**
189
         * Obtiene el n?mero de bandas
190
         * @return
191
         */
192
        public int getBandCount() {
193
                return nBands;
194
        }
195
        
196
        /**
197
         * Lee una bloque completo del raster y devuelve un array tridimensional del tipo correcto. Esta funci?n es util
198
         * para una lectura rapida de todo el fichero sin necesidad de asignar vista. 
199
         * @param nLine N?mero de l?nea a leer
200
         * @param band Banda requerida
201
         * @return Object que es un array unidimendional del tipo de datos del raster
202
         * @throws GdalException
203
         */
204
        public Object readBlock(int posX, int posY, int blockWidth, int blockHeight) throws GdalException, ProcessInterruptedException {
205
                bBandNr = super.getRasterCount();
206
                                
207
                GdalRasterBand[] gdalBand = new GdalRasterBand[bBandNr];
208
                for (int iBand = 0; iBand < gdalBand.length; iBand++) 
209
                        gdalBand[iBand] = super.getRasterBand(iBand + 1);
210
                                
211
                GdalBuffer[] gdalBuf = new GdalBuffer[bBandNr];
212
                                
213
                if (dataType == GDT_Byte) {
214
                        byte[][][] buf = new byte[bBandNr][blockHeight][getRasterXSize()];
215
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
216
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
217
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
218
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
219
                                                buf[iBand][iRow][iCol] = gdalBuf[iBand].buffByte[iRow * blockWidth + iCol];
220
                                }
221
                        }        
222
                        return buf;
223
                } else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16) {
224
                        short[][][] buf = new short[bBandNr][blockHeight][getRasterXSize()];
225
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
226
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
227
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
228
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
229
                                                buf[iBand][iRow][iCol] = gdalBuf[iBand].buffShort[iRow * blockWidth + iCol];
230
                                }
231
                        }        
232
                        return buf;
233
                } else if (dataType == GDT_CInt32 || dataType == GDT_Int32  || dataType == GDT_UInt32) {
234
                        int[][][] buf = new int[bBandNr][blockHeight][getRasterXSize()];
235
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
236
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
237
                                for (int iRow = 0; iRow < blockHeight; iRow++) { 
238
                                        for (int iCol = 0; iCol < blockWidth; iCol++)
239
                                                buf[iBand][iRow][iCol] = gdalBuf[iBand].buffInt[iRow * blockWidth + iCol];
240
                                }
241
                        }        
242
                        return buf;
243
                } else if(dataType == GDT_Float32 || dataType == GDT_CFloat32) {
244
                        float[][][] buf = new float[bBandNr][blockHeight][getRasterXSize()];
245
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
246
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
247
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
248
                                        for (int iCol = 0; iCol < blockWidth; iCol++)
249
                                                buf[iBand][iRow][iCol] = gdalBuf[iBand].buffFloat[iRow * blockWidth + iCol];
250
                                }
251
                        }        
252
                        return buf;
253
                } else if(dataType == GDT_Float64 || dataType == GDT_CFloat64) {
254
                        double[][][] buf = new double[bBandNr][blockHeight][getRasterXSize()];
255
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
256
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
257
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
258
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
259
                                                buf[iBand][iRow][iCol] = gdalBuf[iBand].buffDouble[iRow * blockWidth + iCol];
260
                                }
261
                        }                
262
                        return buf;
263
                }
264
                return null;
265
        }
266
        
267
        /**
268
         * Lee una bloque completo del raster y devuelve un array tridimensional del tipo correcto. Esta funci?n es util
269
         * para una lectura rapida de todo el fichero sin necesidad de asignar vista. 
270
         * @param nLine N?mero de l?nea a leer
271
         * @param band Banda requerida
272
         * @return Object que es un array unidimendional del tipo de datos del raster
273
         * @throws GdalException
274
         */
275
        public Object readBlock(int posX, int posY, int blockWidth, int blockHeight, int iBand) throws GdalException, ProcessInterruptedException {
276
                                
277
                GdalRasterBand gdalBand = null;
278
                gdalBand = super.getRasterBand(iBand + 1);
279
                                
280
                GdalBuffer gdalBuf = null;
281
                                
282
                if (dataType == GDT_Byte) {
283
                        byte[][] buf = new byte[blockHeight][blockWidth];
284
                                gdalBuf = gdalBand.readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
285
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
286
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
287
                                                buf[iRow][iCol] = gdalBuf.buffByte[iRow * blockWidth + iCol];
288
                                }
289
                        return buf;
290
                } else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16) {
291
                        short[][] buf = new short[blockHeight][blockWidth];
292
                                gdalBuf = gdalBand.readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
293
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
294
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
295
                                                buf[iRow][iCol] = gdalBuf.buffShort[iRow * blockWidth + iCol];
296
                                }
297
                        return buf;
298
                } else if (dataType == GDT_CInt32 || dataType == GDT_Int32  || dataType == GDT_UInt32) {
299
                        int[][] buf = new int[blockHeight][blockWidth];
300
                                gdalBuf = gdalBand.readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
301
                                for (int iRow = 0; iRow < blockHeight; iRow++) { 
302
                                        for (int iCol = 0; iCol < blockWidth; iCol++)
303
                                                buf[iRow][iCol] = gdalBuf.buffInt[iRow * blockWidth + iCol];
304
                                }
305
                        return buf;
306
                } else if(dataType == GDT_Float32 || dataType == GDT_CFloat32) {
307
                        float[][] buf = new float[blockHeight][blockWidth];
308
                                gdalBuf = gdalBand.readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
309
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
310
                                        for (int iCol = 0; iCol < blockWidth; iCol++)
311
                                                buf[iRow][iCol] = gdalBuf.buffFloat[iRow * blockWidth + iCol];
312
                                }
313
                        return buf;
314
                } else if(dataType == GDT_Float64 || dataType == GDT_CFloat64) {
315
                        double[][] buf = new double[blockHeight][blockWidth];
316
                                gdalBuf = gdalBand.readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
317
                                for (int iRow = 0; iRow < blockHeight; iRow++) {
318
                                        for (int iCol = 0; iCol < blockWidth; iCol++) 
319
                                                buf[iRow][iCol] = gdalBuf.buffDouble[iRow * blockWidth + iCol];
320
                                }
321
                        return buf;
322
                }
323
                return null;
324
        }
325
        
326
        /**
327
         * Lee una bloque completo del raster y devuelve un array tridimensional del tipo correcto. Esta funci?n es util
328
         * para una lectura rapida de todo el fichero sin necesidad de asignar vista. 
329
         * @param nLine N?mero de l?nea a leer
330
         * @param band Banda requerida
331
         * @return Object que es un array unidimendional del tipo de datos del raster
332
         * @throws GdalException
333
         */
334
        public Object readData(int posX, int posY, int blockWidth, int blockHeight) throws GdalException, InterruptedException {
335
                bBandNr = super.getRasterCount();
336
                                
337
                GdalRasterBand[] gdalBand = new GdalRasterBand[bBandNr];
338
                for (int iBand = 0; iBand < gdalBand.length; iBand++) 
339
                        gdalBand[iBand] = super.getRasterBand(iBand + 1);
340
                                
341
                GdalBuffer[] gdalBuf = new GdalBuffer[bBandNr];
342
                                
343
                if (dataType == GDT_Byte) {
344
                        byte[][] buf = new byte[bBandNr][];
345
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
346
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
347
                                buf[iBand] = gdalBuf[iBand].buffByte;        
348
                        }        
349
                        return buf;
350
                } else if (dataType == GDT_CInt16 || dataType == GDT_Int16  || dataType == GDT_UInt16) {
351
                        short[][] buf = new short[bBandNr][];
352
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
353
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
354
                                buf[iBand] = gdalBuf[iBand].buffShort;
355
                        }        
356
                        return buf;
357
                } else if (dataType == GDT_CInt32 || dataType == GDT_Int32  || dataType == GDT_UInt32) {
358
                        int[][] buf = new int[bBandNr][];
359
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
360
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
361
                                buf[iBand] = gdalBuf[iBand].buffInt;
362
                        }        
363
                        return buf;
364
                } else if(dataType == GDT_Float32 || dataType == GDT_CFloat32) {
365
                        float[][] buf = new float[bBandNr][];
366
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
367
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
368
                                buf[iBand] = gdalBuf[iBand].buffFloat;
369
                        }        
370
                        return buf;
371
                } else if(dataType == GDT_Float64 || dataType == GDT_CFloat64) {
372
                        double[][] buf = new double[bBandNr][];
373
                        for (int iBand = 0; iBand < gdalBuf.length; iBand++) {
374
                                gdalBuf[iBand] = gdalBand[iBand].readRaster(posX, posY, blockWidth, blockHeight, blockWidth, blockHeight, dataType);
375
                                buf[iBand] = gdalBuf[iBand].buffDouble;
376
                        }                
377
                        return buf;
378
                }
379
                                
380
                        return null;
381
        }
382
                
383
}
384

    
385

    
386

    
387