Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1014 / libraries / libCq_CMS_praster / src / org / cresques / io / GdalFile.java @ 13593

History | View | Annotate | Download (24.6 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.awt.Image;
27
import java.awt.geom.NoninvertibleTransformException;
28
import java.awt.geom.Point2D;
29
import java.awt.geom.Rectangle2D;
30
import java.awt.image.BufferedImage;
31
import java.io.File;
32

    
33
import org.cresques.cts.ICoordTrans;
34
import org.cresques.cts.IProjection;
35
import org.cresques.io.data.BandList;
36
import org.cresques.io.data.RasterBuf;
37
import org.cresques.io.datastruct.Metadata;
38
import org.cresques.io.exceptions.NotSupportedExtensionException;
39
import org.cresques.io.exceptions.SupersamplingNotSupportedException;
40
import org.cresques.px.Extent;
41
import org.cresques.util.Utilities;
42

    
43
import es.gva.cit.jgdal.GdalException;
44

    
45
/**
46
 * @author Luis W. Sevilla
47
 */
48
public class GdalFile extends GeoRasterFile {
49
        public final static int         BAND_HEIGHT = 64;
50
        protected GdalNative                 file = null;
51
        /**
52
         * Tama?o de pixel para las imagenes con fichero RMF. No podemos salvarlo en file porque es necesario conocer el
53
         * tama?o de pixel asignado por rl .rmf y el tama?o de pixel real.
54
         */
55
        private double                                pixelSizeX = 0D, pixelSizeY = 0D;
56

    
57
        private Extent v = null;
58
        
59
        public GdalFile(IProjection proj, String fName, boolean hdf){
60
                super(proj, fName);
61
        }
62
        
63
        public GdalFile(IProjection proj, String fName)throws NotSupportedExtensionException{
64
                super(proj, fName);
65
                extent = new Extent();
66
                fName = translateFileName(fName);
67
                try {
68
                        file = new GdalNative(fName, this);
69
                        load();
70
                        readGeoInfo(fName);
71
                        bandCount = file.getRasterCount();
72
                        if ( bandCount > 2) {
73
                                setBand(RED_BAND,   0);
74
                                setBand(GREEN_BAND, 1);
75
                                setBand(BLUE_BAND,  2);
76
                        } else
77
                                setBand(RED_BAND|GREEN_BAND|BLUE_BAND, 0);
78
                } catch (GdalException e) {
79
                        throw new NotSupportedExtensionException("Extension not supported");
80
                } catch(Exception e){
81
                          System.out.println("Error en GdalOpen");
82
                          e.printStackTrace();
83
                          file = null;
84
                }
85

    
86
                //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
87
                setDataType(org.cresques.util.Utilities.getRasterBufTypeFromGdalType(file.getDataType()));
88
        }
89

    
90
        public String translateFileName(String fileName){
91
                String ext = fileName.substring(fileName.lastIndexOf(".")+1);
92
                if (ext.equals("hdr")){
93
                        String path = fileName.substring(0, fileName.lastIndexOf("."));
94
                        File file = new File(path);
95
                        if (file.exists()){
96
                                return path;
97
                        }else{
98
                                file = new File(path + ".dat");
99
                                if (file.exists()){
100
                                        return path + ".dat";
101
                                }
102
                        }
103
                        return null;
104
                }
105
                return fileName;
106
        }
107
        
108
        /**
109
         * Obtenemos o calculamos el extent de la imagen.
110
         */
111
        public GeoFile load() {
112
                extent = new Extent(file.bBoxRot.minX, file.bBoxRot.minY, file.bBoxRot.maxX, file.bBoxRot.maxY);
113
                requestExtent = new Extent(file.bBoxWithoutRot.minX, file.bBoxWithoutRot.minY, file.bBoxWithoutRot.maxX, file.bBoxWithoutRot.maxY);
114
                return this;
115
        }
116

    
117
        /**
118
         * Cierra el fichero de imagen
119
         */
120
        public void close() {
121
                try {
122
                        if(file != null){
123
                                file.close();
124
                                file = null;
125
                        }
126
                } catch (GdalException e) {
127
                        // TODO Auto-generated catch block
128
                        e.printStackTrace();
129
                }
130
        }
131

    
132
        /**
133
         * Asigna a cada banda R,G o B una banda de la imagen
134
         */
135
        public void setBand(int flag, int bandNr) {
136
                super.setBand(flag, bandNr);
137
                if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND) file.rBandNr = bandNr+1;
138
                if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) file.gBandNr = bandNr+1;
139
                if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) file.bBandNr = bandNr+1;
140
        }
141

    
142
        /**
143
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
144
         * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
145
         * ha de estar en coordenadas del fichero.
146
         */
147
        public void setView(Extent e) {
148
                if(rmfExists){
149

    
150
                        Point2D.Double petInit = null, petEnd = null;
151
                        try{
152
                                petInit = new Point2D.Double(e.minX(),  e.maxY());
153
                                petEnd = new Point2D.Double(e.maxX(), e.minY());
154
                                transformRMF.inverseTransform(petInit, petInit);
155
                                transformRMF.inverseTransform(petEnd, petEnd);
156
                                transformTFW.transform(petInit, petInit);
157
                                transformTFW.transform(petEnd, petEnd);
158
                        }catch(NoninvertibleTransformException ex){}
159
                        double h = file.bBoxWithoutRot.maxY - file.bBoxWithoutRot.minY;
160
                        if(!file.isGeoreferenced())
161
                                v = new Extent(        petInit.getX(), h - petInit.getY(), petEnd.getX(), h - petEnd.getY());
162
                        else
163
                                v = new Extent(        petInit.getX(), petInit.getY(), petEnd.getX(), petEnd.getY());
164

    
165
                }else
166
                        v = new Extent(e.minX(), e.minY(), e.maxX(), e.maxY());
167
        }
168

    
169
         /**
170
         * Calcula la transformaci?n que se produce sobre la vista cuando la imagen tiene un fichero .rmf
171
         * asociado. En Gdal el origen de coordenadas en Y es el valor m?nimo y crece hasta el m?ximo. De la
172
         * misma forma calcula la matriz de transformaci?n de la cabecera del fichero o del world file asociado
173
         * @param originX Origen de la imagen en la coordenada X
174
         * @param originY Origen de la imagen en la coordenada Y
175
         */
176
        public void setExtentTransform(double originX, double originY, double psX, double psY) {
177
                transformRMF.setToTranslation(originX, originY);
178
                transformRMF.scale(psX, psY);
179

    
180
                if(file.trans != null){
181
                        transformTFW.setToTranslation(file.trans.adfgeotransform[0], file.trans.adfgeotransform[3]);
182
                        transformTFW.scale(file.trans.adfgeotransform[1], file.trans.adfgeotransform[5]);
183
                }
184
        }
185

    
186
        /**
187
         * Obtiene extent de la vista actual
188
         */
189
        public Extent getView() {
190
                return v;
191
        }
192

    
193
        /**
194
         * Obtiene la anchura del fichero
195
         */
196
        public int getWidth() {
197
                return file.width;
198
        }
199

    
200
        /**
201
         * Obtiene la altura del fichero
202
         */
203
        public int getHeight() {
204
                return file.height;
205
        }
206

    
207
        /* (non-Javadoc)
208
         * @see org.cresques.io.GeoRasterFile#reProject(org.cresques.cts.ICoordTrans)
209
         */
210
        public void reProject(ICoordTrans rp) {
211
                // TODO Auto-generated method stub
212
        }
213

    
214
        /**
215
         * Obtiene la orientaci?n de la imagen a partir del signo del tama?o de pixel para poder
216
         * asignarlo en el setView. Esto es util para poder conocer como debe leerse la image,
217
         * de abajo a arriba, de arriba a abajo, de izquierda a derecha o de derecha a izquierda.
218
         * La posici?n habitual es la que el pixel size en X es positivo y en Y negativo leyendose
219
         * en este caso las X de menor a mayor y las Y de mayor a menor. Los casos posibles son:
220
         * <UL>
221
         * <LI><B>X > 0; Y < 0;</B> {true, false}</LI>
222
         * <LI><B>X > 0; Y > 0;</B> {true, true}</LI>
223
         * <LI><B>X < 0; Y > 0;</B> {false, true}</LI>
224
         * <LI><B>X < 0; Y < 0;</B> {false, false}</LI>
225
         * </UL>
226
         *
227
         * @return
228
         */
229
        private boolean[] getOrientation(){
230
                boolean[] orientation = {true, false};
231
                if(!rmfExists){
232
                        if(file.trans != null && file.trans.adfgeotransform != null && file.trans.adfgeotransform[5] > 0)
233
                                orientation[1] = true;
234
                        if(file.trans != null && file.trans.adfgeotransform != null && file.trans.adfgeotransform[1] < 0)
235
                                orientation[0] = false;
236
                }else{
237
                        if(rmfTransform.getScaleY() > 0)
238
                                orientation[1] = true;
239
                        if(rmfTransform.getScaleX() < 0)
240
                                orientation[0] = false;
241
                }
242
                return orientation;
243
        }
244

    
245
        /* (non-Javadoc)
246
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans)
247
         */
248
        public Image updateImage(int width, int height, ICoordTrans rp) {
249
                int line, pRGBArray[] = null;
250
                Image image = null;
251

    
252
                if (mustVerifySize()) {
253
                        // Work out the correct aspect for the setView call.
254
                        double dFileAspect = (double)v.width()/(double)v.height();
255
                        double dWindowAspect = (double)width /(double)height;
256

    
257
                        if (dFileAspect > dWindowAspect) {
258
                          height =(int)((double)width/dFileAspect);
259
                        } else {
260
                          width = (int)((double)height*dFileAspect);
261
                        }
262
                }
263

    
264
                // Set the view
265
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height, getOrientation());
266
                setStep(file.stepArrayX, file.stepArrayY);
267

    
268
                if(width<=0)width=1;
269
                if(height<=0)height=1;
270

    
271
                image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
272
                //image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
273
                pRGBArray = new int[width/**BAND_HEIGHT*/];
274
                try {
275
                        //int nLin = height % BAND_HEIGHT;
276
                        file.setAlpha(getAlpha());
277
                        setBand(RED_BAND,   rBandNr);
278
                        setBand(GREEN_BAND, gBandNr);
279
                        setBand(BLUE_BAND,  bBandNr);
280
                        for (line=0; line < height; line++) { //+=BAND_HEIGHT) {
281
                                //int bandH = Math.min(BAND_HEIGHT, height-line);
282
                                //file.readBandRGBA(bandH, BAND_HEIGHT, pRGBArray);
283
                                file.readLineRGBA(pRGBArray);
284
                                setRGBLine((BufferedImage) image, 0, line, width, 1/*bandH*/, pRGBArray, 0, width);
285
                        }
286
                } catch (Exception e) {
287
                        // TODO Auto-generated catch block
288
                        e.printStackTrace();
289
                }
290

    
291
                return image;
292
        }
293

    
294
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
295
                int line;
296
                RasterBuf raster = null;
297

    
298
                if(mustVerifySize()){
299
                        // Work out the correct aspect for the setView call.
300
                        double dFileAspect = (double)v.width()/(double)v.height();
301
                        double dWindowAspect = (double)width /(double)height;
302

    
303
                        if (dFileAspect > dWindowAspect) {
304
                          height =(int)((double)width/dFileAspect);
305
                        } else {
306
                          width = (int)((double)height*dFileAspect);
307
                        }
308
                }
309

    
310
                // Set the view
311
                boolean[] orientation = getOrientation();
312
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
313
                        width, height, orientation);
314
                setStep(file.stepArrayX, file.stepArrayY);
315

    
316
                try {
317
                        //Esta funci?n se usa para la renderizaci?n, por eso se crean 4 bandas a pi?on fijo
318
                        raster = new RasterBuf(getDataType(), width, height, 4, true);
319

    
320
                        file.setAlpha(getAlpha());
321
                        setBand(RED_BAND,   rBandNr);
322
                        setBand(GREEN_BAND, gBandNr);
323
                        setBand(BLUE_BAND,  bBandNr);
324

    
325
                        switch(getDataType()){
326
                        case RasterBuf.TYPE_BYTE:
327
                                for (line = 0; line < height; line++)
328
                                        file.readLine(raster.getLineByte(line));
329
                                break;
330
                        case RasterBuf.TYPE_SHORT:;
331
                                for (line = 0; line < height; line++)
332
                                        file.readLine(raster.getLineShort(line));
333
                                break;
334
                        case RasterBuf.TYPE_INT:
335
                                for (line = 0; line < height; line++)
336
                                        file.readLine(raster.getLineInt(line));
337
                                break;
338
                        case RasterBuf.TYPE_FLOAT:
339
                                for (line = 0; line < height; line++)
340
                                        file.readLine(raster.getLineFloat(line));
341
                                break;
342
                        case RasterBuf.TYPE_DOUBLE:
343
                                for (line = 0; line < height; line++)
344
                                        file.readLine(raster.getLineDouble(line));
345
                                break;
346
                        case RasterBuf.TYPE_UNDEFINED:break;
347
                        }
348

    
349
                } catch (Exception e) {
350
                        e.printStackTrace();
351
                }
352

    
353
                return raster;
354
        }
355

    
356
        /**
357
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
358
         * vector de enteros.
359
         * @param image        imagen con los datos actuales
360
         * @param startX        inicio de la posici?n en X dentro de la imagen
361
         * @param startY        inicio de la posici?n en X dentro de la imagen
362
         * @param w        Ancho de la imagen
363
         * @param h        Alto de la imagen
364
         * @param rgbArray        vector que contiene la banda que se va a sustituir
365
         * @param offset        desplazamiento
366
         * @param scansize        tama?o de imagen recorrida por cada p
367
         */
368
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
369
                         int offset, int scansize) {
370
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
371
        }
372

    
373
        /**
374
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
375
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
376
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
377
         * banda correspondiente a flags es sustituida por los datos del vector.
378
         * @param image        imagen con los datos actuales
379
         * @param startX        inicio de la posici?n en X dentro de la imagen
380
         * @param startY        inicio de la posici?n en X dentro de la imagen
381
         * @param w        Ancho de la imagen
382
         * @param h        Alto de la imagen
383
         * @param rgbArray        vector que contiene la banda que se va a sustituir
384
         * @param offset        desplazamiento
385
         * @param scansize        tama?o de imagen recorrida por cada paso
386
         * @param flags        banda que se va a sustituir (Ctes de GeoRasterFile)
387
         */
388
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
389
                         int offset, int scansize, int flags) {
390
                int [] line = new int[rgbArray.length];
391
                image.getRGB(startX, startY, w, h, line, offset, scansize);
392
                if (flags == GeoRasterFile.RED_BAND)
393
                        for (int i=0; i<line.length; i++)
394
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
395
                else if (flags == GeoRasterFile.GREEN_BAND)
396
                        for (int i=0; i<line.length; i++)
397
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
398
                else if (flags == GeoRasterFile.BLUE_BAND)
399
                        for (int i=0; i<line.length; i++)
400
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
401
                image.setRGB(startX, startY, w, h, line, offset, scansize);
402
        }
403

    
404
        /**
405
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
406
         * con los dato de la imagen contenidos en el vector de enteros. De los valores RGB
407
         * que ya contiene se mantienen las bandas que no coinciden con el valor de flags. La
408
         * banda correspondiente a flags es sustituida por los datos del vector.
409
         * @param image        imagen con los datos actuales
410
         * @param startX        inicio de la posici?n en X dentro de la imagen
411
         * @param startY        inicio de la posici?n en X dentro de la imagen
412
         * @param w        Ancho de la imagen
413
         * @param h        Alto de la imagen
414
         * @param rgbArray        vector que contiene la banda que se va a sustituir
415
         * @param offset        desplazamiento
416
         * @param scansize        tama?o de imagen recorrida por cada paso
417
         * @param origBand        Banda origen del GeoRasterFile
418
         * @param destBandFlag        banda que se va a sustituir (Ctes de GeoRasterFile)
419
         */
420
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
421
                         int offset, int scansize, int origBand, int destBandFlag) {
422
                int [] line = new int[rgbArray.length];
423
                image.getRGB(startX, startY, w, h, line, offset, scansize);
424
                if (origBand == 0 && destBandFlag == GeoRasterFile.RED_BAND)
425
                        for (int i=0; i<line.length; i++)
426
                                line[i] = (line[i] & 0x0000ffff) | (rgbArray[i] & 0xffff0000);
427
                else if (origBand == 1 && destBandFlag == GeoRasterFile.GREEN_BAND)
428
                        for (int i=0; i<line.length; i++)
429
                                line[i] = (line[i] & 0x00ff00ff) | (rgbArray[i] & 0xff00ff00);
430
                else if (origBand == 2 && destBandFlag == GeoRasterFile.BLUE_BAND)
431
                        for (int i=0; i<line.length; i++)
432
                                line[i] = (line[i] & 0x00ffff00) | (rgbArray[i] & 0xff0000ff);
433

    
434
                else if (origBand == 0 && destBandFlag == GeoRasterFile.GREEN_BAND)
435
                        for (int i=0; i<line.length; i++)
436
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x00ff0000) >> 8) ;
437
                else if (origBand == 0 && destBandFlag == GeoRasterFile.BLUE_BAND)
438
                        for (int i=0; i<line.length; i++)
439
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x00ff0000) >> 16);
440
                else if (origBand == 1 && destBandFlag == GeoRasterFile.RED_BAND)
441
                        for (int i=0; i<line.length; i++)
442
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x0000ff00) << 8);
443

    
444
                else if (origBand == 1 && destBandFlag == GeoRasterFile.BLUE_BAND)
445
                        for (int i=0; i<line.length; i++)
446
                                line[i] = (line[i] & 0xffffff00) | ((rgbArray[i] & 0x0000ff00) >> 8);
447
                else if (origBand == 2 && destBandFlag == GeoRasterFile.RED_BAND)
448
                        for (int i=0; i<line.length; i++)
449
                                line[i] = (line[i] & 0xff00ffff) | ((rgbArray[i] & 0x000000ff) << 16);
450
                else if (origBand == 2 && destBandFlag == GeoRasterFile.GREEN_BAND)
451
                        for (int i=0; i<line.length; i++)
452
                                line[i] = (line[i] & 0xffff00ff) | ((rgbArray[i] & 0x000000ff) << 8);
453
                image.setRGB(startX, startY, w, h, line, offset, scansize);
454
        }
455

    
456
        private void showOnOpen() {
457
                  // Report en la apertura (quitar)
458
                  System.out.println("Fichero GDAL '"+getName()+"' abierto.");
459
                  System.out.println("Version = "+file.version);
460
                  System.out.println("   Size = ("+file.width+","+file.height+")");
461
                  try {
462
                        System.out.println("   NumBands = ("+file.getRasterCount()+")");
463
                } catch (GdalException e) {
464
                        // TODO Auto-generated catch block
465
                        e.printStackTrace();
466
                }
467
                  //file.pintaInfo();
468
                  file.pintaPaleta();
469

    
470
        }
471

    
472
        /* (non-Javadoc)
473
         * @see org.cresques.io.GeoRasterFile#updateImage(int, int, org.cresques.cts.ICoordTrans, java.awt.Image, int, int)
474
         */
475
        public Image updateImage(int width, int height, ICoordTrans rp, Image img, int origBand, int destBandFlag)throws SupersamplingNotSupportedException{
476
                int line, pRGBArray[] = null;
477

    
478
                if(mustVerifySize()){
479
                        // Work out the correct aspect for the setView call.
480
                        double dFileAspect = (double)v.width()/(double)v.height();
481
                        double dWindowAspect = (double)width /(double)height;
482

    
483
                        if (dFileAspect > dWindowAspect) {
484
                          height =(int)((double)width/dFileAspect);
485
                        } else {
486
                          width = (int)((double)height*dFileAspect);
487
                        }
488
                }
489

    
490
//                 Set the view
491
                boolean[] orientation = getOrientation();
492
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
493
                        width, height, orientation);
494
                setStep(file.stepArrayX, file.stepArrayY);
495

    
496
                if(width<=0)width=1;
497
                if(height<=0)height=1;
498

    
499
                pRGBArray = new int[width];
500
                try {
501
                        setBand(RED_BAND,   rBandNr);
502
                        setBand(GREEN_BAND, gBandNr);
503
                        setBand(BLUE_BAND,  bBandNr);
504
                        file.setAlpha(getAlpha());
505
                        if(img!=null){
506
                                if(orientation[1]){
507
                                        for (line=0; line < height; line++) {
508
                                                file.readLineRGBA(pRGBArray);
509
                                                setRGBLine((BufferedImage) img, 0, height - 1 - line, width, 1, pRGBArray, 0, width, origBand, destBandFlag);
510
                                        }
511
                                }else{
512
                                        for (line=0; line < height; line++) {
513
                                                file.readLineRGBA(pRGBArray);
514
                                                setRGBLine((BufferedImage) img, 0, line, width, 1, pRGBArray, 0, width, origBand, destBandFlag);
515
                                        }
516
                                }
517
                                return img;
518
                        }else{
519
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
520
                                if(orientation[1]){
521
                                        for (line=0; line < height; line++) {
522
                                                file.readLineRGBA(pRGBArray);
523
                                                setRGBLine((BufferedImage) image, 0, height - 1 - line, width, 1, pRGBArray, 0, width);
524
                                        }
525
                                }else{
526
                                        for (line=0; line < height; line++) {
527
                                                file.readLineRGBA(pRGBArray);
528
                                                setRGBLine((BufferedImage) image, 0, line, width, 1, pRGBArray, 0, width);
529
                                        }
530
                                }
531
                                return image;
532
                        }
533
                } catch (Exception e) {
534
                        // TODO Auto-generated catch block
535
                        e.printStackTrace();
536
                }
537

    
538
                return img;
539
        }
540

    
541
        /* (non-Javadoc)
542
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
543
         */
544
        public Object getData(int x, int y, int band) {
545
                if(file != null){
546
                        Object[] data = file.getData(x, y);
547
                        return data[band];
548
                }
549
                return null;
550
        }
551

    
552
        /**
553
         * Devuelve los datos de una ventana solicitada
554
         * @param ulX        coordenada X superior izda.
555
         * @param ulY        coordenada Y superior derecha.
556
         * @param sizeX        tama?o en X de la ventana.
557
         * @param sizeY tama?o en Y de la ventana.
558
         * @param band        Banda solicitada.
559
         */
560
        public byte[] getWindow(int ulX, int ulY, int sizeX, int sizeY, int band){
561

    
562
                return null;
563
        }
564

    
565
        public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) {
566
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
567
                setView(selectedExtent);
568

    
569
                int width = 0;
570
                int height = 0;
571
                if(file.trans != null){
572
                        width = (int)Math.abs(selectedExtent.width() / file.trans.adfgeotransform[1]);//(int)(selectedExtent.width() * file.width) / extent.width();
573
                        height = (int)Math.abs(selectedExtent.height() / file.trans.adfgeotransform[5]);
574
                }else{
575
                        width = (int)Math.abs(selectedExtent.width());
576
                        height = (int)Math.abs(selectedExtent.height());
577
                }
578

    
579
                try {
580
                        file.readWindow(rasterBuf, bandList, x, y, width, height);
581
                } catch (Exception e) {
582
                        e.printStackTrace();
583
                }
584

    
585
                return rasterBuf;
586
        }
587

    
588
        /*
589
         *  (non-Javadoc)
590
         * @see org.gvsig.fmap.driver.GeoRasterFile#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.driver.BandList, org.gvsig.fmap.driver.IBuffer)
591
         */
592
        public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, RasterBuf rasterBuf) {
593
                Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
594
                setView(selectedExtent);
595

    
596
                double width = 0;
597
                double height = 0;
598
                if(getTransform() != null){
599
                        width = (double)(Math.abs(selectedExtent.width() / getTransform()[1]));//(int)(selectedExtent.width() * file.width) / extent.width();
600
                        height = (double)(Math.abs(selectedExtent.height() / getTransform()[5]));
601
                }else{
602
                        width = (double)Math.abs(selectedExtent.width());
603
                        height = (double)Math.abs(selectedExtent.height());
604
                }
605

    
606
                try {
607
                        file.readWindow(rasterBuf, bandList, minX, maxY, maxX, minY, width, height, bufWidth, bufHeight);
608
                } catch (Exception e) {
609
                        e.printStackTrace();
610
                }
611

    
612
                return rasterBuf;
613
        }
614

    
615
        public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf) {
616
                try {
617
                        setView(
618
                        new Extent( Utilities.getMapRectFromPxRect(getExtent().toRectangle2D(),
619
                                                getWidth(),
620
                                                getHeight(),
621
                                                new Rectangle2D.Double(x, y, w, h)))
622
                        );
623
                        file.readWindow(rasterBuf, bandList, x, y, w, h);
624
                } catch (Exception e) {
625
                        e.printStackTrace();
626
                }
627
                return rasterBuf;
628
        }
629

    
630
        public RasterBuf getWindowRasterWithNoData(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) {
631
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
632
                setView(selectedExtent);
633

    
634
                try {
635
                        file.readWindowWithNoData(rasterBuf, bandList, x, y, x + w, y - h, rasterBuf.getWidth(), rasterBuf.getHeight());
636
                } catch (Exception e) {
637
                        e.printStackTrace();
638
                }
639

    
640
                return rasterBuf;
641
        }
642

    
643
        /**
644
         * Obtiene la zona (Norte / Sur)
645
         * @return true si la zona es norte y false si es sur
646
         */
647

    
648
        public boolean getZone(){
649

    
650
                return false;
651
        }
652

    
653
        /**
654
         *Devuelve el n?mero de zona UTM
655
         *@return N?mero de zona
656
         */
657

    
658
        public int getUTM(){
659

    
660
                return 0;
661
        }
662

    
663
        /**
664
         * Obtiene el sistema de coordenadas geograficas
665
         * @return Sistema de coordenadas geogr?ficas
666
         */
667
        public String getGeogCS(){
668
                return new String("");
669
        }
670

    
671
        /**
672
         * Devuelve el tama?o de bloque
673
         * @return Tama?o de bloque
674
         */
675
        public int getBlockSize(){
676
                if(file != null)
677
                        return file.getBlockSize();
678
                else
679
                        return 0;
680
        }
681

    
682
        /**
683
         * Obtiene el objeto que contiene los metadatos
684
         */
685
        public Metadata getMetadata() {
686
                if(file != null)
687
                        return file.getMetadataJavaObject();
688
                else
689
                        return null;
690
        }
691

    
692
        /**
693
         * Obtiene el flag que dice si la imagen est? o no georreferenciada
694
         * @return true si est? georreferenciada y false si no lo est?.
695
         */
696
        public boolean isGeoreferenced() {
697
                if(file != null)
698
                        return file.isGeoreferenced();
699
                else
700
                        return false;
701
        }
702

    
703
        /**
704
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
705
         * el valor de esta variable cada vez que dibuja.
706
         * @return true si se ha supersampleado y false si no se ha hecho.
707
         */
708
        public boolean isSupersampling() {
709
                if(file != null)
710
                        return file.isSupersampling;
711
                else
712
                        return false;
713
        }
714

    
715
        /**
716
         * Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
717
         * un fichero tfw.
718
         * <UL>
719
         * <LI>[1]tama?o de pixel en X</LI>
720
         * <LI>[2]rotaci?n en X</LI>
721
         * <LI>[4]rotaci?n en Y</LI>
722
         * <LI>[5]tama?o de pixel en Y</LI>
723
         * <LI>[0]origen en X</LI>
724
         * <LI>[3]origen en Y</LI>
725
         * </UL>
726
         * Este m?todo debe ser reimplementado por el driver si tiene esta informaci?n. En principio
727
         * Gdal es capaz de proporcionarla de esta forma.
728
         *
729
         * En caso de que exista fichero .rmf asociado al raster pasaremos de la informaci?n de georreferenciaci?n
730
         * del .tfw y devolveremos la que est? asociada al rmf
731
         * @return vector de double con los elementos de la transformaci?n af?n.
732
         */
733
        public double[] getTransform(){
734
                if(file != null && file.trans != null && !this.rmfExists())
735
                        return file.trans.adfgeotransform;
736
                else{
737
                        if(this.rmfExists){
738
                                double[] rmfGeoref = {        rmfTransform.getTranslateX(),
739
                                                                                rmfTransform.getScaleX(),
740
                                                                                rmfTransform.getShearX(),
741
                                                                                rmfTransform.getTranslateY(),
742
                                                                                rmfTransform.getShearY(),
743
                                                                                rmfTransform.getScaleY()};
744
                                return rmfGeoref;
745
                        }
746
                        return null;
747
                }
748

    
749
        }
750

    
751
        /*
752
         *  (non-Javadoc)
753
         * @see org.gvsig.fmap.driver.GeoRasterFile#rasterToWorld(java.awt.geom.Point2D)
754
         */
755
        public Point2D rasterToWorld(Point2D pt) {
756
                return file.rasterToWorld(pt);
757
        }
758

    
759
        /*
760
         *  (non-Javadoc)
761
         * @see org.gvsig.fmap.driver.GeoRasterFile#worldToRaster(java.awt.geom.Point2D)
762
         */
763
        public Point2D worldToRaster(Point2D pt){
764
                return file.worldToRaster(pt);
765
        }
766

    
767
        public void readPalette(){
768
                file.readPalette();
769
        }
770
        
771
}
772

    
773

    
774