Revision 15486 import/ext3D/trunk/ext3D/src/com/iver/ai2/gvsig3d/cacheservices/FLayerCacheService.java
FLayerCacheService.java | ||
---|---|---|
44 | 44 |
import es.upv.ai2.libjosg.planets.Planet.PlanetType; |
45 | 45 |
|
46 | 46 |
public class FLayerCacheService extends RasterCacheService { |
47 |
private boolean _loadFailed = true;
|
|
48 |
private Extent _extent;
|
|
47 |
private boolean _loadFailed = true; |
|
48 |
private Extent _extent; |
|
49 | 49 |
private Rectangle2D _lyrExtentRect; |
50 |
private FLayer _layer;
|
|
50 |
private FLayer _layer; |
|
51 | 51 |
private IProjection _viewProj; |
52 |
private int _dataType;
|
|
52 |
private int _dataType; |
|
53 | 53 |
|
54 | 54 |
// WMS terrain |
55 | 55 |
FMapWMSDriver _wmsDriver = null; |
56 | 56 |
FMapWCSDriver _wcsDriver = null; |
57 |
WMSStatus _wmsStatus = null;
|
|
58 |
WCSStatus _wcsStatus = null;
|
|
57 |
WMSStatus _wmsStatus = null; |
|
58 |
WCSStatus _wcsStatus = null; |
|
59 | 59 |
|
60 |
public FLayerCacheService(Planet planet, String name, FLayer layer, IProjection viewProj) { |
|
60 |
public FLayerCacheService(Planet planet, String name, FLayer layer, |
|
61 |
IProjection viewProj) { |
|
61 | 62 |
super(planet.getPlanetName(), name); |
62 | 63 |
|
63 | 64 |
_layer = layer; |
... | ... | |
65 | 66 |
_loadFailed = true; |
66 | 67 |
|
67 | 68 |
int cacheType = CacheService.GLOBAL; |
68 |
if (planet.getType()==PlanetType.SPHERICAL_MODE)
|
|
69 |
cacheType += SPHERIC;
|
|
69 |
if (planet.getType() == PlanetType.SPHERICAL_MODE)
|
|
70 |
cacheType += SPHERIC;
|
|
70 | 71 |
else |
71 |
cacheType += PLANE;
|
|
72 |
cacheType += PLANE;
|
|
72 | 73 |
setCacheType(cacheType); |
73 | 74 |
|
74 | 75 |
try { |
75 | 76 |
_lyrExtentRect = _layer.getFullExtent(); |
76 |
} |
|
77 |
catch (DriverException e) { |
|
77 |
} catch (DriverException e) { |
|
78 | 78 |
return; |
79 | 79 |
} |
80 | 80 |
|
... | ... | |
82 | 82 |
|
83 | 83 |
Layer3DProps props3D = Layer3DProps.getLayer3DProps(layer); |
84 | 84 |
_dataType = props3D.getType(); |
85 |
if (_dataType==Layer3DProps.layer3DImage)
|
|
85 |
if (_dataType == Layer3DProps.layer3DImage)
|
|
86 | 86 |
setFileExtension(".png"); |
87 | 87 |
else |
88 | 88 |
setFileExtension(".tif"); |
... | ... | |
91 | 91 |
|
92 | 92 |
} |
93 | 93 |
|
94 |
protected class MyCancel implements Cancellable, ICancellable {
|
|
94 |
protected class MyCancel implements Cancellable, ICancellable {
|
|
95 | 95 |
|
96 | 96 |
public boolean isCanceled() { |
97 | 97 |
// TODO Auto-generated method stub |
... | ... | |
103 | 103 |
|
104 | 104 |
} |
105 | 105 |
|
106 |
} |
|
106 |
public Object getID() { |
|
107 |
// TODO Auto-generated method stub |
|
108 |
return null; |
|
109 |
} |
|
107 | 110 |
|
108 |
private Image getTileFromLayer(String fName, Rectangle2D tileExtent) |
|
109 |
{ |
|
111 |
} |
|
112 |
|
|
113 |
private Image getTileFromLayer(String fName, Rectangle2D tileExtent) { |
|
110 | 114 |
BufferedImage image; |
111 | 115 |
|
112 |
if (_dataType==Layer3DProps.layer3DImage) {
|
|
116 |
if (_dataType == Layer3DProps.layer3DImage) {
|
|
113 | 117 |
int size = getTileSize(); |
114 | 118 |
image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); |
115 | 119 |
|
116 | 120 |
ViewPort vp = new ViewPort(_viewProj); |
117 | 121 |
vp.setExtent(tileExtent); // the real extent of the tile |
118 | 122 |
vp.setAdjustable(false); |
119 |
vp.setImageSize(new Dimension(size, size));
|
|
123 |
vp.setImageSize(new Dimension(size, size));
|
|
120 | 124 |
|
121 |
Graphics2D graphics = image.createGraphics(); |
|
122 |
Color backgColor = new Color(255, 255, 255, 0); // transparent background |
|
123 |
graphics.setColor(backgColor); |
|
124 |
graphics.fillRect(0, 0, size, size); |
|
125 |
Graphics2D graphics = image.createGraphics(); |
|
126 |
Color backgColor = new Color(255, 255, 255, 0); // transparent |
|
127 |
// background |
|
128 |
graphics.setColor(backgColor); |
|
129 |
graphics.fillRect(0, 0, size, size); |
|
125 | 130 |
|
126 |
// CancelDraw canceldraw = new MapControl.CancelDraw();
|
|
127 |
// canceldraw.setCanceled(false);
|
|
131 |
// CancelDraw canceldraw = new MapControl.CancelDraw();
|
|
132 |
// canceldraw.setCanceled(false);
|
|
128 | 133 |
|
129 |
// For now we'll use a trick to make sure the layer is drawn |
|
130 |
// even if it has scale dependency |
|
131 |
// In the future, a scale corresponding to the tile should be passed |
|
132 |
double scale = 0.5*(_layer.getMaxScale()+_layer.getMinScale()); |
|
133 |
try { |
|
134 |
_layer.draw(image, graphics, vp, new MyCancel(), scale); |
|
135 |
} |
|
136 |
catch (DriverException e) { |
|
137 |
return null; |
|
138 |
} |
|
134 |
// For now we'll use a trick to make sure the layer is drawn |
|
135 |
// even if it has scale dependency |
|
136 |
// In the future, a scale corresponding to the tile should be passed |
|
137 |
double scale = 0.5 * (_layer.getMaxScale() + _layer.getMinScale()); |
|
138 |
try { |
|
139 |
_layer.draw(image, graphics, vp, new MyCancel(), scale); |
|
140 |
} catch (DriverException e) { |
|
141 |
return null; |
|
142 |
} |
|
139 | 143 |
|
140 |
try {
|
|
144 |
try {
|
|
141 | 145 |
File file = new File(fName); |
142 | 146 |
String format = getFileExtension().substring(1); |
143 | 147 |
saveCachedFile(image, format, file); |
... | ... | |
146 | 150 |
return null; |
147 | 151 |
} |
148 | 152 |
return image; |
149 |
} |
|
150 |
else { // Elevation data |
|
153 |
} else { // Elevation data |
|
151 | 154 |
// RASTER_GRID |
152 |
if (_layer instanceof FLyrRaster) {
|
|
153 |
Grid grid = null;
|
|
154 |
FLyrRaster rasterLayer = (FLyrRaster)_layer;
|
|
155 |
grid = rasterLayer.getSource().getGrid();
|
|
156 |
if (grid == null)
|
|
157 |
return null;
|
|
158 |
|
|
155 |
if (_layer instanceof FLyrRaster) {
|
|
156 |
Grid grid = null;
|
|
157 |
FLyrRaster rasterLayer = (FLyrRaster) _layer;
|
|
158 |
grid = rasterLayer.getSource().getGrid();
|
|
159 |
if (grid == null)
|
|
160 |
return null;
|
|
161 |
|
|
159 | 162 |
grid.clearDrawableBand(); |
160 |
int bands[] = {0};
|
|
161 |
grid.addDrawableBands(bands);
|
|
163 |
int bands[] = { 0 };
|
|
164 |
grid.addDrawableBands(bands); |
|
162 | 165 |
grid.setNoDataValue(0.0); |
163 |
|
|
164 |
grid.setAreaOfInterest(tileExtent.getMinX(), tileExtent.getMinY(), |
|
165 |
tileExtent.getMaxX(), tileExtent.getMaxY(), |
|
166 |
32, 32); |
|
167 | 166 |
|
168 |
RasterBuf raster = grid.getRasterBuf(); |
|
167 |
grid.setAreaOfInterest(tileExtent.getMinX(), tileExtent |
|
168 |
.getMinY(), tileExtent.getMaxX(), tileExtent.getMaxY(), |
|
169 |
32, 32); |
|
170 |
|
|
171 |
RasterBuf raster = grid.getRasterBuf(); |
|
169 | 172 |
try { |
170 | 173 |
raster.save(fName, new Extent(tileExtent)); |
171 | 174 |
} catch (Exception e) { |
172 | 175 |
return null; |
173 | 176 |
} |
174 |
} |
|
175 |
else |
|
176 |
if (_layer instanceof FLyrWMS) { // WMS Elevation |
|
177 |
try { |
|
178 |
FLyrWMS wmsLayer = (FLyrWMS)_layer; |
|
177 |
} else if (_layer instanceof FLyrWMS) { // WMS Elevation |
|
178 |
try { |
|
179 |
FLyrWMS wmsLayer = (FLyrWMS) _layer; |
|
179 | 180 |
if (_wmsDriver == null) { |
180 |
|
|
181 |
URL host = wmsLayer.getHost(); |
|
182 |
_wmsDriver = FMapWMSDriverFactory.getFMapDriverForURL(host); |
|
181 |
|
|
182 |
URL host = wmsLayer.getHost(); |
|
183 |
_wmsDriver = FMapWMSDriverFactory |
|
184 |
.getFMapDriverForURL(host); |
|
183 | 185 |
} |
184 | 186 |
if (_wmsStatus == null) { |
185 |
_wmsStatus = new WMSStatus(); |
|
186 |
_wmsStatus.setFormat(wmsLayer.getFormat()); |
|
187 |
_wmsStatus.setHeight(32); |
|
188 |
_wmsStatus.setWidth(32); |
|
189 |
_wmsStatus.setLayerNames(Utilities.createVector(wmsLayer.getLayerQuery(),",")); |
|
190 |
_wmsStatus.setSrs(wmsLayer.getSRS()); |
|
191 |
|
|
192 |
HashMap props = wmsLayer.getProperties(); |
|
193 |
Vector styles; |
|
194 |
Vector dimensions; |
|
195 |
styles = (Vector)(props.get("styles")); |
|
196 |
dimensions = (Vector)(props.get("dimensions")); |
|
197 |
_wmsStatus.setStyles(styles); |
|
198 |
_wmsStatus.setDimensions(dimensions); |
|
199 |
_wmsStatus.setTransparency(false); |
|
200 |
String getMapStr = wmsLayer.getOnlineResource("GetMap"); |
|
201 |
_wmsStatus.setOnlineResource(getMapStr); |
|
187 |
_wmsStatus = new WMSStatus(); |
|
188 |
_wmsStatus.setFormat(wmsLayer.getFormat()); |
|
189 |
_wmsStatus.setHeight(32); |
|
190 |
_wmsStatus.setWidth(32); |
|
191 |
_wmsStatus.setLayerNames(Utilities.createVector( |
|
192 |
wmsLayer.getLayerQuery(), ",")); |
|
193 |
_wmsStatus.setSrs(wmsLayer.getSRS()); |
|
194 |
|
|
195 |
HashMap props = wmsLayer.getProperties(); |
|
196 |
Vector styles; |
|
197 |
Vector dimensions; |
|
198 |
styles = (Vector) (props.get("styles")); |
|
199 |
dimensions = (Vector) (props.get("dimensions")); |
|
200 |
_wmsStatus.setStyles(styles); |
|
201 |
_wmsStatus.setDimensions(dimensions); |
|
202 |
_wmsStatus.setTransparency(false); |
|
203 |
String getMapStr = wmsLayer.getOnlineResource("GetMap"); |
|
204 |
_wmsStatus.setOnlineResource(getMapStr); |
|
202 | 205 |
} |
203 | 206 |
_wmsStatus.setExtent(tileExtent); |
204 |
|
|
205 |
File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
|
|
206 |
if (f == null)
|
|
207 |
return null;
|
|
208 |
FileUtils.copy(f, new File(fName));
|
|
209 |
|
|
210 |
String fileName = f.getPath();
|
|
211 |
// System.out.println(fileName);
|
|
212 |
|
|
207 |
|
|
208 |
File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
|
|
209 |
if (f == null) |
|
210 |
return null; |
|
211 |
FileUtils.copy(f, new File(fName)); |
|
212 |
|
|
213 |
String fileName = f.getPath(); |
|
214 |
// System.out.println(fileName);
|
|
215 |
|
|
213 | 216 |
} catch (Exception e) { |
214 | 217 |
return null; |
215 | 218 |
} |
216 |
} |
|
217 |
else |
|
218 |
if (_layer instanceof FLyrWCS) { // WCS Elevation |
|
219 |
try { |
|
220 |
FLyrWCS wcsLayer = (FLyrWCS)_layer; |
|
221 |
if (_wcsDriver == null) { |
|
222 |
|
|
223 |
URL host = wcsLayer.getHost(); |
|
224 |
_wcsDriver = FMapWCSDriverFactory.getFMapDriverForURL(host); |
|
225 |
} |
|
226 |
if (_wcsStatus == null) { |
|
227 |
_wcsStatus = new WCSStatus(); |
|
228 |
|
|
229 |
Hashtable props = wcsLayer.getProperties(); |
|
219 |
} else if (_layer instanceof FLyrWCS) { // WCS Elevation |
|
220 |
try { |
|
221 |
FLyrWCS wcsLayer = (FLyrWCS) _layer; |
|
222 |
if (_wcsDriver == null) { |
|
230 | 223 |
|
231 |
_wcsStatus.setCoveraName((String)props.get("name")); |
|
232 |
_wcsStatus.setFormat((String)props.get("format")); |
|
233 |
_wcsStatus.setHeight(32); |
|
234 |
_wcsStatus.setWidth(32); |
|
235 |
_wcsStatus.setSrs((String)props.get("crs")); |
|
236 |
_wcsStatus.setParameters((String)props.get("parameter")); |
|
237 |
String time = (String)props.get("time"); |
|
238 |
if (time.length()>0) |
|
239 |
_wcsStatus.setTime(time); |
|
240 |
_wcsStatus.setOnlineResource(wcsLayer.getOnlineResource("GetCoverage")); |
|
241 |
} |
|
242 |
_wcsStatus.setExtent(tileExtent); |
|
224 |
URL host = wcsLayer.getHost(); |
|
225 |
_wcsDriver = FMapWCSDriverFactory |
|
226 |
.getFMapDriverForURL(host); |
|
227 |
} |
|
228 |
if (_wcsStatus == null) { |
|
229 |
_wcsStatus = new WCSStatus(); |
|
243 | 230 |
|
244 |
File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancel()); |
|
245 |
if (f == null) |
|
246 |
return null; |
|
247 |
|
|
248 |
FileUtils.copy(f, new File(fName)); |
|
249 |
|
|
250 |
String fileName = f.getPath(); |
|
251 |
// System.out.println(fileName); |
|
252 |
|
|
253 |
} catch (Exception e) { |
|
231 |
Hashtable props = wcsLayer.getProperties(); |
|
232 |
|
|
233 |
_wcsStatus.setCoveraName((String) props.get("name")); |
|
234 |
_wcsStatus.setFormat((String) props.get("format")); |
|
235 |
_wcsStatus.setHeight(32); |
|
236 |
_wcsStatus.setWidth(32); |
|
237 |
_wcsStatus.setSrs((String) props.get("crs")); |
|
238 |
_wcsStatus.setParameters((String) props |
|
239 |
.get("parameter")); |
|
240 |
String time = (String) props.get("time"); |
|
241 |
if (time.length() > 0) |
|
242 |
_wcsStatus.setTime(time); |
|
243 |
_wcsStatus.setOnlineResource(wcsLayer |
|
244 |
.getOnlineResource("GetCoverage")); |
|
245 |
} |
|
246 |
_wcsStatus.setExtent(tileExtent); |
|
247 |
|
|
248 |
File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancel()); |
|
249 |
if (f == null) |
|
254 | 250 |
return null; |
255 |
} |
|
251 |
|
|
252 |
FileUtils.copy(f, new File(fName)); |
|
253 |
|
|
254 |
String fileName = f.getPath(); |
|
255 |
// System.out.println(fileName); |
|
256 |
|
|
257 |
} catch (Exception e) { |
|
258 |
return null; |
|
256 | 259 |
} |
260 |
} |
|
257 | 261 |
return null; // no image |
258 | 262 |
} |
259 | 263 |
|
260 | 264 |
} |
261 | 265 |
|
262 | 266 |
public String getTileAsFName(TileNum t) throws CacheServiceException { |
263 |
if(_loadFailed) { throw new CacheServiceException(new Exception()); } |
|
267 |
if (_loadFailed) { |
|
268 |
throw new CacheServiceException(new Exception()); |
|
269 |
} |
|
264 | 270 |
String tileId = t.numToOpTileId(); |
265 | 271 |
String fName = getTileFileName(t); |
266 |
// System.out.println("CMTexture2D: requestTexture: "+tileId);
|
|
272 |
// System.out.println("CMTexture2D: requestTexture: "+tileId);
|
|
267 | 273 |
if (!new File(fName).exists()) { |
268 | 274 |
Rectangle2D tileExtent = computeBBox(t); |
269 | 275 |
getTileFromLayer(fName, tileExtent); |
... | ... | |
272 | 278 |
} |
273 | 279 |
|
274 | 280 |
public Image getTileAsImage(TileNum tileNum) throws CacheServiceException { |
275 |
if(_loadFailed) { throw new CacheServiceException(new Exception()); } |
|
281 |
if (_loadFailed) { |
|
282 |
throw new CacheServiceException(new Exception()); |
|
283 |
} |
|
276 | 284 |
String tileId = tileNum.numToOpTileId(); |
277 | 285 |
String fName = getTileFileName(tileNum); |
278 |
// System.out.println("CMTexture2D: requestTexture: "+tileId);
|
|
286 |
// System.out.println("CMTexture2D: requestTexture: "+tileId);
|
|
279 | 287 |
Rectangle2D tileExtent = computeBBox(tileNum); |
280 | 288 |
return getTileFromLayer(fName, tileExtent); |
281 | 289 |
} |
... | ... | |
283 | 291 |
public boolean intersectsLayer(TileNum tileNum) { |
284 | 292 |
Rectangle2D tileExtent = computeBBox(tileNum); |
285 | 293 |
// TODO: use a reprojected layer extent !!! |
286 |
/*System.err.println("TILE: ("+tileExtent.getMinX()+","+tileExtent.getMinY()+"),("+ |
|
287 |
tileExtent.getMaxX()+","+tileExtent.getMaxY()+")"); |
|
288 |
System.err.println("CAPA: ("+_lyrExtentRect.getMinX()+","+_lyrExtentRect.getMinY()+"),("+ |
|
289 |
_lyrExtentRect.getMaxX()+","+_lyrExtentRect.getMaxY()+")"); |
|
290 |
System.err.println("INTERSECTA LA CAPAAAAAA??????????:"+_lyrExtentRect.intersects(tileExtent)); |
|
291 |
return (_lyrExtentRect.intersects(tileExtent));*/ |
|
292 |
return Math.max(_lyrExtentRect.getMinX(),tileExtent.getMinX()) <= Math.min(_lyrExtentRect.getMaxX(),tileExtent.getMaxX()) && |
|
293 |
Math.max(Math.min(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMinY()) <= Math.min(Math.max(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMaxY()); |
|
294 |
/* |
|
295 |
* System.err.println("TILE: |
|
296 |
* ("+tileExtent.getMinX()+","+tileExtent.getMinY()+"),("+ |
|
297 |
* tileExtent.getMaxX()+","+tileExtent.getMaxY()+")"); |
|
298 |
* System.err.println("CAPA: |
|
299 |
* ("+_lyrExtentRect.getMinX()+","+_lyrExtentRect.getMinY()+"),("+ |
|
300 |
* _lyrExtentRect.getMaxX()+","+_lyrExtentRect.getMaxY()+")"); |
|
301 |
* System.err.println("INTERSECTA LA |
|
302 |
* CAPAAAAAA??????????:"+_lyrExtentRect.intersects(tileExtent)); return |
|
303 |
* (_lyrExtentRect.intersects(tileExtent)); |
|
304 |
*/ |
|
305 |
return Math.max(_lyrExtentRect.getMinX(), tileExtent.getMinX()) <= Math |
|
306 |
.min(_lyrExtentRect.getMaxX(), tileExtent.getMaxX()) |
|
307 |
&& Math.max(Math.min(_lyrExtentRect.getMinY(), _lyrExtentRect |
|
308 |
.getMaxY()), tileExtent.getMinY()) <= Math.min( |
|
309 |
Math.max(_lyrExtentRect.getMinY(), _lyrExtentRect |
|
310 |
.getMaxY()), tileExtent.getMaxY()); |
|
294 | 311 |
} |
295 | 312 |
|
296 | 313 |
} |
Also available in: Unified diff