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 / GdalRasterBand.java @ 3497

History | View | Annotate | Download (15.2 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

    
23
package org.gvsig.jgdal;
24

    
25
import java.util.Vector;
26

    
27
import org.gdal.gdal.Band;
28
import org.gdal.gdal.ColorTable;
29

    
30
/**
31
 * Representa a una banda simple de la im?gen o canal.
32
 * 
33
 * @author Nacho Brodin (nachobrodin@gmail.com).<BR> Equipo de desarrollo gvSIG.<BR> http://www.gvsig.gva.es
34
 * @version 0.0
35
 * @link http://www.gvsig.gva.es
36
 */
37

    
38
public class GdalRasterBand extends Band{
39

    
40

    
41
private boolean hasNoDataValue = false;
42

    
43

    
44
//        private native long getOverviewNat(long cPtr,int i);
45
//        private native long getRasterColorTableNat(long cPtr);
46
//        private native GdalBuffer readRasterNat(long cPtr, 
47
//                        int nXOff, int nYOff, int nXSize, int nYSize,
48
//                        int BufXSize, int BufYSize,
49
//                        int eBufType);
50
//        private native GdalBuffer readRasterWithPaletteNat(long cPtr, 
51
//                        int nXOff, int nYOff, int nXSize, int nYSize,
52
//                        int BufXSize, int BufYSize,
53
//                        int eBufType);
54
//        private native void writeRasterNat(        long cPtr, 
55
//                        int nXOff, int nYOff, int nXSize, int nYSize,
56
//                        GdalBuffer buffer,
57
//                        int eBufType);
58
//        private native double getRasterNoDataValueNat(long cPtr);
59
//        private native int existsNoDataValueNat(long cPtr);
60
//        private native String[] getMetadataNat(long cPtr,String pszDomain);
61
//        private native int getRasterColorInterpretationNat(long cPtr);
62
//        private native int setRasterColorInterpretationNat(long cPtr, int bandType);
63

    
64

    
65
        /**
66
         * Asigna el identificador de la banda
67
         */
68

    
69
         public GdalRasterBand(long cPtr) {
70
                 super(cPtr, true);
71
         }
72

    
73

    
74
         /**
75
          * Lee datos de la banda de la imagen
76
          * 
77
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
78
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
79
          * de la banda accedida.  
80
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
81
          * de la banda accedida.         
82
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
83
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
84
          * @param bufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
85
          * @param bufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
86
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
87
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0. 
88
          * @param eBufType                
89
          */
90

    
91
         public GdalBuffer readRaster(int nXOff, int nYOff, int nXSize, int nYSize,
92
                         int bufXSize, int bufYSize,
93
                         int eBufType)throws GdalException {
94
//                 
95
//                 if (cPtr == 0)
96
//                         throw new GdalException("No se ha podido acceder al archivo.");
97

    
98
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
99
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
100

    
101
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize<1) || (nYSize > getRasterBandYSize()))
102
                         throw new GdalException("Tama?o de ventana incorrecto.");
103

    
104
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
105
                         throw new GdalException("Posicion de la ventana incorrecta.");
106
                 
107
                 if ((eBufType < 1) || (eBufType > 11))
108
                         throw new GdalException("Tipo de datos incorrecto.");
109
                 
110
                 if (bufXSize < 1)
111
                         bufXSize = nXSize;
112
                 
113
                 if (bufYSize < 1)
114
                         bufYSize = nYSize;
115
                 
116
                 GdalBuffer buf = new GdalBuffer();
117
                 int i = 0;
118
                 
119
                 switch(eBufType) {
120
                 case 0:
121
                         i = 0;
122
                         break;
123
                 case 1:
124
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
125
                         break;
126
                 case 2:
127
                 case 3:
128
                 case 8:
129
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
130
                         break;
131
                 case 4:
132
                 case 5:
133
                 case 9:
134
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
135
                         break;
136
                 case 6:
137
                 case 10:
138
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
139
                         break;
140
                 case 7:
141
                 case 11:
142
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
143
                         break;                 
144
                 case 12:
145
                         i = 0;
146
                         break;
147
                 }
148

    
149
                 
150
                 if(i>0)
151
                         return buf;
152
                 else 
153
                         return null;
154
         }
155

    
156
         /**
157
          * Escribe datos en la banda de la imagen
158
          * 
159
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
160
          * de la banda accedida.  
161
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
162
          * de la banda accedida.         
163
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
164
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
165
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
166
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
167
          * @param eBufType
168
          */
169

    
170
         public void writeRaster(int nXOff, int nYOff, int nXSize, int nYSize, GdalBuffer buf, int eBufType)throws GdalException {
171
                 GdalBuffer buffer = new GdalBuffer();
172
                 
173
                 if ((nXOff < 0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
174
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
175

    
176
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
177
                         throw new GdalException("Tama?o de ventana incorrecto.");
178

    
179
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
180
                         throw new GdalException("Posicion de la ventana incorrecta.");
181
                 
182
                 if ((eBufType < 1) || (eBufType > 11))
183
                         throw new GdalException("Tipo de datos incorrecto.");
184
                 
185
                 if (buf == null)
186
                         throw new GdalException("Buffer incorrecto");
187
                 
188
                 switch(eBufType) {
189
                 case 0:
190
                         return;
191
                 case 1:
192
                         buffer.buffByte = buf.buffByte;
193
                         break;
194
                 case 2:
195
                 case 3:
196
                 case 8:
197
                         buffer.buffShort = buf.buffShort;
198
                         break;
199
                 case 4:
200
                 case 5:
201
                 case 9:
202
                         buffer.buffInt = buf.buffInt;
203
                         break;
204
                 case 6:
205
                 case 10:
206
                         buffer.buffFloat = buf.buffFloat;
207
                         break;
208
                 case 7:
209
                 case 11:
210
                         buffer.buffDouble = buf.buffDouble;
211
                         break;                 
212
                 case 12:
213
                         return;
214
                 }
215

    
216
                 
217
                 
218
                 switch(eBufType) {
219
                 case 0:
220
                         break;
221
                 case 1:
222
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffByte);
223
                         break;
224
                 case 2:
225
                 case 3:
226
                 case 8:
227
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffShort);
228
                         break;
229
                 case 4:
230
                 case 5:
231
                 case 9:
232
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffInt);
233
                         break;
234
                 case 6:
235
                 case 10:
236
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffFloat);
237
                         break;
238
                 case 7:
239
                 case 11:
240
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffDouble);
241
                         break;                 
242
                 case 12:
243
                         break;
244
                 }
245
         }
246

    
247
         /**
248
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las X
249
          *@return Tama?o en pixeles del eje X
250
          *@throws GdalException 
251
          */
252

    
253
         public int getRasterBandXSize()throws GdalException {
254
                 return this.GetXSize();
255
         }
256

    
257
         /**
258
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las Y
259
          *@return Tama?o en pixeles del eje Y
260
          *@throws GdalException 
261
          */
262

    
263
         public int getRasterBandYSize()throws GdalException {
264
                 return this.GetYSize();
265
         }
266

    
267

    
268
         /**
269
          * Devuelve el n?mero de overviews que contiene la banda.
270
          * @return N?mero de overviews
271
          * @throws GdalException 
272
          */
273

    
274
         public int getOverviewCount()throws GdalException {
275
                 return this.GetOverviewCount();
276
         }
277

    
278

    
279
         /**
280
          * Obtiene el overview indicado por el ?ndice "i".
281
          * 
282
          * @param i        indice del overview que se quiere recuperar.
283
          * @return GdalRasterBand        Banda correspondiente al overview selecccionado
284
          * @throws GdalException 
285
          */
286

    
287
         public GdalRasterBand getOverview(int i)throws GdalException {
288
                 Band cPtr_ov;
289

    
290
                 if((i < 0) || (i >= getOverviewCount()))
291
                         throw new GdalException("El overview seleccionado no existe");
292

    
293
                 cPtr_ov = this.GetOverview(i);
294
                 
295
                 if (cPtr_ov == null || !(cPtr_ov instanceof GdalRasterBand))
296
                         throw new GdalException("No se ha podido obtener el overview");
297

    
298
                 return (GdalRasterBand)cPtr_ov;
299
         }
300

    
301

    
302
         /**
303
          * Devuelve el tama?o en X del bloque para esa banda
304
          * @return Tama?o en pixeles del bloque en el eje X
305
          * @throws GdalException 
306
          */
307

    
308
         public int getBlockXSize()throws GdalException {
309
                 return this.GetBlockXSize();
310
         }
311

    
312

    
313
         /**
314
          * Devuelve el tama?o en Y del bloque para esa banda
315
          * @return Tama?o en pixeles del bloque en el eje Y
316
          * @throws GdalException 
317
          */
318

    
319
         public int getBlockYSize()throws GdalException {
320
                 return this.GetBlockYSize();
321
         }
322

    
323
         /**
324
          * Devuelve el tipo de datos de la banda
325
          * @return Tama?o en pixeles del bloque en el eje Y
326
          * @throws GdalException 
327
          */
328

    
329
         public int getRasterDataType()throws GdalException {
330
                 return this.getDataType();
331
         }
332

    
333
         /**
334
          * Obtiene la tabla de color asociada a la imagen
335
          */
336
         public GdalColorTable getRasterColorTable()throws GdalException {
337
                 GdalColorTable gct = null;
338
                 
339
//                 if (cPtr == 0)
340
//                         throw new GdalException("No se ha podido acceder al archivo.");
341
                 
342
                 ColorTable ct = this.GetColorTable();
343
                 
344
                 if ((ct == null))
345
                         return null;
346
                 
347
                 gct = ((GdalColorTable)ct);
348

    
349
                 return gct;
350
         }
351

    
352
         /**
353
          * Lee datos de la banda de la im?gen con una paleta asociada
354
          * 
355
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
356
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
357
          * de la banda accedida.  
358
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
359
          * de la banda accedida.         
360
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
361
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
362
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
363
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
364
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
365
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0. 
366
          * @param eBufType                
367
          */
368

    
369
         public GdalBuffer readRasterWithPalette(int nXOff, int nYOff, int nXSize, int nYSize,
370
                         int bufXSize, int bufYSize,
371
                         int eBufType)throws GdalException {
372
                 
373
//                 if (cPtr == 0)
374
//                         throw new GdalException("No se ha podido acceder al archivo.");
375
                 
376
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
377
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
378

    
379
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
380
                         throw new GdalException("Tama?o de ventana incorrecto.");
381

    
382
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
383
                         throw new GdalException("Posicion de la ventana incorrecta.");
384
                 
385
                 if ((eBufType < 1) || (eBufType > 11))
386
                         throw new GdalException("Tipo de datos incorrecto.");
387
                 
388
                 if (bufXSize < 1)
389
                         bufXSize = nXSize;
390
                 
391
                 if (bufYSize < 1)
392
                         bufYSize = nYSize;
393
                 
394
                 
395
                 GdalBuffer buf = new GdalBuffer();
396
                 int i = 0;
397
                 
398
                 switch(eBufType) {
399
                 case 0:
400
                         i = 0;
401
                         break;
402
                 case 1:
403
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
404
                         break;
405
                 case 2:
406
                 case 3:
407
                 case 8:
408
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
409
                         break;
410
                 case 4:
411
                 case 5:
412
                 case 9:
413
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
414
                         break;
415
                 case 6:
416
                 case 10:
417
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
418
                         break;
419
                 case 7:
420
                 case 11:
421
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
422
                         break;                 
423
                 case 12:
424
                         i = 0;
425
                         break;
426
                 }
427

    
428
                 
429
                 if(i>0)
430
                         return buf;
431
                 else 
432
                         return null;
433
         }
434

    
435
         /**
436
          *Devuelve el valor de NoData
437
          */
438
         public double getRasterNoDataValue()throws GdalException {
439
//                 if (cPtr == 0)
440
//                         throw new GdalException("No se ha podido acceder al archivo.");
441
                 
442
                 Double[] aux = new Double[1];
443
                 GetNoDataValue(null);
444
                 return aux[0];
445
         }
446
         
447
         /**
448
          * Obtiene el valorDevuelve el valor de NoData
449
          */
450
         public boolean existsNoDataValue()throws GdalException {
451
//                 if (cPtr == 0)
452
//                         throw new GdalException("No se ha podido acceder al archivo.");
453
                 
454
//                 int result = this.existsNoDataValueNat(cPtr);
455
                
456
                         return this.hasNoDataValue  ;
457
         }
458
         
459
         public void setRasterNoDataValue(double val){
460
                 this.hasNoDataValue = true;
461
                 
462
                 SetNoDataValue(val);
463
         }
464

    
465
         /**
466
          * Obtiene un array de Strings con los metadatos
467
          * 
468
          * @throws GdalException
469
          * @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
470
          */
471

    
472
         public String[] getMetadata()throws GdalException {
473
//                 if (cPtr == 0)
474
//                         throw new GdalException("No se ha podido acceder al archivo.");
475
                 
476
                 Vector<String> aux = this.GetMetadata_List();
477
                 String[] res =  aux.toArray(new String[0]);
478
                 if(res == null)
479
                         return new String[0];
480
                 else return res;
481
         }
482

    
483
         /**
484
          * Obtiene identificador que representa el tipo de banda de color. 
485
          * @return        identificador del tipo de banda de color
486
          * @throws GdalException
487
          */
488

    
489
         public int getRasterColorInterpretation()throws GdalException {
490
//                 if (cPtr == 0)
491
//                         throw new GdalException("No se ha podido acceder al archivo.");
492

    
493
                 int bandType = this.GetColorInterpretation();
494
                 return bandType;                
495
         }
496

    
497

    
498
         /**
499
          * Asigna la interpretaci?n de color de la banda.
500
          * Con algunos formatos no es posible modificar la interpretaci?n de color, 
501
          * tales como tiff y jpg. En el caso de tif, no hay error pero tampoco se
502
          * produce el cambio en la interpretaci?n. En el caso de jpg, gdal lanza un error.
503
          * 0 = "Undefined"
504
          * 1 = "Gray";
505
          * 2 = "Palette";
506
          * 3 = "Red";
507
          * 4 = "Green";
508
          * 5 = "Blue";
509
          * 6 = "Alpha";
510
          * 7 = "Hue";
511
          * 8 = "Saturation";
512
          * 9 = "Lightness";
513
          * 10 = "Cyan";
514
          * 11 = "Magenta";
515
          * 12 = "Yellow";
516
          * 13 = "Black";
517
          * 14 = "YCbCr_Y";
518
          * 15 = "YCbCr_Cb";
519
          * 16 = "YCbCr_Cr";
520
          * @param bandType
521
          * @throws GdalException
522
          */
523
         public void setRasterColorInterpretation(int bandType) throws GdalException{
524
//                 if (cPtr == 0)
525
//                         throw new GdalException("No se ha podido acceder al archivo.");
526
                 
527
                 if ((bandType < 0) || (bandType > 16)){
528
                         throw new GdalException("Tipo de banda incorrecto");
529
                 }
530
                 
531
                 int err = this.SetColorInterpretation(bandType);
532
                 
533
         }
534

    
535
}