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

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

    
261
                        if(file != null)
262
                                bandCount = file.getRasterCount();
263
                } catch (GdalException e) {
264
                        throw new NotSupportedExtensionException("Extension not supported", e);
265
                } catch(Exception e) {
266
                        System.out.println("Error en GdalOpen");
267
                        e.printStackTrace();
268
                        file = null;
269
                }
270

    
271
                //Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
272
                int[] dt = new int[file.getDataType().length];
273
                for (int i = 0; i < dt.length; i++)
274
                        dt[i] = GdalNative.getRasterBufTypeFromGdalType(file.getDataType()[i]);
275
                setDataType(dt);
276

    
277
                super.init();
278

    
279
                try {
280
                        loadFromRmf(getRmfBlocksManager());
281
                } catch (ParsingException e) {
282
                        //No lee desde rmf
283
                        logger.debug("Problems reading from the RMF file", e);
284
                }
285
        }
286

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

    
330
        /*
331
         * (non-Javadoc)
332
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
333
         */
334
        public String translateFileName(String fileName) {
335
                if(fileName.endsWith("hdr"))
336
                        return fileName.substring(0, fileName.lastIndexOf("."));
337
                return fileName;
338
        }
339

    
340
        /**
341
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
342
         * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
343
         * ha de estar en coordenadas del fichero.
344
         */
345
        public void setView(Extent e) {
346
                viewRequest = new ExtentImpl(e);
347
        }
348

    
349
        /*
350
         * (non-Javadoc)
351
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
352
         */
353
        public Extent getView() {
354
                return viewRequest;
355
        }
356

    
357
        /*
358
         * (non-Javadoc)
359
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
360
         */
361
        public double getWidth() {
362
                return file.width;
363
        }
364

    
365
        /*
366
         * (non-Javadoc)
367
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
368
         */
369
        public double getHeight() {
370
                return file.height;
371
        }
372

    
373
        /**
374
         * Read a line from the file
375
         * @param line
376
         * @param band
377
         * @return
378
         * @throws InvalidSetViewException
379
         * @throws FileNotOpenException
380
         * @throws RasterDriverException
381
         * @Deprecated This operation is deprecated because is not useful and in the future
382
         * it will not be maintained. The abstract operation has dissapear
383
         */
384
        public Object readCompleteLine(int line, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
385
                if(line > this.getHeight() || band > this.getBandCount())
386
                        throw new InvalidSetViewException("Request out of grid");
387

    
388
                try{
389
                        return file.readCompleteLine(line, band);
390
                }catch(GdalException e){
391
                        throw new RasterDriverException("Error reading data from Gdal library");
392
                }
393
        }
394

    
395
        /*
396
         *  (non-Javadoc)
397
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
398
         */
399
        public Object readBlock(int pos, int blockHeight, double scale)
400
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
401
                if(pos < 0)
402
                        throw new InvalidSetViewException("Request out of grid");
403

    
404
                if((pos + blockHeight) > getHeight())
405
                        blockHeight = Math.abs(((int)getHeight()) - pos);
406
                try{
407
                        return file.readBlock(pos, blockHeight, scale);
408
                }catch(GdalException e){
409
                        throw new RasterDriverException("Error reading data from Gdal library");
410
                }
411
        }
412

    
413
        /*
414
         * (non-Javadoc)
415
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
416
         */
417
        public Object getData(int x, int y, int band)throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
418
                if(file != null){
419
                        if(x < 0 || y < 0 || x >= file.width || y >= file.height)
420
                                throw new InvalidSetViewException("Request out of grid");
421
                        Object[] data = file.getData(x, y);
422
                        return data[band];
423
                }
424
                throw new FileNotOpenException("GdalNative not exist");
425
        }
426
        
427
        /*
428
         * (non-Javadoc)
429
         * @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)
430
         */
431
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
432
                        BandList bandList, TileListener listener, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
433
                 
434
        }
435

    
436
        /*
437
         * (non-Javadoc)
438
         * @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)
439
         */
440
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf, TaskStatus status) 
441
                throws ProcessInterruptedException, RasterDriverException {
442
                setView(ex);
443

    
444
                try {
445
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
446
                                        rasterBuf.getWidth(), rasterBuf.getHeight(), true, status);
447
                } catch (GdalException e) {
448
                        throw new RasterDriverException("Error reading data", e);
449
                }
450

    
451
                return rasterBuf;
452
        }
453

    
454
        /*
455
         * (non-Javadoc)
456
         * @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)
457
         */
458
        public Buffer getWindow(double ulx, double uly, double w, double h, 
459
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
460
                //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
461
                //tenemos que averiguar si lrx es x + w o x -w, asi como si lry es y + h o y - h
462
                Extent ext = getExtent();
463
                Point2D pInit = rasterToWorld(new Point2D.Double(0, 0));
464
                Point2D pEnd = rasterToWorld(new Point2D.Double(getWidth(), getHeight()));
465
                double wRaster = Math.abs(pEnd.getX() - pInit.getX());
466
                double hRaster = Math.abs(pEnd.getY() - pInit.getY());
467
                double lrx = (((ext.getULX() - wRaster) > ext.maxX()) || ((ext.getULX() - wRaster) < ext.minX())) ? (ulx + w) : (ulx - w);
468
                double lry = (((ext.getULY() - hRaster) > ext.maxY()) || ((ext.getULY() - hRaster) < ext.minY())) ? (uly + h) : (uly - h);
469

    
470
                Extent selectedExtent = new ExtentImpl(ulx, uly, lrx, lry);
471
                setView(selectedExtent);
472

    
473
                try {
474
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
475
                                        rasterBuf.getWidth(), rasterBuf.getHeight(), adjustToExtent, status);
476
                } catch (GdalException e) {
477
                        throw new RasterDriverException("Error reading data");
478
                }
479

    
480
                return rasterBuf;
481
        }
482

    
483
        /*
484
         * (non-Javadoc)
485
         * @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)
486
         */
487
        public Buffer getWindow(Extent extent, int bufWidth, int bufHeight, 
488
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
489
                //draw method
490
                setView(extent);
491

    
492
                double width = 0;
493
                double height = 0;
494

    
495
                Point2D ul = new Point2D.Double(viewRequest.getULX(), viewRequest.getULY());
496
                Point2D lr = new Point2D.Double(viewRequest.getLRX(), viewRequest.getLRY());
497
                ul = worldToRaster(ul);
498
                lr = worldToRaster(lr);
499
                ul.setLocation(ul.getX() < 0 ? 1 : ul.getX(), ul.getY() < 0 ? 1 : ul.getY());
500
                lr.setLocation(lr.getX() < 0 ? 1 : lr.getX(), lr.getY() < 0 ? 1 : lr.getY());
501
                
502
                ul.setLocation(ul.getX() - 0.5, ul.getY() - 0.5);
503
                lr.setLocation(lr.getX() - 0.5, lr.getY() - 0.5);
504
                
505
                adjustPoints(ul, lr);
506
                
507
                width = Math.abs(((int)lr.getX()) - ((int)ul.getX())) + 1;
508
                height = Math.abs(((int)lr.getY()) - ((int)ul.getY())) + 1;
509

    
510
                try {
511
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), 
512
                                        width, height, bufWidth, bufHeight, adjustToExtent, status);
513
                } catch (GdalException e) {
514
                        throw new RasterDriverException("Error reading data");
515
                }
516

    
517
                return rasterBuf;
518
        }
519
        
520

    
521
        private void adjustPoints(Point2D ul, Point2D lr) {
522
                double a = (ul.getX() - (int)ul.getX());
523
                double b = (ul.getY() - (int)ul.getY());
524
                ul.setLocation(        (a > 0.95 || a < 0.05) ? Math.round(ul.getX()) : ul.getX(), 
525
                                                (b > 0.95 || b < 0.05) ? Math.round(ul.getY()) : ul.getY());
526
                lr.setLocation(        (a > 0.95 || a < 0.05) ? Math.round(lr.getX()) : lr.getX(), 
527
                                                (b > 0.95 || b < 0.05) ? Math.round(lr.getY()) : lr.getY());
528
        }
529

    
530
        /*
531
         * (non-Javadoc)
532
         * @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)
533
         */
534
        public Buffer getWindow(int x, int y, int w, int h, 
535
                        BandList bandList, Buffer rasterBuf, TaskStatus status) 
536
                throws ProcessInterruptedException, RasterDriverException {
537
                try {
538
                        setView(
539
                        new ExtentImpl( rasterUtil.getMapRectFromPxRect(getExtent().toRectangle2D(),
540
                                                getWidth(),
541
                                                getHeight(),
542
                                                new Rectangle2D.Double(x, y, w, h)))
543
                        );
544
                        file.readWindow(rasterBuf, bandList, x, y, w, h, status);
545
                } catch (GdalException e) {
546
                        throw new RasterDriverException("Error reading data");
547
                }
548
                return rasterBuf;
549
        }
550

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

    
562
        /*
563
         * (non-Javadoc)
564
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
565
         */
566
        public DataStoreMetadata getMetadata() {
567
                if(file != null)
568
                        return file.metadata;
569
                else
570
                        return null;
571
        }
572

    
573
        /*
574
         * (non-Javadoc)
575
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
576
         */
577
        public Transparency getTransparency() {
578
                return file.fileTransparency;
579
        }
580

    
581
        /*
582
         * (non-Javadoc)
583
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
584
         */
585
        public boolean isGeoreferenced() {
586
                if(file != null)
587
                        return file.georeferenced;
588
                else
589
                        return false;
590
        }
591

    
592
        /**
593
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
594
         * el valor de esta variable cada vez que dibuja.
595
         * @return true si se ha supersampleado y false si no se ha hecho.
596
         */
597
        public boolean isSupersampling() {
598
                if(file != null)
599
                        return file.isSupersampling;
600
                else
601
                        return false;
602
        }
603

    
604
        public GdalNative getNative(){
605
                return file;
606
        }
607

    
608
        /*
609
         * (non-Javadoc)
610
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
611
         */
612
        public void setAffineTransform(AffineTransform t){
613
                super.setAffineTransform(t);
614
                file.setExternalTransform(t);
615
        }
616

    
617
        /*
618
         * (non-Javadoc)
619
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
620
         */
621
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
622
                if(band >= getBandCount())
623
                        throw new BandAccessException("Wrong band");
624
                try {
625
                        return file.getRasterBand(band + 1).getOverviewCount();
626
                } catch (GdalException e) {
627
                        throw new RasterDriverException("");
628
                }
629
        }
630

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

    
647
        /*
648
         * (non-Javadoc)
649
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
650
         */
651
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
652
                if (band >= getBandCount())
653
                        throw new BandAccessException("Wrong band");
654
                try {
655
                        if (overview >= file.getRasterBand(band + 1).getOverviewCount())
656
                                throw new BandAccessException("Wrong overview count");
657
                        return file.getRasterBand(band + 1).getOverview(overview).getRasterBandYSize();
658
                } catch (GdalException e) {
659
                        throw new RasterDriverException("");
660
                }
661
        }
662

    
663
        /*
664
         * (non-Javadoc)
665
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isOverviewsSupported()
666
         */
667
        public boolean isOverviewsSupported() {
668
                return true;
669
        }
670

    
671
        /*
672
         * (non-Javadoc)
673
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
674
         */
675
        public boolean isReproyectable() {
676
                return true;
677
        }
678
        
679
        /*
680
         * (non-Javadoc)
681
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#needEnhanced()
682
         */
683
        public boolean needEnhanced() {
684
                return (getDataType()[0] != Buffer.TYPE_BYTE); 
685
                //Desconozco pq raz?n estaba esta condici?n. Quiz?s haya que volver a a?adirla
686
                //Eliminada 30/5/2013
687
                //|| (getBandCount() == 1 && getDataType()[0] == Buffer.TYPE_BYTE));
688
        }
689

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