Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / provider / tile / FileTileServer.java @ 900

History | View | Annotate | Download (4.95 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.impl.provider.tile;
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.raster.cache.tile.TileCacheLibrary;
28
import org.gvsig.raster.cache.tile.TileCacheLocator;
29
import org.gvsig.raster.cache.tile.TileCacheManager;
30
import org.gvsig.raster.cache.tile.provider.CacheStruct;
31
import org.gvsig.raster.cache.tile.provider.Downloader;
32
import org.gvsig.raster.cache.tile.provider.TileServer;
33
import org.gvsig.raster.impl.provider.DefaultRasterProvider;
34

    
35
/** 
36
* Data server for the tile cache in a GdalProvider 
37
* @author Nacho Brodin (nachobrodin@gmail.com)
38
*/
39
public class FileTileServer implements TileServer {
40
        private CacheStruct                struct         = null;
41
        private Downloader                 downloader     = null;
42
        private DefaultRasterProvider      provider       = null;
43
        private String                     suffix         = "tif";
44
        private boolean                    externalStruct = false;
45
        
46
        public FileTileServer(DefaultRasterProvider prov) {
47
                this.provider = prov;
48
        }
49
        
50
        /*
51
         * (non-Javadoc)
52
         * @see org.gvsig.raster.cache.tile.provider.TileServer#getDownloader()
53
         */
54
        public Downloader getDownloader() {
55
                //The downloader is built the first time or if the structure is assigned from the client. 
56
                boolean firstLoad = (downloader == null);
57
                
58
                if(firstLoad || externalStruct) {
59
                        downloader = new TileDownloaderForFiles(provider, getStruct(), 
60
                                        getStruct().getTileSizeByLevel(0)[0], 
61
                                        getStruct().getTileSizeByLevel(0)[1], 
62
                                        getFileSuffix());
63
                        externalStruct = false;
64
                        return downloader;
65
                }
66
                
67
                //If the tile size has changed. 
68
                if(((TileDownloaderForFiles)downloader).getTileSize()[0] != TileCacheLibrary.DEFAULT_TILEWIDTH ||
69
                   ((TileDownloaderForFiles)downloader).getTileSize()[1] != TileCacheLibrary.DEFAULT_TILEHEIGHT) {
70
                        downloader = new TileDownloaderForFiles(provider, getStruct(), 
71
                                        TileCacheLibrary.DEFAULT_TILEWIDTH, 
72
                                        TileCacheLibrary.DEFAULT_TILEHEIGHT, 
73
                                        getFileSuffix());
74
                }
75
                return downloader;
76
        }
77

    
78
        public CacheStruct getStruct() {
79
                if(struct == null) {
80
                        TileCacheManager  manager = TileCacheLocator.getManager();
81
                        
82
                        int coordinates = CacheStruct.FLAT;
83
                        if(provider.getProjection() != null)
84
                                coordinates = (provider.getProjection() != null && provider.getProjection().isProjected()) ? CacheStruct.FLAT : CacheStruct.GEOGRAFIC;
85
                        else {
86
                                Extent e = provider.getExtent();
87
                                if(e.getULX() >= -180 && e.getULX() <= 180 && e.getLRX() >= -180 && e.getLRX() <= 180 && 
88
                                        e.getULY() >= -90 && e.getULY() <= 90 && e.getLRY() >= -90 && e.getLRY() <= 90) {
89
                                        coordinates = CacheStruct.GEOGRAFIC;
90
                                }
91
                        }
92
                        
93
                        String epsg = null;
94
                        IProjection proj = provider.getProjection();
95
                        if(proj != null)
96
                                epsg = proj.getAbrev();
97
                        
98
                        struct = manager.createCacheStructure(coordinates, 
99
                                        TileCacheLibrary.DEFAULT_LEVELS, 
100
                                        provider.getExtent().toRectangle2D(), 
101
                                        provider.getCellSize(), 
102
                                        TileCacheLibrary.DEFAULT_TILEWIDTH, 
103
                                        TileCacheLibrary.DEFAULT_TILEHEIGHT,
104
                                        provider.getURIOfFirstProvider(),
105
                                        TileCacheLibrary.DEFAULT_STRUCTURE,
106
                                        RasterLibrary.pathTileCache,
107
                                        getFileSuffix(),
108
                                        epsg,
109
                                        provider.getFileSize());
110
                }
111
                return struct;
112
        }
113
        
114
        /*
115
         * (non-Javadoc)
116
         * @see org.gvsig.raster.cache.tile.provider.TileServer#setStruct(org.gvsig.raster.cache.tile.provider.CacheStruct)
117
         */
118
        public void setStruct(CacheStruct struct) {
119
                if(struct != null) {
120
                        this.struct = struct;
121
                        if(struct.getTileSizeByLevel(0) != null) {
122
                                downloader = new TileDownloaderForFiles(provider, 
123
                                                struct, 
124
                                                struct.getTileSizeByLevel(0)[0], 
125
                                                struct.getTileSizeByLevel(0)[1], 
126
                                                getFileSuffix());
127
                                externalStruct = true;
128
                        }
129
                }
130
        }
131
        
132
        /*
133
         * (non-Javadoc)
134
         * @see org.gvsig.raster.cache.tile.provider.TileServer#getFileExtension()
135
         */
136
        public String getFileSuffix() {
137
                return suffix;
138
        }
139

    
140
        /*
141
         * (non-Javadoc)
142
         * @see org.gvsig.raster.cache.tile.provider.TileServer#setFileSuffix(java.lang.String)
143
         */
144
        public void setFileSuffix(String extension) {
145
                this.suffix = extension;
146
        }
147
}