Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libjni-gdal / src / main / java / es / gva / cit / jgdal / GdalRasterBand.java @ 21213

History | View | Annotate | Download (14.8 KB)

1
/**********************************************************************
2
 * $Id: GdalRasterBand.java 15691 2007-10-31 10:49:53Z nbrodin $
3
 *
4
 * Name:     GdalRasterBand.java
5
 * Project:  JGDAL. Interface java to gdal (Frank Warmerdam).
6
 * Purpose:  Basic Funcions about raster bands. 
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

    
51
package es.gva.cit.jgdal;
52

    
53
/**
54
 * Representa a una banda simple de la im?gen o canal.
55
 * 
56
 * @author Nacho Brodin <brodin_ign@gva.es>.<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
57
 * @version 0.0
58
 * @link http://www.gvsig.gva.es
59
 */
60

    
61
public class GdalRasterBand extends JNIBase{
62

    
63

    
64
        private native long getOverviewNat(long cPtr,int i);
65
        private native long getRasterColorTableNat(long cPtr);
66
        private native GdalBuffer readRasterNat(long cPtr, 
67
                        int nXOff, int nYOff, int nXSize, int nYSize,
68
                        int BufXSize, int BufYSize,
69
                        int eBufType);
70
        private native GdalBuffer readRasterWithPaletteNat(long cPtr, 
71
                        int nXOff, int nYOff, int nXSize, int nYSize,
72
                        int BufXSize, int BufYSize,
73
                        int eBufType);
74
        private native void writeRasterNat(        long cPtr, 
75
                        int nXOff, int nYOff, int nXSize, int nYSize,
76
                        GdalBuffer buffer,
77
                        int eBufType);
78
        private native double getRasterNoDataValueNat(long cPtr);
79
        private native int existsNoDataValueNat(long cPtr);
80
        private native String[] getMetadataNat(long cPtr,String pszDomain);
81
        private native int getRasterColorInterpretationNat(long cPtr);
82
        private native int setRasterColorInterpretationNat(long cPtr, int bandType);
83

    
84

    
85
        /**
86
         * Asigna el identificador de la banda
87
         */
88

    
89
         public GdalRasterBand(long cPtr) {
90
                 this.cPtr=cPtr;
91
         }
92

    
93

    
94
         /**
95
          * Lee datos de la banda de la im?gen
96
          * 
97
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
98
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
99
          * de la banda accedida.  
100
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
101
          * de la banda accedida.         
102
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
103
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
104
          * @param bufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
105
          * @param bufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
106
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
107
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0. 
108
          * @param eBufType                
109
          */
110

    
111
         public GdalBuffer readRaster(int nXOff, int nYOff, int nXSize, int nYSize,
112
                         int bufXSize, int bufYSize,
113
                         int eBufType)throws GdalException {
114
                 
115
                 if (cPtr == 0)
116
                         throw new GdalException("No se ha podido acceder al archivo.");
117

    
118
                 if ((nXOff<0) || (nXOff > baseSimpleFunctions(5, "", "")) || (nYOff < 0) || (nYOff > baseSimpleFunctions(6, "", "")))
119
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
120

    
121
                 if ((nXSize < 1) || (nXSize > baseSimpleFunctions(5, "", "")) || (nYSize<1) || (nYSize > baseSimpleFunctions(6, "", "")))
122
                         throw new GdalException("Tama?o de ventana incorrecto.");
123

    
124
                 if (((nXSize + nXOff) > (baseSimpleFunctions(5, "", ""))) || ((nYSize + nYOff) > (baseSimpleFunctions(6, "", ""))))
125
                         throw new GdalException("Posicion de la ventana incorrecta.");
126
                 
127
                 if ((eBufType < 1) || (eBufType > 11))
128
                         throw new GdalException("Tipo de datos incorrecto.");
129
                 
130
                 if (bufXSize < 1)
131
                         bufXSize = nXSize;
132
                 
133
                 if (bufYSize < 1)
134
                         bufYSize = nYSize;
135
                 
136
                 GdalBuffer buffer = readRasterNat(cPtr, nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType);
137
                 if(buffer!=null)
138
                         return buffer;
139
                 else 
140
                         return null;
141
         }
142

    
143
         /**
144
          * Escribe datos en la banda de la im?gen
145
          * 
146
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
147
          * de la banda accedida.  
148
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
149
          * de la banda accedida.         
150
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
151
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
152
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
153
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
154
          * @param eBufType
155
          */
156

    
157
         public void writeRaster(int nXOff, int nYOff, int nXSize, int nYSize, GdalBuffer buf, int eBufType)throws GdalException{
158
                 GdalBuffer buffer=new GdalBuffer();
159
                 
160
                 if ((nXOff<0) || (nXOff > baseSimpleFunctions(5, "", "")) || (nYOff < 0) || (nYOff > baseSimpleFunctions(6, "", "")))
161
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
162

    
163
                 if ((nXSize < 1) || (nXSize > baseSimpleFunctions(5, "", "")) || (nYSize<1) || (nYSize > baseSimpleFunctions(6, "", "")))
164
                         throw new GdalException("Tama?o de ventana incorrecto.");
165

    
166
                 if (((nXSize + nXOff) > (baseSimpleFunctions(5, "", ""))) || ((nYSize + nYOff) > (baseSimpleFunctions(6, "", ""))))
167
                         throw new GdalException("Posicion de la ventana incorrecta.");
168
                 
169
                 if ((eBufType < 1) || (eBufType > 11))
170
                         throw new GdalException("Tipo de datos incorrecto.");
171
                 
172
                 if (buf == null)
173
                         throw new GdalException("Buffer incorrecto");
174
                 
175
                 switch(eBufType){
176
                 case 0:
177
                         return;
178
                 case 1:
179
                         buffer.buffByte=buf.buffByte;
180
                         break;
181
                 case 2:
182
                 case 3:
183
                 case 8:
184
                         buffer.buffShort=buf.buffShort;
185
                         break;
186
                 case 4:
187
                 case 5:
188
                 case 9:
189
                         buffer.buffInt=buf.buffInt;
190
                         break;
191
                 case 6:
192
                 case 10:
193
                         buffer.buffFloat=buf.buffFloat;
194
                         break;
195
                 case 7:
196
                 case 11:
197
                         buffer.buffDouble=buf.buffDouble;
198
                         break;                 
199
                 case 12:
200
                         return;
201
                 }
202

    
203
                 writeRasterNat(cPtr, nXOff, nYOff, nXSize, nYSize, buffer, eBufType);                  
204
         }
205

    
206
         /**
207
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las X
208
          *@return Tama?o en pixeles del eje X
209
          *@throws GdalException 
210
          */
211

    
212
         public int getRasterBandXSize()throws GdalException {
213
                 String msg1="Error en getRasterBandXSize(). La llamada getRasterBand no tuvo exito";
214
                 String msg2="Tama?o de banda erroneo devuelto por GetRasterBandXSize";
215

    
216
                 return baseSimpleFunctions(0,msg1,msg2);
217
         }
218

    
219
         /**
220
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las Y
221
          *@return Tama?o en pixeles del eje Y
222
          *@throws GdalException 
223
          */
224

    
225
         public int getRasterBandYSize()throws GdalException {
226
                 String msg1="Error en getRasterBandYSize(). La llamada getRasterBand no tuvo exito";
227
                 String msg2="Tama?o de banda erroneo devuelto por GetRasterBandYSize";
228

    
229
                 return baseSimpleFunctions(1,msg1,msg2);
230
         }
231

    
232

    
233
         /**
234
          * Devuelve el n?mero de overviews que contiene la banda.
235
          * @return N?mero de overviews
236
          * @throws GdalException 
237
          */
238

    
239
         public int getOverviewCount()throws GdalException {
240
                 String msg1="Error en getOverviewCount(). La llamada getRasterBand no tuvo exito";                
241
                 String msg2="Error al obtener el n?mero de overviews";
242

    
243
                 return baseSimpleFunctions(2,msg1,msg2);
244
         }
245

    
246

    
247
         /**
248
          * Obtiene el overview indicado por el ?ndice "i".
249
          * 
250
          * @param i        indice del overview que se quiere recuperar.
251
          * @return GdalRasterBand        Banda correspondiente al overview selecccionado
252
          * @throws GdalException 
253
          */
254

    
255
         public GdalRasterBand getOverview(int i)throws GdalException {
256
                 long cPtr_ov;
257

    
258
                 if((i < 0) || (i >= getOverviewCount()))
259
                         throw new GdalException("El overview seleccionado no existe");
260

    
261
                 cPtr_ov = getOverviewNat(cPtr,i);
262
                 
263
                 if (cPtr_ov == 0)
264
                         throw new GdalException("No se ha podido obtener el overview");
265

    
266
                 return new GdalRasterBand(cPtr_ov);
267
         }
268

    
269

    
270
         /**
271
          * Devuelve el tama?o en X del bloque para esa banda
272
          * @return Tama?o en pixeles del bloque en el eje X
273
          * @throws GdalException 
274
          */
275

    
276
         public int getBlockXSize()throws GdalException {
277
                 String msg1="Error en getBlockXSize(). La llamada getRasterBand no tuvo exito";
278
                 String msg2="Tama?o de bloque erroneo devuelto por GetBlockXSize";
279

    
280
                 return baseSimpleFunctions(3,msg1,msg2);
281
         }
282

    
283

    
284
         /**
285
          * Devuelve el tama?o en Y del bloque para esa banda
286
          * @return Tama?o en pixeles del bloque en el eje Y
287
          * @throws GdalException 
288
          */
289

    
290
         public int getBlockYSize()throws GdalException {
291
                 String msg1="Error en getBlockXSize(). La llamada getRasterBand no tuvo exito";
292
                 String msg2="Tama?o de bloque erroneo devuelto por GetBlockYSize";
293

    
294
                 return baseSimpleFunctions(4,msg1,msg2);
295
         }
296

    
297
         /**
298
          * Devuelve el tipo de datos de la banda
299
          * @return Tama?o en pixeles del bloque en el eje Y
300
          * @throws GdalException 
301
          */
302

    
303
         public int getRasterDataType()throws GdalException {
304
                 String msg1="Error en getRasterDataType(). La llamada getRasterBand no tuvo exito";
305
                 String msg2="Tipo de dato devuelto por GetRasterDataType erroneo";
306

    
307
                 return baseSimpleFunctions(9,msg1,msg2);
308
         }
309

    
310
         /**
311
          * Obtiene la tabla de color asociada a la imagen
312
          */
313
         public GdalColorTable getRasterColorTable()throws GdalException {
314
                 GdalColorTable gct = null;
315
                 
316
                 if (cPtr == 0)
317
                         throw new GdalException("No se ha podido acceder al archivo.");
318
                 
319
                 long cPtr_ct = getRasterColorTableNat(cPtr);
320
                 
321
                 if ((cPtr_ct == 0) || (cPtr_ct == -1))
322
                         return null;
323
                 
324
                 gct = new GdalColorTable(cPtr_ct);
325

    
326
                 return gct;
327
         }
328

    
329
         /**
330
          * Lee datos de la banda de la im?gen con una paleta asociada
331
          * 
332
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
333
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
334
          * de la banda accedida.  
335
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
336
          * de la banda accedida.         
337
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
338
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
339
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
340
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
341
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
342
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0. 
343
          * @param eBufType                
344
          */
345

    
346
         public GdalBuffer readRasterWithPalette(int nXOff, int nYOff, int nXSize, int nYSize,
347
                         int bufXSize, int bufYSize,
348
                         int eBufType)throws GdalException {
349
                 
350
                 if (cPtr == 0)
351
                         throw new GdalException("No se ha podido acceder al archivo.");
352
                 
353
                 if ((nXOff<0) || (nXOff > baseSimpleFunctions(5, "", "")) || (nYOff < 0) || (nYOff > baseSimpleFunctions(6, "", "")))
354
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
355

    
356
                 if ((nXSize < 1) || (nXSize > baseSimpleFunctions(5, "", "")) || (nYSize<1) || (nYSize > baseSimpleFunctions(6, "", "")))
357
                         throw new GdalException("Tama?o de ventana incorrecto.");
358

    
359
                 if (((nXSize + nXOff) > (baseSimpleFunctions(5, "", ""))) || ((nYSize + nYOff) > (baseSimpleFunctions(6, "", ""))))
360
                         throw new GdalException("Posicion de la ventana incorrecta.");
361
                 
362
                 if ((eBufType < 1) || (eBufType > 11))
363
                         throw new GdalException("Tipo de datos incorrecto.");
364
                 
365
                 if (bufXSize < 1)
366
                         bufXSize = nXSize;
367
                 
368
                 if (bufYSize < 1)
369
                         bufYSize = nYSize;
370
                 
371
                 
372
                 GdalBuffer buffer = readRasterWithPaletteNat(cPtr, nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType);
373

    
374

    
375
                 if(buffer!=null)
376
                         return buffer;
377
                 else 
378
                         return null;
379
         }
380

    
381
         /**
382
          *Devuelve el valor de NoData
383
          */
384
         public double getRasterNoDataValue()throws GdalException {
385
                 if (cPtr == 0)
386
                         throw new GdalException("No se ha podido acceder al archivo.");
387
                 
388
                 return getRasterNoDataValueNat(cPtr);
389
         }
390
         
391
         /**
392
          * Obtiene el valorDevuelve el valor de NoData
393
          */
394
         public boolean existsNoDataValue()throws GdalException {
395
                 if (cPtr == 0)
396
                         throw new GdalException("No se ha podido acceder al archivo.");
397
                 
398
                 int result = existsNoDataValueNat(cPtr);
399
                 
400
                 if(result == 1)
401
                         return true;
402
                 else 
403
                         return false;
404
         }
405

    
406
         /**
407
          * Obtiene un array de Strings con los metadatos
408
          * 
409
          * @throws GdalException
410
          * @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
411
          */
412

    
413
         public String[] getMetadata()throws GdalException {
414
                 if (cPtr == 0)
415
                         throw new GdalException("No se ha podido acceder al archivo.");
416
                 
417
                 String[] res = getMetadataNat(cPtr,null);
418
                 if(res == null)
419
                         return new String[0];
420
                 else return res;
421
         }
422

    
423
         /**
424
          * Obtiene identificador que representa el tipo de banda de color. 
425
          * @return        identificador del tipo de banda de color
426
          * @throws GdalException
427
          */
428

    
429
         public int getRasterColorInterpretation()throws GdalException {
430
                 if (cPtr == 0)
431
                         throw new GdalException("No se ha podido acceder al archivo.");
432

    
433
                 int bandType = getRasterColorInterpretationNat(cPtr);
434
                 return bandType;                
435
         }
436

    
437

    
438
         /**
439
          * Asigna la interpretaci?n de color de la banda.
440
          * Con algunos formatos no es posible modificar la interpretaci?n de color, 
441
          * tales como tiff y jpg. En el caso de tif, no hay error pero tampoco se
442
          * produce el cambio en la interpretaci?n. En el caso de jpg, gdal lanza un error.
443
          * 0 = "Undefined"
444
          * 1 = "Gray";
445
          * 2 = "Palette";
446
          * 3 = "Red";
447
          * 4 = "Green";
448
          * 5 = "Blue";
449
          * 6 = "Alpha";
450
          * 7 = "Hue";
451
          * 8 = "Saturation";
452
          * 9 = "Lightness";
453
          * 10 = "Cyan";
454
          * 11 = "Magenta";
455
          * 12 = "Yellow";
456
          * 13 = "Black";
457
          * 14 = "YCbCr_Y";
458
          * 15 = "YCbCr_Cb";
459
          * 16 = "YCbCr_Cr";
460
          * @param bandType
461
          * @throws GdalException
462
          */
463
         public void setRasterColorInterpretation(int bandType) throws GdalException{
464
                 if (cPtr == 0)
465
                         throw new GdalException("No se ha podido acceder al archivo.");
466
                 
467
                 if ((bandType < 0) || (bandType > 16)){
468
                         throw new GdalException("Tipo de banda incorrecto");
469
                 }
470
                 
471
                 int err = setRasterColorInterpretationNat(cPtr, bandType);
472
                 
473
         }
474

    
475
}