Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wcs / trunk / org.gvsig.raster.wcs / org.gvsig.raster.wcs.io / src / main / java / org / gvsig / raster / wcs / io / WCSProvider.java @ 418

History | View | Annotate | Download (34.6 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.wcs.io;
23

    
24
import java.awt.geom.AffineTransform;
25
import java.awt.geom.NoninvertibleTransformException;
26
import java.awt.geom.Point2D;
27
import java.awt.geom.Rectangle2D;
28
import java.io.File;
29
import java.io.IOException;
30
import java.net.URL;
31
import java.util.Hashtable;
32

    
33
import org.gvsig.compat.net.ICancellable;
34
import org.gvsig.fmap.dal.DALLocator;
35
import org.gvsig.fmap.dal.DataStore;
36
import org.gvsig.fmap.dal.DataStoreParameters;
37
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
38
import org.gvsig.fmap.dal.coverage.dataset.io.gdal.GdalProvider;
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.BandNotFoundInListException;
43
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
44
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
45
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
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.exception.RemoteServiceException;
49
import org.gvsig.fmap.dal.coverage.explorer.WCSServerExplorer;
50
import org.gvsig.fmap.dal.coverage.store.parameter.WCSDataParameters;
51
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
52
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
53
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
54
import org.gvsig.metadata.MetadataLocator;
55
import org.gvsig.raster.cache.tile.TileCacheLocator;
56
import org.gvsig.raster.cache.tile.TileCacheManager;
57
import org.gvsig.raster.cache.tile.exception.TileGettingException;
58
import org.gvsig.raster.cache.tile.provider.TileListener;
59
import org.gvsig.raster.cache.tile.provider.TileServer;
60
import org.gvsig.raster.impl.DefaultRasterManager;
61
import org.gvsig.raster.impl.datastruct.BandListImpl;
62
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
63
import org.gvsig.raster.impl.datastruct.ExtentImpl;
64
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
65
import org.gvsig.raster.impl.provider.RasterProvider;
66
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
67
import org.gvsig.raster.impl.store.AbstractRasterDataStore;
68
import org.gvsig.raster.impl.store.DefaultStoreFactory;
69
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
70
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
71
import org.gvsig.raster.impl.store.properties.RemoteDataStoreStatistics;
72
import org.gvsig.remoteclient.wcs.WCSStatus;
73
import org.gvsig.tools.ToolsLocator;
74
import org.gvsig.tools.extensionpoint.ExtensionPoint;
75
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
76
/**
77
 * Clase que representa al driver de acceso a datos de wcs.
78
 *
79
 * @author Nacho Brodin (nachobrodin@gmail.com)
80
 */
81
public class WCSProvider extends DefaultRasterProvider implements RemoteRasterProvider {
82
        public static String                NAME                     = "Wcs Store";
83
        public static String                DESCRIPTION              = "Wcs Raster file";
84
        public static final String          METADATA_DEFINITION_NAME = "WcsStore";
85
        
86
        private Extent                      viewRequest              = null;
87
        private static Hashtable<URL, WCSConnector>    
88
                                            drivers                  = new Hashtable<URL, WCSConnector> ();
89
        private boolean                     open                     = false;
90
        private DataStoreTransparency       fileTransparency         = null;
91
        private File                        lastRequest              = null;
92
        
93
        public static void register() {
94
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
95
                ExtensionPoint point = extensionPoints.get("RasterReader");
96
                point.append("wcs", "", WCSProvider.class);
97
                
98
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
99
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
100
                        dataman.registerStoreProvider(NAME,
101
                                        WCSProvider.class, WCSDataParametersImpl.class);
102
                }
103

    
104
                if (!dataman.getExplorerProviders().contains(WCSServerExplorerImpl.NAME)) {
105
                        dataman.registerExplorerProvider(WCSServerExplorer.NAME, WCSServerExplorerImpl.class, DefaultWCSServerExplorerParameters.class);
106
                }
107
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
108
        }
109
        
110
        public WCSProvider() throws NotSupportedExtensionException {
111
                super();
112
        }
113
        
114
        /**
115
         * Constructor. Abre el dataset.
116
         * @param proj Proyecci?n
117
         * @param fName Nombre del fichero
118
         * @throws NotSupportedExtensionException
119
         */
120
        public WCSProvider(String params) throws NotSupportedExtensionException {
121
                super(params);
122
                if(params instanceof String) {
123
                        WCSDataParametersImpl p = new WCSDataParametersImpl();
124
                        p.setHost((String)params);
125
                        super.init(p, null, ToolsLocator.getDynObjectManager()
126
                                        .createDynObject(
127
                                                        MetadataLocator.getMetadataManager().getDefinition(
128
                                                                        DataStore.METADATA_DEFINITION_NAME)));
129
                        init(p, null);
130
                }
131
        }
132
        
133
        public WCSProvider(WCSDataParametersImpl params,
134
                        AbstractRasterDataStore storeServices) throws NotSupportedExtensionException {
135
                super(params, storeServices, ToolsLocator.getDynObjectManager()
136
                                .createDynObject(
137
                                                MetadataLocator.getMetadataManager().getDefinition(
138
                                                                DataStore.METADATA_DEFINITION_NAME)));
139
                init(params, storeServices);
140
        }
141
        
142
        /**
143
         * Gets the connector from the URL
144
         * @return
145
         * @throws RemoteServiceException
146
         */
147
        private WCSConnector getConnector() throws RemoteServiceException {
148
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
149
                URL url = null;
150
                try {
151
                        url = new URL(p.getHost());
152
                } catch (Exception e) {
153
                        throw new RemoteServiceException("Malformed URL",e);
154
                }
155
                try {
156
                        return WCSProvider.getConnectorFromURL(url);
157
                } catch (IOException e) {
158
                        throw new RemoteServiceException("Error getting the connector",e);
159
                }
160
        }
161
        
162
        /**
163
         * Crea las referencias al fichero y carga
164
         * las estructuras con la informaci?n y los metadatos.
165
         * @param proj Proyecci?n
166
         * @param param Parametros de carga
167
         * @throws NotSupportedExtensionException
168
         */
169
        public void init (DataStoreParameters params,
170
                        DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
171
                setParam(params);
172
                
173
                try {
174
                        getDataTypeFromServer();
175
                } catch (RasterDriverException e) {
176
                        e.printStackTrace();
177
                }
178
                
179
                //Temporal hastq que sepamos el tipo de dato y n?mero de bandas
180
                mustTileRequest = false;
181
                
182
                stats = new RemoteDataStoreStatistics(this);
183
        }
184
        
185
        public static final WCSConnector getConnectorFromURL(URL url) throws IOException {
186
                WCSConnector drv = (WCSConnector) drivers.get(url);
187
                if (drv == null) {
188
                        drv = new WCSConnector(url);
189
                        drivers.put(url, drv);
190
                }
191
                return drv;
192
        }
193
        
194
        /**
195
         * Obtiene el objeto que contiene que contiene la interpretaci?n de
196
         * color por banda
197
         * @return
198
         */
199
        public DataStoreColorInterpretation getColorInterpretation() {
200
                if(colorInterpretation == null) {
201
                        colorInterpretation = new DataStoreColorInterpretation();
202
                        colorInterpretation.initColorInterpretation(getBandCount());
203
                        if(getBandCount() == 3) {
204
                                colorInterpretation.setColorInterpValue(0, DataStoreColorInterpretation.RED_BAND);
205
                                colorInterpretation.setColorInterpValue(1, DataStoreColorInterpretation.GREEN_BAND);
206
                                colorInterpretation.setColorInterpValue(2, DataStoreColorInterpretation.BLUE_BAND);
207
                        } else {
208
                                for (int i = 0; i < getBandCount(); i++) {
209
                                        colorInterpretation.setColorInterpValue(i, DataStoreColorInterpretation.GRAY_BAND);
210
                                }
211
                        }
212
                }
213
                return colorInterpretation;
214
        }
215
        
216
        /*
217
         * (non-Javadoc)
218
         * @see org.gvsig.fmap.dal.coverage.dataset.RasterDataSet#getAffineTransform()
219
         */
220
        public AffineTransform getAffineTransform() {
221
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
222
                Point2D resolution = null;
223
                try {
224
                        resolution = getConnector().getMaxResolution(p.getCoverageName());
225
                        Extent e = getExtent();
226
                        ownTransformation = new AffineTransform(
227
                                        resolution.getX(), 
228
                                        0, 
229
                                        0, 
230
                                        -resolution.getY(), 
231
                                        e.getULX() - (resolution.getX() / 2),
232
                                        e.getULY() - (resolution.getY() / 2));
233
                        externalTransformation = (AffineTransform) ownTransformation.clone();
234
                        return ownTransformation;
235
                } catch (RemoteServiceException e1) {
236
                        e1.printStackTrace();
237
                }
238
                return null;
239
        }
240
        
241
        /**
242
         * Calcula el extent en coordenadas del mundo real
243
         * @return Extent
244
         */
245
        public Extent getExtent() {
246
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
247
                try {
248
                        Rectangle2D r = getConnector().getFullExtent(p.getCoverageName(), p.getSRSCode());
249
                        return new ExtentImpl(r.getX(),  r.getY() + r.getHeight(), r.getX() + r.getWidth(), r.getY());
250
                } catch (RemoteServiceException e1) {
251
                        e1.printStackTrace();
252
                } catch (IOException e) {
253
                        e.printStackTrace();
254
                }
255
                return null;
256
        }
257

    
258
        
259
        /*
260
         * (non-Javadoc)
261
         * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLayerExtent(java.lang.String, java.lang.String)
262
         */
263
        public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException {
264
                return null;
265
        }
266

    
267
        /*
268
         * (non-Javadoc)
269
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
270
         */
271
        public RasterProvider load() {
272
                return this;
273
        }
274
        
275
        /*
276
         * (non-Javadoc)
277
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
278
         */
279
        public boolean isOpen() {
280
                return open;
281
        }
282

    
283
        /*
284
         * (non-Javadoc)
285
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
286
         */
287
        public void close() {
288
                open = false;
289
        }
290
        
291
        /*
292
         * (non-Javadoc)
293
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
294
         */
295
        public Transparency getTransparency() {
296
                if(fileTransparency == null)
297
                        fileTransparency = new DataStoreTransparency();
298
                return fileTransparency;
299
        }
300

    
301
        /*
302
         * (non-Javadoc)
303
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
304
         */
305
        public String translateFileName(String fileName) {
306
                return fileName;
307
        }
308

    
309
        /*
310
         * (non-Javadoc)
311
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
312
         */
313
        public void setView(Extent e) {
314
                viewRequest = e;
315
        }
316

    
317
        /*
318
         * (non-Javadoc)
319
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
320
         */
321
        public Extent getView() {
322
                return viewRequest;
323
        }
324
        
325
        /*
326
         * (non-Javadoc)
327
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
328
         */
329
        public double getWidth() {
330
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
331
                Point2D resolution = null;
332
                try {
333
                        resolution = getConnector().getMaxResolution(p.getCoverageName());
334
                        Extent e = getExtent();
335
                        return e.width() / resolution.getX(); 
336
                } catch (RemoteServiceException e1) {
337
                        e1.printStackTrace();
338
                }
339
                return 0;
340
        }
341

    
342
        /*
343
         * (non-Javadoc)
344
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
345
         */
346
        public double getHeight() {
347
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
348
                Point2D resolution = null;
349
                try {
350
                        resolution = getConnector().getMaxResolution(p.getCoverageName());
351
                        Extent e = getExtent();
352
                        return e.height() / resolution.getY(); 
353
                } catch (RemoteServiceException e1) {
354
                        e1.printStackTrace();
355
                }
356
                return 0;
357
        }
358

    
359
        /*
360
         * (non-Javadoc)
361
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#readCompleteLine(int, int)
362
         */
363
        public Object readCompleteLine(int line, int band)
364
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
365
                return null;
366
        }
367
        
368
        /**
369
         * When the remote layer has fixed size this method downloads the file and return its reference. 
370
         * File layer has in the long side FIXED_SIZE pixels and the bounding box is complete. This file could be
371
         * useful to build an histogram or calculate statistics. This represents a sample of data.
372
         * @return
373
         * @throws RasterDriverException
374
         */
375
        public File getFileLayer() throws RasterDriverException {
376
                Extent e = getExtent();
377
                Rectangle2D bBox = new Rectangle2D.Double(e.getULX(), e.getLRY(), e.width(), e.height());
378
                WCSStatus wcsStatus = loadWCSStatus(bBox);
379
                
380
                return downloadFile(wcsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), 
381
                                (int)getWidth(), (int)getHeight());
382
        }
383

    
384
        /**
385
         * Reads a complete block of data and returns an tridimensional array of the right type. This function is useful
386
         * to read a file very fast without setting a view. In a WMS service when the size is fixed then it will read the
387
         * entire image but when the source hasn't pixel size it will read a sample of data. This set of data will have
388
         * the size defined in FIXED_SIZE. 
389
         * 
390
         * @param pos Posici?n donde se empieza  a leer
391
         * @param blockHeight Altura m?xima del bloque leido
392
         * @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
393
         * @throws InvalidSetViewException
394
         * @throws FileNotOpenException
395
         * @throws RasterDriverException
396
         */
397
        public Object readBlock(int pos, int blockHeight) 
398
        throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
399
                File lastFile = getFileLayer();
400
                BandList bandList = new BandListImpl();
401
                for (int i = 0; i < 3; i++) {
402
                        try {
403
                                bandList.addBand(new DatasetBandImpl(lastFile.getPath(), pos, Buffer.TYPE_BYTE, 3), pos);
404
                        } catch (BandNotFoundInListException e1) {
405
                        }
406
                }
407
                bandList.setDrawableBands(new int[]{0, 1, 2});
408

    
409
                try {
410
                        GdalProvider driver = new GdalProvider(lastFile.getPath());
411
                        return driver.readBlock(pos, blockHeight);
412
                } catch (NotSupportedExtensionException exc) {
413
                        throw new RasterDriverException("Error building GdalDriver", exc);
414
                }
415
        }
416

    
417
        /*
418
         * (non-Javadoc)
419
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getData(int, int, int)
420
         */
421
        public Object getData(int x, int y, int band)
422
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
423
                return null;
424
        }
425
        
426
        /**
427
         * Gets the georeferencing file name form a raster file
428
         * @param file
429
         * a raster file
430
         * @return
431
         * a georeferencing file
432
         */
433
        private String getWorldFile(String file){                
434
                String worldFile = file;
435
                int index = file.lastIndexOf(".");
436
                if (index > 0) {                        
437
                        worldFile = file.substring(0, index) + getExtensionWorldFile();
438
                } else {
439
                        worldFile = file + getExtensionWorldFile();
440
                }
441
                return worldFile;
442
        }
443
        
444
        /**
445
         * Obtiene la extensi?n del fichero de georreferenciaci?n
446
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
447
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
448
         */
449
        private String getExtensionWorldFile() {
450
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
451
                String extWorldFile = ".wld";
452
                if(p.getFormat().equals("image/tif") || p.getFormat().equals("image/tiff")) {
453
                        extWorldFile = ".tfw";
454
                }
455
                return extWorldFile;
456
        }
457
        
458
        private WCSStatus loadWCSStatus(Rectangle2D bBox) {
459
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
460
                WCSStatus wcsStatus = new WCSStatus();
461
                wcsStatus.setCoveraName(p.getCoverageName());
462
                wcsStatus.setSrs(p.getSRSCode());
463
                wcsStatus.setFormat(p.getFormat());
464
                wcsStatus.setOnlineResource(p.getOnlineResource() != null ? (String) p.getOnlineResource().get("GetCoverage") : null);
465
                wcsStatus.setExtent(bBox);
466
                wcsStatus.setHeight(p.getHeight());
467
                wcsStatus.setWidth(p.getWidth());
468
                wcsStatus.setDepth(p.getDepth());
469
                wcsStatus.setParameters(p.getParameter());
470
                wcsStatus.setTime(p.getTime());
471
                return wcsStatus;
472
        }
473
        
474
        /**
475
         * This function downloads the file and creates the georeferencing file
476
         * @param wcsStatus
477
         * @param ulx
478
         * @param uly
479
         * @param lrx
480
         * @param lry
481
         * @param w
482
         * @param h
483
         * @return
484
         * @throws RasterDriverException
485
         */
486
        private File downloadFile(WCSStatus wcsStatus, double ulx, double uly, double lrx, double lry, int w, int h) throws RasterDriverException {
487
                try {
488
                        lastRequest = getConnector().getCoverage(wcsStatus, ((WCSDataParametersImpl)parameters).getCancellable());
489
                } catch (RemoteServiceException e) {
490
                        throw new RasterDriverException(e.getMessage(), e);
491
                }
492
                
493
                String nameWorldFile = getWorldFile(lastRequest.getPath());
494
                try {
495
                        fileUtil.createWorldFile(nameWorldFile, new ExtentImpl(ulx, uly, lrx, lry), w, h);
496
                } catch (IOException e) {
497
                        throw new RasterDriverException("Error downloading file", e);
498
                }
499

    
500
                return lastRequest;
501
        }
502
        
503
        /**
504
         * Assigns the list of bands RGB and read a window of data
505
         * @param rasterBuf
506
         * @param bandList
507
         * @param lastFile
508
         * @param ulx
509
         * @param uly
510
         * @param lrx
511
         * @param lry
512
         * @return
513
         * @throws RasterDriverException
514
         * @throws ProcessInterruptedException
515
         */
516
        public Buffer getBuffer(Buffer rasterBuf, BandList bandList, File lastFile, 
517
                        double ulx, double uly, double lrx, double lry) throws RasterDriverException, ProcessInterruptedException {
518
                try {
519
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
520
                        String serverName = bandList.getBand(0).getFileName();
521
                        for (int i = 0; i < bandList.getBandCount(); i++) {
522
                                bandList.getBand(i).setFileName(lastFile.getPath());
523
                        }
524
                        
525
                        GdalProvider driver = new GdalProvider(lastFile.getPath());
526
                        Buffer buf = driver.getWindow(new ExtentImpl(ulx, uly, lrx, lry), bandList, rasterBuf);
527
                        
528
                        for (int i = 0; i < bandList.getBandCount(); i++) {
529
                                bandList.getBand(i).setFileName(serverName);
530
                        }
531
                        
532
                        return buf;
533
                } catch (NotSupportedExtensionException e) {
534
                        throw new RasterDriverException("Error building GdalDriver", e);
535
                }
536
        }
537
        
538
        /*
539
         * (non-Javadoc)
540
         * @see org.gvsig.raster.impl.provider.RasterProvider#isTiled()
541
         */
542
        public boolean isTiled() {
543
                return mustTileRequest;
544
        }
545
        
546
        /*
547
         * (non-Javadoc)
548
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
549
         */
550
        public void getWindow(Extent ex, int bufWidth, int bufHeight, 
551
                        BandList bandList, TileListener listener) throws ProcessInterruptedException, RasterDriverException {
552
                 if(mustTileRequest) {
553
                         
554
                 } else {
555
                         Buffer raster = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, 3, true);
556
                         getWindow(ex, bufWidth, bufHeight, bandList, raster, true);
557
                         raster.setDataExtent(ex.toRectangle2D());
558
                         
559
                         TileCacheManager m = TileCacheLocator.getManager();
560
                         org.gvsig.raster.cache.tile.Tile t = m.createTile(-1, 0, 0);
561
                         t.setData(new Object[]{raster});
562
                         t.setUl(new Point2D.Double(ex.getULX(), ex.getULY()));
563
                         t.setLr(new Point2D.Double(ex.getLRX(), ex.getLRY()));
564
                         t.setDownloaderParams("AffineTransform", getAffineTransform());
565
                         t.setDownloaderParams("Tiling", new Boolean(false));
566
                         try {
567
                                 listener.tileReady(t);
568
                         } catch (TileGettingException e) {
569
                                 throw new RasterDriverException("Error throwing a tile", e);
570
                         }
571

    
572
                         //listener.nextBuffer(raster, null, new ExtentImpl(minX, minY, maxX, maxY), getAffineTransform(), null, false);
573
                         listener.endReading();
574
                 }
575
        }
576

    
577
        /*
578
         * (non-Javadoc)
579
         * @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)
580
         */
581
        public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) 
582
                throws ProcessInterruptedException, RasterDriverException {
583
                Rectangle2D bBox = ex.toRectangle2D();
584
                WCSStatus wmsStatus = loadWCSStatus(bBox);
585
                
586
                lastRequest = downloadFile(wmsStatus, ex.getULX(), ex.getULY(), ex.getLRX(), ex.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
587
                
588
                if (lastRequest == null) {
589
                        return rasterBuf;
590
                }
591
                
592
                try {
593
                        GdalProvider driver = new GdalProvider(lastRequest.getPath());
594
                        /*bandCount = driver.getBandCount();
595
                        setDataType(driver.getDataType());
596
                        rasterBuf = changeBufferDataType(getDataType()[0], rasterBuf, bandList);*/
597
                        
598
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
599
                        String serverName = bandList.getBand(0).getFileName();
600
                        for (int i = 0; i < bandList.getBandCount(); i++) {
601
                                bandList.getBand(i).setFileName(lastRequest.getPath());
602
                        }
603
                        
604
                        Buffer buf = driver.getWindow(ex, bandList, rasterBuf);
605
                        
606
                        for (int i = 0; i < bandList.getBandCount(); i++) {
607
                                bandList.getBand(i).setFileName(serverName);
608
                        }
609
                        driver.close();
610
                        return buf;
611
                } catch (NotSupportedExtensionException e) {
612
                        throw new RasterDriverException("Error building GdalDriver", e);
613
                }
614
        }
615

    
616
        /*
617
         * (non-Javadoc)
618
         * @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)
619
         */
620
        public Buffer getWindow(double ulx, double uly, double w, double h, 
621
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
622
                Rectangle2D bBox = new Rectangle2D.Double(ulx, uly, w, h);
623
                WCSStatus wcsStatus = loadWCSStatus(bBox);
624
                
625
                lastRequest = downloadFile(wcsStatus, ulx, uly, ulx + w, uly - h, rasterBuf.getWidth(), rasterBuf.getHeight());
626
                
627
                if (lastRequest == null) {
628
                        return rasterBuf;
629
                }
630
                
631
                try {
632
                        GdalProvider driver = new GdalProvider(lastRequest.getPath());
633
                        /*bandCount = driver.getBandCount();
634
                        setDataType(driver.getDataType());
635
                        rasterBuf = changeBufferDataType(getDataType()[0], rasterBuf, bandList);*/
636
                        
637
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
638
                        String serverName = bandList.getBand(0).getFileName();
639
                        for (int i = 0; i < bandList.getBandCount(); i++) {
640
                                bandList.getBand(i).setFileName(lastRequest.getPath());
641
                        }
642
                        
643
                        Buffer buf = driver.getWindow(ulx, uly, w, h, bandList, rasterBuf, adjustToExtent);
644
                        
645
                        for (int i = 0; i < bandList.getBandCount(); i++) {
646
                                bandList.getBand(i).setFileName(serverName);
647
                        }
648
                        driver.close();
649
                        return buf;
650
                } catch (NotSupportedExtensionException e) {
651
                        throw new RasterDriverException("Error building GdalDriver", e);
652
                }
653
        }
654

    
655
        /*
656
         * (non-Javadoc)
657
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
658
         */
659
        public Buffer getWindow(Extent extent, 
660
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
661
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
662
                Rectangle2D bBox = null;
663
                p.setWidth(bufWidth);
664
                p.setHeight(bufHeight);
665
                
666
                /*if(p.getExtent() != null) {
667
                        bBox = p.getExtent();
668
                        extent = new ExtentImpl(p.getExtent());
669
                } else {*/
670
                        bBox = extent.toRectangle2D();
671
                //}
672
                
673
                //extent.toRectangle2D();//new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly));
674
                WCSStatus wcsStatus = loadWCSStatus(bBox);
675
                lastRequest = downloadFile(wcsStatus, extent.getULX(), extent.getULY(), extent.getLRX(), extent.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
676
                
677
                if (lastRequest == null) {
678
                        return rasterBuf;
679
                }
680
                
681
                try {
682
                        GdalProvider driver = new GdalProvider(lastRequest.getPath());
683
                        /*bandCount = driver.getBandCount();
684
                        setDataType(driver.getDataType());
685
                        rasterBuf = changeBufferDataType(getDataType()[0], rasterBuf, bandList);*/
686
                        
687
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
688
                        String serverName = bandList.getBand(0).getFileName();
689
                        for (int i = 0; i < bandList.getBandCount(); i++) {
690
                                bandList.getBand(i).setFileName(lastRequest.getPath());
691
                        }
692
                        
693
                        Buffer buf = driver.getWindow(extent, bufWidth, bufHeight, bandList, rasterBuf, adjustToExtent);
694

    
695
                        for (int i = 0; i < bandList.getBandCount(); i++) {
696
                                bandList.getBand(i).setFileName(serverName);
697
                        }
698
                        driver.close();
699
                        return buf;
700
                } catch (NotSupportedExtensionException e) {
701
                        throw new RasterDriverException("Error building GdalDriver", e);
702
                }
703
        }
704

    
705
        /*
706
         * (non-Javadoc)
707
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
708
         */
709
        public Buffer getWindow(int x, int y, int w, int h, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
710
                Point2D p1 = rasterToWorld(new Point2D.Double(x, y));
711
                Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h));
712
                Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY()));
713
                WCSStatus wcsStatus = loadWCSStatus(bBox);
714
                
715
                lastRequest = downloadFile(wcsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight());
716
                
717
                if (lastRequest == null) {
718
                        return rasterBuf;
719
                }
720

    
721
                GdalProvider driver = null;
722
                try {
723
                        driver = new GdalProvider(lastRequest.getPath());
724
                        /*bandCount = driver.getBandCount();
725
                        setDataType(driver.getDataType());
726
                        rasterBuf = changeBufferDataType(getDataType()[0], rasterBuf,bandList);*/
727
                        
728
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
729
                        String serverName = bandList.getBand(0).getFileName();
730
                        for (int i = 0; i < bandList.getBandCount(); i++) {
731
                                bandList.getBand(i).setFileName(lastRequest.getPath());
732
                        }
733
                        
734
                        Buffer buf = driver.getWindow(0, 0, w, h, rasterBuf.getWidth(), rasterBuf.getHeight(), bandList, rasterBuf);
735

    
736
                        for (int i = 0; i < bandList.getBandCount(); i++) {
737
                                bandList.getBand(i).setFileName(serverName);
738
                        }
739
                        driver.close();
740
                        return buf;
741
                } catch (NotSupportedExtensionException e) {
742
                        throw new RasterDriverException("Error building GdalDriver", e);
743
                }
744
        }
745
        
746
        /**
747
         * When a WCS provider is opened the information of data type and number of bands is not
748
         * available. Only after the first time a raster has been downloaded it can be know.
749
         * @param newDataType
750
         * @param buf
751
         * @param bandList
752
         * @return
753
         * @throws RasterDriverException 
754
         */
755
        private void getDataTypeFromServer() throws RasterDriverException {
756
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
757
                Extent ext = getExtent();
758
                Rectangle2D bBox = ext.toRectangle2D();
759
                int w = 0;
760
                int h = 0;
761
                if(ext.width() > ext.height()) {
762
                        w = 200;
763
                        h = (int)((ext.height() * w) / ext.width());
764
                } else {
765
                        h = 200;
766
                        w = (int)((ext.width() * h) / ext.height());
767
                }
768
                p.setWidth(w);
769
                p.setHeight(h);
770
                p.setExtent(bBox);
771
                WCSStatus wcsStatus = loadWCSStatus(bBox);
772
                
773
                lastRequest = downloadFile(wcsStatus, ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), w, h);
774
                GdalProvider driver;
775
                try {
776
                        driver = new GdalProvider(lastRequest.getPath());
777
                        setDataType(driver.getDataType());
778
                        bandCount = driver.getBandCount();
779
                        driver.close();
780
                } catch (NotSupportedExtensionException e) {
781
                        throw new RasterDriverException("", e);
782
                }
783
        }
784
        
785
        /**
786
         * When a WCS provider is opened the information of data type and number of bands is not
787
         * available. Only after the first time a raster has been downloaded it can be know.
788
         * @param newDataType
789
         * @param buf
790
         * @param bandList
791
         * @return
792
         */
793
        /*private Buffer changeBufferDataType(int newDataType, Buffer buf, BandList bandList) {
794
                Buffer newBuffer = null;
795
                if(buf.getDataType() != newDataType) {
796
                        newBuffer = DefaultRasterManager.getInstance().createReadOnlyBuffer(newDataType, buf.getWidth(), buf.getHeight(), buf.getBandCount()); 
797
                        buf.free();
798
                } else 
799
                        return buf;
800
                
801
                bandList.clear();
802
                for(int i = 0; i < getBandCount(); i++)
803
                        try {
804
                                int dataType = getDataType()[i];
805
                                DatasetBand band = new DatasetBandImpl(getFName(), i, dataType, getBandCount());
806
                                bandList.addBand(band, i);
807
                        } catch(BandNotFoundInListException ex) {
808
                                //No a?adimos la banda
809
                        }
810
                return newBuffer;
811
        }*/
812

    
813
        /*
814
         * (non-Javadoc)
815
         * @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)
816
         */
817
        public Buffer getWindow(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
818
                return null;
819
        }
820

    
821
        /*
822
         * (non-Javadoc)
823
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
824
         */
825
        public int getBlockSize() {
826
                return 0;
827
        }
828

    
829
        /*
830
         * (non-Javadoc)
831
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
832
         */
833
        public void setAffineTransform(AffineTransform t){
834
                
835
        }
836

    
837
        /*
838
         * (non-Javadoc)
839
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
840
         */
841
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
842
                return 0;
843
        }
844

    
845
        /*
846
         * (non-Javadoc)
847
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
848
         */
849
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
850
                return 0;
851
        }
852

    
853
        /*
854
         * (non-Javadoc)
855
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewHeight(int, int)
856
         */
857
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
858
                return 0;
859
        }
860

    
861
        /*
862
         * (non-Javadoc)
863
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
864
         */
865
        public boolean overviewsSupport() {
866
                return false;
867
        }
868

    
869
        /*
870
         * (non-Javadoc)
871
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#isReproyectable()
872
         */
873
        public boolean isReproyectable() {
874
                return false;
875
        }
876

    
877
        /*
878
         * (non-Javadoc)
879
         * @see org.gvsig.fmap.dal.raster.spi.CoverageStoreProvider#getName()
880
         */
881
        public String getName() {
882
                return NAME;
883
        }
884
        
885
        /**
886
         * Convierte un punto desde coordenadas pixel a coordenadas del mundo.
887
         * @param pt Punto a transformar
888
         * @return punto transformado en coordenadas del mundo
889
         */
890
        public Point2D rasterToWorld(Point2D pt) {
891
                Point2D p = new Point2D.Double();
892
                getAffineTransform().transform(pt, p);
893
                return p;
894
        }
895

    
896
        /**
897
         * Convierte un punto desde del mundo a coordenadas pixel.
898
         * @param pt Punto a transformar
899
         * @return punto transformado en coordenadas pixel
900
         */
901
        public Point2D worldToRaster(Point2D pt) {
902
                Point2D p = new Point2D.Double();
903
                try {
904
                        getAffineTransform().inverseTransform(pt, p);
905
                } catch (NoninvertibleTransformException e) {
906
                        return pt;
907
                }
908
                return p;
909
        }
910
        
911
        /*
912
         * (non-Javadoc)
913
         * @see org.gvsig.raster.impl.provider.RasterProvider#setStatus(org.gvsig.raster.impl.provider.RasterProvider)
914
         */
915
        public void setStatus(RasterProvider provider) {
916
                if(provider instanceof WCSProvider) {
917
                }
918
        }
919
        
920
        /*
921
         * (non-Javadoc)
922
         * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLastRequest()
923
         */
924
        public File getLastRequest() {
925
                return lastRequest;
926
        }
927
        
928
        /*
929
         * (non-Javadoc)
930
         * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getBufferLastRequest()
931
         */
932
        public Buffer getBufferLastRequest() throws ProcessInterruptedException, RasterDriverException {
933
                try {
934
                        GdalProvider driver = new GdalProvider(lastRequest.getPath());
935
                        
936
                        BandList bandList = new BandListImpl();
937
                        for (int i = 0; i < driver.getBandCount(); i++) {
938
                                try {
939
                                        bandList.addBand(new DatasetBandImpl(lastRequest.getPath(), i, Buffer.TYPE_BYTE, 3), i);
940
                                } catch (BandNotFoundInListException e1) {
941
                                }
942
                        }
943
                        bandList.setDrawableBands(new int[]{0, 1, 2});
944
                        Buffer newBuffer = DefaultRasterManager.getInstance().createBuffer(driver.getDataType()[0], (int)driver.getWidth(), (int)driver.getHeight(), driver.getBandCount(), true); 
945
                        Buffer buf = driver.getWindow(driver.getExtent(), 
946
                                                                                (int)driver.getWidth(), 
947
                                                                                (int)driver.getHeight(), 
948
                                                                                bandList, 
949
                                                                                newBuffer, 
950
                                                                                true);
951
                        driver.close();
952
                        return buf;
953
                } catch (NotSupportedExtensionException e) {
954
                        throw new RasterDriverException("Error building GdalDriver", e);
955
                }
956
        }
957
        
958
        /**
959
         * ASigna el par?metro de inicializaci?n del driver.
960
         */
961
        @Override
962
        public void setParam(DataStoreParameters param) {
963
                if(param instanceof WCSDataParameters)
964
                        this.name = ((WCSDataParameters)param).getHost();
965
                this.param = param;
966
        }
967
        
968
        /**
969
     * @param wmsStatus
970
     * @param i
971
     * @param j
972
     * @param max_value
973
     * @return
974
     * @throws RemoteServiceException
975
     */
976
    public String getFeatureInfo(int i, int j, int max_value, ICancellable cancellable) throws RemoteServiceException {
977
            /*WCSStatus wcsStatus = loadWCSStatus(lastExtent);
978
            return getConnector().getFeatureInfo(wcsStatus, i, j, max_value, cancellable);*/
979
            return null;
980
    }
981
    
982
    /*
983
         * (non-Javadoc)
984
         * @see org.gvsig.raster.impl.provider.RasterProvider#getInfoByPoint(double, double)
985
         */
986
        public String getInfoByPoint(double x, double y, ICancellable cancellable) throws RemoteServiceException {
987
                return getFeatureInfo((int) x, (int) y, Integer.MAX_VALUE, cancellable);
988
        }
989
        
990
        /**
991
         * Gets the suffix of the downloaded image
992
         * @return
993
         */
994
        public String getFileSuffix() {
995
                WCSDataParametersImpl p = (WCSDataParametersImpl)parameters;
996
                String format = p.getFormat();
997
                if (format == null){
998
                        return "xml";
999
                }
1000
                if (format.indexOf("png") >= 0){
1001
                return "png";
1002
                }        
1003
            if (format.indexOf("xml") >= 0){
1004
                return "xml";
1005
            }        
1006
            if (format.indexOf("gif") >= 0){
1007
                return "gif";
1008
            }
1009
            if (format.indexOf("tif") >= 0){
1010
                return "tif";
1011
            }
1012
            if (format.indexOf("bmp") >= 0){
1013
                return "bmp";
1014
            }
1015
            if (format.indexOf("jpg") >= 0
1016
                || format.indexOf("jpeg") >= 0){
1017
                return "jpg";                         
1018
            }
1019
                return "xml";
1020
        }
1021
        
1022
        /*
1023
         * (non-Javadoc)
1024
         * @see org.gvsig.raster.impl.provider.RasterProvider#getTileServer()
1025
         */
1026
        public TileServer getTileServer() {
1027
                return null;
1028
        }
1029
        
1030
        /*
1031
         * (non-Javadoc)
1032
         * @see org.gvsig.raster.impl.provider.RasterProvider#isRasterEnclosed()
1033
         */
1034
        public boolean isRasterEnclosed() {
1035
                return true;
1036
        }
1037
        
1038
        /*
1039
         * (non-Javadoc)
1040
         * @see org.gvsig.raster.impl.provider.RasterProvider#getRMFFile()
1041
         */
1042
        public String getRMFFile() {
1043
                if(lastRequest != null)
1044
                        return fileUtil.getNameWithoutExtension(lastRequest.getAbsolutePath()) + ".rmf";
1045
                return null;
1046
        }
1047
}