Statistics
| Revision:

gvsig-raster / org.gvsig.raster.gdal / trunk / org.gvsig.raster.gdal / org.gvsig.raster.gdal.io / src / main / java / org / gvsig / raster / gdal / io / GdalProvider.java @ 1083

History | View | Annotate | Download (23.8 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
package org.gvsig.raster.gdal.io;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.Point2D;
26
import java.awt.geom.Rectangle2D;
27
import java.io.BufferedReader;
28
import java.io.File;
29
import java.io.FileNotFoundException;
30
import java.io.FileReader;
31
import java.io.IOException;
32

    
33
import org.gvsig.fmap.dal.DALFileLocator;
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataStore;
36
import org.gvsig.fmap.dal.coverage.RasterLocator;
37
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
38
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
39
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
40
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
41
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
42
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
43
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
44
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
45
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
46
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
47
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
48
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
49
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
50
import org.gvsig.jgdal.GdalException;
51
import org.gvsig.metadata.MetadataLocator;
52
import org.gvsig.raster.cache.tile.provider.TileListener;
53
import org.gvsig.raster.cache.tile.provider.TileServer;
54
import org.gvsig.raster.impl.datastruct.ExtentImpl;
55
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
56
import org.gvsig.raster.impl.provider.RasterProvider;
57
import org.gvsig.raster.impl.provider.tile.FileTileServer;
58
import org.gvsig.raster.impl.store.AbstractRasterDataParameters;
59
import org.gvsig.raster.impl.store.DefaultStoreFactory;
60
import org.gvsig.raster.impl.store.properties.DataStoreMetadata;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.dynobject.DynObject;
63
import org.gvsig.tools.extensionpoint.ExtensionPoint;
64
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
65
import org.gvsig.tools.task.Cancellable;
66
import org.gvsig.tools.task.TaskStatus;
67
/**
68
 * This class represents the data access for gdal formats.
69
 *
70
 * @author Luis W. Sevilla
71
 * @author Nacho Brodin (nachobrodin@gmail.com)
72
 */
73
@SuppressWarnings("deprecation")
74
public class GdalProvider extends DefaultRasterProvider {
75
        public static String        NAME                     = "Gdal Store";
76
        public static String        DESCRIPTION              = "Gdal Raster file";
77
        public static final String  METADATA_DEFINITION_NAME = "GdalStore";
78
        
79
        public static final String  FORMAT_GTiff    = "GTiff";
80
        public static final String  FORMAT_VRT      = "VRT";
81
        public static final String  FORMAT_NITF     = "NITF";
82
        public static final String  FORMAT_HFA      = "HFA";
83
        public static final String  FORMAT_ELAS     = "ELAS";
84
        public static final String  FORMAT_MEM      = "MEM";
85
        public static final String  FORMAT_BMP      = "BMP";
86
        public static final String  FORMAT_PCIDSK   = "PCIDSK";
87
        public static final String  FORMAT_ILWIS    = "ILWIS";
88
        public static final String  FORMAT_HDF4     = "HDF4Image";
89
        public static final String  FORMAT_PNM      = "PNM";
90
        public static final String  FORMAT_ENVI     = "ENVI";
91
        public static final String  FORMAT_EHDR     = "EHdr";
92
        public static final String  FORMAT_PAUX     = "PAux";
93
        public static final String  FORMAT_MFF      = "MFF";
94
        public static final String  FORMAT_MFF2     = "MFF2";
95
        public static final String  FORMAT_BT       = "BT";
96
        public static final String  FORMAT_IDA      = "IDA";
97
        public static final String  FORMAT_RMF      = "RMF";
98
        public static final String  FORMAT_RST      = "RST";
99
        public static final String  FORMAT_LEVELLER = "Leveller";
100
        public static final String  FORMAT_TERRAGEN = "Terragen";
101
        public static final String  FORMAT_ERS      = "ERS";
102
        public static final String  FORMAT_INGR     = "INGR";
103
        public static final String  FORMAT_GSAG     = "GSAG";
104
        public static final String  FORMAT_GSBG     = "GSBG";
105
        public static final String  FORMAT_ADRG     = "ADRG";
106
        public static final int     BAND_HEIGHT     = 64;
107
        protected GdalNative        file            = null;
108
        private Extent              viewRequest     = null;
109
        protected static String[]   formatList      = null;
110
        
111
        public static void register() {
112
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
113
                registerFormats();
114
                
115
                ExtensionPoint point = extensionPoints.get("DefaultRasterProvider");
116
                point.append("reader", GdalProvider.NAME, GdalProvider.class);
117
                
118
                RasterLocator.getManager().registerFileProvidersTiled(GdalProvider.class);
119
                
120
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
121
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
122
                        dataman.registerStoreProvider(NAME,
123
                                        GdalProvider.class, GdalDataParameters.class);
124
                }
125
                
126
                if(DALFileLocator.getFilesystemServerExplorerManager() != null)
127
                        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
128
                                        NAME, DESCRIPTION,
129
                                        GdalFilesystemServerExplorer.class);
130
                
131
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
132
        }
133
        
134
        private static void registerFormats() {
135
                formatList      = new String[] {
136
                                "bmp", 
137
                                "gif",
138
                                "tif",
139
                                "tiff",
140
                                "jpg",
141
                                "jpeg",
142
                                "png",
143
                                "vrt",
144
                                "dat", // Envi
145
                                "lan", // Erdas
146
                                "gis", // Erdas
147
                                "img", // Erdas
148
                                "pix", // PCI Geomatics
149
                                "aux", // PCI Geomatics
150
                                "adf", // ESRI Grids
151
                                "mpr", // Ilwis
152
                                "mpl", // Ilwis
153
                                "map", // PC Raster
154
                                "asc",
155
                                "pgm", //Ficheros PNM en escala de grises
156
                                "ppm", //Ficheros PNM en RGB
157
                                "rst", //IDRISIS
158
                                "rmf", //Raster Matrix Format
159
                                "nos",
160
                                "kap",
161
                                "hdr",
162
                                "raw",
163
                                "ers"};
164
                for (int i = 0; i < formatList.length; i++) 
165
                        RasterLocator.getManager().addFormat(formatList[i], GdalProvider.class);
166
        }
167
        
168
        /*
169
         * (non-Javadoc)
170
         * @see org.gvsig.raster.impl.provider.RasterProvider#getFormatList()
171
         */
172
        public String[] getFormatList() {
173
                return formatList;
174
        }
175
        
176
        /**
177
         * Returns true if the extension is supported and false if doesn't
178
         * @param ext
179
         * @return
180
         */
181
        public boolean isExtensionSupported(String ext) {
182
                if(ext.indexOf(".") != -1)
183
                        ext = ext.substring(ext.lastIndexOf(".") + 1, ext.length());
184
                for (int i = 0; i < formatList.length; i++) {
185
                        if(formatList[i].compareTo(ext) == 0)
186
                                return true;
187
                }
188
                return false;
189
        }
190
        
191
        /**
192
         * Mandatory constructor to instantiate an empty provider
193
         */
194
        public GdalProvider() {
195
        }
196
        
197
        /**
198
         * Constructor. Abre el dataset.
199
         * @param proj Proyecci?n
200
         * @param fName Nombre del fichero
201
         * @throws NotSupportedExtensionException
202
         */
203
        public GdalProvider(String params) throws NotSupportedExtensionException {
204
                super(params);
205
                if(params instanceof String) {
206
                        GdalDataParameters p = new GdalDataParameters();
207
                        p.setURI((String)params);
208
                        super.init(p, null, ToolsLocator.getDynObjectManager()
209
                                        .createDynObject(
210
                                                        MetadataLocator.getMetadataManager().getDefinition(
211
                                                                        DataStore.METADATA_DEFINITION_NAME)));
212
                        init(p, null);
213
                }
214
        }
215
        
216
        public GdalProvider (GdalDataParameters params,
217
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
218
                super(params, storeServices, ToolsLocator.getDynObjectManager()
219
                                .createDynObject(
220
                                                MetadataLocator.getMetadataManager().getDefinition(
221
                                                                DataStore.METADATA_DEFINITION_NAME)));
222
                init(params, storeServices);
223
        }
224
        
225
        public GdalProvider(AbstractRasterDataParameters params,
226
                        DataStoreProviderServices storeServices, DynObject metadata) {
227
                super(params, storeServices, metadata);
228
        }
229
        
230
        /**
231
         * Creates file references and loads structures with the information and metadata
232
         * @param params load parameters
233
         * @throws NotSupportedExtensionException
234
         */
235
        public void init (AbstractRasterDataParameters params,
236
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
237
                try {
238
                        setParam(storeServices, params);
239
                        validRmf(params.getURI());
240
                        file = new GdalNative(translateFileName(params.getURI()));
241
                        setColorInterpretation(file.colorInterpr);
242
                        setColorTable(file.palette);
243
                        noData = file.getNoDataValue();
244
                        wktProjection = file.getProjectionRef();
245
                        //CrsWkt crs = new CrsWkt(wktProjection);
246
                        //IProjection proj = CRSFactory.getCRS("EPSG:23030");
247
                        ownTransformation = file.getOwnTransformation();
248
                        externalTransformation = (AffineTransform)ownTransformation.clone();
249
                        load();
250

    
251
                        if(file != null)
252
                                bandCount = file.getRasterCount();
253
                } catch (GdalException e) {
254
                        throw new NotSupportedExtensionException("Extension not supported", e);
255
                } catch(Exception e) {
256
                                System.out.println("Error en GdalOpen");
257
                                e.printStackTrace();
258
                                file = null;
259
                }
260

    
261
                //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
262
                int[] dt = new int[file.getDataType().length];
263
                for (int i = 0; i < dt.length; i++)
264
                        dt[i] = rasterUtil.getRasterBufTypeFromGdalType(file.getDataType()[i]);
265
                setDataType(dt);
266

    
267
                super.init();
268

    
269
                try {
270
                        loadFromRmf(getRmfBlocksManager());
271
                } catch (ParsingException e) {
272
                        //No lee desde rmf
273
                }
274
        }
275

    
276
        /**
277
         * Comprueba si el fichero abierto es un RasterMetaFile o una imagen
278
         * raster.
279
         * @throws GdalException
280
         */
281
        private void validRmf(String file) throws GdalException {
282
                if(file.endsWith(".rmf")) {
283
                        File f = new File(file);
284
                        FileReader fr;
285
                        try {
286
                                fr = new FileReader(f);
287
                                BufferedReader br = new BufferedReader(fr);
288
                                char[] buffer = new char[5];
289
                                        br.read(buffer);
290
                                        StringBuffer st = new StringBuffer(new String(buffer));
291
                                        if(st.toString().equals("<?xml"))
292
                                                throw new GdalException("RasterMetaFile");
293
                        } catch (FileNotFoundException e) {
294
                                throw new GdalException("File Not Found");
295
                        } catch (IOException e) {
296
                                throw new GdalException("");
297
                        }
298
                }
299
        }
300
        
301
        /*
302
         * (non-Javadoc)
303
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
304
         */
305
        public RasterProvider load() {
306
                return this;
307
        }
308
        
309
        /*
310
         * (non-Javadoc)
311
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
312
         */
313
        public boolean isOpen() {
314
                if(file != null && file.isOpen())
315
                        return true;
316
                return false;
317
        }
318

    
319
        /*
320
         * (non-Javadoc)
321
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
322
         */
323
        public String translateFileName(String fileName) {
324
                if(fileName.endsWith("hdr"))
325
                        return fileName.substring(0, fileName.lastIndexOf("."));
326
                return fileName;
327
        }
328

    
329
        /**
330
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
331
         * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
332
         * ha de estar en coordenadas del fichero.
333
         */
334
        public void setView(Extent e) {
335
                viewRequest = new ExtentImpl(e);
336
        }
337

    
338
        /*
339
         * (non-Javadoc)
340
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
341
         */
342
        public Extent getView() {
343
                return viewRequest;
344
        }
345

    
346
        /*
347
         * (non-Javadoc)
348
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
349
         */
350
        public double getWidth() {
351
                return file.width;
352
        }
353

    
354
        /*
355
         * (non-Javadoc)
356
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
357
         */
358
        public double getHeight() {
359
                return file.height;
360
        }
361

    
362
        /**
363
         * Read a line from the file
364
         * @param line
365
         * @param band
366
         * @return
367
         * @throws InvalidSetViewException
368
         * @throws FileNotOpenException
369
         * @throws RasterDriverException
370
         * @Deprecated This operation is deprecated because is not useful and in the future
371
         * it will not be maintained. The abstract operation has dissapear
372
         */
373
        public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
374
                if(line > this.getHeight() || band > this.getBandCount())
375
                        throw new InvalidSetViewException("Request out of grid");
376

    
377
                try{
378
                        return file.readCompleteLine(line, band);
379
                }catch(GdalException e){
380
                        throw new RasterDriverException("Error reading data from Gdal library");
381
                }
382
        }
383

    
384
        /*
385
         *  (non-Javadoc)
386
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
387
         */
388
        public Object readBlock(int pos, int blockHeight, double scale)
389
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
390
                if(pos < 0)
391
                        throw new InvalidSetViewException("Request out of grid");
392

    
393
                if((pos + blockHeight) > getHeight())
394
                        blockHeight = Math.abs(((int)getHeight()) - pos);
395
                try{
396
                        return file.readBlock(pos, blockHeight, scale);
397
                }catch(GdalException e){
398
                        throw new RasterDriverException("Error reading data from Gdal library");
399
                }
400
        }
401

    
402
        /*
403
         * (non-Javadoc)
404
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
405
         */
406
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
407
                if(file != null){
408
                        if(x < 0 || y < 0 || x >= file.width || y >= file.height)
409
                                throw new InvalidSetViewException("Request out of grid");
410
                        Object[] data = file.getData(x, y);
411
                        return data[band];
412
                }
413
                throw new FileNotOpenException("GdalNative not exist");
414
        }
415
        
416
        /*
417
         * (non-Javadoc)
418
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
419
         */
420
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
421
                        BandList bandList, TileListener listener, Cancellable status) throws ProcessInterruptedException, RasterDriverException {
422
                 
423
        }
424

    
425
        /*
426
         * (non-Javadoc)
427
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
428
         */
429
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf, TaskStatus status) 
430
                throws ProcessInterruptedException, RasterDriverException {
431
                setView(ex);
432

    
433
                try {
434
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
435
                                        rasterBuf.getWidth(), rasterBuf.getHeight(), true, status);
436
                } catch (GdalException e) {
437
                        throw new RasterDriverException("Error reading data", e);
438
                }
439

    
440
                return rasterBuf;
441
        }
442

    
443
        /*
444
         * (non-Javadoc)
445
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
446
         */
447
        public Buffer getWindow(double ulx, double uly, double w, double h, 
448
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
449
                //El incremento o decremento de las X e Y depende de los signos de rotaci?n y escala en la matriz de transformaci?n. Por esto
450
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
451
                Extent ext = getExtent();
452
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
453
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
454
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
455
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
456
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
457
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
458

    
459
                Extent selectedExtent = new ExtentImpl(ulx, uly, lrx, lry);
460
                setView(selectedExtent);
461

    
462
                try {
463
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
464
                                        rasterBuf.getWidth(), rasterBuf.getHeight(), adjustToExtent, status);
465
                } catch (GdalException e) {
466
                        throw new RasterDriverException("Error reading data");
467
                }
468

    
469
                return rasterBuf;
470
        }
471

    
472
        /*
473
         * (non-Javadoc)
474
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
475
         */
476
        public Buffer getWindow(Extent extent, int bufWidth, int bufHeight, 
477
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
478
                //draw method
479
                setView(extent);
480

    
481
                double width = 0;
482
                double height = 0;
483

    
484
                Point2D ul = new Point2D.Double(viewRequest.getULX(), viewRequest.getULY());
485
                Point2D lr = new Point2D.Double(viewRequest.getLRX(), viewRequest.getLRY());
486
                ul = worldToRaster(ul);
487
                lr = worldToRaster(lr);
488
                ul.setLocation(ul.getX() < 0 ? 1 : ul.getX(), ul.getY() < 0 ? 1 : ul.getY());
489
                lr.setLocation(lr.getX() < 0 ? 1 : lr.getX(), lr.getY() < 0 ? 1 : lr.getY());
490
                
491
                ul.setLocation(ul.getX() - 0.5, ul.getY() - 0.5);
492
                lr.setLocation(lr.getX() - 0.5, lr.getY() - 0.5);
493
                
494
                adjustPoints(ul, lr);
495
                
496
                width = Math.abs(((int)lr.getX()) - ((int)ul.getX())) + 1;
497
                height = Math.abs(((int)lr.getY()) - ((int)ul.getY())) + 1;
498

    
499
                try {
500
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
501
                                        width, height, bufWidth, bufHeight, adjustToExtent, status);
502
                } catch (GdalException e) {
503
                        throw new RasterDriverException("Error reading data");
504
                }
505

    
506
                return rasterBuf;
507
        }
508
        
509

    
510
        private void adjustPoints(Point2D ul, Point2D lr) {
511
                double a = (ul.getX() - (int)ul.getX());
512
                double b = (ul.getY() - (int)ul.getY());
513
                ul.setLocation(        (a > 0.95 || a < 0.05) ? Math.round(ul.getX()) : ul.getX(), 
514
                                                (b > 0.95 || b < 0.05) ? Math.round(ul.getY()) : ul.getY());
515
                lr.setLocation(        (a > 0.95 || a < 0.05) ? Math.round(lr.getX()) : lr.getX(), 
516
                                                (b > 0.95 || b < 0.05) ? Math.round(lr.getY()) : lr.getY());
517
        }
518

    
519
        /*
520
         * (non-Javadoc)
521
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
522
         */
523
        public Buffer getWindow(int x, int y, int w, int h, 
524
                        BandList bandList, Buffer rasterBuf, TaskStatus status) 
525
                throws ProcessInterruptedException, RasterDriverException {
526
                try {
527
                        setView(
528
                        new ExtentImpl( rasterUtil.getMapRectFromPxRect(getExtent().toRectangle2D(),
529
                                                getWidth(),
530
                                                getHeight(),
531
                                                new Rectangle2D.Double(x, y, w, h)))
532
                        );
533
                        file.readWindow(rasterBuf, bandList, x, y, w, h, status);
534
                } catch (GdalException e) {
535
                        throw new RasterDriverException("Error reading data");
536
                }
537
                return rasterBuf;
538
        }
539

    
540
        /*
541
         * (non-Javadoc)
542
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
543
         */
544
        public int getBlockSize(){
545
                if(file != null)
546
                        return file.getBlockSize();
547
                else
548
                        return 0;
549
        }
550

    
551
        /*
552
         * (non-Javadoc)
553
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
554
         */
555
        public DataStoreMetadata getMetadata() {
556
                if(file != null)
557
                        return file.metadata;
558
                else
559
                        return null;
560
        }
561

    
562
        /*
563
         * (non-Javadoc)
564
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
565
         */
566
        public Transparency getTransparency() {
567
                return file.fileTransparency;
568
        }
569

    
570
        /*
571
         * (non-Javadoc)
572
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
573
         */
574
        public boolean isGeoreferenced() {
575
                if(file != null)
576
                        return file.georeferenced;
577
                else
578
                        return false;
579
        }
580

    
581
        /**
582
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
583
         * el valor de esta variable cada vez que dibuja.
584
         * @return true si se ha supersampleado y false si no se ha hecho.
585
         */
586
        public boolean isSupersampling() {
587
                if(file != null)
588
                        return file.isSupersampling;
589
                else
590
                        return false;
591
        }
592

    
593
        public GdalNative getNative(){
594
                return file;
595
        }
596

    
597
        /*
598
         * (non-Javadoc)
599
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
600
         */
601
        public void setAffineTransform(AffineTransform t){
602
                super.setAffineTransform(t);
603
                file.setExternalTransform(t);
604
        }
605

    
606
        /*
607
         * (non-Javadoc)
608
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
609
         */
610
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
611
                if(band >= getBandCount())
612
                        throw new BandAccessException("Wrong band");
613
                try {
614
                        return file.getRasterBand(band + 1).getOverviewCount();
615
                } catch (GdalException e) {
616
                        throw new RasterDriverException("");
617
                }
618
        }
619

    
620
        /*
621
         * (non-Javadoc)
622
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
623
         */
624
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
625
                if (band >= getBandCount())
626
                        throw new BandAccessException("Wrong band");
627
                try {
628
                        if (overview >= file.getRasterBand(band + 1).getOverviewCount())
629
                                throw new BandAccessException("Wrong overview count");
630
                        return file.getRasterBand(band + 1).getOverview(overview).getRasterBandXSize();
631
                } catch (GdalException e) {
632
                        throw new RasterDriverException("");
633
                }
634
        }
635

    
636
        /*
637
         * (non-Javadoc)
638
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
639
         */
640
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
641
                if (band >= getBandCount())
642
                        throw new BandAccessException("Wrong band");
643
                try {
644
                        if (overview >= file.getRasterBand(band + 1).getOverviewCount())
645
                                throw new BandAccessException("Wrong overview count");
646
                        return file.getRasterBand(band + 1).getOverview(overview).getRasterBandYSize();
647
                } catch (GdalException e) {
648
                        throw new RasterDriverException("");
649
                }
650
        }
651

    
652
        /*
653
         * (non-Javadoc)
654
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isOverviewsSupported()
655
         */
656
        public boolean isOverviewsSupported() {
657
                return true;
658
        }
659

    
660
        /*
661
         * (non-Javadoc)
662
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
663
         */
664
        public boolean isReproyectable() {
665
                return true;
666
        }
667
        
668
        /*
669
         * (non-Javadoc)
670
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#needEnhanced()
671
         */
672
        public boolean needEnhanced() {
673
                return (getDataType()[0] != Buffer.TYPE_BYTE || 
674
                                (getBandCount() == 1 && getDataType()[0] == Buffer.TYPE_BYTE));
675
        }
676

    
677
        /*
678
         * (non-Javadoc)
679
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
680
         */
681
        public String getName() {
682
                return NAME;
683
        }
684
        
685
        /*
686
         * (non-Javadoc)
687
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
688
         */
689
        public void setStatus(RasterProvider provider) {
690
                if(provider instanceof GdalProvider) {
691
                        //Not implemented yet
692
                }
693
        }
694
        
695
        /*
696
         * (non-Javadoc)
697
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
698
         */
699
        public TileServer getTileServer() {
700
                if(tileServer == null)
701
                        tileServer = new FileTileServer(this);
702
                return tileServer;
703
        }
704
        
705
        /*
706
         * (non-Javadoc)
707
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
708
         */
709
        public void close() {
710
                if(file != null){
711
                        file.dispose();
712
                }
713
                try {
714
                        finalize();
715
                } catch (Throwable e) {
716
                }
717
        }
718
        
719
        /*
720
         * (non-Javadoc)
721
         * @see java.lang.Object#finalize()
722
         */
723
        protected void finalize() throws Throwable {
724
                file           = null;
725
                viewRequest    = null;
726
                if(formatList != null) {
727
                        for (int i = 0; i < formatList.length; i++) {
728
                                formatList[i] = null;
729
                        }
730
                        formatList = null;
731
                }
732
                super.finalize();
733
        }
734
}