root / import / ext3D / trunk / libCacheService / src / org / gvsig / cacheservice / CacheService.java @ 14926
History | View | Annotate | Download (6.93 KB)
1 |
package org.gvsig.cacheservice; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Point; |
5 |
import java.awt.geom.Rectangle2D; |
6 |
import java.io.File; |
7 |
/**
|
8 |
*
|
9 |
* @author Rafael Gait�n <rgaitan@dsic.upv.es>, modified code from cq RasterCache of Luis W. Sevilla.
|
10 |
*
|
11 |
*/
|
12 |
public abstract class CacheService { |
13 |
|
14 |
/**
|
15 |
* Tipo planetario. Las coordenadas son Lat/Lon
|
16 |
*/
|
17 |
public static int SPHERIC = 0x10; |
18 |
/**
|
19 |
* Tipo plano (UTM). Las coordenadas son m�tricas.
|
20 |
*/
|
21 |
public static int PLANE = 0x20; |
22 |
/**
|
23 |
* el cach� se realiza en funci�n del tama�o del fichero,
|
24 |
* con los tiles relativos a su coordenada 0,0.
|
25 |
*/
|
26 |
public static int LOCAL = 0x100; |
27 |
/**
|
28 |
* el cach� se realiza con parametros globales, de manera que
|
29 |
* el tileado es con respecto a una rejilla de referencia, y no
|
30 |
* calculado especialmente para ese raster.
|
31 |
*/
|
32 |
public static int GLOBAL = 0x200; |
33 |
|
34 |
public static final Rectangle2D sphericGlobalBounds = |
35 |
new Rectangle2D.Double(-180,-90,360,180); |
36 |
public static final Rectangle2D planeGlobalBounds = |
37 |
new Rectangle2D.Double(-20000000,-10000000,40000000,20000000); |
38 |
|
39 |
public static int TILEID_TYPE_GOOGLE = 0x01; |
40 |
public static int TILEID_TYPE_OSGPLANET = 0x02; |
41 |
public static int TILEID_TYPE_VIRTUALEARTH = 0x03; |
42 |
|
43 |
private String _name = ""; |
44 |
private String _planet = ""; |
45 |
|
46 |
private int _cacheType = GLOBAL | SPHERIC; |
47 |
private int _tileIdType = TILEID_TYPE_OSGPLANET; |
48 |
private String _cacheRootDir = "/data/cache"; |
49 |
private String _cacheDir = null; |
50 |
private int _tileSize = 256; |
51 |
|
52 |
|
53 |
private boolean _withHashDirs = false; |
54 |
private int _hashDirsNumber = 100; |
55 |
|
56 |
private int _minLevel = 0; |
57 |
private int _maxLevel = 20; |
58 |
|
59 |
private Rectangle2D _bounds; |
60 |
|
61 |
private Rectangle2D _fullExtent = sphericGlobalBounds; |
62 |
|
63 |
/**
|
64 |
* @param planet
|
65 |
* @param name
|
66 |
* @param server
|
67 |
*/
|
68 |
public CacheService(String planet, String name) { |
69 |
super();
|
70 |
this._name = name;
|
71 |
this._planet = planet;
|
72 |
setBounds(sphericGlobalBounds); |
73 |
} |
74 |
|
75 |
|
76 |
/**
|
77 |
* Devuelve el tama�o de Tile como Dimension;
|
78 |
* @return
|
79 |
*/
|
80 |
public Dimension computeSz() { |
81 |
return new Dimension(getTileSize(),getTileSize()); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Calcula el Extent de un tile a partir de su level y num
|
86 |
* @param tileNum
|
87 |
* @return
|
88 |
*/
|
89 |
public Rectangle2D computeBBox(TileNum tileNum) { |
90 |
int partsX = getFullSize(tileNum.getLevel()).width/getTileSize();
|
91 |
int partsY = partsX/2; |
92 |
double stepX = (getFullExtent().getMaxX()-getFullExtent().getMinX())/partsX;
|
93 |
double stepY = (getFullExtent().getMaxY()-getFullExtent().getMinY())/partsY;
|
94 |
double l = getFullExtent().getMinX()+(double)tileNum.getX()*stepX; |
95 |
//double r = l+stepX;
|
96 |
double b;
|
97 |
if ((_cacheType & SPHERIC) == SPHERIC)
|
98 |
b = getFullExtent().getMaxY()-(double)tileNum.getY()*stepY - stepY;
|
99 |
else
|
100 |
b = getFullExtent().getMinY()+(double)tileNum.getY()*stepY;
|
101 |
|
102 |
return new Rectangle2D.Double(l,b,stepX, Math.abs(stepY)); |
103 |
} |
104 |
|
105 |
public Dimension getFullSize(int level) { |
106 |
int w = getTileSize() * (int)Math.pow(2,level); |
107 |
return new Dimension(w, w/2); |
108 |
} |
109 |
|
110 |
/**
|
111 |
* Devuelve el numero de Tiles (ancho y alto) para ese nivel.
|
112 |
* @param level
|
113 |
* @return
|
114 |
*/
|
115 |
public Dimension getLevelSize(int level) { |
116 |
int w = (int)Math.pow(2,level); |
117 |
return new Dimension(w, w/2); |
118 |
} |
119 |
|
120 |
public boolean isFileInCache(String fName) { |
121 |
File cacheFile = new File(fName); |
122 |
return cacheFile.exists();
|
123 |
} |
124 |
|
125 |
|
126 |
public void setCacheDir(String dir) { |
127 |
_cacheDir = dir; |
128 |
} |
129 |
public String getCacheDir() { |
130 |
File file;
|
131 |
if (_cacheDir == null) { |
132 |
// Compruebo si existe el directorio del planeta
|
133 |
_cacheDir = getCacheRootDir()+"/"+getPlanet()+"/"; |
134 |
file = new File(_cacheDir); |
135 |
if (!file.exists()) file.mkdir();
|
136 |
// Compruebo si existe el directorio del servidor
|
137 |
_cacheDir += getName()+"/";
|
138 |
} |
139 |
file = new File(_cacheDir); |
140 |
if (!file.exists()) file.mkdirs();
|
141 |
return _cacheDir;
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* @return Returns the cacheRootDir.
|
146 |
*/
|
147 |
public String getCacheRootDir() { |
148 |
return _cacheRootDir;
|
149 |
} |
150 |
/**
|
151 |
* @param cacheRootDir The cacheRootDir to set.
|
152 |
*/
|
153 |
public void setCacheRootDir(String cacheRootDir) { |
154 |
_cacheRootDir = cacheRootDir; |
155 |
} |
156 |
|
157 |
/**
|
158 |
* @return Returns the name.
|
159 |
*/
|
160 |
public String getName() { |
161 |
return _name;
|
162 |
} |
163 |
/**
|
164 |
* @param name The name to set.
|
165 |
*/
|
166 |
public void setName(String name) { |
167 |
_name = name; |
168 |
} |
169 |
/**
|
170 |
* @return Returns the planet.
|
171 |
*/
|
172 |
public String getPlanet() { |
173 |
return _planet;
|
174 |
} |
175 |
/**
|
176 |
* @param planet The planet to set.
|
177 |
*/
|
178 |
public void setPlanet(String planet) { |
179 |
_planet = planet; |
180 |
} |
181 |
/**
|
182 |
* @return Returns the tileSize.
|
183 |
*/
|
184 |
public int getTileSize() { |
185 |
return _tileSize;
|
186 |
} |
187 |
/**
|
188 |
* @param tileSize The tileSize to set.
|
189 |
*/
|
190 |
public void setTileSize(int tileSize) { |
191 |
_tileSize = tileSize; |
192 |
} |
193 |
/**
|
194 |
* @return Returns the tileIdType.
|
195 |
*/
|
196 |
public int getTileIdType() { |
197 |
return _tileIdType;
|
198 |
} |
199 |
/**
|
200 |
* @param tileIdType The tileIdType to set.
|
201 |
*/
|
202 |
public void setTileIdType(int tileIdType) { |
203 |
_tileIdType = tileIdType; |
204 |
} |
205 |
/**
|
206 |
* @return Returns the maxLevel.
|
207 |
*/
|
208 |
public int getMaxLevel() { |
209 |
return _maxLevel;
|
210 |
} |
211 |
/**
|
212 |
* @param maxLevel The maxLevel to set.
|
213 |
*/
|
214 |
public void setMaxLevel(int maxLevel) { |
215 |
_maxLevel = maxLevel; |
216 |
} |
217 |
/**
|
218 |
* @return Returns the minLevel.
|
219 |
*/
|
220 |
public int getMinLevel() { |
221 |
return _minLevel;
|
222 |
} |
223 |
/**
|
224 |
* @param minLevel The minLevel to set.
|
225 |
*/
|
226 |
public void setMinLevel(int minLevel) { |
227 |
_minLevel = minLevel; |
228 |
} |
229 |
/**
|
230 |
* @return Returns the bounds.
|
231 |
*/
|
232 |
public Rectangle2D getBounds() { |
233 |
return _bounds;
|
234 |
} |
235 |
|
236 |
public void setBounds(Rectangle2D r) { |
237 |
_bounds = r; |
238 |
} |
239 |
/**
|
240 |
* @return Returns the cacheType.
|
241 |
*/
|
242 |
public int getCacheType() { |
243 |
return _cacheType;
|
244 |
} |
245 |
/**
|
246 |
* @param cacheType The cacheType to set.
|
247 |
*/
|
248 |
public void setCacheType(int cacheType) { |
249 |
_cacheType = cacheType; |
250 |
if ((cacheType & GLOBAL) == GLOBAL)
|
251 |
if ((cacheType & PLANE) == PLANE)
|
252 |
setFullExtent(planeGlobalBounds); |
253 |
else if ((cacheType & SPHERIC) == SPHERIC) |
254 |
setFullExtent(sphericGlobalBounds); |
255 |
} |
256 |
/**
|
257 |
* @return Returns the fullExtent.
|
258 |
*/
|
259 |
public Rectangle2D getFullExtent() { |
260 |
return _fullExtent;
|
261 |
} |
262 |
/**
|
263 |
* @param fullExtent The fullExtent to set.
|
264 |
*/
|
265 |
public void setFullExtent(Rectangle2D fullExtent) { |
266 |
_fullExtent = fullExtent; |
267 |
} |
268 |
/**
|
269 |
* Gets Number of Hash Directories. 100 by default.
|
270 |
* @return Returns the hashDirsOrder.
|
271 |
*/
|
272 |
public int getHashDirsNumber() { |
273 |
return _hashDirsNumber;
|
274 |
} |
275 |
/**
|
276 |
* Sets Number of Hash Directories.
|
277 |
* @param hashDirsOrder The hashDirsOrder to set.
|
278 |
*/
|
279 |
public void setHashDirsNumber(int hashDirsNumber) { |
280 |
_hashDirsNumber = hashDirsNumber; |
281 |
} |
282 |
/**
|
283 |
* returns if this cache has HashDirectories. False by difault.
|
284 |
* @return Returns the withHashDirs.
|
285 |
*/
|
286 |
public boolean isWithHashDirs() { |
287 |
return _withHashDirs;
|
288 |
} |
289 |
/**
|
290 |
* sets wheter this cache has HashDirectories or no.
|
291 |
* @param withHashDirs The withHashDirs to set.
|
292 |
*/
|
293 |
public void setWithHashDirs(boolean withHashDirs) { |
294 |
_withHashDirs = withHashDirs; |
295 |
} |
296 |
/**
|
297 |
* Compute HashDirNumbre from tileNumber.
|
298 |
* @param num tileNumber (without level).
|
299 |
* @return
|
300 |
*/
|
301 |
public String getHashDir(Point num) { |
302 |
String hd = null; |
303 |
hd = ""+(num.x % _hashDirsNumber);
|
304 |
return hd;
|
305 |
} |
306 |
} |