Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / tags / pre-remove-jgdal / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / jgdal / Gdal.java @ 3739

History | View | Annotate | Download (11 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
5
* 
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
* 
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
* MA  02110-1301, USA.
20
* 
21
*/
22
package org.gvsig.jgdal;
23

    
24
import java.io.IOException;
25
import java.util.Vector;
26

    
27
import org.gdal.gdal.Band;
28
import org.gdal.gdal.Dataset;
29
import org.gdal.gdal.Driver;
30
import org.gdal.gdal.gdal;
31
import org.gdal.gdal.gdalJNI;
32
import org.gdal.ogr.ogrJNI;
33
import org.gvsig.jogr.OGRTools;
34
/**
35
 * Contiene las funcionalidades necesarias para el acceso a los elementos de un
36
 * dataset de gdal correspondiente a una imagen
37
 * 
38
 * @author Nacho Brodin (nachobrodin@gmail.com)
39
 */
40
public class Gdal extends gdalJNI {
41
        // CONSTANTES
42

    
43
        // GDALAccess
44
        public static int GA_ReadOnly   = 0;
45
        public static int GA_Update     = 1;
46

    
47
        // GDALDataType
48
        public static int GDT_Unknown   = 0;
49
        public static int GDT_Byte      = 1; // Buffer byte (8)
50
        public static int GDT_UInt16    = 2; // Buffer short (16)
51
        public static int GDT_Int16     = 3; // Buffer short (16)
52
        public static int GDT_UInt32    = 4; // Buffer int (32)
53
        public static int GDT_Int32     = 5; // Buffer int (32)
54
        public static int GDT_Float32   = 6; // Buffer float (32)
55
        public static int GDT_Float64   = 7; // Buffer double (64)
56
        public static int GDT_CInt16    = 8; // Buffer short (16)
57
        public static int GDT_CInt32    = 9; // Buffer int (32)
58
        public static int GDT_CFloat32  = 10; // Buffer float (32)
59
        public static int GDT_CFloat64  = 11; // Buffer double (64)
60
        public static int GDT_TypeCount = 12;
61

    
62
        private String    pszFilename   = "";
63
        private long cPtr;
64
        private Dataset dataset;
65
        
66
        //private Dataset gdalDataset;
67

    
68
//        private native long openNat(String pszFilename, int access);
69
//        private native long openArrayNat(byte[] pszFilename, int access);
70
//        private native int getGeoTransformNat(long cPtr, GeoTransform adfgeotransform);
71
//        private native int setGeoTransformNat(long cPtr, GeoTransform adfgeotransform);
72
//        private native String[] getMetadataNat(long cPtr, String pszDomain);
73
//        private native String getProjectionRefNat(long cPtr);
74
//        private native void closeNat(long cPtr);
75
//        private native long getRasterBandNat(long cPtr, int hBand);
76
//        private native int setProjectionNat(long cPtr, String proj);
77
//        private native String getDriverShortNameNat(long cPtr);
78
        
79
        //private static native long getGDALDriverManagerNat(); 
80
//        private static native long getDriverByNameNat(String name);
81
//        private native String getColorInterpretationNameNat(long cPtr, int colorInterp);
82
        
83
        /**
84
         *Constructor a partir de la direcci?n de memoria 
85
         */
86
//        public Gdal(long cPtr){
87
//                super();
88
//        }
89
        
90
//        public Gdal(long cPtr, boolean access){
91
//                super();
92
//                gdalJNI.AllRegister();
93
//                ogrJNI.RegisterAll();
94
//                this.cPtr = cPtr;
95
//        }
96
        
97
        /**
98
         *Constructor generico
99
         */
100
        public Gdal(){
101
                super();
102
                //this(-1, false);
103
        }
104
        
105
        public Gdal(Dataset dataset){
106
                super();
107
                this.dataset = dataset;
108
        }
109

    
110

    
111
        /**
112
         * Devuelve la direcci?n de memoria del objeto dataset en C.
113
         */
114
        public long getPtro(){
115
                return cPtr;
116
                }
117
        
118
        /**
119
         * Abre el fichero de imagen.
120
         *
121
         * @param pszFilename        Nombre del fichero.
122
         * @param access        Apertura en solo lectura o escritura.
123
         * @throws GdalException
124
         */
125
        public void open(String pszFilename, int access)throws GdalException, IOException {
126
                if ((access < 0) || (access > 1))
127
                        throw new GdalException("Tipo de acceso al dataset incorrecto");
128

    
129
                this.dataset = gdal.Open(pszFilename, access);
130
//                cPtr = openArrayNat(pszFilename.getBytes(), access);
131
                
132
                if (this.dataset == null)
133
                        throw new GdalException("No se ha podido acceder al archivo.");
134
                
135
                cPtr = GdalDataset.getCPtr(this.dataset);
136
        }
137
        
138
        /**
139
         * Obtiene un array de Strings con los metadatos
140
         * 
141
         * @throws GdalException
142
         * @return Array de Strings que corresponden a los metadatos que ofrece la imagen
143
         */
144
        public String[] getMetadata()throws GdalException {
145
                return getMetadata(null);
146
                
147
        }
148
        
149
        
150
        /**
151
         * Obtiene un array de Strings con los metadatos
152
         * 
153
         * @throws GdalException
154
         * @return Array de Strings que corresponden a los metadatos que ofrece la imagen
155
         */
156
        public String[] getMetadata(String domain)throws GdalException {
157
                Vector res = this.dataset.GetMetadata_List(domain);
158
                if(res == null)
159
                        return new String[0];
160
                else return OGRTools.safeVectorToStringArray(res);
161
                
162
        }
163
        
164
        /**
165
         * Obtiene el n?mero de bandas de la imagen
166
         * 
167
         * @throws GdalException
168
         * @param hBand        Entero que corresponde al n?mero de banda que se quiere recuperar
169
         * @return Objeto GdalRasterBand que representa la banda recuperada        
170
         */
171
        
172
        public GdalRasterBand getRasterBand(int hBand)throws GdalException {
173
                Band rb;
174
                        
175
                if ((hBand < 1) || (hBand > getRasterCount()))
176
                        throw new GdalException("Banda seleccionada incorrecta");
177
                
178
                if (cPtr == 0)
179
                                throw new GdalException("No se ha podido acceder al archivo.");
180
                
181
                rb = this.dataset.GetRasterBand(hBand);
182
                
183
                return new GdalRasterBand(rb);
184
        }
185
        
186
                
187
        
188
        /**
189
         * Obtiene la dimensi?n del raster en el eje X.
190
         * 
191
         * @return        Devuelve un entero con la longitud de la imagen en el eje X en pixels.
192
         */
193
        public int getRasterXSize() {
194
                return this.dataset.getRasterXSize();
195
        }
196
                
197
        
198
        /**
199
         * Obtiene la dimensi?n del raster en el eje Y.
200
         * 
201
         * @return        Devuelve un entero con la longitud de la imagen en el eje Y en pixels.
202
         */
203
        public int getRasterYSize() {
204
                return this.dataset.getRasterYSize();
205
        }
206
        
207
        
208
        /**
209
         * Obtiene el n?mero de bandas de la imagen
210
         * 
211
         * @return        Devuelve un entero con el n?mero de bandas que contiene la imagen.
212
         */
213
        public int getRasterCount() {
214
                return this.dataset.getRasterCount();
215
        }
216
        
217
        
218
        /**
219
         * Obtiene el vector geoTransform de la imagen que contiene los valores Origen y pixelSize
220
         * 
221
         * @return        Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
222
         * @throws GdalException
223
         */
224
        public GeoTransform getGeoTransform()throws GdalException {
225
                GeoTransform gt=new GeoTransform();
226
                                
227
                if (cPtr == 0)
228
                                throw new GdalException("No se ha podido acceder al archivo.");
229
                
230
//                if()
231
//                        throw new GdalException("Error en getGeoTransform(). No se han obtenido valores para geoTransform.");
232
                this.dataset.GetGeoTransform(gt.adfgeotransform);
233
                return gt;
234
        }
235
        
236
        /**
237
         * Obtiene el Nombre corto asociado al driver del dataset
238
         * 
239
         * @return        Cadena con el nombre del driver
240
         * @throws GdalException
241
         */
242
        public String getDriverShortName()throws GdalException {
243
                if (cPtr == 0)
244
                                throw new GdalException("No se ha podido acceder al archivo.");
245
                
246
                String shortName =this.dataset.GetDriver().getShortName();
247
                
248
                if(shortName == null)
249
                        throw new GdalException("Error en getDriverShortName(). No ha podido obtenerse el driver");
250
                else 
251
                        return shortName;
252
        }
253
        
254
        /**
255
         * A?ade el vector geoTransform a la imagen que contiene los valores Origen y pixelSize
256
         * 
257
         * @return        Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
258
         * @throws GdalException
259
         */
260
        public void setGeoTransform(GeoTransform gt)throws GdalException {
261
                if (GdalDataset.getCPtr(this.dataset) == 0)
262
                                throw new GdalException("No se ha podido acceder al archivo.");
263
                if (gt == null)
264
                        throw new GdalException("el objeto " + gt.getClass().getName() + " es null");
265
                
266
                int res = this.dataset.SetGeoTransform(gt.adfgeotransform);
267
        }
268
        
269
        /**
270
         * Obtiene el sistema de coordenadas de referencia de la imagen.
271
         * 
272
         * @return        Devuelve un String con los datos del sistema de coordenadas de referencia.
273
         * @throws GdalException
274
         */
275
        public String getProjectionRef()throws GdalException {
276
                if (cPtr == 0)
277
                                throw new GdalException("No se ha podido acceder al archivo.");
278
                
279
                String res = this.dataset.GetProjectionRef();
280
                
281
                if(res == null)return new String("");
282
                else return res;
283
        }
284
        
285
        /**
286
         * Cierra el fichero de imagen.
287
         *
288
         * @throws GdalException 
289
         */
290
        public void close()throws GdalException {
291
//                if (cPtr == 0)
292
//                                throw new GdalException("No se ha podido acceder al archivo.");
293
//                
294
//                closeNat(cPtr);
295
                this.dataset.delete();
296
        }
297
        
298
        /**
299
         * Obtiene un driver a trav?s de su nombre
300
         * 
301
         * @param name        Nombre del driver
302
         */
303
        public static GdalDriver getDriverByName(String name)throws GdalException {
304
                
305
                if (name == null)
306
                        throw new GdalException("El nombre del driver es null");
307
                gdal.AllRegister();
308
                ogrJNI.RegisterAll();
309
                Driver ptrdrv = gdal.GetDriverByName(name);
310
                
311
                return new GdalDriver(ptrdrv);
312
        }
313
        
314
        
315
        /**
316
         * Obtiene el numero de bandas de la imagen
317
         * 
318
         * @return        Devuelve un entero con el numero de bandas que contiene la imagen.
319
         * @throws GdalException
320
         */
321
        public int getGCPCount()throws GdalException {
322
//                String msg1="Error en GDALGetRasterCount. . La llamada GDALOpen no tuvo ?xito";
323
//                String msg2="Error en el conteo de n?mero de bandas";
324
                return this.dataset.GetGCPCount();
325
        }
326
        
327
        /**
328
         *Asigna la proyecci?n especificada en la cadena que se le pasa por par?metro.
329
         *@param proj        proyecci?n
330
         *@throws GdalException 
331
         */
332
        public void setProjection(String proj)throws GdalException {
333
                if (cPtr == 0)
334
                                throw new GdalException("No se ha podido acceder al archivo.");
335
                if (proj == null)
336
                                throw new GdalException("La proyeccion es null");
337
                
338
                int res = this.dataset.SetProjection(proj);
339
                
340
                if(res < 0)
341
                        throw new GdalException("Error en setProjection(). No se ha podido asignar la proyecci?n.");
342
        }
343
        
344
        /**
345
         * Obtiene la cadena que representa el tipo de banda de color. Los tipos posibles son:
346
         * <UL>
347
         *  <LI>0 = "Undefined" </LI>
348
         *  <LI>1 = "Gray";</LI>
349
         *  <LI>2 = "Palette";</LI>
350
         *  <LI>3 = "Red";</LI>
351
         *  <LI>4 = "Green";</LI>
352
         *  <LI>5 = "Blue";</LI>
353
         *  <LI>6 = "Alpha";</LI>
354
         *  <LI>7 = "Hue";</LI>
355
         *  <LI>8 = "Saturation";</LI>
356
         *  <LI>9 = "Lightness";</LI>
357
         *  <LI>10 = "Cyan";</LI>
358
         *  <LI>11 = "Magenta";</LI>
359
         *  <LI>12 = "Yellow";</LI>
360
         *  <LI>13 = "Black";</LI>
361
         *  <LI>14 = "YCbCr_Y";</LI>
362
         *  <LI>15 = "YCbCr_Cb";</LI>
363
         *  <LI>16 = "YCbCr_Cr";</LI>
364
         * </UL>
365
         * @return        Cadena con el nombre del tipo de banda de color
366
         * @throws GdalException
367
         */
368
        public String getColorInterpretationName(int colorInterp)throws GdalException {
369
                if ((colorInterp < 0) || (colorInterp > 16))
370
                        throw new GdalException("Valor de interpretacion de color fuera de rango");
371
                
372
                String bandTypeName = gdal.GetColorInterpretationName(colorInterp);
373
                
374
                if(bandTypeName == null)
375
                        throw new GdalException("Error en getColorInterpretationName(). No ha podido obtenerse el tipo de banda de color");
376
                else 
377
                        return bandTypeName;
378
        }        
379
        
380
}