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

History | View | Annotate | Download (34.1 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.store.props.Transparency;
50
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
51
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
52
import org.gvsig.metadata.MetadataLocator;
53
import org.gvsig.raster.cache.tile.TileCacheLocator;
54
import org.gvsig.raster.cache.tile.TileCacheManager;
55
import org.gvsig.raster.cache.tile.exception.TileGettingException;
56
import org.gvsig.raster.cache.tile.provider.TileListener;
57
import org.gvsig.raster.cache.tile.provider.TileServer;
58
import org.gvsig.raster.impl.DefaultRasterManager;
59
import org.gvsig.raster.impl.datastruct.BandListImpl;
60
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
61
import org.gvsig.raster.impl.datastruct.ExtentImpl;
62
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
63
import org.gvsig.raster.impl.provider.RasterProvider;
64
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
65
import org.gvsig.raster.impl.store.DefaultStoreFactory;
66
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
67
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
68
import org.gvsig.raster.impl.store.properties.RemoteDataStoreStatistics;
69
import org.gvsig.remoteclient.wcs.WCSStatus;
70
import org.gvsig.tools.ToolsLocator;
71
/**
72
 * Clase que representa al driver de acceso a datos de wcs.
73
 *
74
 * @author Nacho Brodin (nachobrodin@gmail.com)
75
 */
76
public class WCSProvider extends DefaultRasterProvider implements RemoteRasterProvider {
77
        public static String                NAME                     = "Wcs Store";
78
        public static String                DESCRIPTION              = "Wcs Raster file";
79
        public static final String          METADATA_DEFINITION_NAME = "WcsStore";
80
        
81
        private Extent                      viewRequest              = null;
82
        private static Hashtable<URL, WCSConnector>    
83
                                            drivers                  = new Hashtable<URL, WCSConnector> ();
84
        private boolean                     open                     = false;
85
        private DataStoreTransparency       fileTransparency         = null;
86
        private File                        lastRequest              = null;
87
        
88
        public static void register() {
89
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
90
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
91
                        dataman.registerStoreProvider(NAME,
92
                                        WCSProvider.class, WCSDataParameters.class);
93
                }
94

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

    
249
        
250
        /*
251
         * (non-Javadoc)
252
         * @see org.gvsig.raster.impl.provider.RemoteRasterProvider#getLayerExtent(java.lang.String, java.lang.String)
253
         */
254
        public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException {
255
                return null;
256
        }
257

    
258
        /*
259
         * (non-Javadoc)
260
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#load()
261
         */
262
        public RasterProvider load() {
263
                return this;
264
        }
265
        
266
        /*
267
         * (non-Javadoc)
268
         * @see org.gvsig.raster.impl.provider.RasterProvider#isOpen()
269
         */
270
        public boolean isOpen() {
271
                return open;
272
        }
273

    
274
        /*
275
         * (non-Javadoc)
276
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#close()
277
         */
278
        public void close() {
279
                open = false;
280
        }
281
        
282
        /*
283
         * (non-Javadoc)
284
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getTransparency()
285
         */
286
        public Transparency getTransparency() {
287
                if(fileTransparency == null)
288
                        fileTransparency = new DataStoreTransparency();
289
                return fileTransparency;
290
        }
291

    
292
        /*
293
         * (non-Javadoc)
294
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#translateFileName(java.lang.String)
295
         */
296
        public String translateFileName(String fileName) {
297
                return fileName;
298
        }
299

    
300
        /*
301
         * (non-Javadoc)
302
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setView(org.gvsig.fmap.dal.coverage.datastruct.Extent)
303
         */
304
        public void setView(Extent e) {
305
                viewRequest = e;
306
        }
307

    
308
        /*
309
         * (non-Javadoc)
310
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getView()
311
         */
312
        public Extent getView() {
313
                return viewRequest;
314
        }
315
        
316
        /*
317
         * (non-Javadoc)
318
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWidth()
319
         */
320
        public double getWidth() {
321
                WCSDataParameters p = (WCSDataParameters)parameters;
322
                Point2D resolution = null;
323
                try {
324
                        resolution = getConnector().getMaxResolution(p.getCoverageName());
325
                        Extent e = getExtent();
326
                        return e.width() / resolution.getX(); 
327
                } catch (RemoteServiceException e1) {
328
                        e1.printStackTrace();
329
                }
330
                return 0;
331
        }
332

    
333
        /*
334
         * (non-Javadoc)
335
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getHeight()
336
         */
337
        public double getHeight() {
338
                WCSDataParameters p = (WCSDataParameters)parameters;
339
                Point2D resolution = null;
340
                try {
341
                        resolution = getConnector().getMaxResolution(p.getCoverageName());
342
                        Extent e = getExtent();
343
                        return e.height() / resolution.getY(); 
344
                } catch (RemoteServiceException e1) {
345
                        e1.printStackTrace();
346
                }
347
                return 0;
348
        }
349

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

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

    
400
                try {
401
                        GdalProvider driver = new GdalProvider(lastFile.getPath());
402
                        return driver.readBlock(pos, blockHeight);
403
                } catch (NotSupportedExtensionException exc) {
404
                        throw new RasterDriverException("Error building GdalDriver", exc);
405
                }
406
        }
407

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

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

    
563
                         //listener.nextBuffer(raster, null, new ExtentImpl(minX, minY, maxX, maxY), getAffineTransform(), null, false);
564
                         listener.endReading();
565
                 }
566
        }
567

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

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

    
646
        /*
647
         * (non-Javadoc)
648
         * @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)
649
         */
650
        public Buffer getWindow(Extent extent, 
651
                        int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException {
652
                WCSDataParameters p = (WCSDataParameters)parameters;
653
                Rectangle2D bBox = null;
654
                p.setWidth(bufWidth);
655
                p.setHeight(bufHeight);
656
                
657
                /*if(p.getExtent() != null) {
658
                        bBox = p.getExtent();
659
                        extent = new ExtentImpl(p.getExtent());
660
                } else {*/
661
                        bBox = extent.toRectangle2D();
662
                //}
663
                
664
                //extent.toRectangle2D();//new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly));
665
                WCSStatus wcsStatus = loadWCSStatus(bBox);
666
                lastRequest = downloadFile(wcsStatus, extent.getULX(), extent.getULY(), extent.getLRX(), extent.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
667
                
668
                if (lastRequest == null) {
669
                        return rasterBuf;
670
                }
671
                
672
                try {
673
                        GdalProvider driver = new GdalProvider(lastRequest.getPath());
674
                        /*bandCount = driver.getBandCount();
675
                        setDataType(driver.getDataType());
676
                        rasterBuf = changeBufferDataType(getDataType()[0], rasterBuf, bandList);*/
677
                        
678
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
679
                        String serverName = bandList.getBand(0).getFileName();
680
                        for (int i = 0; i < bandList.getBandCount(); i++) {
681
                                bandList.getBand(i).setFileName(lastRequest.getPath());
682
                        }
683
                        
684
                        Buffer buf = driver.getWindow(extent, bufWidth, bufHeight, bandList, rasterBuf, adjustToExtent);
685

    
686
                        for (int i = 0; i < bandList.getBandCount(); i++) {
687
                                bandList.getBand(i).setFileName(serverName);
688
                        }
689
                        driver.close();
690
                        return buf;
691
                } catch (NotSupportedExtensionException e) {
692
                        throw new RasterDriverException("Error building GdalDriver", e);
693
                }
694
        }
695

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

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

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

    
807
        /*
808
         * (non-Javadoc)
809
         * @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)
810
         */
811
        public Buffer getWindow(int x, int y, int w, int h, int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
812
                return null;
813
        }
814

    
815
        /*
816
         * (non-Javadoc)
817
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getBlockSize()
818
         */
819
        public int getBlockSize() {
820
                return 0;
821
        }
822

    
823
        /*
824
         * (non-Javadoc)
825
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#setAffineTransform(java.awt.geom.AffineTransform)
826
         */
827
        public void setAffineTransform(AffineTransform t){
828
                
829
        }
830

    
831
        /*
832
         * (non-Javadoc)
833
         * @see org.gvsig.raster.impl.provider.RasterProvider#getOverviewCount(int)
834
         */
835
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
836
                return 0;
837
        }
838

    
839
        /*
840
         * (non-Javadoc)
841
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getOverviewWidth(int, int)
842
         */
843
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
844
                return 0;
845
        }
846

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

    
855
        /*
856
         * (non-Javadoc)
857
         * @see org.gvsig.raster.impl.provider.DefaultRasterProvider#overviewsSupport()
858
         */
859
        public boolean overviewsSupport() {
860
                return false;
861
        }
862

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

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

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