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 / downloader / WMSTileServer.java @ 4181

History | View | Annotate | Download (4.62 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.downloader;
23

    
24
import org.cresques.cts.IProjection;
25
import org.gvsig.fmap.dal.coverage.RasterLibrary;
26
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
27
import org.gvsig.fmap.dal.coverage.exception.RemoteServiceException;
28
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
29
import org.gvsig.raster.cache.tile.TileCacheLibrary;
30
import org.gvsig.raster.cache.tile.TileCacheLocator;
31
import org.gvsig.raster.cache.tile.TileCacheManager;
32
import org.gvsig.raster.cache.tile.provider.CacheStruct;
33
import org.gvsig.raster.cache.tile.provider.Downloader;
34
import org.gvsig.raster.cache.tile.provider.TileServer;
35
import org.gvsig.raster.impl.provider.RasterProvider;
36
import org.gvsig.raster.wms.io.WMSProvider;
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
/**
41
* Data server for the tile cache in a WMSProvider
42
* @author Nacho Brodin (nachobrodin@gmail.com)
43
*/
44
public class WMSTileServer implements TileServer {
45
        private static Logger              logger               = LoggerFactory.getLogger(WMSTileServer.class);
46
        private CacheStruct                struct               = null;
47
        private Downloader                 downloader           = null;
48
        private RasterDataStore            store                = null;
49
        private String                     suffix               = ".tif";
50

    
51
        public WMSTileServer(RasterDataStore store) {
52
                this.store = store;
53
                this.suffix = ((RasterProvider)store.getProvider()).getFileSuffix();
54
        }
55

    
56
        public Downloader getDownloader() {
57
                if(downloader == null ||
58
                   ((TileDownloaderForWMS)downloader).getTileSize()[0] != TileCacheLibrary.ALTERNATIVE_TILESIZE ||
59
                   ((TileDownloaderForWMS)downloader).getTileSize()[1] != TileCacheLibrary.ALTERNATIVE_TILESIZE) {
60
                        try {
61
                                downloader = new TileDownloaderForWMS(
62
                                                store,
63
                                                TileCacheLibrary.ALTERNATIVE_TILESIZE,
64
                                                TileCacheLibrary.ALTERNATIVE_TILESIZE);
65
                        } catch (RemoteServiceException e) {
66
                                return null;
67
                        }
68
                }
69
                return downloader;
70
        }
71

    
72
        public CacheStruct getStruct() {
73
                if(struct == null) {
74
                        TileCacheManager  manager = TileCacheLocator.getManager();
75

    
76
                        int coordinates = CacheStruct.FLAT;
77
                        if(store.getProjection() != null)
78
                                coordinates = (store.getProjection() != null && store.getProjection().isProjected()) ? CacheStruct.FLAT : CacheStruct.GEOGRAFIC;
79
                        else {
80
                                Extent e = store.getExtent();
81
                                if(e.getULX() >= -180 && e.getULX() <= 180 && e.getLRX() >= -180 && e.getLRX() <= 180 &&
82
                                        e.getULY() >= -90 && e.getULY() <= 90 && e.getLRY() >= -90 && e.getLRY() <= 90) {
83
                                        coordinates = CacheStruct.GEOGRAFIC;
84
                                }
85
                        }
86

    
87
                        String epsg = null;
88
                        IProjection proj = store.getProjection();
89
                        if(proj != null)
90
                                epsg = proj.getAbrev();
91

    
92
                        struct = manager.createCacheStructure(coordinates,
93
                                        TileCacheLibrary.DEFAULT_LEVELS,
94
                                        store.getExtent().toRectangle2D(),
95
                                        Double.POSITIVE_INFINITY,//provider.getCellSize(),
96
                                        TileCacheLibrary.ALTERNATIVE_TILESIZE,
97
                                        TileCacheLibrary.ALTERNATIVE_TILESIZE,
98
                                        ((RasterProvider)store.getProvider()).getURIOfFirstProvider().getPath(),
99
                                        ((WMSProvider)store.getProvider()).getParameters().getLayerQuery(),
100
                                        TileCacheLibrary.DEFAULT_STRUCTURE,
101
                                        RasterLibrary.pathTileCache,
102
                                        getFileSuffix(),
103
                                        epsg,
104
                                        0);
105
                }
106
                return struct;
107
        }
108

    
109
        public void setStruct(CacheStruct struct) {
110
                if(struct != null) {
111
                        this.struct = struct;
112
                        if(struct.getTileSizeByLevel(0) != null) {
113
                                try {
114
                                        downloader = new TileDownloaderForWMS(store,
115
                                                        struct.getTileSizeByLevel(0)[0],
116
                                                        struct.getTileSizeByLevel(0)[1]);
117
                                } catch (RemoteServiceException ex) {
118
                                        logger.error("Constructing TileDownloaderForWCS: " + ex.getMessage());
119
                                }
120
                        }
121
                }
122
        }
123

    
124
        public String getFileSuffix() {
125
                return suffix;
126
        }
127

    
128
        public void setFileSuffix(String extension) {
129
                this.suffix = extension;
130
        }
131
}