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 @ 3497

History | View | Annotate | Download (10.4 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.Dataset;
28
import org.gdal.gdal.Driver;
29
import org.gdal.gdal.gdal;
30
import org.gdal.gdal.gdalJNI;
31
import org.gvsig.jogr.OGRTools;
32
/**
33
 * Contiene las funcionalidades necesarias para el acceso a los elementos de un
34
 * dataset de gdal correspondiente a una imagen
35
 * 
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class Gdal extends Dataset {
39
        // CONSTANTES
40

    
41
        // GDALAccess
42
        public static int GA_ReadOnly   = 0;
43
        public static int GA_Update     = 1;
44

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

    
60
        private String    pszFilename   = "";
61
        private long cPtr;
62

    
63
//        private native long openNat(String pszFilename, int access);
64
//        private native long openArrayNat(byte[] pszFilename, int access);
65
//        private native int getGeoTransformNat(long cPtr, GeoTransform adfgeotransform);
66
//        private native int setGeoTransformNat(long cPtr, GeoTransform adfgeotransform);
67
//        private native String[] getMetadataNat(long cPtr, String pszDomain);
68
//        private native String getProjectionRefNat(long cPtr);
69
//        private native void closeNat(long cPtr);
70
//        private native long getRasterBandNat(long cPtr, int hBand);
71
//        private native int setProjectionNat(long cPtr, String proj);
72
//        private native String getDriverShortNameNat(long cPtr);
73
        
74
        //private static native long getGDALDriverManagerNat(); 
75
//        private static native long getDriverByNameNat(String name);
76
//        private native String getColorInterpretationNameNat(long cPtr, int colorInterp);
77
        
78
        /**
79
         *Constructor a partir de la direcci?n de memoria 
80
         */
81
        public Gdal(long cPtr){
82
                super(cPtr,true);
83
                this.cPtr = cPtr;
84
        }
85
        
86
        /**
87
         *Constructor generico
88
         */
89
        public Gdal(){
90
        }
91

    
92

    
93
        /**
94
         * Devuelve la direcci?n de memoria del objeto dataset en C.
95
         */
96
        public long getPtro(){
97
                return cPtr;
98
                }
99
        
100
        /**
101
         * Abre el fichero de imagen.
102
         *
103
         * @param pszFilename        Nombre del fichero.
104
         * @param access        Apertura en solo lectura o escritura.
105
         * @throws GdalException
106
         */
107
        public void open(String pszFilename, int access)throws GdalException, IOException {
108
                if ((access < 0) || (access > 1))
109
                        throw new GdalException("Tipo de acceso al dataset incorrecto");
110

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