Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wmts / trunk / org.gvsig.raster.wmts / org.gvsig.raster.wmts.io / src / main / java / org / gvsig / raster / wmts / io / downloader / WMTSCacheStruct.java @ 2618

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

    
24
import java.awt.geom.Point2D;
25
import java.awt.geom.Rectangle2D;
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import org.gvsig.fmap.dal.coverage.RasterLocator;
30
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
31
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
32
import org.gvsig.fmap.dal.coverage.util.MathUtils;
33
import org.gvsig.raster.cache.tile.Tile;
34
import org.gvsig.raster.cache.tile.TileCacheLocator;
35
import org.gvsig.raster.cache.tile.exception.TileBuildException;
36
import org.gvsig.raster.cache.tile.provider.CacheStruct;
37
import org.gvsig.raster.wmts.io.WMTSDataParameters;
38
import org.gvsig.raster.wmts.io.WMTSProvider;
39
import org.gvsig.raster.wmts.ogc.WMTSStatus;
40
import org.gvsig.raster.wmts.ogc.struct.WMTSTile;
41
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix;
42
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixLimits;
43
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet;
44
import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSetLink;
45

    
46
/**
47
 * Implementation for a structure of a cache
48
 * @author Nacho Brodin (nachobrodin@gmail.com)
49
 */
50
public class WMTSCacheStruct implements CacheStruct {
51
    private String                        layerName           = null;
52
        private String                        serverURL           = null;
53
        private WMTSTileMatrixSet             tileMatrixSet       = null;
54
        private List<WMTSTileMatrixLimits>    tileMatrixSetLimits = null;
55
        private double[]                      pixelSize           = null;
56
    private RasterDataStore               store               = null;
57
    
58
    public WMTSCacheStruct(RasterDataStore store, WMTSTileMatrixSetLink tileMatrixSetLink) {
59
            this.store = store;
60
            this.tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
61
                this.tileMatrixSetLimits = tileMatrixSetLink.getTileMatrixLimits();
62
                pixelSize = ((WMTSProvider)store.getProvider()).getPixelSizeByLevel();
63
                serverURL = ((WMTSProvider)store.getProvider()).getURIOfFirstProvider();
64
                layerName = ((WMTSDataParameters)store.getParameters()).getLayer().getTitle();
65
    }
66

    
67
        public int getNumberOfLevels() {
68
                return tileMatrixSet.getTileMatrix().size();
69
        }
70

    
71
        public String getLayerName() {
72
                return layerName;
73
        }
74

    
75
        public String getServerURL() {
76
                return serverURL + getDimension();
77
        }
78

    
79
        public int[] getTileSizeByLevel(int level) {
80
                return new int[] {
81
                        ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth(),
82
                        ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight()
83
                };
84
        }
85

    
86
        public int getLayerWidthOfTileMatrixByLevel(int level) {
87
                if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
88
                        WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
89
                        return l.getMaxTileRow() - l.getMinTileRow();
90
                } else {
91
                        WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level);
92
                        return (int)tm.getMatrixWidth();
93
                }
94
        }
95

    
96
        public int getLayerHeightOfTileMatrixByLevel(int level) {
97
                if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
98
                        WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
99
                        return l.getMaxTileCol() - l.getMinTileCol();
100
                } else {
101
                        WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level);
102
                        return (int)tm.getMatrixHeight();
103
                }
104
        }
105

    
106
        public int getLayerInitXTilePositionByLevel(int level) {
107
                if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
108
                        WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
109
                        return l.getMinTileCol();
110
                } else
111
                        return 0;
112
        }
113

    
114
        public int getLayerInitYTilePositionByLevel(int level) {
115
                if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) {
116
                        WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level);
117
                        return l.getMinTileRow();
118
                } else 
119
                        return 0;
120
        }
121

    
122
        public long getWorldHeightOfTileMatrixByLevel(int level) {
123
                return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixWidth();
124
        }
125

    
126
        public long getWorldWidthOfTileMatrixByLevel(int level) {
127
                return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixHeight();
128
        }
129

    
130
        public double getPixelSizeByLevel(int level) {
131
                if(level < pixelSize.length)
132
                        return pixelSize[level];
133
                else
134
                        return pixelSize[pixelSize.length - 1];
135
        }
136

    
137
        public Point2D[] getTileExtent(Tile tile) {
138
                return getTileExtent(tile.getLevel(), tile.getCol(), tile.getRow());
139
        }
140

    
141
        public Point2D[] getTileExtent(int level, int col, int row) {
142
                double[] ul = tileMatrixSet.getBoundingBox().getUpperCorner();
143
                long tileWidth = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth();
144
                long tileHeight = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight();
145
                double mtsWidthTile = tileWidth * pixelSize[level];
146
                double mtsHeightTile = tileHeight * pixelSize[level];
147
                double ulx = ul[0] + (col * mtsWidthTile);
148
                double uly = ulx + mtsWidthTile;
149
                double lrx = ul[1] - (row * mtsHeightTile);
150
                double lry = lrx - mtsHeightTile;
151
                return new Point2D[]{new Point2D.Double(ulx, uly), new Point2D.Double(lrx, lry)};
152
        }
153

    
154
        public Tile getTileStructure(int level, int tileCol, int tileRow, Point2D ul, Point2D lr) throws TileBuildException  {
155
                int[] size = getTileSizeByLevel(level);
156
                Tile tile = TileCacheLocator.getManager().createTile(level, tileRow, tileCol);
157
                tile.setUl(ul);
158
                tile.setLr(lr);
159
                tile.setWidthPx(size[0]);
160
                tile.setHeightPx(size[1]);
161
                
162
                Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), 
163
                                Math.min(ul.getY(), lr.getY()), 
164
                                Math.abs(ul.getX() - lr.getX()), 
165
                                Math.abs(ul.getY() - lr.getY()));
166
                int bufWidth = (int)(r.getWidth() / getPixelSizeByLevel(level));
167
                int bufHeight = (int)(r.getHeight() / getPixelSizeByLevel(level));
168
                try {
169
                        WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight);
170
                        tile.setDownloaderParams("WMTSStatus", status);
171
                } catch (RasterDriverException e) {
172
                        throw new TileBuildException(e); 
173
                }
174
                        
175
                return tile;
176
        }
177
        
178
        public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelRequest) throws TileBuildException {
179
                Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), 
180
                                Math.min(ul.getY(), lr.getY()), 
181
                                Math.abs(ul.getX() - lr.getX()), 
182
                                Math.abs(ul.getY() - lr.getY()));
183
                int bufWidth = (int)(r.getWidth() / mtsPixelRequest);
184
                int bufHeight = (int)(r.getHeight() / mtsPixelRequest);
185
                try {
186
                        WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight);
187
                        
188
                        //Conversi?n a tiles de la librer?a
189
                        List<WMTSTile> tiles = status.getTileList();
190
                        List<Tile> tileList = new ArrayList<Tile>();
191
                        for (int i = 0; i < tiles.size(); i++) {
192
                                WMTSTile tOrigin = (WMTSTile) tiles.get(i);
193
                                Tile t = TileCacheLocator.getManager().createTile(status.getLevel(), tOrigin.getCol(), tOrigin.getRow());
194
                                t.setUl(new Point2D.Double(tOrigin.getULX(), tOrigin.getULY()));
195
                                t.setLr(new Point2D.Double(tOrigin.getLRX(), tOrigin.getLRY()));
196
                                t.setWidthPx(tOrigin.getWidthPx());
197
                                t.setHeightPx(tOrigin.getHeightPx());
198
                                t.setDownloaderParams("WMTSStatus", status.cloneStatus());
199
                                tileList.add(t);
200
                        }
201
                        return tileList;
202
                } catch (RasterDriverException e) {
203
                        throw new TileBuildException(e); 
204
                }
205
        }
206

    
207
        public double[] getTileSizeInRealCoordsByLevel(int level) {
208
                return new double[] {
209
                                getPixelSizeByLevel(level) * getTileSizeByLevel(level)[0],
210
                                getPixelSizeByLevel(level) *  getTileSizeByLevel(level)[1]
211
                        };
212
        }
213

    
214
        public String getFileSuffix() {
215
                return ((WMTSProvider)store.getProvider()).getFileSuffix();
216
        }
217

    
218
        public boolean compare(CacheStruct struct) {
219
                MathUtils mu = RasterLocator.getManager().getMathUtils();
220
                //Compara: n?mero de niveles, tama?o de tile, 
221
                //anchoXalto de la matriz, tama?o de pixel por nivel, 
222
                //coordenadas de al menos un tile de la matriz
223
                if(struct.getNumberOfLevels() == getNumberOfLevels()) {
224
                        for (int i = 0; i < getNumberOfLevels(); i++) {
225
                                if(        struct.getTileSizeByLevel(i)[0] == getTileSizeByLevel(i)[0] && 
226
                                        struct.getTileSizeByLevel(i)[1] == getTileSizeByLevel(i)[1] &&
227
                                        struct.getWorldHeightOfTileMatrixByLevel(i) == getWorldHeightOfTileMatrixByLevel(i) &&
228
                                        struct.getWorldWidthOfTileMatrixByLevel(i) == getWorldWidthOfTileMatrixByLevel(i) &&
229
                                        mu.clipDecimals(struct.getPixelSizeByLevel(i), 2) == mu.clipDecimals(getPixelSizeByLevel(i), 2) &&
230
                                        compareExtents(struct.getTileExtent(i, 0, 0), getTileExtent(i, 0, 0))) {
231
                                        return true;
232
                                }
233
                        }
234
                }
235
                return false;
236
        }
237
        
238
        private boolean compareExtents(Point2D[] p, Point2D[] p1) {
239
                return (p[0].getX() == p1[0].getX() && p[0].getY() == p1[0].getY() &&
240
                                p[1].getX() == p1[1].getX() && p[1].getY() == p1[1].getY());
241
        }
242

    
243
        public String getEPSG() {
244
                /*IProjection proj = provider.getProjection();
245
                if(proj != null)
246
                        return proj.getAbrev();
247
                return null;*/
248
                return ((WMTSProvider)store.getProvider()).getSRSCode();
249
        }
250
        
251
        private String getDimension() {
252
                WMTSDataParameters p = (WMTSDataParameters)(((WMTSProvider)store.getProvider()).getDataParameters());
253
                String dimension = p.getDimension();
254
                String dimensionValue = p.getDimensionSelectedValue();
255
                if(dimension != null && dimensionValue != null)
256
                        return dimension + "_" + dimensionValue;
257
                return "";
258
        }
259

    
260
        public String getFileSize() {
261
                return "0";
262
        }
263

    
264
        public List<Tile> getTileList(int x, int y, int w, int h) {
265
                return null;
266
        }
267

    
268
        public ArrayList<Tile> getTileList(Rectangle2D r) {
269
                return null;
270
        }
271
}