Statistics
| Revision:

root / trunk / libraries / libjni-gdal / src / es / gva / cit / jgdal / Gdal.java @ 7765

History | View | Annotate | Download (11.7 KB)

1
/**********************************************************************
2
 * $Id: Gdal.java 7765 2006-10-03 07:05:18Z nacho $
3
 *
4
 * Name:     Gdal.java
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:  Dataset's Basic Funcions. 
7
 * Author:   Nacho Brodin, brodin_ign@gva.es
8
 *
9
 **********************************************************************/
10
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
11
*
12
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
13
*
14
* This program is free software; you can redistribute it and/or
15
* modify it under the terms of the GNU General Public License
16
* as published by the Free Software Foundation; either version 2
17
* of the License, or (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
27
*
28
* For more information, contact:
29
*
30
*  Generalitat Valenciana
31
*   Conselleria d'Infraestructures i Transport
32
*   Av. Blasco Ib??ez, 50
33
*   46010 VALENCIA
34
*   SPAIN
35
*
36
*      +34 963862235
37
*   gvsig@gva.es
38
*      www.gvsig.gva.es
39
*
40
*    or
41
*
42
*   IVER T.I. S.A
43
*   Salamanca 50
44
*   46005 Valencia
45
*   Spain
46
*
47
*   +34 963163400
48
*   dac@iver.es
49
* 
50
* [01] 01-Oct-2005 nbt New call to JNI function openArrayNat to convert name string to char array.
51
*/
52

    
53
package es.gva.cit.jgdal;
54

    
55

    
56
import java.io.*;
57
import java.util.Date;
58

    
59
//import es.gva.cit.jgdal.GdalException;
60

    
61
/**
62
 * Contiene las funcionalidades necesarias para el acceso a los
63
 * elementos de un dataset de gdal correspondiente a una im?gen 
64
 * 
65
 * @author Nacho Brodin <brodin_ign@gva.es>.<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
66
 * @version 0.0
67
 * @link http://www.gvsig.gva.es
68
 */
69

    
70
public class Gdal extends JNIBase{
71
        
72
                
73
        //CONSTANTES
74
        
75
        //GDALAccess
76
        public static int GA_ReadOnly=0;
77
        public static int GA_Update=1;
78
        
79
        //GDALDataType
80
        public static int GDT_Unknown = 0;
81
        public static int GDT_Byte = 1;                        //Buffer byte (8)
82
        public static int GDT_UInt16 = 2;                //Buffer short (16)
83
        public static int GDT_Int16 = 3;                //Buffer short (16)
84
        public static int GDT_UInt32 = 4;                //Buffer int (32)
85
        public static int GDT_Int32 = 5;                //Buffer int (32)
86
        public static int GDT_Float32 = 6;                //Buffer float (32)
87
        public static int GDT_Float64 = 7;                //Buffer double (64)
88
        public static int GDT_CInt16 = 8;                //Buffer short (16)
89
        public static int GDT_CInt32 = 9;                //Buffer int (32)
90
        public static int GDT_CFloat32 = 10;        //Buffer float (32)
91
        public static int GDT_CFloat64 = 11;        //Buffer double (64)
92
        public static int GDT_TypeCount = 12;
93
        
94
        private String pszFilename = "";
95
                                
96
        private native long openNat(String pszFilename, int access);
97
        private native long openArrayNat(byte[] pszFilename, int access);
98
        private native int getGeoTransformNat(long cPtr,GeoTransform adfgeotransform);
99
        private native int setGeoTransformNat(long cPtr,GeoTransform adfgeotransform);
100
        private native String[] getMetadataNat(long cPtr,String pszDomain);
101
        private native String getProjectionRefNat(long cPtr);
102
        private native void closeNat(long cPtr);
103
        private native int getRasterBandNat(long cPtr,int hBand);
104
        private native int setProjectionNat(long cPtr, String proj);
105
        private native String getDriverShortNameNat(long cPtr);
106
        
107
        //private static native long getGDALDriverManagerNat(); 
108
        private static native long getDriverByNameNat(String name);
109
        private native String getColorInterpretationNameNat(long cPtr, int colorInterp);
110
        
111
        /**
112
         *Constructor a partir de la direcci?n de memoria 
113
         */
114
        
115
        public Gdal(long cPtr){
116
                this.cPtr=cPtr;
117
        }
118
        
119
        /**
120
         *Constructor generico
121
         */
122
        
123
        public Gdal(){}
124
        
125
        /**
126
         * Devuelve la direcci?n de memoria del objeto dataset en C.
127
         */
128
        
129
        public long getPtro(){return cPtr;}
130
        
131
        /**
132
         * Abre el fichero de im?gen.
133
         *
134
         * @param pszFilename        Nombre del fichero.
135
         * @param access        Apertura en solo lectura o escritura.
136
         * @throws GdalException
137
         */
138
        
139
        public void open(String pszFilename, int access)throws GdalException, IOException{
140
                        
141
                File f = new File( pszFilename );
142
                if(!f.exists())
143
                  throw new IOException("El archivo "+pszFilename+" no existe");
144
                  
145
            if(!f.canRead())
146
              throw new IOException("El archivo no puede leerse");
147
        
148
            cPtr=openArrayNat(pszFilename.getBytes(), access);
149
            //cPtr=openNat(pszFilename, access);
150
            
151
            if(cPtr<=0)
152
                    throw new GdalException("Error en Open de GDAL");
153
         
154
        }
155
        
156
        /**
157
         * Obtiene un array de Strings con los metadatos
158
         * 
159
         * @throws GdalException
160
         * @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
161
         */
162
        
163
        public String[] getMetadata()throws GdalException{
164
                
165
                        
166
                if(cPtr <= 0)
167
                        throw new GdalException("Error en GDALGetMetadata(). La llamada GDALOpen no tuvo exito");
168
                String[] res = getMetadataNat(cPtr,null);
169
                if(res == null)
170
                        return new String[0];
171
                else return res;
172
                
173
        }
174
        
175
        /**
176
         * Obtiene el n?mero de bandas de la im?gen
177
         * 
178
         * @throws GdalException
179
         * @param hBand        Entero que corresponde al n?mero de banda que se quiere recuperar
180
         * @return Objeto GdalRasterBand que representa la banda recuperada        
181
         */
182
        
183
        public GdalRasterBand getRasterBand(int hBand)throws GdalException{
184
                
185
                long cPtr_rb;
186
                
187
                if(cPtr <= 0)
188
                        throw new GdalException("Error en GDALGetRasterBand(). La llamada GDALOpen no tuvo exito");
189
                            
190
                cPtr_rb = getRasterBandNat(cPtr,hBand);
191
                
192
                if(cPtr_rb < 0)
193
                        throw new GdalException("Error en GetRasterBand(). No ha podido obtenerse una banda");
194
                else return new GdalRasterBand(cPtr_rb);
195
                                
196
        }
197
        
198
                
199
        
200
        /**
201
         * Obtiene la dimensi?n del raster en el eje X.
202
         * 
203
         * @return        Devuelve un entero con la longitud de la im?gen en el eje X en pixels.
204
         * @throws GdalException 
205
         */
206
        
207
        public int getRasterXSize()throws GdalException{
208
                
209
                String msg1="Error en GDALGetRasterXSize. La llamada GDALOpen no tuvo ?xito";
210
                String msg2="Error en tama?o X";
211
                return baseSimpleFunctions(5,msg1,msg2);
212
                
213
        }
214
                
215
        
216
        /**
217
         * Obtiene la dimensi?n del raster en el eje Y.
218
         * 
219
         * @return        Devuelve un entero con la longitud de la im?gen en el eje Y en pixels.
220
         * @throws GdalException 
221
         */
222
        
223
        public int getRasterYSize()throws GdalException{
224
                
225
                String msg1="Error en GDALGetRasterYSize. La llamada GDALOpen no tuvo ?xito";
226
                String msg2="Error en tama?o Y";
227
                return baseSimpleFunctions(6,msg1,msg2);
228
                
229
        }
230
        
231
        
232
        /**
233
         * Obtiene el n?mero de bandas de la im?gen
234
         * 
235
         * @return        Devuelve un entero con el n?mero de bandas que contiene la im?gen.
236
         * @throws GdalException
237
         */
238
        
239
        public int getRasterCount()throws GdalException{
240
                
241
                String msg1="Error en GDALGetRasterCount. . La llamada GDALOpen no tuvo ?xito";
242
                String msg2="Error en el conteo de n?mero de bandas";
243
                return baseSimpleFunctions(7,msg1,msg2);
244
                
245
        }
246
        
247
        
248
        /**
249
         * Obtiene el vector geoTransform de la im?gen que contiene los valores Origen y pixelSize
250
         * 
251
         * @return        Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
252
         * @throws GdalException
253
         */
254
        
255
        public GeoTransform getGeoTransform()throws GdalException{
256
                
257
                                
258
                if(cPtr <= 0)
259
                        throw new GdalException("Error en getGeoTransform(). La llamada Open no tuvo exito");
260
                
261
                GeoTransform gt=new GeoTransform();
262
                
263
                if(getGeoTransformNat(cPtr,gt)<0)
264
                        throw new GdalException("Error en getGeoTransform(). No se han obtenido valores para geoTransform.");
265
                else{
266
        
267
                        return gt;
268
                }
269
        }
270
        
271
        /**
272
         * Obtiene el Nombre corto asociado al driver del dataset
273
         * 
274
         * @return        Cadena con el nombre del driver
275
         * @throws GdalException
276
         */
277
        
278
        public String getDriverShortName()throws GdalException{
279
                
280
                                
281
                if(cPtr <= 0)
282
                        throw new GdalException("Error en getDriverShortName(). La llamada Open no tuvo exito");
283
                String shortName = getDriverShortNameNat(cPtr);
284
                
285
                if(shortName == null)
286
                        throw new GdalException("Error en getDriverShortName(). No ha podido obtenerse el driver");
287
                else 
288
                        return shortName;
289
                
290
        }
291
        
292
        /**
293
         * A?ade el vector geoTransform a la im?gen que contiene los valores Origen y pixelSize
294
         * 
295
         * @return        Devuelve un vector de doubles que contiene los valores de coordenadas de origen y pixelSize.
296
         * @throws GdalException
297
         */
298
        
299
        public void setGeoTransform(GeoTransform gt)throws GdalException{
300
                
301
                                
302
                if(cPtr <= 0)
303
                        throw new GdalException("Error en setGeoTransform(). La llamada Open no tuvo exito");                
304
                
305
                int res = setGeoTransformNat(cPtr,gt);
306
                
307
                if(res<0)
308
                        throw new GdalException("Error en getGeoTransform(). No se ha podido a?adir valores para geoTransform. Es posible que el formato no lo soporte.");
309
        
310
        }
311
        
312
        /**
313
         * Obtiene el sistema de coordenadas de referencia de la im?gen.
314
         * 
315
         * @return        Devuelve un String con los datos del sistema de coordenadas de referencia.
316
         * @throws GdalException
317
         */
318
        
319
        public String getProjectionRef()throws GdalException{
320
                
321
                if(cPtr <= 0)
322
                        throw new GdalException("Error en getGetProjectionRef(). La llamada Open no tuvo exito");
323
                
324
                
325
                String res = getProjectionRefNat(cPtr);
326
                
327
                if(res == null)return new String("");
328
                else return res;
329
                
330
        }
331
        
332
        /**
333
         * Cierra el fichero de im?gen.
334
         *
335
         * @throws GdalException 
336
         */
337
        
338
        public void close()throws GdalException{
339
                
340
                if(cPtr <= 0)
341
                        throw new GdalException("Error en Close(). La llamada Open no tuvo exito");
342
                closeNat(cPtr);        
343
                
344
        }
345
        
346
        /**
347
         * Obtiene un driver a trav?s de su nombre
348
         * 
349
         * @param name        Nombre del driver
350
         */
351
        
352
        public static GdalDriver getDriverByName(String name)throws GdalException{
353
                
354
                long ptrdrv=-1;
355
                
356
                ptrdrv=getDriverByNameNat(name);
357
                //System.out.println("ptrdrv="+ptrdrv);
358
                if(ptrdrv > 0)
359
                        return (new GdalDriver(ptrdrv));
360
                else 
361
                        throw new GdalException("Error en getDriverByName(). No se ha podido obtener el driver.");
362
                
363
        }
364
        
365
        
366
        /**
367
         * Obtiene el n?mero de bandas de la im?gen
368
         * 
369
         * @return        Devuelve un entero con el n?mero de bandas que contiene la im?gen.
370
         * @throws GdalException
371
         */
372
        
373
        public int getGCPCount()throws GdalException{
374
                
375
                String msg1="Error en GDALGetRasterCount. . La llamada GDALOpen no tuvo ?xito";
376
                String msg2="Error en el conteo de n?mero de bandas";
377
                return baseSimpleFunctions(8,msg1,msg2);
378
                
379
        }
380
        
381
        /**
382
         *Asigna la proyecci?n especificada en la cadena que se le pasa por par?metro.
383
         *@param proj        proyecci?n
384
         *@throws GdalException 
385
         */
386
        
387
        public void setProjection(String proj)throws GdalException{
388
                
389
                if(cPtr <= 0)
390
                        throw new GdalException("Error en setProjection(). La llamada Open no tuvo exito");
391
                
392
                int res = setProjectionNat(cPtr, proj);
393
                
394
                if(res<0)
395
                        throw new GdalException("Error en setProjection(). No se ha podido asignar la proyecci?n.");
396
        }
397
        
398
        /**
399
         * Obtiene la cadena que representa el tipo de banda de color. Los tipos posibles son:
400
         * <UL>
401
         *  <LI>0 = "Undefined" </LI>
402
         *  <LI>1 = "Gray";</LI>
403
         *  <LI>2 = "Palette";</LI>
404
         *  <LI>3 = "Red";</LI>
405
         *  <LI>4 = "Green";</LI>
406
         *  <LI>5 = "Blue";</LI>
407
         *  <LI>6 = "Alpha";</LI>
408
         *  <LI>7 = "Hue";</LI>
409
         *  <LI>8 = "Saturation";</LI>
410
         *  <LI>9 = "Lightness";</LI>
411
         *  <LI>10 = "Cyan";</LI>
412
         *  <LI>11 = "Magenta";</LI>
413
         *  <LI>12 = "Yellow";</LI>
414
         *  <LI>13 = "Black";</LI>
415
         *  <LI>14 = "YCbCr_Y";</LI>
416
         *  <LI>15 = "YCbCr_Cb";</LI>
417
         *  <LI>16 = "YCbCr_Cr";</LI>
418
         * </UL>
419
         * @return        Cadena con el nombre del tipo de banda de color
420
         * @throws GdalException
421
         */
422
        
423
        public String getColorInterpretationName(int colorInterp)throws GdalException{
424
                
425
                if(cPtr <= 0)
426
                        throw new GdalException("Error en getColorInterpretationName(). La llamada Open no tuvo exito");
427
                String bandTypeName = getColorInterpretationNameNat(cPtr, colorInterp);
428
                
429
                if(bandTypeName == null)
430
                        throw new GdalException("Error en getColorInterpretationName(). No ha podido obtenerse el tipo de banda de color");
431
                else 
432
                        return bandTypeName;
433
                
434
        }
435
        
436
}