Revision 15404
import/ext3D/branches/ext3D_v1.1/ext3D/src/com/iver/ai2/gvsig3d/cacheservices/CMSFileRasterService.java | ||
---|---|---|
1 |
package com.iver.ai2.gvsig3d.cacheservices; |
|
2 |
|
|
3 |
import java.awt.Image; |
|
4 |
import java.awt.image.BufferedImage; |
|
5 |
import java.io.File; |
|
6 |
import java.io.IOException; |
|
7 |
|
|
8 |
import javax.imageio.ImageIO; |
|
9 |
|
|
10 |
import org.cresques.cts.ProjectionPool; |
|
11 |
import org.cresques.io.GeoRasterFile; |
|
12 |
import org.cresques.px.Extent; |
|
13 |
import org.gvsig.cacheservice.CacheServiceException; |
|
14 |
import org.gvsig.cacheservice.RasterCacheService; |
|
15 |
import org.gvsig.cacheservice.TileNum; |
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
public class CMSFileRasterService extends RasterCacheService { |
|
20 |
private GeoRasterFile _rasterFile; |
|
21 |
private boolean _loadFailed = true; |
|
22 |
private Extent _extent; |
|
23 |
public CMSFileRasterService(String planet, String name) { |
|
24 |
super(planet, name); |
|
25 |
// TODO Auto-generated constructor stub |
|
26 |
if(GeoRasterFile.fileIsSupported(name)) { |
|
27 |
// System.out.println("CMTexture2D: loadFile: "+name); |
|
28 |
/// loading Raster File with CMS |
|
29 |
//_rasterFile = GeoRasterFile.openFile(ProjectionPool.get("EPSG:23030"),name); |
|
30 |
if(_rasterFile != null) { |
|
31 |
_rasterFile.setBand(GeoRasterFile.RED_BAND, 0); |
|
32 |
_rasterFile.setBand(GeoRasterFile.GREEN_BAND, 1); |
|
33 |
_rasterFile.setBand(GeoRasterFile.BLUE_BAND, 2); |
|
34 |
_extent = _rasterFile.getExtent(); |
|
35 |
setFullExtent(_extent.toRectangle2D()); |
|
36 |
//setBounds( _extent.toRectangle2D()); |
|
37 |
_loadFailed = false; |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
|
|
43 |
private Extent checkExtent(Extent sz) { |
|
44 |
double vx = sz.minX(); |
|
45 |
double vy = sz.minY(); |
|
46 |
double vx2 = sz.maxX(); |
|
47 |
double vy2 = sz.maxY(); |
|
48 |
if (sz.minX() < _extent.minX()) { |
|
49 |
vx = _extent.minX(); |
|
50 |
} |
|
51 |
|
|
52 |
if (sz.minY() < _extent.minY()) { |
|
53 |
vy = _extent.minY(); |
|
54 |
} |
|
55 |
|
|
56 |
if (sz.maxX() > _extent.maxX()) { |
|
57 |
vx2 = _extent.maxX(); |
|
58 |
} |
|
59 |
|
|
60 |
if (sz.maxY() > _extent.maxY()) { |
|
61 |
vy2 = _extent.maxY(); |
|
62 |
} |
|
63 |
|
|
64 |
return new Extent(vx, vy, vx2, vy2); |
|
65 |
} |
|
66 |
|
|
67 |
private Image getTileFromCMSFile(String fName) |
|
68 |
{ |
|
69 |
BufferedImage awtImage = null; |
|
70 |
_rasterFile.setView(checkExtent(new Extent(getBounds()))); |
|
71 |
awtImage = (BufferedImage)_rasterFile.updateImage(256,256,null); |
|
72 |
try { |
|
73 |
ImageIO.write(awtImage, getFileExtension().substring(0), |
|
74 |
new File(fName)); |
|
75 |
} catch (IOException e1) { |
|
76 |
// TODO Auto-generated catch block |
|
77 |
e1.printStackTrace(); |
|
78 |
return null; |
|
79 |
} |
|
80 |
return awtImage; |
|
81 |
} |
|
82 |
|
|
83 |
public String getTileAsFName(TileNum t) throws CacheServiceException { |
|
84 |
if(_loadFailed) { throw new CacheServiceException(new Exception()); } |
|
85 |
String tileId = t.numToOpTileId(); |
|
86 |
String fName = getTileFileName(t); |
|
87 |
// System.out.println("CMTexture2D: requestTexture: "+tileId); |
|
88 |
if (!new File(fName).exists()) { |
|
89 |
if(getTileFromCMSFile(fName)==null) return null; |
|
90 |
} |
|
91 |
return fName; |
|
92 |
} |
|
93 |
|
|
94 |
public Image getTileAsImage(TileNum tileNum) throws CacheServiceException { |
|
95 |
if(_loadFailed) { throw new CacheServiceException(new Exception()); } |
|
96 |
String tileId = tileNum.numToOpTileId(); |
|
97 |
String fName = getTileFileName(tileNum); |
|
98 |
System.out.println("CMTexture2D: requestTexture: "+tileId); |
|
99 |
return getTileFromCMSFile(fName); |
|
100 |
} |
|
101 |
|
|
102 |
} |
Also available in: Unified diff