Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / trunk / org.gvsig.raster.wms / org.gvsig.raster.wms.io / src / main / java / org / gvsig / raster / wms / io / WMSProvider.java @ 4181

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

    
24
import java.awt.Image;
25
import java.awt.geom.AffineTransform;
26
import java.awt.geom.NoninvertibleTransformException;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29
import java.io.File;
30
import java.io.IOException;
31
import java.net.URI;
32
import java.net.URISyntaxException;
33
import java.net.URL;
34
import java.util.Hashtable;
35
import java.util.List;
36
import java.util.Vector;
37

    
38
import javax.swing.ImageIcon;
39

    
40
import org.apache.commons.io.FilenameUtils;
41
import org.cresques.cts.ICoordTrans;
42
import org.cresques.cts.IProjection;
43

    
44
import org.gvsig.compat.net.ICancellable;
45
import org.gvsig.fmap.crs.CRSFactory;
46
import org.gvsig.fmap.dal.DALLocator;
47
import org.gvsig.fmap.dal.DataStore;
48
import org.gvsig.fmap.dal.DataStoreParameters;
49
import org.gvsig.fmap.dal.coverage.RasterLocator;
50
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
51
import org.gvsig.fmap.dal.coverage.datastruct.BandList;
52
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
53
import org.gvsig.fmap.dal.coverage.exception.BandAccessException;
54
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException;
55
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
56
import org.gvsig.fmap.dal.coverage.exception.InfoByPointException;
57
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException;
58
import org.gvsig.fmap.dal.coverage.exception.InvalidSourceException;
59
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException;
60
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
61
import org.gvsig.fmap.dal.coverage.exception.QueryException;
62
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
63
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
64
import org.gvsig.fmap.dal.coverage.store.RasterQuery;
65
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
66
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer;
67
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
68
import org.gvsig.fmap.dal.exception.InitializeException;
69
import org.gvsig.fmap.dal.exception.OpenException;
70
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
71
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
72
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
73
import org.gvsig.metadata.MetadataLocator;
74
import org.gvsig.raster.cache.tile.provider.TileServer;
75
import org.gvsig.raster.impl.buffer.SpiRasterQuery;
76
import org.gvsig.raster.impl.datastruct.BandListImpl;
77
import org.gvsig.raster.impl.datastruct.DatasetBandImpl;
78
import org.gvsig.raster.impl.datastruct.ExtentImpl;
79
import org.gvsig.raster.impl.provider.AbstractRasterProvider;
80
import org.gvsig.raster.impl.provider.RasterProvider;
81
import org.gvsig.raster.impl.provider.RemoteRasterProvider;
82
import org.gvsig.raster.impl.store.DefaultRasterStore;
83
import org.gvsig.raster.impl.store.DefaultStoreFactory;
84
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
85
import org.gvsig.raster.impl.store.properties.DataStoreTransparency;
86
import org.gvsig.raster.impl.store.properties.RemoteStoreHistogram;
87
import org.gvsig.raster.util.DefaultProviderServices;
88
import org.gvsig.raster.wms.io.downloader.WMSTileServer;
89
import org.gvsig.remoteclient.utils.Utilities;
90
import org.gvsig.remoteclient.wms.WMSStatus;
91
import org.gvsig.tools.ToolsLocator;
92

    
93
import org.slf4j.Logger;
94
import org.slf4j.LoggerFactory;
95
/**
96
 * Clase que representa al driver de acceso a datos de wms.
97
 *
98
 * @author Nacho Brodin (nachobrodin@gmail.com)
99
 */
100
public class WMSProvider extends AbstractRasterProvider implements RemoteRasterProvider {
101
        public static String                NAME                     = "Wms Store";
102
        public static String                DESCRIPTION              = "Wms Raster file";
103
        public static final String          METADATA_DEFINITION_NAME = "WmsStore";
104
        private static final Logger         logger                    = LoggerFactory.getLogger(WMSProvider.class);
105
        private static final int            FIXED_SIZE               = 800;
106
        private Extent                      viewRequest              = null;
107
        private static Hashtable<String, WMSConnector>
108
                                            drivers                  = new Hashtable<String, WMSConnector> ();
109
        private boolean                     open                     = false;
110
        private DataStoreTransparency       fileTransparency         = null;
111
        private final int                                        minTilePrintWidth        = 12;
112
        private final int                                        minTilePrintHeight       = 12;
113
        //The out size depends on the last request
114
        private int                         lastWidthRequest         = 0;
115
        private int                         lastHeightRequest        = 0;
116
        //Only for fixed size. Complete extent and FIXED_SIZE in long side
117
        private File                        fileLayerPixelSize       = null;
118
        private File                        lastRequest              = null;
119
        private AbstractRasterProvider       lastRequestProvider      = null;
120

    
121
        public static void register() {
122
                DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager();
123
                if (dataman != null && !dataman.getStoreProviders().contains(NAME)) {
124
                        dataman.registerStoreProvider(NAME,
125
                                        WMSProvider.class, WMSDataParametersImpl.class);
126
                }
127

    
128
                if (!dataman.getExplorerProviders().contains(WMSServerExplorer.NAME)) {
129
                        dataman.registerExplorerProvider(WMSServerExplorer.NAME, WMSServerExplorer.class, WMSServerExplorerParameters.class);
130
                }
131
                dataman.registerStoreFactory(NAME, DefaultStoreFactory.class);
132
        }
133

    
134
        public WMSProvider() throws NotSupportedExtensionException {
135
                super();
136
        }
137

    
138
        public void registerTileProviderFormats(Class<RasterProvider> c) {
139

    
140
        }
141

    
142
        /**
143
         * Constructor. Abre el dataset.
144
         * @param proj Proyecci?n
145
         * @param fName Nombre del fichero
146
         * @throws OpenException
147
         * @throws NotSupportedExtensionException
148
     * @deprecated use {@link #WMSProvider(URI)}, this constructor will be removed in gvSIG 2.5
149
         */
150
    public WMSProvider(String params) throws InitializeException, OpenException {
151
        super(params);
152
        logger.info("Deprecated use of WMSProvider constructor");
153
        if (params instanceof String) {
154
            WMSDataParameters p = new WMSDataParametersImpl();
155
            try {
156
                p.setURI(new URI((String) params));
157
            } catch (URISyntaxException e) {
158
                throw new OpenException("Can't create URI from" + (String) params, e);
159
            }
160
            super.init(
161
                p,
162
                null,
163
                ToolsLocator.getDynObjectManager().createDynObject(
164
                    MetadataLocator.getMetadataManager().getDefinition(DataStore.METADATA_DEFINITION_NAME)));
165
            init(p, null);
166
        }
167
    }
168

    
169
    /**
170
     * Constructor. Abre el dataset.
171
     * @param fName Nombre del fichero
172
     * @throws OpenException
173
     * @throws NotSupportedExtensionException
174
     * @deprecated use {@link #WMSProvider(URI)}, this constructor will be removed in gvSIG 2.5
175
     */
176
    public WMSProvider(URI uri) throws InitializeException {
177
        super(uri);
178
        logger.info("Deprecated use of ErmapperProvider constructor");
179
        WMSDataParameters p = new WMSDataParametersImpl();
180
        p.setURI(uri);
181
        super.init(
182
            p,
183
            null,
184
            ToolsLocator.getDynObjectManager().createDynObject(
185
                MetadataLocator.getMetadataManager().getDefinition(DataStore.METADATA_DEFINITION_NAME)));
186
        init(p, null);
187
    }
188

    
189
        public WMSProvider(WMSDataParameters params,
190
                        DataStoreProviderServices storeServices) throws InitializeException {
191
                super(params, storeServices, ToolsLocator.getDynObjectManager()
192
                                .createDynObject(
193
                                                MetadataLocator.getMetadataManager().getDefinition(
194
                                                                DataStore.METADATA_DEFINITION_NAME)));
195
                init(params, storeServices);
196
        }
197

    
198
        /**
199
         * Gets the connector from the URL
200
         * @return
201
         * @throws RemoteServiceException
202
         */
203
        public WMSConnector getConnector() throws RemoteServiceException {
204
                WMSDataParameters p = (WMSDataParameters)parameters;
205
                URL url = null;
206
                try {
207
                        url =p.getURI().toURL();
208
                } catch (Exception e) {
209
                        throw new RemoteServiceException("Malformed URL",e);
210
                }
211
                try {
212
                        return WMSProvider.getConnectorFromURL(url, false);
213
                } catch (IOException e) {
214
                        throw new RemoteServiceException("Error getting the connector",e);
215
                }
216
        }
217

    
218
        /**
219
         * Crea las referencias al fichero y carga
220
         * las estructuras con la informaci?n y los metadatos.
221
         * @param proj Proyecci?n
222
         * @param param Parametros de carga
223
         * @throws NotSupportedExtensionException
224
         */
225
        public void init (DataStoreParameters params,
226
                        DataStoreProviderServices storeServices) throws InitializeException {
227
                setParam(storeServices, params);
228
                setDataType(new int[]{Buffer.TYPE_BYTE, Buffer.TYPE_BYTE, Buffer.TYPE_BYTE});
229
                bandCount = 3;
230
                open = true;
231
                try {
232
                        loadInitialInfo();
233
                } catch (RasterDriverException e) {
234
                        throw new InitializeException(e.getMessage(), e);
235
                }
236
        }
237

    
238
        /**
239
         * When a WMS provider is opened the information of data type, number of bands and transparency
240
         * is not available. Only after the first time a raster has been downloaded it can be know.
241
         * @param newDataType
242
         * @param buf
243
         * @param bandList
244
         * @return
245
         * @throws RasterDriverException
246
         */
247
        private void loadInitialInfo() throws RasterDriverException {
248
                WMSDataParametersImpl p = (WMSDataParametersImpl)parameters;
249
                Extent ext = getExtent();
250
                Rectangle2D bBox = ext.toRectangle2D();
251
                int w = 0;
252
                int h = 0;
253
                if(ext.width() > ext.height()) {
254
                        w = 200;
255
                        h = (int)((ext.height() * w) / ext.width());
256
                } else {
257
                        h = 200;
258
                        w = (int)((ext.width() * h) / ext.height());
259
                }
260
                p.setWidth(w);
261
                p.setHeight(h);
262
                p.setExtent(bBox);
263
                if(p.getSRSCode() != null) {
264
                        try {
265
                                IProjection proj = CRSFactory.getCRS(p.getSRSCode());
266
                                setProjection(proj, false);
267
                        } catch (Exception e) {
268
                        }
269
                }
270
                WMSStatus wmsStatus = loadWMSStatus(bBox);
271

    
272
                lastRequest = downloadFile(wmsStatus, ext.getULX(), ext.getULY(), ext.getLRX(), ext.getLRY(), w, h);
273
                AbstractRasterProvider driver;
274
                try {
275
                        driver = DefaultProviderServices.loadProvider(lastRequest);
276
                        setDataType(driver.getDataType());
277
                        bandCount = driver.getBandCount();
278
                        if(bandCount > 3) {
279
                                getColorInterpretation().setColorInterpValue(3, DataStoreColorInterpretation.ALPHA_BAND);
280
                                getTransparency().setTransparencyBand(3);
281
                        }
282
                        setColorTable(driver.getColorTable());
283
                        driver.close();
284
                } catch (ProviderNotRegisteredException e) {
285
                        throw new RasterDriverException("", e);
286
                } catch (InitializeException e) {
287
                        throw new RasterDriverException("", e);
288
                }
289
        }
290

    
291
        public static final WMSConnector getConnectorFromURL(URL url, boolean updating) throws IOException {
292
                WMSConnector drv = null;
293
                if(!updating) {
294
                        drv = (WMSConnector) drivers.get(url.toString());
295
                } else {
296
                        if(drivers.get(url.toString()) != null)
297
                                drivers.remove(url.toString());
298
                }
299

    
300
                if (drv == null) {
301
                        drv = new WMSConnector(url);
302
                        drivers.put(url.toString(), drv);
303
                }
304

    
305
                return drv;
306
        }
307

    
308
        /**
309
         * Obtiene el objeto que contiene que contiene la interpretaci?n de
310
         * color por banda
311
         * @return
312
         */
313
        public ColorInterpretation getColorInterpretation() {
314
                if(super.getColorInterpretation() == null) {
315
                        ColorInterpretation colorInterpretation = new DataStoreColorInterpretation(getBandCount());
316
                        if(getBandCount() == 1 || getBandCount() == 2)
317
                                colorInterpretation = DataStoreColorInterpretation.createGrayInterpretation();
318
                        if(getBandCount() == 3)
319
                                colorInterpretation = DataStoreColorInterpretation.createRGBInterpretation();
320
                        if(getBandCount() == 4)
321
                                colorInterpretation = DataStoreColorInterpretation.createRGBAInterpretation();
322
                        setColorInterpretation(colorInterpretation);
323
                }
324
                return super.getColorInterpretation();
325
        }
326

    
327
        public AffineTransform getAffineTransform() {
328
                WMSDataParameters p = (WMSDataParameters)parameters;
329
                if(p.isSizeFixed()) {
330
                        Extent e = getExtent(); // FIXME: it should also be taken from parameters instead of from the full layer extent
331
                        double psX = e.width() / (p.getWidth() - 1);
332
                        double psY = -(e.height() / (p.getHeight() - 1));
333
                        ownTransformation = new AffineTransform(
334
                                        psX ,
335
                                        0,
336
                                        0,
337
                                        psY,
338
                                        e.getULX() - (psX / 2),
339
                                        e.getULY() - (psY / 2));
340
                } else {
341
                        Rectangle2D bbox = p.getExtent();
342
                        double psX = bbox.getWidth() / p.getWidth();
343
                        double psY = -(bbox.getHeight() / p.getHeight());
344
                        ownTransformation = new AffineTransform(
345
                                        psX,
346
                                        0,
347
                                        0,
348
                                        psY,
349
                                        bbox.getX(),
350
                                        (bbox.getY()+bbox.getHeight())); // FIXME: check for other CRSs such as 4326
351
                }
352
                externalTransformation = (AffineTransform) ownTransformation.clone();
353
                return ownTransformation;
354
        }
355

    
356
        /**
357
         * Calcula el extent en coordenadas del mundo real
358
         * @return Extent
359
         */
360
        public Extent getExtent() {
361
                WMSDataParameters p = (WMSDataParameters)parameters;
362
                Vector<?> layerNames = Utilities.createVector(p.getLayerQuery(), ",");
363

    
364
                String[] ln = new String[layerNames.size()];
365
                for (int i = 0; i < ln.length; i++) {
366
                        ln[i] = (String)layerNames.get(i);
367
                }
368
                Rectangle2D r = null;
369
                try {
370
                        r = getConnector().getLayersExtent(ln, p.getSRSCode());
371
                        if (r == null) {
372
                                String latLonID = "EPSG:4326";
373
                                r = getConnector().getLayersExtent(ln, latLonID);
374
                                if (r == null) {
375
                                        r = getConnector().getLayersExtent(ln, "CRS:84");
376
                                }
377

    
378
                                IProjection reqProj = CRSFactory.getCRS(p.getSRSCode());
379
                                IProjection latLonProj = CRSFactory.getCRS(latLonID);
380
                                if ((reqProj != null) && (latLonProj != null)) {
381
                                        ICoordTrans ct = latLonProj.getCT(reqProj);
382
                                        Rectangle2D reprojectedRect = ct.convert(r);
383
                                        r = reprojectedRect;
384
                                }
385
                        }
386
                } catch (RemoteServiceException e) {
387
                }
388
                if(r == null)
389
                        return null;
390
                return new ExtentImpl(r.getMinX(), r.getMaxY(), r.getMaxX(), r.getMinY());
391
        }
392

    
393
        public Rectangle2D getLayerExtent(String layerName, String srs) throws RemoteServiceException {
394
                return getConnector().getLayersExtent(new String[]{layerName}, srs);
395
        }
396

    
397
        public RasterProvider load() {
398
                return this;
399
        }
400

    
401
        public boolean isOpen() {
402
                return open;
403
        }
404

    
405
        public void close() {
406
                open = false;
407
        }
408

    
409
        public Transparency getTransparency() {
410
                if(fileTransparency == null)
411
                        fileTransparency = new DataStoreTransparency(getColorInterpretation());
412
                return fileTransparency;
413
        }
414

    
415
        public URI translateURI(URI uri) {
416
                return uri;
417
        }
418

    
419
        public void setView(Extent e) {
420
                viewRequest = e;
421
        }
422

    
423
        public Extent getView() {
424
                return viewRequest;
425
        }
426

    
427
        public double getWidth() {
428
                WMSDataParameters p = (WMSDataParameters)parameters;
429
                return p.getWidth();
430
        }
431

    
432
        public double getHeight() {
433
                WMSDataParameters p = (WMSDataParameters)parameters;
434
                return p.getHeight();
435
        }
436

    
437
        /**
438
         * Gets WMS parameters
439
         * @return
440
         */
441
        public WMSDataParameters getParameters() {
442
                return (WMSDataParameters)parameters;
443
        }
444

    
445
        public Object readCompleteLine(int line, int band)
446
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
447
                return null;
448
        }
449

    
450
        /**
451
         * When the remote layer has fixed size this method downloads the file and return its reference.
452
         * File layer has in the long side FIXED_SIZE pixels and the bounding box is complete. This file could be
453
         * useful to build an histogram or calculate statistics. This represents a sample of data.
454
         * @return
455
         * @throws RasterDriverException
456
         */
457
        public File getFileLayer() throws RasterDriverException {
458
                if(fileLayerPixelSize != null)
459
                        return fileLayerPixelSize;
460

    
461
                WMSDataParameters p = (WMSDataParameters)parameters;
462
                Extent e = getExtent();
463
                Rectangle2D bBox = new Rectangle2D.Double(e.getULX(), e.getLRY(), e.width(), e.height());
464
                WMSStatus wmsStatus = loadWMSStatus(bBox);
465

    
466
                if(!p.isSizeFixed()) {
467
                        int w = 0;
468
                        int h = 0;
469
                        if(e.width() > e.height()) {
470
                                w = FIXED_SIZE;
471
                                h = (int)((e.height() * FIXED_SIZE) / e.width());
472
                        } else {
473
                                h = FIXED_SIZE;
474
                                w = (int)((e.width() * FIXED_SIZE) / e.height());
475
                        }
476
                        wmsStatus.setWidth(w);
477
                        wmsStatus.setHeight(h);
478
                        fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(), w, h);
479
                } else {
480
                        fileLayerPixelSize = downloadFile(wmsStatus, e.getULX(), e.getULY(), e.getLRX(), e.getLRY(),
481
                                        p.getFixedSize().width, p.getFixedSize().height);
482
                }
483
                return fileLayerPixelSize;
484
        }
485

    
486
        /**
487
         * Reads a complete block of data and returns an tridimensional array of the right type. This function is useful
488
         * to read a file very fast without setting a view. In a WMS service when the size is fixed then it will read the
489
         * entire image but when the source hasn't pixel size it will read a sample of data. This set of data will have
490
         * the size defined in FIXED_SIZE.
491
         *
492
         * @param pos Posici?n donde se empieza  a leer
493
         * @param blockHeight Altura m?xima del bloque leido
494
         * @return Object que es un array tridimendional del tipo de datos del raster. (Bandas X Filas X Columnas)
495
         * @throws InvalidSetViewException
496
         * @throws FileNotOpenException
497
         * @throws RasterDriverException
498
         */
499
        public Object readBlock(int pos, int blockHeight, double scale)
500
        throws InvalidSetViewException, FileNotOpenException, RasterDriverException, ProcessInterruptedException {
501
                File lastFile = getFileLayer();
502
                BandList bandList = new BandListImpl();
503
                for (int i = 0; i < 3; i++) {
504
                        try {
505
                                bandList.addBand(new DatasetBandImpl(lastFile.getPath(), pos, Buffer.TYPE_BYTE, 3));
506
                        } catch (BandNotFoundInListException e1) {
507
                        }
508
                }
509
                bandList.setDrawableBands(new int[]{0, 1, 2});
510

    
511
                try {
512
                        lastRequestProvider = openLastRequest();
513
                        return lastRequestProvider.readBlock(pos, blockHeight, scale);
514
                } catch (ProviderNotRegisteredException exc) {
515
                        throw new RasterDriverException("Error building GdalDriver", exc);
516
                } catch (InitializeException exc) {
517
                        throw new RasterDriverException("Error building GdalDriver", exc);
518
                }
519
        }
520

    
521
        public double getLastRequestHeight() throws RasterDriverException {
522
                if(lastRequestProvider == null) {
523
                        try {
524
                                lastRequestProvider = openLastRequest();
525
                        } catch (ProviderNotRegisteredException e) {
526
                                throw new RasterDriverException("Error building GdalDriver", e);
527
                        } catch (InitializeException e) {
528
                                throw new RasterDriverException("Error building GdalDriver", e);
529
                        }
530
                }
531
                return lastRequestProvider.getHeight();
532
        }
533

    
534
        public double getLastRequestWidth() throws RasterDriverException {
535
                if(lastRequestProvider == null) {
536
                        try {
537
                                lastRequestProvider = openLastRequest();
538
                        } catch (ProviderNotRegisteredException e) {
539
                                throw new RasterDriverException("Error building GdalDriver", e);
540
                        } catch (InitializeException e) {
541
                                throw new RasterDriverException("Error building GdalDriver", e);
542
                        }
543
                }
544
                return lastRequestProvider.getWidth();
545
        }
546

    
547
        public File getLastRequest() {
548
                return lastRequest;
549
        }
550

    
551
        public Buffer getBufferLastRequest() throws ProcessInterruptedException, RasterDriverException {
552
                try {
553
                        lastRequestProvider = openLastRequest();
554
                        return getDownloadedRaster(lastRequestProvider);
555
                } catch (ProviderNotRegisteredException e) {
556
                        throw new RasterDriverException("Error building GdalDriver", e);
557
                } catch (InitializeException e) {
558
                        throw new RasterDriverException("Error building GdalDriver", e);
559
                } catch (QueryException e) {
560
                        throw new RasterDriverException("Error building GdalDriver", e);
561
                }
562
        }
563

    
564
        /**
565
         * Opens the last request downloaded
566
         * @return
567
         * @throws ProviderNotRegisteredException
568
         * @throws InitializeException
569
         * @throws RasterDriverException
570
         */
571
        private AbstractRasterProvider openLastRequest() throws ProviderNotRegisteredException, InitializeException, RasterDriverException {
572
                if(lastRequestProvider != null)
573
                        lastRequestProvider.close();
574
                File lastFile = getFileLayer();
575
                lastRequestProvider = DefaultProviderServices.loadProvider(new File(lastFile.getPath()));
576
                setColorTable(lastRequestProvider.getColorTable());
577
                return lastRequestProvider;
578
        }
579

    
580
        public Object getData(int x, int y, int band)
581
                throws InvalidSetViewException, FileNotOpenException, RasterDriverException {
582
                return null;
583
        }
584

    
585
        /**
586
         * Gets the georeferencing file name form a raster file
587
         * @param file
588
         * a raster file
589
         * @return
590
         * a georeferencing file
591
         */
592
        private String getWorldFile(String file){
593
                String worldFile = file;
594
                int index = file.lastIndexOf(".");
595
                if (index > 0){
596
                        worldFile = file.substring(0, index) + getExtensionWorldFile();
597
                }
598
                return worldFile;
599
        }
600

    
601
        /**
602
         * Obtiene la extensi?n del fichero de georreferenciaci?n
603
         * @return String con la extensi?n del fichero de georreferenciaci?n dependiendo
604
         * del valor del formato obtenido del servidor. Por defecto asignaremos un .wld
605
         */
606
        private String getExtensionWorldFile() {
607
                WMSDataParameters p = (WMSDataParameters)parameters;
608
                String extWorldFile = ".wld";
609
                if(p.getFormat().equals("image/tif") || p.getFormat().equals("image/tiff")) {
610
                        extWorldFile = ".tfw";
611
                }
612
                return extWorldFile;
613
        }
614

    
615
        public WMSStatus loadWMSStatus(Rectangle2D bBox) {
616
                WMSDataParameters p = (WMSDataParameters)parameters;
617
                WMSStatus wmsStatus = new WMSStatus();
618
                wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ","));
619
                wmsStatus.setSrs(p.getSRSCode());
620
                wmsStatus.setFormat(p.getFormat());
621
                wmsStatus.setInfoFormat(p.getInfoFormat());
622
                List<RemoteWMSStyle> listStyles = p.getStyles();
623
                Vector<?> v = listStyles != null ? new Vector<RemoteWMSStyle>(listStyles) : null;
624
                wmsStatus.setStyles(v);
625
                wmsStatus.setDimensions(p.getDimensions());
626
                wmsStatus.setTransparency(p.isWmsTransparent());
627
                wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap"));
628
                if(p.isSizeFixed()) {
629
                        wmsStatus.setExtent(getExtent().toRectangle2D());
630
                } else
631
                        wmsStatus.setExtent(bBox);
632
                wmsStatus.setHeight(p.getHeight());
633
                wmsStatus.setWidth(p.getWidth());
634
                wmsStatus.setXyAxisOrder(p.isXyAxisOrder());
635
                IProjection proj = getProjection();
636
                if(proj != null)
637
                        wmsStatus.setProjected(proj.isProjected());
638
                return wmsStatus;
639
        }
640

    
641
        /**
642
         * This function downloads the file and creates the georeferencing file
643
         * @param wmsStatus
644
         * @param ulx
645
         * @param uly
646
         * @param lrx
647
         * @param lry
648
         * @param w
649
         * @param h
650
         * @return
651
         * @throws RasterDriverException
652
         */
653
        private File downloadFile(WMSStatus wmsStatus, double ulx, double uly, double lrx, double lry, int w, int h) throws RasterDriverException {
654
                WMSDataParameters p = (WMSDataParameters)parameters;
655
                try {
656
                        lastRequest = getConnector().getMap(wmsStatus, ((WMSDataParameters)parameters).getCancellable());
657
                } catch (RemoteServiceException e) {
658
                        throw new RasterDriverException(e.getMessage(), e);
659
                }
660

    
661
                if(lastRequest == null)
662
                        return null;
663

    
664
                String nameWorldFile = getWorldFile(lastRequest.getPath());
665
                try {
666
                        if(p.isSizeFixed()) {
667
                                Extent e = getExtent();
668
                                fileUtil.createWorldFile(nameWorldFile, e, p.getWidth(), p.getHeight());
669
                        } else {
670
                                fileUtil.createWorldFile(nameWorldFile, new ExtentImpl(ulx, uly, lrx, lry), w, h);
671
                        }
672
                } catch (IOException e) {
673
                        throw new RasterDriverException("Error creating world file", e);
674
                }
675

    
676
                return lastRequest;
677
        }
678

    
679
        @Override
680
        public void loadBuffer(SpiRasterQuery q)
681
                        throws ProcessInterruptedException, RasterDriverException {
682
                Extent bbox = q.getRequestBoundingBox();
683
                lastWidthRequest = q.getBufWidth();
684
                lastHeightRequest = q.getBufHeight();
685
                WMSStatus wmsStatus = loadWMSStatus(bbox.toRectangle2D());
686

    
687
                lastRequest = downloadFile(wmsStatus,
688
                                bbox.getULX(),
689
                                bbox.getULY(),
690
                                bbox.getLRX(),
691
                                bbox.getLRY(),
692
                                q.getBufWidth(),
693
                                q.getBufHeight());
694

    
695
                if (lastRequest == null) {
696
                        return;
697
                }
698

    
699
                Buffer b = null;
700
                try {
701
                        b = getDownloadedRaster(lastRequest);
702
                } catch (QueryException e) {
703
                        throw new RasterDriverException("Error building GdalDriver", e);
704
                } catch (ProviderNotRegisteredException e) {
705
                        throw new RasterDriverException("Error building GdalDriver", e);
706
                } catch (InitializeException e) {
707
                        throw new RasterDriverException("Error building GdalDriver", e);
708
                }
709
                q.setBufferResult(b);
710
        }
711

    
712
        private Buffer getDownloadedRaster(File f) throws ProcessInterruptedException, QueryException, ProviderNotRegisteredException, InitializeException {
713
                AbstractRasterProvider provider = DefaultProviderServices.loadProvider(new File(f.getPath()));
714
                setColorTable(provider.getColorTable());
715
                return getDownloadedRaster(provider);
716
        }
717

    
718
        private Buffer getDownloadedRaster(AbstractRasterProvider provider) throws ProcessInterruptedException, QueryException, ProviderNotRegisteredException, InitializeException {
719
                DefaultRasterStore store = new DefaultRasterStore();
720
                store.setProvider(provider);
721

    
722
                RasterQuery q = RasterLocator.getManager().createQuery();
723
                q.setAreaOfInterest();
724
                if(getColorInterpretation() != null)
725
                        q.setDrawableBands(getColorInterpretation().buildRenderBands());
726
                if(store.getBandCount() > 3)
727
                        q.forceARGBRequest();
728
                else
729
                        q.forceRGBRequest();
730

    
731
                Buffer buf = store.query(q);
732

    
733
                store.close();
734
                return buf;
735
        }
736

    
737
        /**
738
         * Assigns the list of bands RGB and read a window of data
739
         * @param rasterBuf
740
         * @param bandList
741
         * @param lastFile
742
         * @param ulx
743
         * @param uly
744
         * @param lrx
745
         * @param lry
746
         * @return
747
         * @throws RasterDriverException
748
         * @throws ProcessInterruptedException
749
         */
750
        /*public Buffer getBuffer(Buffer rasterBuf, BandList bandList, File lastFile,
751
                        double ulx, double uly, double lrx, double lry) throws RasterDriverException, ProcessInterruptedException {
752
                try {
753
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
754
                        String serverName = bandList.getBand(0).getFileName();
755
                        for (int i = 0; i < bandList.getBandCount(); i++) {
756
                                bandList.getBand(i).setFileName(lastFile.getPath());
757
                        }
758

759
                        AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastFile.getPath()));
760
                        setColorTable(driver.getColorTable());
761

762
                        DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
763
                        q.setAreaOfInterest(new ExtentImpl(ulx, uly, lrx, lry));
764
                        q.setBandList(bandList);
765
                        q.setBuffer(rasterBuf);
766
                        Buffer buf = driver.getDataSet(q);
767

768
                        for (int i = 0; i < bandList.getBandCount(); i++) {
769
                                bandList.getBand(i).setFileName(serverName);
770
                        }
771

772
                        return buf;
773
                } catch (ProviderNotRegisteredException e) {
774
                        throw new RasterDriverException("Error building GdalDriver", e);
775
                } catch (InitializeException e) {
776
                        throw new RasterDriverException("Error building GdalDriver", e);
777
                }
778
        }*/
779

    
780
        /*public void getWindow(Extent ex, int bufWidth, int bufHeight,
781
                        BandList bandList, TileListener listener, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
782

783
                Buffer raster = DefaultRasterManager.getInstance().createBuffer(getDataType()[0], bufWidth, bufHeight, 3, true);
784
                getWindow(ex, bufWidth, bufHeight, bandList, raster, true, null);
785
                raster.setDataExtent(ex.toRectangle2D());
786

787
                TileCacheManager m = TileCacheLocator.getManager();
788
                org.gvsig.raster.cache.tile.Tile t = m.createTile(-1, 0, 0);
789
                t.setData(new Object[]{raster});
790
                t.setUl(new Point2D.Double(ex.getULX(), ex.getULY()));
791
                t.setLr(new Point2D.Double(ex.getLRX(), ex.getLRY()));
792
                t.setDownloaderParams("AffineTransform", getAffineTransform());
793
                t.setDownloaderParams("Tiling", new Boolean(false));
794
                try {
795
                        listener.tileReady(t);
796
                } catch (TileGettingException e) {
797
                        throw new RasterDriverException("Error throwing a tile", e);
798
                }
799

800
                //listener.nextBuffer(raster, null, new ExtentImpl(minX, minY, maxX, maxY), getAffineTransform(), null, false);
801
                listener.endReading();
802
        }*/
803

    
804
        /*public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf, TaskStatus status)
805
                throws ProcessInterruptedException, RasterDriverException {
806
                Rectangle2D bBox = ex.toRectangle2D();
807
                lastWidthRequest = rasterBuf.getWidth();
808
                lastHeightRequest = rasterBuf.getHeight();
809
                WMSStatus wmsStatus = loadWMSStatus(bBox);
810

811
                lastRequest = downloadFile(wmsStatus, ex.getULX(), ex.getULY(), ex.getLRX(), ex.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
812

813
                if (lastRequest == null) {
814
                        return rasterBuf;
815
                }
816

817
                try {
818
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
819
                        String serverName = bandList.getBand(0).getFileName();
820
                        for (int i = 0; i < bandList.getBandCount(); i++) {
821
                                bandList.getBand(i).setFileName(lastRequest.getPath());
822
                        }
823

824
                        AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
825
                        setColorTable(driver.getColorTable());
826

827
                        DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
828
                        q.setAreaOfInterest(ex);
829
                        q.setBandList(bandList);
830
                        q.setBuffer(rasterBuf);
831
                        Buffer buf = driver.getDataSet(q);
832

833
                        for (int i = 0; i < bandList.getBandCount(); i++) {
834
                                bandList.getBand(i).setFileName(serverName);
835
                        }
836
                        driver.close();
837
                        return buf;
838
                } catch (ProviderNotRegisteredException e) {
839
                        throw new RasterDriverException("Error building GdalDriver", e);
840
                } catch (InitializeException e) {
841
                        throw new RasterDriverException("Error building GdalDriver", e);
842
                }
843
        }*/
844

    
845
        /*public Buffer getWindow(double ulx, double uly, double w, double h,
846
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
847
                Rectangle2D bBox = new Rectangle2D.Double(ulx, uly, w, h);
848
                lastWidthRequest = rasterBuf.getWidth();
849
                lastHeightRequest = rasterBuf.getHeight();
850
                WMSStatus wmsStatus = loadWMSStatus(bBox);
851

852
                lastRequest = downloadFile(wmsStatus, ulx, uly, ulx + w, uly - h, rasterBuf.getWidth(), rasterBuf.getHeight());
853

854
                if (lastRequest == null) {
855
                        return rasterBuf;
856
                }
857

858
                try {
859
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
860
                        String serverName = bandList.getBand(0).getFileName();
861
                        for (int i = 0; i < bandList.getBandCount(); i++) {
862
                                bandList.getBand(i).setFileName(lastRequest.getPath());
863
                        }
864

865
                        AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
866
                        setColorTable(driver.getColorTable());
867

868
                        DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
869
                        q.setAreaOfInterest(ulx, uly, w, h);
870
                        q.setBandList(bandList);
871
                        q.setBuffer(rasterBuf);
872
                        q.setAdjustToExtent(adjustToExtent);
873

874
                        Buffer buf = driver.getDataSet(q);
875

876
                        for (int i = 0; i < bandList.getBandCount(); i++) {
877
                                bandList.getBand(i).setFileName(serverName);
878
                        }
879

880
                        return buf;
881
                } catch (ProviderNotRegisteredException e) {
882
                        throw new RasterDriverException("Error building GdalDriver", e);
883
                } catch (InitializeException e) {
884
                        throw new RasterDriverException("Error building GdalDriver", e);
885
                }
886
        }*/
887

    
888
        /*public Buffer getWindow(Extent extent, int bufWidth, int bufHeight,
889
                        BandList bandList, Buffer rasterBuf, boolean adjustToExtent, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
890
                Rectangle2D bBox = extent.toRectangle2D();//new Rectangle2D.Double(ulx, lry, Math.abs(lrx - ulx), Math.abs(lry - uly));
891
                lastWidthRequest = rasterBuf.getWidth();
892
                lastHeightRequest = rasterBuf.getHeight();
893
                WMSStatus wmsStatus = loadWMSStatus(bBox);
894
                lastRequest = downloadFile(wmsStatus, extent.getULX(), extent.getULY(), extent.getLRX(), extent.getLRY(), rasterBuf.getWidth(), rasterBuf.getHeight());
895

896
                if (lastRequest == null) {
897
                        return rasterBuf;
898
                }
899

900
                try {
901
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
902
                        String serverName = bandList.getBand(0).getFileName();
903
                        for (int i = 0; i < bandList.getBandCount(); i++) {
904
                                bandList.getBand(i).setFileName(lastRequest.getPath());
905
                        }
906

907
                        AbstractRasterProvider driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
908
                        setColorTable(driver.getColorTable());
909

910
                        DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
911
                        q.setAreaOfInterest(extent, bufWidth, bufHeight);
912
                        q.setBandList(bandList);
913
                        q.setBuffer(rasterBuf);
914
                        q.setAdjustToExtent(adjustToExtent);
915
                        Buffer buf = driver.getDataSet(q);
916

917
                        for (int i = 0; i < bandList.getBandCount(); i++) {
918
                                bandList.getBand(i).setFileName(serverName);
919
                        }
920

921
                        return buf;
922
                } catch (ProviderNotRegisteredException e) {
923
                        throw new RasterDriverException("Error building GdalDriver", e);
924
                } catch (InitializeException e) {
925
                        throw new RasterDriverException("Error building GdalDriver", e);
926
                }
927
        }*/
928

    
929
//        public Buffer getWindow(int x, int y,
930
//                        BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException {
931
//                int w = rasterBuf.getWidth();
932
//                int h = rasterBuf.getHeight();
933
//                Point2D p1 = rasterToWorld(new Point2D.Double(x, y));
934
//                Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h));
935
//                lastWidthRequest = rasterBuf.getWidth();
936
//                lastHeightRequest = rasterBuf.getHeight();
937
//                Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY()));
938
//                WMSStatus wmsStatus = loadWMSStatus(bBox);
939
//
940
//                lastRequest = downloadFile(wmsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight());
941
//
942
//                if (lastRequest == null) {
943
//                        return rasterBuf;
944
//                }
945
//
946
//                AbstractRasterProvider driver = null;
947
//                try {
948
//                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
949
//                        String serverName = bandList.getBand(0).getFileName();
950
//                        for (int i = 0; i < bandList.getBandCount(); i++) {
951
//                                bandList.getBand(i).setFileName(lastRequest.getPath());
952
//                        }
953
//
954
//                        driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
955
//                        Buffer buf = driver.getWindow(0, 0, w, h, bandList, rasterBuf);
956
//
957
//                        for (int i = 0; i < bandList.getBandCount(); i++) {
958
//                                bandList.getBand(i).setFileName(serverName);
959
//                        }
960
//
961
//                        return buf;
962
//                } catch (ProviderNotRegisteredException e) {
963
//                        throw new RasterDriverException("Error building GdalDriver", e);
964
//                } catch (InitializeException e) {
965
//                        throw new RasterDriverException("Error building GdalDriver", e);
966
//                }
967
//        }
968

    
969
        /*public Buffer getWindow(int x, int y, int w, int h,
970
                        BandList bandList, Buffer rasterBuf, TaskStatus status) throws ProcessInterruptedException, RasterDriverException {
971
                Point2D p1 = rasterToWorld(new Point2D.Double(x, y));
972
                Point2D p2 = rasterToWorld(new Point2D.Double(x + w, y + h));
973
                lastWidthRequest = rasterBuf.getWidth();
974
                lastHeightRequest = rasterBuf.getHeight();
975
                Rectangle2D bBox = new Rectangle2D.Double(p1.getX(), p1.getY(), Math.abs(p1.getX() - p1.getX()), Math.abs(p1.getY() - p2.getY()));
976
                WMSStatus wmsStatus = loadWMSStatus(bBox);
977

978
                lastRequest = downloadFile(wmsStatus, p1.getX(), p1.getY(), p2.getX(), p2.getY(), rasterBuf.getWidth(), rasterBuf.getHeight());
979

980
                if (lastRequest == null) {
981
                        return rasterBuf;
982
                }
983

984
                AbstractRasterProvider driver = null;
985
                try {
986
                        //El nombre de fichero que ha puesto en el bandList es el del servidor y no el del fichero en disco
987
                        String serverName = bandList.getBand(0).getFileName();
988
                        for (int i = 0; i < bandList.getBandCount(); i++) {
989
                                bandList.getBand(i).setFileName(lastRequest.getPath());
990
                        }
991

992
                        driver = DefaultProviderServices.loadProvider(new File(lastRequest.getPath()));
993
                        setColorTable(driver.getColorTable());
994

995
                        DefaultRasterQuery q = (DefaultRasterQuery)RasterLocator.getManager().createQuery();
996
                        q.setAreaOfInterest(0, 0, w, h);
997
                        q.setBandList(bandList);
998
                        q.setBuffer(rasterBuf);
999
                        Buffer buf = driver.getDataSet(q);
1000

1001
                        for (int i = 0; i < bandList.getBandCount(); i++) {
1002
                                bandList.getBand(i).setFileName(serverName);
1003
                        }
1004

1005
                        return buf;
1006
                } catch (ProviderNotRegisteredException e) {
1007
                        throw new RasterDriverException("Error building GdalDriver", e);
1008
                } catch (InitializeException e) {
1009
                        throw new RasterDriverException("Error building GdalDriver", e);
1010
                }
1011
        }*/
1012

    
1013
        public Image getImageLegend() {
1014
                try {
1015
                        WMSStatus wmsStatus = loadWMSStatus(getExtent().toRectangle2D());
1016
                        wmsStatus.setOnlineResource((String) getParameters().getOnlineResource().get("GetCapabilities"));
1017
                        File file = getConnector().getLegendGraphic(wmsStatus, getParameters().getLayerQuery(), null);
1018
                        Image img = null;
1019
                        if ((file != null) && (file.length() > 0)) {
1020
                                img = new ImageIcon(file.getAbsolutePath()).getImage();
1021
                        }
1022
                        return img;
1023
                } catch (Exception e) {
1024
                        logger.info("Problems in GetLegendGraphic", e);
1025
                }
1026
                return null;
1027
        }
1028

    
1029
        public int getBlockSize() {
1030
                return 0;
1031
        }
1032

    
1033
        public void setAffineTransform(AffineTransform t){
1034

    
1035
        }
1036

    
1037
        public int getOverviewCount(int band) throws BandAccessException, RasterDriverException {
1038
                return 0;
1039
        }
1040

    
1041
        public int getOverviewWidth(int band, int overview) throws BandAccessException, RasterDriverException {
1042
                return 0;
1043
        }
1044

    
1045
        public int getOverviewHeight(int band, int overview) throws BandAccessException, RasterDriverException {
1046
                return 0;
1047
        }
1048

    
1049
        public boolean isOverviewsSupported() {
1050
                return false;
1051
        }
1052

    
1053
        public boolean isReproyectable() {
1054
                return false;
1055
        }
1056

    
1057
        public String getProviderName() {
1058
                return NAME;
1059
        }
1060

    
1061
        public String getName() {
1062
                return this.getParameters().getName();
1063
        }
1064

    
1065
        public String getFullName() {
1066
                return getURI().getPath();
1067
        }
1068

    
1069
        /**
1070
         * Convierte un punto desde coordenadas pixel a coordenadas del mundo.
1071
         * @param pt Punto a transformar
1072
         * @return punto transformado en coordenadas del mundo
1073
         */
1074
        public Point2D rasterToWorld(Point2D pt) {
1075
                Point2D p = new Point2D.Double();
1076
                getAffineTransform().transform(pt, p);
1077
                return p;
1078
        }
1079

    
1080
        /**
1081
         * Convierte un punto desde del mundo a coordenadas pixel.
1082
         * @param pt Punto a transformar
1083
         * @return punto transformado en coordenadas pixel
1084
         */
1085
        public Point2D worldToRaster(Point2D pt) {
1086
                Point2D p = new Point2D.Double();
1087
                try {
1088
                        getAffineTransform().inverseTransform(pt, p);
1089
                } catch (NoninvertibleTransformException e) {
1090
                        return pt;
1091
                }
1092
                return p;
1093
        }
1094

    
1095
        public void setStatus(RasterProvider provider) {
1096
                if(provider instanceof WMSProvider) {
1097
                }
1098
        }
1099

    
1100
        /**
1101
         * ASigna el par?metro de inicializaci?n del driver.
1102
         */
1103
        public void setParam(DataStoreProviderServices storeServices, DataStoreParameters param) {
1104
                if(param instanceof WMSDataParameters)
1105
                        this.uri = ((WMSDataParameters)param).getURI();
1106
                this.param = param;
1107
        }
1108

    
1109

    
1110

    
1111
        public String getInfoByPoint(int x, int y, Extent bbox, int w, int h, ICancellable cancellable) throws InfoByPointException {
1112
                WMSDataParameters p = (WMSDataParameters)parameters;
1113
                WMSStatus wmsStatus = new WMSStatus();
1114
                wmsStatus.setLayerNames(Utilities.createVector(p.getLayerQuery(), ","));
1115
                wmsStatus.setSrs(p.getSRSCode());
1116
                wmsStatus.setFormat(p.getFormat());
1117
                wmsStatus.setInfoFormat(p.getInfoFormat());
1118
                List<RemoteWMSStyle> listStyles = p.getStyles();
1119
                Vector<?> v = listStyles != null ? new Vector<RemoteWMSStyle>(listStyles) : null;
1120
                wmsStatus.setStyles(v);
1121
                wmsStatus.setDimensions(p.getDimensions());
1122
                wmsStatus.setTransparency(p.isWmsTransparent());
1123
                wmsStatus.setOnlineResource((String) p.getOnlineResource().get("GetMap"));
1124
                wmsStatus.setExtent(bbox.toRectangle2D());
1125
                wmsStatus.setHeight(h);
1126
                wmsStatus.setWidth(w);
1127
                wmsStatus.setXyAxisOrder(p.isXyAxisOrder());
1128

    
1129
                try {
1130
                        return getConnector().getFeatureInfo(wmsStatus, x, y, Integer.MAX_VALUE, cancellable);
1131
                } catch (RemoteServiceException e) {
1132
                        throw new InfoByPointException("Error in getFeatureInfo", e);
1133
                }
1134
        }
1135

    
1136
        /**
1137
         * Gets the suffix of the downloaded image
1138
         * @return
1139
         */
1140
        public String getFileSuffix() {
1141
                WMSDataParameters p = (WMSDataParameters)parameters;
1142
                String format = p.getFormat();
1143
                if (format == null){
1144
                        return "xml";
1145
                }
1146
                if (format.indexOf("png") >= 0){
1147
                return "png";
1148
                }
1149
            if (format.indexOf("xml") >= 0){
1150
                return "xml";
1151
            }
1152
            if (format.indexOf("gif") >= 0){
1153
                return "gif";
1154
            }
1155
            if (format.indexOf("tif") >= 0){
1156
                return "tif";
1157
            }
1158
            if (format.indexOf("bmp") >= 0){
1159
                return "bmp";
1160
            }
1161
            if (format.indexOf("jpg") >= 0
1162
                || format.indexOf("jpeg") >= 0){
1163
                return "jpg";
1164
            }
1165
                return "xml";
1166
        }
1167

    
1168
        public TileServer getTileServer() {
1169
                if(tileServer == null) {
1170
                        DefaultRasterStore store = new DefaultRasterStore();
1171
                        store.setProvider(this);
1172
                        tileServer = new WMSTileServer(store);
1173
                }
1174
                return tileServer;
1175
        }
1176

    
1177
        public boolean isRasterEnclosed() {
1178
                WMSDataParameters p = (WMSDataParameters)parameters;
1179
                if(p.isSizeFixed()) {
1180
                        return false;
1181
                }
1182
                return true;
1183
        }
1184

    
1185
        public File getRMFFile() {
1186
                if(lastRequest != null)
1187
                    return new File(FilenameUtils.removeExtension(lastRequest.getAbsolutePath()) + ".rmf");
1188
                return null;
1189
        }
1190

    
1191
        public HistogramComputer getHistogramComputer() {
1192
                if (histogram == null)
1193
                        histogram = new RemoteStoreHistogram(this);
1194
                return histogram;
1195
        }
1196

    
1197
    /* (non-Javadoc)
1198
     * @see org.gvsig.raster.impl.provider.RasterProvider#addFile(java.io.File)
1199
     */
1200
    @Override
1201
    public void addFile(File file) throws InvalidSourceException {
1202
        //do nothing
1203
    }
1204

    
1205
    /* (non-Javadoc)
1206
     * @see org.gvsig.raster.impl.provider.RasterProvider#removeFile(java.io.File)
1207
     */
1208
    @Override
1209
    public void removeFile(File file) {
1210
        //do nothing
1211
    }
1212
}