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 @ 4272
History | View | Annotate | Download (10.2 KB)
1 | 2485 | nbrodin | /* 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 | 4272 | fdiaz | import java.io.File; |
27 | import java.net.URL; |
||
28 | 2485 | nbrodin | import java.util.ArrayList; |
29 | import java.util.List; |
||
30 | |||
31 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
32 | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
||
33 | import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
||
34 | import org.gvsig.fmap.dal.coverage.util.MathUtils; |
||
35 | import org.gvsig.raster.cache.tile.Tile; |
||
36 | import org.gvsig.raster.cache.tile.TileCacheLocator; |
||
37 | import org.gvsig.raster.cache.tile.exception.TileBuildException; |
||
38 | import org.gvsig.raster.cache.tile.provider.CacheStruct; |
||
39 | import org.gvsig.raster.wmts.io.WMTSDataParameters; |
||
40 | import org.gvsig.raster.wmts.io.WMTSProvider; |
||
41 | import org.gvsig.raster.wmts.ogc.WMTSStatus; |
||
42 | import org.gvsig.raster.wmts.ogc.struct.WMTSTile; |
||
43 | import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrix; |
||
44 | import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixLimits; |
||
45 | import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSet; |
||
46 | import org.gvsig.raster.wmts.ogc.struct.WMTSTileMatrixSetLink; |
||
47 | |||
48 | /**
|
||
49 | * Implementation for a structure of a cache
|
||
50 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
51 | */
|
||
52 | public class WMTSCacheStruct implements CacheStruct { |
||
53 | private String layerName = null; |
||
54 | private String serverURL = null; |
||
55 | private WMTSTileMatrixSet tileMatrixSet = null; |
||
56 | private List<WMTSTileMatrixLimits> tileMatrixSetLimits = null; |
||
57 | private double[] pixelSize = null; |
||
58 | private RasterDataStore store = null; |
||
59 | 4272 | fdiaz | |
60 | 2485 | nbrodin | public WMTSCacheStruct(RasterDataStore store, WMTSTileMatrixSetLink tileMatrixSetLink) {
|
61 | this.store = store;
|
||
62 | this.tileMatrixSet = tileMatrixSetLink.getTileMatrixSet();
|
||
63 | this.tileMatrixSetLimits = tileMatrixSetLink.getTileMatrixLimits();
|
||
64 | pixelSize = ((WMTSProvider)store.getProvider()).getPixelSizeByLevel(); |
||
65 | 4272 | fdiaz | serverURL = ((WMTSProvider)store.getProvider()).getURIOfFirstProvider().getPath(); |
66 | 2485 | nbrodin | layerName = ((WMTSDataParameters)store.getParameters()).getLayer().getTitle(); |
67 | } |
||
68 | |||
69 | public int getNumberOfLevels() { |
||
70 | return tileMatrixSet.getTileMatrix().size();
|
||
71 | } |
||
72 | |||
73 | public String getLayerName() { |
||
74 | return layerName;
|
||
75 | } |
||
76 | |||
77 | public String getServerURL() { |
||
78 | 2618 | nbrodin | return serverURL + getDimension();
|
79 | 2485 | nbrodin | } |
80 | |||
81 | public int[] getTileSizeByLevel(int level) { |
||
82 | return new int[] { |
||
83 | ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth(), |
||
84 | ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight() |
||
85 | }; |
||
86 | } |
||
87 | |||
88 | public int getLayerWidthOfTileMatrixByLevel(int level) { |
||
89 | if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) { |
||
90 | WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level); |
||
91 | return l.getMaxTileRow() - l.getMinTileRow();
|
||
92 | } else {
|
||
93 | WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level); |
||
94 | return (int)tm.getMatrixWidth(); |
||
95 | } |
||
96 | } |
||
97 | |||
98 | public int getLayerHeightOfTileMatrixByLevel(int level) { |
||
99 | if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) { |
||
100 | WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level); |
||
101 | return l.getMaxTileCol() - l.getMinTileCol();
|
||
102 | } else {
|
||
103 | WMTSTileMatrix tm = (WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level); |
||
104 | return (int)tm.getMatrixHeight(); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | public int getLayerInitXTilePositionByLevel(int level) { |
||
109 | if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) { |
||
110 | WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level); |
||
111 | return l.getMinTileCol();
|
||
112 | } else
|
||
113 | return 0; |
||
114 | } |
||
115 | |||
116 | public int getLayerInitYTilePositionByLevel(int level) { |
||
117 | if(tileMatrixSetLimits != null && tileMatrixSetLimits.size() > level) { |
||
118 | WMTSTileMatrixLimits l = (WMTSTileMatrixLimits)tileMatrixSetLimits.get(level); |
||
119 | return l.getMinTileRow();
|
||
120 | 4272 | fdiaz | } else
|
121 | 2485 | nbrodin | return 0; |
122 | } |
||
123 | |||
124 | public long getWorldHeightOfTileMatrixByLevel(int level) { |
||
125 | return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixWidth();
|
||
126 | } |
||
127 | |||
128 | public long getWorldWidthOfTileMatrixByLevel(int level) { |
||
129 | return ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getMatrixHeight();
|
||
130 | } |
||
131 | |||
132 | public double getPixelSizeByLevel(int level) { |
||
133 | if(level < pixelSize.length)
|
||
134 | return pixelSize[level];
|
||
135 | else
|
||
136 | return pixelSize[pixelSize.length - 1]; |
||
137 | } |
||
138 | |||
139 | public Point2D[] getTileExtent(Tile tile) { |
||
140 | return getTileExtent(tile.getLevel(), tile.getCol(), tile.getRow());
|
||
141 | } |
||
142 | |||
143 | public Point2D[] getTileExtent(int level, int col, int row) { |
||
144 | double[] ul = tileMatrixSet.getBoundingBox().getUpperCorner(); |
||
145 | long tileWidth = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileWidth();
|
||
146 | long tileHeight = ((WMTSTileMatrix)tileMatrixSet.getTileMatrix().get(level)).getTileHeight();
|
||
147 | double mtsWidthTile = tileWidth * pixelSize[level];
|
||
148 | double mtsHeightTile = tileHeight * pixelSize[level];
|
||
149 | double ulx = ul[0] + (col * mtsWidthTile); |
||
150 | double uly = ulx + mtsWidthTile;
|
||
151 | double lrx = ul[1] - (row * mtsHeightTile); |
||
152 | double lry = lrx - mtsHeightTile;
|
||
153 | return new Point2D[]{new Point2D.Double(ulx, uly), new Point2D.Double(lrx, lry)}; |
||
154 | } |
||
155 | |||
156 | public Tile getTileStructure(int level, int tileCol, int tileRow, Point2D ul, Point2D lr) throws TileBuildException { |
||
157 | int[] size = getTileSizeByLevel(level); |
||
158 | Tile tile = TileCacheLocator.getManager().createTile(level, tileRow, tileCol); |
||
159 | tile.setUl(ul); |
||
160 | tile.setLr(lr); |
||
161 | tile.setWidthPx(size[0]);
|
||
162 | tile.setHeightPx(size[1]);
|
||
163 | 4272 | fdiaz | |
164 | Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), |
||
165 | Math.min(ul.getY(), lr.getY()),
|
||
166 | Math.abs(ul.getX() - lr.getX()),
|
||
167 | 2485 | nbrodin | Math.abs(ul.getY() - lr.getY()));
|
168 | int bufWidth = (int)(r.getWidth() / getPixelSizeByLevel(level)); |
||
169 | int bufHeight = (int)(r.getHeight() / getPixelSizeByLevel(level)); |
||
170 | try {
|
||
171 | WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight); |
||
172 | tile.setDownloaderParams("WMTSStatus", status);
|
||
173 | } catch (RasterDriverException e) {
|
||
174 | 4272 | fdiaz | throw new TileBuildException(e); |
175 | 2485 | nbrodin | } |
176 | 4272 | fdiaz | |
177 | 2485 | nbrodin | return tile;
|
178 | } |
||
179 | 4272 | fdiaz | |
180 | 2485 | nbrodin | public List<Tile> getTileList(Point2D ul, Point2D lr, double mtsPixelRequest) throws TileBuildException { |
181 | 4272 | fdiaz | Rectangle2D r = new Rectangle2D.Double(Math.min(ul.getX(), lr.getX()), |
182 | Math.min(ul.getY(), lr.getY()),
|
||
183 | Math.abs(ul.getX() - lr.getX()),
|
||
184 | 2485 | nbrodin | Math.abs(ul.getY() - lr.getY()));
|
185 | int bufWidth = (int)(r.getWidth() / mtsPixelRequest); |
||
186 | int bufHeight = (int)(r.getHeight() / mtsPixelRequest); |
||
187 | try {
|
||
188 | WMTSStatus status = ((WMTSProvider)store.getProvider()).buildWMTSStatus(r, bufWidth, bufHeight); |
||
189 | 4272 | fdiaz | |
190 | 2485 | nbrodin | //Conversi?n a tiles de la librer?a
|
191 | List<WMTSTile> tiles = status.getTileList();
|
||
192 | List<Tile> tileList = new ArrayList<Tile>(); |
||
193 | for (int i = 0; i < tiles.size(); i++) { |
||
194 | WMTSTile tOrigin = (WMTSTile) tiles.get(i); |
||
195 | Tile t = TileCacheLocator.getManager().createTile(status.getLevel(), tOrigin.getCol(), tOrigin.getRow()); |
||
196 | t.setUl(new Point2D.Double(tOrigin.getULX(), tOrigin.getULY())); |
||
197 | t.setLr(new Point2D.Double(tOrigin.getLRX(), tOrigin.getLRY())); |
||
198 | t.setWidthPx(tOrigin.getWidthPx()); |
||
199 | t.setHeightPx(tOrigin.getHeightPx()); |
||
200 | t.setDownloaderParams("WMTSStatus", status.cloneStatus());
|
||
201 | tileList.add(t); |
||
202 | } |
||
203 | return tileList;
|
||
204 | } catch (RasterDriverException e) {
|
||
205 | 4272 | fdiaz | throw new TileBuildException(e); |
206 | 2485 | nbrodin | } |
207 | } |
||
208 | |||
209 | public double[] getTileSizeInRealCoordsByLevel(int level) { |
||
210 | return new double[] { |
||
211 | getPixelSizeByLevel(level) * getTileSizeByLevel(level)[0],
|
||
212 | getPixelSizeByLevel(level) * getTileSizeByLevel(level)[1]
|
||
213 | }; |
||
214 | } |
||
215 | |||
216 | public String getFileSuffix() { |
||
217 | return ((WMTSProvider)store.getProvider()).getFileSuffix();
|
||
218 | } |
||
219 | |||
220 | public boolean compare(CacheStruct struct) { |
||
221 | MathUtils mu = RasterLocator.getManager().getMathUtils(); |
||
222 | 4272 | fdiaz | //Compara: n?mero de niveles, tama?o de tile,
|
223 | //anchoXalto de la matriz, tama?o de pixel por nivel,
|
||
224 | 2485 | nbrodin | //coordenadas de al menos un tile de la matriz
|
225 | if(struct.getNumberOfLevels() == getNumberOfLevels()) {
|
||
226 | for (int i = 0; i < getNumberOfLevels(); i++) { |
||
227 | 4272 | fdiaz | if( struct.getTileSizeByLevel(i)[0] == getTileSizeByLevel(i)[0] && |
228 | 2485 | nbrodin | struct.getTileSizeByLevel(i)[1] == getTileSizeByLevel(i)[1] && |
229 | struct.getWorldHeightOfTileMatrixByLevel(i) == getWorldHeightOfTileMatrixByLevel(i) && |
||
230 | struct.getWorldWidthOfTileMatrixByLevel(i) == getWorldWidthOfTileMatrixByLevel(i) && |
||
231 | mu.clipDecimals(struct.getPixelSizeByLevel(i), 2) == mu.clipDecimals(getPixelSizeByLevel(i), 2) && |
||
232 | compareExtents(struct.getTileExtent(i, 0, 0), getTileExtent(i, 0, 0))) { |
||
233 | return true; |
||
234 | } |
||
235 | } |
||
236 | } |
||
237 | return false; |
||
238 | } |
||
239 | 4272 | fdiaz | |
240 | 2485 | nbrodin | private boolean compareExtents(Point2D[] p, Point2D[] p1) { |
241 | return (p[0].getX() == p1[0].getX() && p[0].getY() == p1[0].getY() && |
||
242 | p[1].getX() == p1[1].getX() && p[1].getY() == p1[1].getY()); |
||
243 | } |
||
244 | |||
245 | public String getEPSG() { |
||
246 | /*IProjection proj = provider.getProjection();
|
||
247 | if(proj != null)
|
||
248 | return proj.getAbrev();
|
||
249 | return null;*/
|
||
250 | return ((WMTSProvider)store.getProvider()).getSRSCode();
|
||
251 | } |
||
252 | 4272 | fdiaz | |
253 | 2618 | nbrodin | private String getDimension() { |
254 | WMTSDataParameters p = (WMTSDataParameters)(((WMTSProvider)store.getProvider()).getDataParameters()); |
||
255 | String dimension = p.getDimension();
|
||
256 | String dimensionValue = p.getDimensionSelectedValue();
|
||
257 | if(dimension != null && dimensionValue != null) |
||
258 | return dimension + "_" + dimensionValue; |
||
259 | return ""; |
||
260 | } |
||
261 | 2485 | nbrodin | |
262 | public String getFileSize() { |
||
263 | return "0"; |
||
264 | } |
||
265 | |||
266 | public List<Tile> getTileList(int x, int y, int w, int h) { |
||
267 | return null; |
||
268 | } |
||
269 | |||
270 | public ArrayList<Tile> getTileList(Rectangle2D r) { |
||
271 | return null; |
||
272 | } |
||
273 | } |