Statistics
| Revision:

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

History | View | Annotate | Download (24.6 KB)

1 8026 nacho
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4 12539 jmvivo
 * Copyright (C) 2004-5.
5
 *
6 8026 nacho
 * 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 12539 jmvivo
 *
22 8026 nacho
 * 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 13110 maquerol
import java.io.File;
32 8026 nacho
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 12543 maquerol
59
        public GdalFile(IProjection proj, String fName, boolean hdf){
60
                super(proj, fName);
61
        }
62
63 8026 nacho
        public GdalFile(IProjection proj, String fName)throws NotSupportedExtensionException{
64
                super(proj, fName);
65
                extent = new Extent();
66 13110 maquerol
                fName = translateFileName(fName);
67 8026 nacho
                try {
68
                        file = new GdalNative(fName, this);
69
                        load();
70
                        readGeoInfo(fName);
71 12539 jmvivo
                        bandCount = file.getRasterCount();
72 8026 nacho
                        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 12539 jmvivo
86 8026 nacho
                //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
87
                setDataType(org.cresques.util.Utilities.getRasterBufTypeFromGdalType(file.getDataType()));
88
        }
89 12539 jmvivo
90 13110 maquerol
        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 8026 nacho
        /**
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 12539 jmvivo
117 8026 nacho
        /**
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 12539 jmvivo
132 8026 nacho
        /**
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 12539 jmvivo
142 8026 nacho
        /**
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 12539 jmvivo
        public void setView(Extent e) {
148 8026 nacho
                if(rmfExists){
149 12539 jmvivo
150 8026 nacho
                        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 8631 nacho
                                transformRMF.inverseTransform(petInit, petInit);
155
                                transformRMF.inverseTransform(petEnd, petEnd);
156
                                transformTFW.transform(petInit, petInit);
157
                                transformTFW.transform(petEnd, petEnd);
158 8026 nacho
                        }catch(NoninvertibleTransformException ex){}
159 8631 nacho
                        double h = file.bBoxWithoutRot.maxY - file.bBoxWithoutRot.minY;
160
                        if(!file.isGeoreferenced())
161 12539 jmvivo
                                v = new Extent(        petInit.getX(), h - petInit.getY(), petEnd.getX(), h - petEnd.getY());
162 8631 nacho
                        else
163
                                v = new Extent(        petInit.getX(), petInit.getY(), petEnd.getX(), petEnd.getY());
164 12539 jmvivo
165 8026 nacho
                }else
166 12539 jmvivo
                        v = new Extent(e.minX(), e.minY(), e.maxX(), e.maxY());
167 8026 nacho
        }
168 12539 jmvivo
169 8631 nacho
         /**
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 12539 jmvivo
        public void setExtentTransform(double originX, double originY, double psX, double psY) {
177 8631 nacho
                transformRMF.setToTranslation(originX, originY);
178
                transformRMF.scale(psX, psY);
179 12539 jmvivo
180
                if(file.trans != null){
181 8631 nacho
                        transformTFW.setToTranslation(file.trans.adfgeotransform[0], file.trans.adfgeotransform[3]);
182
                        transformTFW.scale(file.trans.adfgeotransform[1], file.trans.adfgeotransform[5]);
183
                }
184
        }
185 12539 jmvivo
186 8026 nacho
        /**
187
         * Obtiene extent de la vista actual
188
         */
189 12539 jmvivo
        public Extent getView() {
190
                return v;
191 8026 nacho
        }
192 12539 jmvivo
193 8026 nacho
        /**
194
         * Obtiene la anchura del fichero
195
         */
196 12539 jmvivo
        public int getWidth() {
197
                return file.width;
198 8026 nacho
        }
199 12539 jmvivo
200 8026 nacho
        /**
201
         * Obtiene la altura del fichero
202
         */
203 12539 jmvivo
        public int getHeight() {
204 8026 nacho
                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 12539 jmvivo
214 8631 nacho
        /**
215
         * Obtiene la orientaci?n de la imagen a partir del signo del tama?o de pixel para poder
216 12539 jmvivo
         * 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 8631 nacho
         * 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 12539 jmvivo
         *
227 8631 nacho
         * @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 12539 jmvivo
245 8026 nacho
        /* (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 12539 jmvivo
252 8026 nacho
                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 12539 jmvivo
257 8026 nacho
                        if (dFileAspect > dWindowAspect) {
258
                          height =(int)((double)width/dFileAspect);
259
                        } else {
260
                          width = (int)((double)height*dFileAspect);
261
                        }
262
                }
263 12539 jmvivo
264 8026 nacho
                // Set the view
265 8631 nacho
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(), width, height, getOrientation());
266 8026 nacho
                setStep(file.stepArrayX, file.stepArrayY);
267 12539 jmvivo
268 8026 nacho
                if(width<=0)width=1;
269
                if(height<=0)height=1;
270 12539 jmvivo
271 8026 nacho
                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 12539 jmvivo
291 8026 nacho
                return image;
292
        }
293 12539 jmvivo
294 8026 nacho
        public RasterBuf getRaster(int width, int height, ICoordTrans rp) {
295
                int line;
296
                RasterBuf raster = null;
297 12539 jmvivo
298 8026 nacho
                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 12539 jmvivo
303 8026 nacho
                        if (dFileAspect > dWindowAspect) {
304
                          height =(int)((double)width/dFileAspect);
305
                        } else {
306
                          width = (int)((double)height*dFileAspect);
307
                        }
308
                }
309 12539 jmvivo
310 8026 nacho
                // Set the view
311 8631 nacho
                boolean[] orientation = getOrientation();
312
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
313
                        width, height, orientation);
314 8026 nacho
                setStep(file.stepArrayX, file.stepArrayY);
315 12539 jmvivo
316 8026 nacho
                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 12539 jmvivo
320 8026 nacho
                        file.setAlpha(getAlpha());
321
                        setBand(RED_BAND,   rBandNr);
322
                        setBand(GREEN_BAND, gBandNr);
323
                        setBand(BLUE_BAND,  bBandNr);
324 12539 jmvivo
325 8026 nacho
                        switch(getDataType()){
326 12539 jmvivo
                        case RasterBuf.TYPE_BYTE:
327
                                for (line = 0; line < height; line++)
328 8026 nacho
                                        file.readLine(raster.getLineByte(line));
329
                                break;
330 12539 jmvivo
                        case RasterBuf.TYPE_SHORT:;
331
                                for (line = 0; line < height; line++)
332 8026 nacho
                                        file.readLine(raster.getLineShort(line));
333
                                break;
334
                        case RasterBuf.TYPE_INT:
335 12539 jmvivo
                                for (line = 0; line < height; line++)
336 8026 nacho
                                        file.readLine(raster.getLineInt(line));
337
                                break;
338
                        case RasterBuf.TYPE_FLOAT:
339 12539 jmvivo
                                for (line = 0; line < height; line++)
340 8026 nacho
                                        file.readLine(raster.getLineFloat(line));
341
                                break;
342
                        case RasterBuf.TYPE_DOUBLE:
343 12539 jmvivo
                                for (line = 0; line < height; line++)
344 8026 nacho
                                        file.readLine(raster.getLineDouble(line));
345
                                break;
346
                        case RasterBuf.TYPE_UNDEFINED:break;
347
                        }
348 12539 jmvivo
349 8026 nacho
                } catch (Exception e) {
350
                        e.printStackTrace();
351
                }
352 12539 jmvivo
353 8026 nacho
                return raster;
354
        }
355 12539 jmvivo
356 8026 nacho
        /**
357 12539 jmvivo
         * Asigna al objeto Image los valores con los dato de la imagen contenidos en el
358 8026 nacho
         * 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 12539 jmvivo
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
369 8026 nacho
                         int offset, int scansize) {
370
                image.setRGB(startX, startY, w, h, rgbArray, offset, scansize);
371
        }
372 12539 jmvivo
373 8026 nacho
        /**
374 12539 jmvivo
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
375 8026 nacho
         * 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 12539 jmvivo
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
389 8026 nacho
                         int offset, int scansize, int flags) {
390 12539 jmvivo
                int [] line = new int[rgbArray.length];
391 8026 nacho
                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 12539 jmvivo
404 8026 nacho
        /**
405 12539 jmvivo
         * Asigna al objeto Image la mezcla entre los valores que ya tiene y los valores
406 8026 nacho
         * 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 12539 jmvivo
        protected void setRGBLine(BufferedImage image, int startX, int startY, int w, int h, int[] rgbArray,
421 8026 nacho
                         int offset, int scansize, int origBand, int destBandFlag) {
422 12539 jmvivo
                int [] line = new int[rgbArray.length];
423 8026 nacho
                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 12539 jmvivo
434 8026 nacho
                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 12539 jmvivo
444 8026 nacho
                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 12539 jmvivo
456 8026 nacho
        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 12539 jmvivo
478 8026 nacho
                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 12539 jmvivo
483 8026 nacho
                        if (dFileAspect > dWindowAspect) {
484
                          height =(int)((double)width/dFileAspect);
485
                        } else {
486
                          width = (int)((double)height*dFileAspect);
487
                        }
488
                }
489
490 8631 nacho
//                 Set the view
491
                boolean[] orientation = getOrientation();
492
                file.setView(v.minX(), v.maxY(), v.maxX(), v.minY(),
493
                        width, height, orientation);
494 8026 nacho
                setStep(file.stepArrayX, file.stepArrayY);
495
496
                if(width<=0)width=1;
497
                if(height<=0)height=1;
498 12539 jmvivo
499 8026 nacho
                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 8631 nacho
                                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 12539 jmvivo
                                        }
511 8631 nacho
                                }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 8026 nacho
                                }
517
                                return img;
518
                        }else{
519
                                Image image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
520 8631 nacho
                                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 12539 jmvivo
                                        }
525 8631 nacho
                                }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 8026 nacho
                                return image;
532
                        }
533
                } catch (Exception e) {
534
                        // TODO Auto-generated catch block
535
                        e.printStackTrace();
536
                }
537 12539 jmvivo
538 8026 nacho
                return img;
539
        }
540 12539 jmvivo
541 8026 nacho
        /* (non-Javadoc)
542
         * @see org.cresques.io.GeoRasterFile#getData(int, int, int)
543
         */
544
        public Object getData(int x, int y, int band) {
545 10645 nacho
                if(file != null){
546
                        Object[] data = file.getData(x, y);
547
                        return data[band];
548
                }
549 8026 nacho
                return null;
550
        }
551 12539 jmvivo
552 8026 nacho
        /**
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 12539 jmvivo
562 8026 nacho
                return null;
563
        }
564 12539 jmvivo
565
        public RasterBuf getWindowRaster(double x, double y, double w, double h, BandList bandList, RasterBuf rasterBuf) {
566 8026 nacho
                Extent selectedExtent = new Extent(x, y, x + w, y - h);
567
                setView(selectedExtent);
568 12539 jmvivo
569 8026 nacho
                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 12539 jmvivo
579 8026 nacho
                try {
580
                        file.readWindow(rasterBuf, bandList, x, y, width, height);
581
                } catch (Exception e) {
582
                        e.printStackTrace();
583
                }
584 12539 jmvivo
585 8026 nacho
                return rasterBuf;
586
        }
587 12539 jmvivo
588 10645 nacho
        /*
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 12539 jmvivo
        public RasterBuf getWindowRaster(double minX, double minY, double maxX, double maxY, int bufWidth, int bufHeight, BandList bandList, RasterBuf rasterBuf) {
593 10645 nacho
                Extent selectedExtent = new Extent(minX, minY, maxX, maxY);
594
                setView(selectedExtent);
595 12539 jmvivo
596 10645 nacho
                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 12539 jmvivo
606 10645 nacho
                try {
607
                        file.readWindow(rasterBuf, bandList, minX, maxY, maxX, minY, width, height, bufWidth, bufHeight);
608
                } catch (Exception e) {
609
                        e.printStackTrace();
610
                }
611 12539 jmvivo
612 10645 nacho
                return rasterBuf;
613
        }
614 12539 jmvivo
615 8026 nacho
        public RasterBuf getWindowRaster(int x, int y, int w, int h, BandList bandList, RasterBuf rasterBuf) {
616
                try {
617
                        setView(
618 12539 jmvivo
                        new Extent( Utilities.getMapRectFromPxRect(getExtent().toRectangle2D(),
619
                                                getWidth(),
620 8026 nacho
                                                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 12539 jmvivo
630 10645 nacho
        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 12539 jmvivo
634 10645 nacho
                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 12539 jmvivo
640 10645 nacho
                return rasterBuf;
641
        }
642 12539 jmvivo
643 8026 nacho
        /**
644
         * Obtiene la zona (Norte / Sur)
645
         * @return true si la zona es norte y false si es sur
646
         */
647 12539 jmvivo
648 8026 nacho
        public boolean getZone(){
649 12539 jmvivo
650 8026 nacho
                return false;
651
        }
652 12539 jmvivo
653 8026 nacho
        /**
654
         *Devuelve el n?mero de zona UTM
655 12539 jmvivo
         *@return N?mero de zona
656 8026 nacho
         */
657 12539 jmvivo
658 8026 nacho
        public int getUTM(){
659 12539 jmvivo
660
                return 0;
661 8026 nacho
        }
662 12539 jmvivo
663 8026 nacho
        /**
664
         * Obtiene el sistema de coordenadas geograficas
665
         * @return Sistema de coordenadas geogr?ficas
666
         */
667
        public String getGeogCS(){
668 12539 jmvivo
                return new String("");
669 8026 nacho
        }
670 12539 jmvivo
671 8026 nacho
        /**
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 12539 jmvivo
682 8026 nacho
        /**
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 12539 jmvivo
692 8026 nacho
        /**
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 12539 jmvivo
                else
700 8026 nacho
                        return false;
701
        }
702 12539 jmvivo
703 8026 nacho
        /**
704
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
705 12539 jmvivo
         * el valor de esta variable cada vez que dibuja.
706 8026 nacho
         * @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 12539 jmvivo
                else
712 8026 nacho
                        return false;
713
        }
714 12539 jmvivo
715 8026 nacho
        /**
716
         * Obtiene los par?metros de la transformaci?n af?n que corresponde con los elementos de
717
         * un fichero tfw.
718 12539 jmvivo
         * <UL>
719 8026 nacho
         * <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 12539 jmvivo
         *
729 8631 nacho
         * 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 8026 nacho
         * @return vector de double con los elementos de la transformaci?n af?n.
732
         */
733
        public double[] getTransform(){
734 8631 nacho
                if(file != null && file.trans != null && !this.rmfExists())
735 8026 nacho
                        return file.trans.adfgeotransform;
736 8631 nacho
                else{
737
                        if(this.rmfExists){
738 12539 jmvivo
                                double[] rmfGeoref = {        rmfTransform.getTranslateX(),
739 8631 nacho
                                                                                rmfTransform.getScaleX(),
740 12539 jmvivo
                                                                                rmfTransform.getShearX(),
741 8631 nacho
                                                                                rmfTransform.getTranslateY(),
742
                                                                                rmfTransform.getShearY(),
743
                                                                                rmfTransform.getScaleY()};
744
                                return rmfGeoref;
745
                        }
746
                        return null;
747
                }
748 12539 jmvivo
749 8026 nacho
        }
750 10645 nacho
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 12539 jmvivo
759 10645 nacho
        /*
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 12539 jmvivo
767 10645 nacho
        public void readPalette(){
768
                file.readPalette();
769
        }
770 12543 maquerol
771 8026 nacho
}
772
773