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

History | View | Annotate | Download (16 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
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

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

    
40
public class GdalRasterBand extends Band{
41

    
42
        public static final Logger logger = LoggerFactory.getLogger(GdalRasterBand.class);
43

    
44
private boolean hasNoDataValue = false;
45

    
46

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

    
67

    
68
        /**
69
         * Asigna el identificador de la banda
70
         */
71

    
72
         public GdalRasterBand(long cPtr) {
73
                 super(cPtr, true);
74
         }
75

    
76
         public GdalRasterBand(Band band) {
77
                 super(Band.getCPtr(band), true);
78
         }
79

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

    
97
         public GdalBuffer readRaster(int nXOff, int nYOff, int nXSize, int nYSize,
98
                         int bufXSize, int bufYSize,
99
                         int eBufType)throws GdalException {
100
//                 
101
//                 if (cPtr == 0)
102
//                         throw new GdalException("No se ha podido acceder al archivo.");
103

    
104
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
105
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
106

    
107
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize<1) || (nYSize > getRasterBandYSize()))
108
                         throw new GdalException("Tama?o de ventana incorrecto.");
109
                 
110
                 logger.error("Posicion de la ventana incorrecta. (" + nXSize + " + " + nXOff + ") > " + getRasterBandXSize() +
111
                                 " || (" + nYSize + " + " + nYOff + ") > " +  getRasterBandYSize());
112
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize()))){
113
                         nYOff = 0;
114
                         logger.error("-----------------Error---------------------");
115
                         throw new GdalException("Posicion de la ventana incorrecta.");
116
                 }
117
                 
118
                 if ((eBufType < 1) || (eBufType > 11))
119
                         throw new GdalException("Tipo de datos incorrecto.");
120
                 
121
                 if (bufXSize < 1)
122
                         bufXSize = nXSize;
123
                 
124
                 if (bufYSize < 1)
125
                         bufYSize = nYSize;
126
                 
127
                 GdalBuffer buf = new GdalBuffer();
128
                 int i = 0;
129
                 
130
                 switch(eBufType) {
131
                 case 0:
132
                         i = 0;
133
                         break;
134
                 case 1:
135
                         buf.reservaByte(bufXSize * bufYSize);
136
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
137
                         break;
138
                 case 2:
139
                 case 3:
140
                 case 8:
141
                         buf.reservaShort(bufXSize * bufYSize);
142
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
143
                         break;
144
                 case 4:
145
                 case 5:
146
                 case 9:
147
                         buf.reservaInt(bufXSize * bufYSize);
148
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
149
                         break;
150
                 case 6:
151
                 case 10:
152
                         buf.reservaFloat(bufXSize * bufYSize);
153
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
154
                         break;
155
                 case 7:
156
                 case 11:
157
                         buf.reservaDouble(bufXSize * bufYSize);
158
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
159
                         break;                 
160
                 case 12:
161
                         i = 0;
162
                         break;
163
                 }
164

    
165
                 
166
                 if(i>=0)
167
                         return buf;
168
                 else 
169
                         return null;
170
         }
171

    
172
         /**
173
          * Escribe datos en la banda de la imagen
174
          * 
175
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
176
          * de la banda accedida.  
177
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
178
          * de la banda accedida.         
179
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
180
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
181
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
182
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
183
          * @param eBufType
184
          */
185

    
186
         public void writeRaster(int nXOff, int nYOff, int nXSize, int nYSize, GdalBuffer buf, int eBufType)throws GdalException {
187
                 GdalBuffer buffer = new GdalBuffer();
188
                 
189
                 if ((nXOff < 0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
190
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
191

    
192
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
193
                         throw new GdalException("Tama?o de ventana incorrecto.");
194

    
195
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
196
                         throw new GdalException("Posicion de la ventana incorrecta.");
197
                 
198
                 if ((eBufType < 1) || (eBufType > 11))
199
                         throw new GdalException("Tipo de datos incorrecto.");
200
                 
201
                 if (buf == null)
202
                         throw new GdalException("Buffer incorrecto");
203
                 
204
                 switch(eBufType) {
205
                 case 0:
206
                         return;
207
                 case 1:
208
                         buffer.buffByte = buf.buffByte;
209
                         break;
210
                 case 2:
211
                 case 3:
212
                 case 8:
213
                         buffer.buffShort = buf.buffShort;
214
                         break;
215
                 case 4:
216
                 case 5:
217
                 case 9:
218
                         buffer.buffInt = buf.buffInt;
219
                         break;
220
                 case 6:
221
                 case 10:
222
                         buffer.buffFloat = buf.buffFloat;
223
                         break;
224
                 case 7:
225
                 case 11:
226
                         buffer.buffDouble = buf.buffDouble;
227
                         break;                 
228
                 case 12:
229
                         return;
230
                 }
231

    
232
                 
233
                 
234
                 switch(eBufType) {
235
                 case 0:
236
                         break;
237
                 case 1:
238
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffByte);
239
                         break;
240
                 case 2:
241
                 case 3:
242
                 case 8:
243
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffShort);
244
                         break;
245
                 case 4:
246
                 case 5:
247
                 case 9:
248
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffInt);
249
                         break;
250
                 case 6:
251
                 case 10:
252
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffFloat);
253
                         break;
254
                 case 7:
255
                 case 11:
256
                         this.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffDouble);
257
                         break;                 
258
                 case 12:
259
                         break;
260
                 }
261
         }
262

    
263
         /**
264
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las X
265
          *@return Tama?o en pixeles del eje X
266
          *@throws GdalException 
267
          */
268

    
269
         public int getRasterBandXSize()throws GdalException {
270
                 return this.GetXSize();
271
         }
272

    
273
         /**
274
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las Y
275
          *@return Tama?o en pixeles del eje Y
276
          *@throws GdalException 
277
          */
278

    
279
         public int getRasterBandYSize()throws GdalException {
280
                 return this.GetYSize();
281
         }
282

    
283

    
284
         /**
285
          * Devuelve el n?mero de overviews que contiene la banda.
286
          * @return N?mero de overviews
287
          * @throws GdalException 
288
          */
289

    
290
         public int getOverviewCount()throws GdalException {
291
                 return this.GetOverviewCount();
292
         }
293

    
294

    
295
         /**
296
          * Obtiene el overview indicado por el ?ndice "i".
297
          * 
298
          * @param i        indice del overview que se quiere recuperar.
299
          * @return GdalRasterBand        Banda correspondiente al overview selecccionado
300
          * @throws GdalException 
301
          */
302

    
303
         public GdalRasterBand getOverview(int i)throws GdalException {
304
                 Band cPtr_ov;
305

    
306
                 if((i < 0) || (i >= getOverviewCount()))
307
                         throw new GdalException("El overview seleccionado no existe");
308

    
309
                 cPtr_ov = this.GetOverview(i);
310
                 
311
                 if (cPtr_ov == null || !(cPtr_ov instanceof Band))
312
                         throw new GdalException("No se ha podido obtener el overview");
313

    
314
                 return new GdalRasterBand(Band.getCPtr(cPtr_ov));
315
         }
316

    
317

    
318
         /**
319
          * Devuelve el tama?o en X del bloque para esa banda
320
          * @return Tama?o en pixeles del bloque en el eje X
321
          * @throws GdalException 
322
          */
323

    
324
         public int getBlockXSize()throws GdalException {
325
                 return this.GetBlockXSize();
326
         }
327

    
328

    
329
         /**
330
          * Devuelve el tama?o en Y del bloque para esa banda
331
          * @return Tama?o en pixeles del bloque en el eje Y
332
          * @throws GdalException 
333
          */
334

    
335
         public int getBlockYSize()throws GdalException {
336
                 return this.GetBlockYSize();
337
         }
338

    
339
         /**
340
          * Devuelve el tipo de datos de la banda
341
          * @return Tama?o en pixeles del bloque en el eje Y
342
          * @throws GdalException 
343
          */
344

    
345
         public int getRasterDataType()throws GdalException {
346
                 return this.getDataType();
347
         }
348

    
349
         /**
350
          * Obtiene la tabla de color asociada a la imagen
351
          */
352
         public GdalColorTable getRasterColorTable()throws GdalException {
353
//                 if (cPtr == 0)
354
//                         throw new GdalException("No se ha podido acceder al archivo.");
355
                 
356
                 ColorTable ct = this.GetColorTable();
357
                 
358
                 if ((ct == null))
359
                         return null;
360
                 //throw new GdalException("No se ha podido acceder al archivo.");
361
                 
362
                 return new GdalColorTable(GdalColorTable.getCPtr(ct));
363
         }
364

    
365
         /**
366
          * Lee datos de la banda de la im?gen con una paleta asociada
367
          * 
368
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
369
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
370
          * de la banda accedida.  
371
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
372
          * de la banda accedida.         
373
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
374
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
375
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
376
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
377
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
378
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0. 
379
          * @param eBufType                
380
          */
381

    
382
         public GdalBuffer readRasterWithPalette(int nXOff, int nYOff, int nXSize, int nYSize,
383
                         int bufXSize, int bufYSize,
384
                         int eBufType)throws GdalException {
385
                 
386
//                 if (cPtr == 0)
387
//                         throw new GdalException("No se ha podido acceder al archivo.");
388
                 
389
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
390
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
391

    
392
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
393
                         throw new GdalException("Tama?o de ventana incorrecto.");
394

    
395
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
396
                         throw new GdalException("Posicion de la ventana incorrecta.");
397
                 
398
                 if ((eBufType < 1) || (eBufType > 11))
399
                         throw new GdalException("Tipo de datos incorrecto.");
400
                 
401
                 if (bufXSize < 1)
402
                         bufXSize = nXSize;
403
                 
404
                 if (bufYSize < 1)
405
                         bufYSize = nYSize;
406
                 
407
                 
408
                 GdalBuffer buf = new GdalBuffer();
409
                 int i = 0;
410
                 
411
                 switch(eBufType) {
412
                 case 0:
413
                         i = 0;
414
                         break;
415
                 case 1:
416
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
417
                         break;
418
                 case 2:
419
                 case 3:
420
                 case 8:
421
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
422
                         break;
423
                 case 4:
424
                 case 5:
425
                 case 9:
426
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
427
                         break;
428
                 case 6:
429
                 case 10:
430
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
431
                         break;
432
                 case 7:
433
                 case 11:
434
                         i = this.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
435
                         break;                 
436
                 case 12:
437
                         i = 0;
438
                         break;
439
                 }
440

    
441
                 
442
                 if(i>0)
443
                         return buf;
444
                 else 
445
                         return null;
446
         }
447

    
448
         /**
449
          *Devuelve el valor de NoData
450
          */
451
         public double getRasterNoDataValue()throws GdalException {
452
//                 if (cPtr == 0)
453
//                         throw new GdalException("No se ha podido acceder al archivo.");
454
                 
455
                 Double[] aux = new Double[1];
456
                 GetNoDataValue(null);
457
                 return aux[0];
458
         }
459
         
460
         /**
461
          * Obtiene el valorDevuelve el valor de NoData
462
          */
463
         public boolean existsNoDataValue()throws GdalException {
464
//                 if (cPtr == 0)
465
//                         throw new GdalException("No se ha podido acceder al archivo.");
466
                 
467
//                 int result = this.existsNoDataValueNat(cPtr);
468
                
469
                         return this.hasNoDataValue  ;
470
         }
471
         
472
         public void setRasterNoDataValue(double val){
473
                 this.hasNoDataValue = true;
474
                 
475
                 SetNoDataValue(val);
476
         }
477

    
478
         /**
479
          * Obtiene un array de Strings con los metadatos
480
          * 
481
          * @throws GdalException
482
          * @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
483
          */
484

    
485
         public String[] getMetadata()throws GdalException {
486
//                 if (cPtr == 0)
487
//                         throw new GdalException("No se ha podido acceder al archivo.");
488
                 
489
                 Vector<String> aux = this.GetMetadata_List();
490
                 String[] res =  aux.toArray(new String[0]);
491
                 if(res == null)
492
                         return new String[0];
493
                 else return res;
494
         }
495

    
496
         /**
497
          * Obtiene identificador que representa el tipo de banda de color. 
498
          * @return        identificador del tipo de banda de color
499
          * @throws GdalException
500
          */
501

    
502
         public int getRasterColorInterpretation()throws GdalException {
503
//                 if (cPtr == 0)
504
//                         throw new GdalException("No se ha podido acceder al archivo.");
505

    
506
                 int bandType = this.GetColorInterpretation();
507
                 return bandType;                
508
         }
509

    
510

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

    
548
}