Statistics
| Revision:

svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / libraries / libjni-gdal / src / es / gva / cit / jgdal / Gdal.java @ 5222

History | View | Annotate | Download (10.5 KB)

1
/**********************************************************************
2
 * $Id: Gdal.java 5222 2006-05-16 15:40:43Z  $
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
        
95
        private String pszFilename = "";
96
                                
97

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