Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.io / org.gvsig.raster.io.base / src / main / java / org / gvsig / fmap / dal / coverage / dataset / io / gdal / GdalProvider.java @ 472

History | View | Annotate | Download (24.9 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.fmap.dal.coverage.dataset.io.gdal;
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.dataset.io.tile.downloader.FileTileServer;
39
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
40
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
41
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
42
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
43
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
44
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
45
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
46
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
47
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
48
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters;
49
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
50
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
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.store.AbstractRasterDataParameters;
58
import org.gvsig.raster.impl.store.DefaultStoreFactory;
59
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
60
import org.gvsig.raster.impl.store.properties.DataStoreMetadata;
61
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
62
import org.gvsig.tools.ToolsLocator;
63
import org.gvsig.tools.extensionpoint.ExtensionPoint;
64
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
65

    
66
import es.gva.cit.jgdal.GdalException;
67
/**
68
 * Clase que representa al driver de acceso a datos de gdal.
69
 *
70
 * @author Luis W. Sevilla
71
 * @author Nacho Brodin (nachobrodin@gmail.com)
72
 */
73
public class GdalProvider extends DefaultRasterProvider {
74
        public static String        NAME                     = "Gdal Store";
75
        public static String        DESCRIPTION              = "Gdal Raster file";
76
        public static final String  METADATA_DEFINITION_NAME = "GdalStore";
77
        
78
        public static final String  FORMAT_GTiff    = "GTiff";
79
        public static final String  FORMAT_VRT      = "VRT";
80
        public static final String  FORMAT_NITF     = "NITF";
81
        public static final String  FORMAT_HFA      = "HFA";
82
        public static final String  FORMAT_ELAS     = "ELAS";
83
        public static final String  FORMAT_MEM      = "MEM";
84
        public static final String  FORMAT_BMP      = "BMP";
85
        public static final String  FORMAT_PCIDSK   = "PCIDSK";
86
        public static final String  FORMAT_ILWIS    = "ILWIS";
87
        public static final String  FORMAT_HDF4     = "HDF4Image";
88
        public static final String  FORMAT_PNM      = "PNM";
89
        public static final String  FORMAT_ENVI     = "ENVI";
90
        public static final String  FORMAT_EHDR     = "EHdr";
91
        public static final String  FORMAT_PAUX     = "PAux";
92
        public static final String  FORMAT_MFF      = "MFF";
93
        public static final String  FORMAT_MFF2     = "MFF2";
94
        public static final String  FORMAT_BT       = "BT";
95
        public static final String  FORMAT_IDA      = "IDA";
96
        public static final String  FORMAT_RMF      = "RMF";
97
        public static final String  FORMAT_RST      = "RST";
98
        public static final String  FORMAT_LEVELLER = "Leveller";
99
        public static final String  FORMAT_TERRAGEN = "Terragen";
100
        public static final String  FORMAT_ERS      = "ERS";
101
        public static final String  FORMAT_INGR     = "INGR";
102
        public static final String  FORMAT_GSAG     = "GSAG";
103
        public static final String  FORMAT_GSBG     = "GSBG";
104
        public static final String  FORMAT_ADRG     = "ADRG";
105
        public static final int     BAND_HEIGHT     = 64;
106
        protected GdalNative        file            = null;
107
        private static String[]     formatList      = new String[] {
108
                "bmp", 
109
                "gif",
110
                "tif",
111
                "tiff",
112
                "jpg",
113
                "jpeg",
114
                "png",
115
                "vrt",
116
                "dat", // Envi
117
                "lan", // Erdas
118
                "gis", // Erdas
119
                "img", // Erdas
120
                "pix", // PCI Geomatics
121
                "aux", // PCI Geomatics
122
                "adf", // ESRI Grids
123
                "mpr", // Ilwis
124
                "mpl", // Ilwis
125
                "map", // PC Raster
126
                "asc",
127
                "pgm", //Ficheros PNM en escala de grises
128
                "ppm", //Ficheros PNM en RGB
129
                "rst", //IDRISIS
130
                "rmf", //Raster Matrix Format
131
                "nos",
132
                "kap",
133
                "hdr",
134
                "raw",
135
                "ers",};
136

    
137
        private Extent       viewRequest   = null;
138
        private TileServer   tileServer    = null;
139

    
140
        public static void register() {
141
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
142
                ExtensionPoint point = extensionPoints.get("RasterReader");
143
                addFormatsToRegistry(point, GdalProvider.class);
144
                
145
                point = extensionPoints.get("DefaultDriver");
146
                point.append("reader", "", GdalProvider.class);
147
                
148
                RasterLocator.getManager().registerFileProvidersTiled(GdalProvider.class);
149
                
150
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
151
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
152
                        dataman.registerStoreProvider(NAME,
153
                                        GdalProvider.class, GdalDataParameters.class);
154
                }
155
                
156
                if(DALFileLocator.getFilesystemServerExplorerManager() != null)
157
                        DALFileLocator.getFilesystemServerExplorerManager().registerProvider(
158
                                        NAME, DESCRIPTION,
159
                                        GdalFilesystemServerExplorer.class);
160
                
161
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
162
        }
163
        
164
        /**
165
         * Adds the list of formats to the extension point
166
         * @param point
167
         */
168
        public static void addFormatsToRegistry(ExtensionPoint point, Class<?> c) {
169
                for (int i = 0; i < formatList.length; i++) {
170
                        point.append(formatList[i], "", c);
171
                }
172
        }
173
        
174
        /*
175
         * (non-Javadoc)
176
         * @see org.gvsig.raster.impl.provider.RasterProvider#registerTileProviderFormats(java.lang.Class)
177
         */
178
        public void registerTileProviderFormats(Class<RasterProvider> c) {
179
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
180
                ExtensionPoint point = extensionPoints.get("RasterReader");
181
                for (int i = 0; i < formatList.length; i++) {
182
                        point.append(formatList[i], "", c);
183
                }
184
        }
185
        
186
        /**
187
         * Returns true if the extension is supported and false if doesn't
188
         * @param ext
189
         * @return
190
         */
191
        public static boolean isExtensionSupported(String ext) {
192
                for (int i = 0; i < formatList.length; i++) {
193
                        if(formatList[i].compareTo(ext) == 0)
194
                                return true;
195
                }
196
                return false;
197
        }
198
        
199
        public GdalProvider() {}
200
        
201
        /**
202
         * Constructor. Abre el dataset.
203
         * @param proj Proyecci?n
204
         * @param fName Nombre del fichero
205
         * @throws NotSupportedExtensionException
206
         */
207
        public GdalProvider(String params) throws NotSupportedExtensionException {
208
                super(params);
209
                if(params instanceof String) {
210
                        GdalDataParameters p = new GdalDataParameters();
211
                        p.setURI((String)params);
212
                        super.init(p, null, ToolsLocator.getDynObjectManager()
213
                                        .createDynObject(
214
                                                        MetadataLocator.getMetadataManager().getDefinition(
215
                                                                        DataStore.METADATA_DEFINITION_NAME)));
216
                        init(p, null);
217
                }
218
        }
219
        
220
        public GdalProvider (GdalDataParameters params,
221
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
222
                super(params, storeServices, ToolsLocator.getDynObjectManager()
223
                                .createDynObject(
224
                                                MetadataLocator.getMetadataManager().getDefinition(
225
                                                                DataStore.METADATA_DEFINITION_NAME)));
226
                init(params, storeServices);
227
        }
228

    
229
        /**
230
         * Crea las referencias al fichero y carga
231
         * las estructuras con la informaci?n y los metadatos.
232
         * @param proj Proyecci?n
233
         * @param param Parametros de carga
234
         * @throws NotSupportedExtensionException
235
         */
236
        public void init (AbstractRasterDataParameters params,
237
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
238
                try {
239
                        if(((RasterFileStoreParameters)params).getFile().exists()) {
240
                                setParam(storeServices, params);
241
                                validRmf(params.getURI());
242
                                file = new GdalNative(translateFileName(params.getURI()));
243
                                setColorTable(file.palette);
244
                                noData = file.getNoDataValue();
245
                                wktProjection = file.getProjectionRef();
246
                                //CrsWkt crs = new CrsWkt(wktProjection);
247
                                //IProjection proj = CRSFactory.getCRS("EPSG:23030");
248
                                noDataEnabled = file.existsNoDataValue();
249
                                ownTransformation = file.getOwnTransformation();
250
                                externalTransformation = (AffineTransform)ownTransformation.clone();
251
                                load();
252
                        } else
253
                                setParam(storeServices, params);
254
                                //TODO: FUNCIONALIDAD: Formatos gestionados por gdal que no tienen extensi?n. Estos tendr?n un objeto IRegistrableRasterFormat Por ej: Grass
255
                        bandCount = file.getRasterCount();
256
                } catch (GdalException e) {
257
                        throw new NotSupportedExtensionException("Extension not supported", e);
258
                } catch(Exception e) {
259
                                System.out.println("Error en GdalOpen");
260
                                e.printStackTrace();
261
                                file = null;
262
                }
263

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

    
270
                super.init();
271

    
272
                try {
273
                        loadFromRmf(getRmfBlocksManager());
274
                } catch (ParsingException e) {
275
                        //No lee desde rmf
276
                }
277
        }
278

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

    
322
        /*
323
         * (non-Javadoc)
324
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
325
         */
326
        public void close() {
327
                try {
328
                        if(file != null){
329
                                file.close();
330
                                file = null;
331
                        }
332
                        viewRequest = null;
333
                } catch (GdalException e) {
334
                        e.printStackTrace();
335
                }
336
        }
337

    
338
        /*
339
         * (non-Javadoc)
340
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
341
         */
342
        public String translateFileName(String fileName) {
343
                if(fileName.endsWith("hdr"))
344
                        return fileName.substring(0, fileName.lastIndexOf("."));
345
                return fileName;
346
        }
347

    
348
        /**
349
         * Asigna el extent de la vista actual. existe un fichero .rmf debemos hacer una transformaci?n
350
         * de la vista asignada ya que la petici?n viene en coordenadas del fichero .rmf y la vista (v)
351
         * ha de estar en coordenadas del fichero.
352
         */
353
        public void setView(Extent e) {
354
                viewRequest = new ExtentImpl(e);
355
        }
356

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

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

    
373
        /*
374
         * (non-Javadoc)
375
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
376
         */
377
        public double getHeight() {
378
                return file.height;
379
        }
380

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

    
396
                try{
397
                        return file.readCompleteLine(line, band);
398
                }catch(GdalException e){
399
                        throw new RasterDriverException("Error reading data from Gdal library");
400
                }
401
        }
402

    
403
        /*
404
         *  (non-Javadoc)
405
         * @see org.gvsig.raster.dataset.RasterDataset#readBlock(int, int)
406
         */
407
        public Object readBlock(int pos, int blockHeight)
408
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
409
                if(pos < 0)
410
                        throw new InvalidSetViewException("Request out of grid");
411

    
412
                if((pos + blockHeight) > getHeight())
413
                        blockHeight = Math.abs(((int)getHeight()) - pos);
414
                try{
415
                        return file.readBlock(pos, blockHeight);
416
                }catch(GdalException e){
417
                        throw new RasterDriverException("Error reading data from Gdal library");
418
                }
419
        }
420

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

    
444
        /*
445
         * (non-Javadoc)
446
         * @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)
447
         */
448
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
449
                throws ProcessInterruptedException, RasterDriverException {
450
                setView(ex);
451

    
452
                try {
453
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight(), true);
454
                } catch (GdalException e) {
455
                        throw new RasterDriverException("Error reading data");
456
                }
457

    
458
                return rasterBuf;
459
        }
460

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

    
477
                Extent selectedExtent = new ExtentImpl(ulx, uly, lrx, lry);
478
                setView(selectedExtent);
479

    
480
                try {
481
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight(), adjustToExtent);
482
                } catch (GdalException e) {
483
                        throw new RasterDriverException("Error reading data");
484
                }
485

    
486
                return rasterBuf;
487
        }
488

    
489
        /*
490
         * (non-Javadoc)
491
         * @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)
492
         */
493
        public Buffer getWindow(Extent extent, 
494
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
495
                setView(extent);
496

    
497
                double width = 0;
498
                double height = 0;
499

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

    
515
                try {
516
                        file.readWindow(rasterBuf, bandList, viewRequest.getULX(), viewRequest.getULY(), viewRequest.getLRX(), viewRequest.getLRY(), width, height, bufWidth, bufHeight, adjustToExtent);
517
                } catch (GdalException e) {
518
                        throw new RasterDriverException("Error reading data");
519
                }
520

    
521
                return rasterBuf;
522
        }
523
        
524

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

    
534
        /*
535
         * (non-Javadoc)
536
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
537
         */
538
        public Buffer getWindow(int x, int y, 
539
                        BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
540
                try {
541
                        int w = rasterBuf.getWidth();
542
                        int h = rasterBuf.getHeight();
543
                        setView(
544
                        new ExtentImpl( rasterUtil.getMapRectFromPxRect(getExtent().toRectangle2D(),
545
                                                getWidth(),
546
                                                getHeight(),
547
                                                new Rectangle2D.Double(x, y, w, h)))
548
                        );
549
                        file.readWindow(rasterBuf, bandList, x, y, w, h);
550
                } catch (GdalException e) {
551
                        throw new RasterDriverException("Error reading data");
552
                }
553
                return rasterBuf;
554
        }
555

    
556
        /*
557
         * (non-Javadoc)
558
         * @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)
559
         */
560
        public Buffer getWindow(int x, int y, int w, int h, 
561
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
562
                try {
563
                        setView(
564
                        new ExtentImpl( rasterUtil.getMapRectFromPxRect(getExtent().toRectangle2D(),
565
                                                getWidth(),
566
                                                getHeight(),
567
                                                new Rectangle2D.Double(x, y, w, h)))
568
                        );
569
                        file.readWindow(rasterBuf, bandList, x, y, w, h, bufWidth, bufHeight);
570
                } catch (GdalException e) {
571
                        throw new RasterDriverException("Error reading data");
572
                }
573
                return rasterBuf;
574
        }
575

    
576
        /*
577
         * (non-Javadoc)
578
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
579
         */
580
        public int getBlockSize(){
581
                if(file != null)
582
                        return file.getBlockSize();
583
                else
584
                        return 0;
585
        }
586

    
587
        /*
588
         * (non-Javadoc)
589
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getMetadata()
590
         */
591
        public DataStoreMetadata getMetadata() {
592
                if(file != null)
593
                        return file.metadata;
594
                else
595
                        return null;
596
        }
597

    
598
        /*
599
         * (non-Javadoc)
600
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getColorInterpretation()
601
         */
602
        public DataStoreColorInterpretation getColorInterpretation(){
603
                if(file != null)
604
                        return file.colorInterpr;
605
                return null;
606
        }
607

    
608
        /*
609
         * (non-Javadoc)
610
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setColorInterpretation(org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation)
611
         */
612
        public void setColorInterpretation(DataStoreColorInterpretation colorInterpretation){
613
                if(file != null)
614
                        file.colorInterpr = colorInterpretation;
615
        }
616

    
617
        /*
618
         * (non-Javadoc)
619
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
620
         */
621
        public DataStoreTransparency getTransparency() {
622
                return file.fileTransparency;
623
        }
624

    
625
        /*
626
         * (non-Javadoc)
627
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isGeoreferenced()
628
         */
629
        public boolean isGeoreferenced() {
630
                if(file != null)
631
                        return file.georeferenced;
632
                else
633
                        return false;
634
        }
635

    
636
        /**
637
         * Informa de si el driver ha supersampleado en el ?ltimo dibujado. Es el driver el que colocar?
638
         * el valor de esta variable cada vez que dibuja.
639
         * @return true si se ha supersampleado y false si no se ha hecho.
640
         */
641
        public boolean isSupersampling() {
642
                if(file != null)
643
                        return file.isSupersampling;
644
                else
645
                        return false;
646
        }
647

    
648
        public GdalNative getNative(){
649
                return file;
650
        }
651

    
652
        /*
653
         * (non-Javadoc)
654
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
655
         */
656
        public void setAffineTransform(AffineTransform t){
657
                super.setAffineTransform(t);
658
                file.setExternalTransform(t);
659
        }
660

    
661
        /*
662
         * (non-Javadoc)
663
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
664
         */
665
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
666
                if(band >= getBandCount())
667
                        throw new BandAccessException("Wrong band");
668
                try {
669
                        return file.getRasterBand(band + 1).getOverviewCount();
670
                } catch (GdalException e) {
671
                        throw new RasterDriverException("");
672
                }
673
        }
674

    
675
        /*
676
         * (non-Javadoc)
677
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
678
         */
679
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
680
                if (band >= getBandCount())
681
                        throw new BandAccessException("Wrong band");
682
                try {
683
                        if (overview >= file.getRasterBand(band + 1).getOverviewCount())
684
                                throw new BandAccessException("Wrong overview count");
685
                        return file.getRasterBand(band + 1).getOverview(overview).getRasterBandXSize();
686
                } catch (GdalException e) {
687
                        throw new RasterDriverException("");
688
                }
689
        }
690

    
691
        /*
692
         * (non-Javadoc)
693
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
694
         */
695
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
696
                if (band >= getBandCount())
697
                        throw new BandAccessException("Wrong band");
698
                try {
699
                        if (overview >= file.getRasterBand(band + 1).getOverviewCount())
700
                                throw new BandAccessException("Wrong overview count");
701
                        return file.getRasterBand(band + 1).getOverview(overview).getRasterBandYSize();
702
                } catch (GdalException e) {
703
                        throw new RasterDriverException("");
704
                }
705
        }
706

    
707
        /*
708
         * (non-Javadoc)
709
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
710
         */
711
        public boolean overviewsSupport() {
712
                return true;
713
        }
714

    
715
        /*
716
         * (non-Javadoc)
717
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
718
         */
719
        public boolean isReproyectable() {
720
                return true;
721
        }
722

    
723
        /*
724
         * (non-Javadoc)
725
         * @see org.gvsig.fmap.dal.spi.DataStoreProvider#getName()
726
         */
727
        public String getName() {
728
                return NAME;
729
        }
730
        
731
        /*
732
         * (non-Javadoc)
733
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
734
         */
735
        public void setStatus(RasterProvider provider) {
736
                if(provider instanceof GdalProvider) {
737
                        //Not implemented yet
738
                }
739
        }
740
        
741
        /*
742
         * (non-Javadoc)
743
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
744
         */
745
        public TileServer getTileServer() {
746
                if(tileServer == null)
747
                        tileServer = new FileTileServer(this);
748
                return tileServer;
749
        }
750
}