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

History | View | Annotate | Download (4.58 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 java.net.URI;
25

    
26
import org.cresques.cts.IProjection;
27

    
28
import org.gvsig.fmap.dal.coverage.RasterLibrary;
29
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
30
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
31
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters;
32
import org.gvsig.raster.cache.tile.TileCacheLibrary;
33
import org.gvsig.raster.cache.tile.TileCacheLocator;
34
import org.gvsig.raster.cache.tile.TileCacheManager;
35
import org.gvsig.raster.cache.tile.provider.CacheStruct;
36
import org.gvsig.raster.cache.tile.provider.Downloader;
37
import org.gvsig.raster.cache.tile.provider.TileServer;
38

    
39
/**
40
* Data server for the tile cache in a GdalProvider
41
* @author Nacho Brodin (nachobrodin@gmail.com)
42
*/
43
public class FileTileServer implements TileServer {
44
        protected   CacheStruct                struct         = null;
45
        protected   Downloader                 downloader     = null;
46
        protected   RasterDataStore            store          = null;
47
        protected   String                     suffix         = "tif";
48
        protected   boolean                    externalStruct = false;
49

    
50
        public FileTileServer(RasterDataStore store) {
51
                this.store = store;
52
        }
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(store, 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(store, 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(store.getProjection() != null)
84
                                coordinates = (store.getProjection() != null && store.getProjection().isProjected()) ? CacheStruct.FLAT : CacheStruct.GEOGRAFIC;
85
                        else {
86
                                Extent e = store.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 = store.getProjection();
95
                        if(proj != null)
96
                                epsg = proj.getAbrev();
97

    
98
                        URI uri = ((RasterDataParameters)store.getParameters()).getURI();
99
                        struct = manager.createCacheStructure(coordinates,
100
                                        TileCacheLibrary.DEFAULT_LEVELS,
101
                                        store.getExtent().toRectangle2D(),
102
                                        store.getCellSize(),
103
                                        TileCacheLibrary.DEFAULT_TILEWIDTH,
104
                                        TileCacheLibrary.DEFAULT_TILEHEIGHT,
105
                                        uri.getPath(),
106
                                        TileCacheLibrary.DEFAULT_STRUCTURE,
107
                                        RasterLibrary.pathTileCache,
108
                                        getFileSuffix(),
109
                                        epsg,
110
                                        store.getFileSize());
111
                }
112
                return struct;
113
        }
114

    
115
        public void setStruct(CacheStruct struct) {
116
                if(struct != null) {
117
                        this.struct = struct;
118
                        if(struct.getTileSizeByLevel(0) != null) {
119
                                downloader = new TileDownloaderForFiles(store,
120
                                                struct,
121
                                                struct.getTileSizeByLevel(0)[0],
122
                                                struct.getTileSizeByLevel(0)[1],
123
                                                getFileSuffix());
124
                                externalStruct = true;
125
                        }
126
                }
127
        }
128

    
129
        public String getFileSuffix() {
130
                return suffix;
131
        }
132

    
133
        public void setFileSuffix(String extension) {
134
                this.suffix = extension;
135
        }
136
}