Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / trunk / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / jgdal / GdalRasterBand.java @ 4182

History | View | Annotate | Download (13.9 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 {
41

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

    
44
        private boolean hasNoDataValue = false;
45

    
46
    private Band band;
47

    
48
         public GdalRasterBand(Band band) {
49
                 this.band = band;
50
         }
51

    
52
         /**
53
          * Lee datos de la banda de la imagen
54
          *
55
          * @return        Devuelve un vector de bytes con el trozo de raster le?do.
56
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
57
          * de la banda accedida.
58
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
59
          * de la banda accedida.
60
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
61
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
62
          * @param bufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
63
          * @param bufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
64
          * En caso de que bufXSize o bufYSize sean menores que 1, pasan a tener el mismo valor que
65
          * nXSize y nYSize respectivamente para evitar buffers con tama?o 0.
66
          * @param eBufType
67
          */
68

    
69
         public GdalBuffer readRaster(int nXOff, int nYOff, int nXSize, int nYSize,
70
                         int bufXSize, int bufYSize,
71
                         int eBufType)throws GdalException {
72

    
73
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
74
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
75

    
76
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize<1) || (nYSize > getRasterBandYSize()))
77
                         throw new GdalException("Tama?o de ventana incorrecto.");
78

    
79
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize()))){
80
                         throw new GdalException("Posicion de la ventana incorrecta.");
81
                 }
82

    
83
                 if ((eBufType < 1) || (eBufType > 11))
84
                         throw new GdalException("Tipo de datos incorrecto.");
85

    
86
                 if (bufXSize < 1)
87
                         bufXSize = nXSize;
88

    
89
                 if (bufYSize < 1)
90
                         bufYSize = nYSize;
91

    
92
                 GdalBuffer buf = new GdalBuffer();
93
                 int i = 0;
94

    
95
                 switch(eBufType) {
96
                 case 0:
97
                         i = 0;
98
                         break;
99
                 case 1:
100
                         buf.reservaByte(bufXSize * bufYSize);
101
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
102
                         break;
103
                 case 2:
104
                 case 3:
105
                 case 8:
106
                         buf.reservaShort(bufXSize * bufYSize);
107
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
108
                         break;
109
                 case 4:
110
                 case 5:
111
                 case 9:
112
                         buf.reservaInt(bufXSize * bufYSize);
113
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
114
                         break;
115
                 case 6:
116
                 case 10:
117
                         buf.reservaFloat(bufXSize * bufYSize);
118
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
119
                         break;
120
                 case 7:
121
                 case 11:
122
                         buf.reservaDouble(bufXSize * bufYSize);
123
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
124
                         break;
125
                 case 12:
126
                         i = 0;
127
                         break;
128
                 }
129

    
130

    
131
                 if(i>=0)
132
                         return buf;
133
                 else
134
                         return null;
135
         }
136

    
137
         /**
138
          * Escribe datos en la banda de la imagen
139
          *
140
          * @param nXOff        El desplazamiento del pixel desde la esquina superior derecha
141
          * de la banda accedida.
142
          * @param nYOff        El desplazamiento de l?nea desde la esquina superior derecha
143
          * de la banda accedida.
144
          * @param nXSize        Ancho de la regi?n en pixels de la banda que ser? accedida
145
          * @param nYSize        Altura de la regi?n en l?neas de la banda que ser? accedida
146
          * @param BufXSize        Ancho del buffer donde la regi?n de la im?gen ser? guardada
147
          * @param BufYSize        Altura del buffer donde la regi?n de la im?gen ser? guardada
148
          * @param eBufType
149
          */
150

    
151
         public void writeRaster(int nXOff, int nYOff, int nXSize, int nYSize, GdalBuffer buf, int eBufType)throws GdalException {
152
                 GdalBuffer buffer = new GdalBuffer();
153

    
154
                 if ((nXOff < 0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
155
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
156

    
157
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
158
                         throw new GdalException("Tama?o de ventana incorrecto.");
159

    
160
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
161
                         throw new GdalException("Posicion de la ventana incorrecta.");
162

    
163
                 if ((eBufType < 1) || (eBufType > 11))
164
                         throw new GdalException("Tipo de datos incorrecto.");
165

    
166
                 if (buf == null)
167
                         throw new GdalException("Buffer incorrecto");
168

    
169
                 switch(eBufType) {
170
                 case 0:
171
                         return;
172
                 case 1:
173
                         buffer.buffByte = buf.buffByte;
174
                         break;
175
                 case 2:
176
                 case 3:
177
                 case 8:
178
                         buffer.buffShort = buf.buffShort;
179
                         break;
180
                 case 4:
181
                 case 5:
182
                 case 9:
183
                         buffer.buffInt = buf.buffInt;
184
                         break;
185
                 case 6:
186
                 case 10:
187
                         buffer.buffFloat = buf.buffFloat;
188
                         break;
189
                 case 7:
190
                 case 11:
191
                         buffer.buffDouble = buf.buffDouble;
192
                         break;
193
                 case 12:
194
                         return;
195
                 }
196

    
197

    
198

    
199
                 switch(eBufType) {
200
                 case 0:
201
                         break;
202
                 case 1:
203
                         this.band.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffByte);
204
                         break;
205
                 case 2:
206
                 case 3:
207
                 case 8:
208
                         this.band.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffShort);
209
                         break;
210
                 case 4:
211
                 case 5:
212
                 case 9:
213
                         this.band.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffInt);
214
                         break;
215
                 case 6:
216
                 case 10:
217
                         this.band.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffFloat);
218
                         break;
219
                 case 7:
220
                 case 11:
221
                         this.band.WriteRaster(nXOff, nYOff, nXSize, nYSize, eBufType, buffer.buffDouble);
222
                         break;
223
                 case 12:
224
                         break;
225
                 }
226
         }
227

    
228
         /**
229
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las X
230
          *@return Tama?o en pixeles del eje X
231
          *@throws GdalException
232
          */
233

    
234
         public int getRasterBandXSize()throws GdalException {
235
                 return this.band.GetXSize();
236
         }
237

    
238
         /**
239
          *Obtiene el tama?o en pixeles de la im?gen en el eje de las Y
240
          *@return Tama?o en pixeles del eje Y
241
          *@throws GdalException
242
          */
243

    
244
         public int getRasterBandYSize()throws GdalException {
245
                 return this.band.GetYSize();
246
         }
247

    
248

    
249
         /**
250
          * Devuelve el n?mero de overviews que contiene la banda.
251
          * @return N?mero de overviews
252
          * @throws GdalException
253
          */
254

    
255
         public int getOverviewCount()throws GdalException {
256
                 return this.band.GetOverviewCount();
257
         }
258

    
259

    
260
         /**
261
          * Obtiene el overview indicado por el ?ndice "i".
262
          *
263
          * @param i        indice del overview que se quiere recuperar.
264
          * @return GdalRasterBand        Banda correspondiente al overview selecccionado
265
          * @throws GdalException
266
          */
267

    
268
         public GdalRasterBand getOverview(int i)throws GdalException {
269
                 Band band_ov;
270

    
271
                 if((i < 0) || (i >= getOverviewCount()))
272
                         throw new GdalException("El overview seleccionado no existe");
273

    
274
                 band_ov = this.band.GetOverview(i);
275

    
276
                 if (band_ov == null || !(band_ov instanceof Band))
277
                         throw new GdalException("No se ha podido obtener el overview");
278

    
279
                 return new GdalRasterBand(band_ov);
280
         }
281

    
282

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

    
289
         public int getBlockXSize()throws GdalException {
290
                 return this.band.GetBlockXSize();
291
         }
292

    
293

    
294
         /**
295
          * Devuelve el tama?o en Y del bloque para esa banda
296
          * @return Tama?o en pixeles del bloque en el eje Y
297
          * @throws GdalException
298
          */
299

    
300
         public int getBlockYSize()throws GdalException {
301
                 return this.band.GetBlockYSize();
302
         }
303

    
304
         /**
305
          * Devuelve el tipo de datos de la banda
306
          * @return Tama?o en pixeles del bloque en el eje Y
307
          * @throws GdalException
308
          */
309

    
310
         public int getRasterDataType()throws GdalException {
311
                 return this.band.getDataType();
312
         }
313

    
314
         /**
315
          * Obtiene la tabla de color asociada a la imagen
316
          */
317
         public GdalColorTable getRasterColorTable()throws GdalException {
318
                 ColorTable ct = this.band.GetColorTable();
319

    
320
                 if ((ct == null)) {
321
                         return null;
322
                 }
323

    
324
                 return new GdalColorTable(ct);
325
         }
326

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

    
344
         public GdalBuffer readRasterWithPalette(int nXOff, int nYOff, int nXSize, int nYSize,
345
                         int bufXSize, int bufYSize,
346
                         int eBufType)throws GdalException {
347

    
348
                 if ((nXOff<0) || (nXOff > getRasterBandXSize()) || (nYOff < 0) || (nYOff > getRasterBandYSize()))
349
                         throw new GdalException("Desplazamiento de la ventana fuera de rango.");
350

    
351
                 if ((nXSize < 1) || (nXSize > getRasterBandXSize()) || (nYSize < 1) || (nYSize > getRasterBandYSize()))
352
                         throw new GdalException("Tama?o de ventana incorrecto.");
353

    
354
                 if (((nXSize + nXOff) > (getRasterBandXSize())) || ((nYSize + nYOff) > (getRasterBandYSize())))
355
                         throw new GdalException("Posicion de la ventana incorrecta.");
356

    
357
                 if ((eBufType < 1) || (eBufType > 11))
358
                         throw new GdalException("Tipo de datos incorrecto.");
359

    
360
                 if (bufXSize < 1)
361
                         bufXSize = nXSize;
362

    
363
                 if (bufYSize < 1)
364
                         bufYSize = nYSize;
365

    
366

    
367
                 GdalBuffer buf = new GdalBuffer();
368
                 int i = 0;
369

    
370
                 switch(eBufType) {
371
                 case 0:
372
                         i = 0;
373
                         break;
374
                 case 1:
375
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffByte);
376
                         break;
377
                 case 2:
378
                 case 3:
379
                 case 8:
380
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffShort);
381
                         break;
382
                 case 4:
383
                 case 5:
384
                 case 9:
385
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffInt);
386
                         break;
387
                 case 6:
388
                 case 10:
389
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffFloat);
390
                         break;
391
                 case 7:
392
                 case 11:
393
                         i = this.band.ReadRaster(nXOff, nYOff, nXSize, nYSize, bufXSize, bufYSize, eBufType, buf.buffDouble);
394
                         break;
395
                 case 12:
396
                         i = 0;
397
                         break;
398
                 }
399

    
400

    
401
                 if(i>0)
402
                         return buf;
403
                 else
404
                         return null;
405
         }
406

    
407
         /**
408
          *Devuelve el valor de NoData
409
          */
410
         public double getRasterNoDataValue()throws GdalException {
411

    
412
                 Double[] aux = new Double[1];
413
                 this.band.GetNoDataValue(aux);
414
                 return aux[0];
415
         }
416

    
417
         /**
418
          * Obtiene el valorDevuelve el valor de NoData
419
          */
420
         public boolean existsNoDataValue()throws GdalException {
421
                         return this.hasNoDataValue  ;
422
         }
423

    
424
         public void setRasterNoDataValue(double val){
425
                 this.hasNoDataValue = true;
426

    
427
                 this.band.SetNoDataValue(val);
428
         }
429

    
430
         /**
431
          * Obtiene un array de Strings con los metadatos
432
          *
433
          * @throws GdalException
434
          * @return Array de Strings que corresponden a los metadatos que ofrece la im?gen
435
          */
436

    
437
         public String[] getMetadata()throws GdalException {
438

    
439
                 @SuppressWarnings("unchecked")
440
         Vector<String> aux = this.band.GetMetadata_List();
441
                 String[] res =  aux.toArray(new String[0]);
442
                 if(res == null)
443
                         return new String[0];
444
                 else return res;
445
         }
446

    
447
         /**
448
          * Obtiene identificador que representa el tipo de banda de color.
449
          * @return        identificador del tipo de banda de color
450
          * @throws GdalException
451
          */
452

    
453
         public int getRasterColorInterpretation()throws GdalException {
454

    
455
                 int bandType = this.band.GetColorInterpretation();
456
                 return bandType;
457
         }
458

    
459

    
460
         /**
461
          * Asigna la interpretaci?n de color de la banda.
462
          * Con algunos formatos no es posible modificar la interpretaci?n de color,
463
          * tales como tiff y jpg. En el caso de tif, no hay error pero tampoco se
464
          * produce el cambio en la interpretaci?n. En el caso de jpg, gdal lanza un error.
465
          * 0 = "Undefined"
466
          * 1 = "Gray";
467
          * 2 = "Palette";
468
          * 3 = "Red";
469
          * 4 = "Green";
470
          * 5 = "Blue";
471
          * 6 = "Alpha";
472
          * 7 = "Hue";
473
          * 8 = "Saturation";
474
          * 9 = "Lightness";
475
          * 10 = "Cyan";
476
          * 11 = "Magenta";
477
          * 12 = "Yellow";
478
          * 13 = "Black";
479
          * 14 = "YCbCr_Y";
480
          * 15 = "YCbCr_Cb";
481
          * 16 = "YCbCr_Cr";
482
          * @param bandType
483
          * @throws GdalException
484
          */
485
         public void setRasterColorInterpretation(int bandType) throws GdalException{
486

    
487
                 if ((bandType < 0) || (bandType > 16)){
488
                         throw new GdalException("Tipo de banda incorrecto");
489
                 }
490

    
491
                 @SuppressWarnings("unused")
492
         int err = this.band.SetColorInterpretation(bandType);
493

    
494
         }
495

    
496
}