setState(STARTING);
IGeoResource handle = getContext().getLayer().findGeoResource(TileSet.class);
try {
TileSet tileset = handle.resolve(TileSet.class, new SubProgressMonitor(monitor, 30));
AbstractOpenWebService server = null;
if (handle.canResolve(WebMapServer.class)) {
server = handle.resolve(WebMapServer.class, new SubProgressMonitor(monitor, 30));
}
if (handle.canResolve(TiledWebMapServer.class)) {
server = handle.resolve(TiledWebMapServer.class,
new SubProgressMonitor(monitor, 30));
}
// determine the bounds that need to be rendered
ReferencedEnvelope bounds = getRenderBounds();
if (bounds == null) {
ReferencedEnvelope viewbounds = getContext().getImageBounds();
if (getContext().getCRS().equals(viewbounds.getCoordinateReferenceSystem())) {
bounds = viewbounds;
}
}
// ensure the bounds are in the right CRS
if (!bounds.getCoordinateReferenceSystem().equals(
tileset.getCoordinateReferenceSystem())) {
// need to reproject the bounds to the tile coordinate reference system.
MathTransform transform = CRS.findMathTransform(
bounds.getCoordinateReferenceSystem(),
tileset.getCoordinateReferenceSystem());
bounds = new ReferencedEnvelope(JTS.transform(bounds, transform),
tileset.getCoordinateReferenceSystem());
}
// determine scale factor used to determine zoom level
// compute the scale factor based on the viewport size; we cannot use the bounds and
// tile size becuase the
// bounds may not be the full size of the tile and we might get the wrong scale
double scaleFactor = getContext().getViewportModel().getBounds().getWidth()
/ getContext().getMapDisplay().getWidth();
// create a TileRange to handle loading the tiles
com.vividsolutions.jts.geom.Envelope bnds = new com.vividsolutions.jts.geom.Envelope(
bounds.getMinX(), bounds.getMaxX(), bounds.getMinY(), bounds.getMaxY());
Map<String, Tile> tilesInRange = tileset.getTilesFromViewportScale(bnds, scaleFactor);
// look up the preference for caching tiles on-disk or in
// memory and use the proper tilerange for that.
TileRange range = null;
String value = CatalogPlugin.getDefault().getPreferenceStore()
.getString(PreferenceConstants.P_WMSCTILE_CACHING);
if (value.equals(WMSCTileCaching.ONDISK.toString())) {
range = new TileRangeOnDisk(server, tileset, bnds, tilesInRange,
requestTileWorkQueue, writeTileWorkQueue);
} else {
range = new TileRangeInMemory(server, tileset, bnds, tilesInRange,
requestTileWorkQueue);
}
// create an empty raster symbolizer for rendering
RasterSymbolizer style = styleBuilder.createRasterSymbolizer();
// setup how much each tile is worth for the monitor work %
int tileCount = range.getTileCount();
int tileWorth = (tileCount / 100) * tileCount;
int thisid = 0;
if (testing) {
staticid++;
thisid = staticid;
}
// first render any tiles that are ready and render non-ready tiles with blank images
Map<String, Tile> tiles = range.getTiles();
Set<String> notRenderedTiles = new HashSet<String>();
Set<String> renderedTiles = new HashSet<String>();
for( String key : tiles.keySet() ) {
if (monitor.isCanceled()) {
setState(CANCELLED);
if (testing) {
System.out.println("monitor CANCELED!!!: " + thisid); //$NON-NLS-1$
}
return;
}
Tile tile = tiles.get(key);
if (tile != null && tile.getBufferedImage() != null
&& tile.getTileState() != WMSTile.INERROR) {
renderTile(graphics, tile, tileset.getCoordinateReferenceSystem(), style);
renderedTiles.add(key);
monitor.worked(tileWorth); // inc the monitor work by 1 tile
} else {
// set the tile blank (removing any previous content) and add it
// to be drawn later
renderBlankTile(graphics, tile, tileset.getCoordinateReferenceSystem());
notRenderedTiles.add(key);
}
}
setState(RENDERING);
// if the tilerange is not already completed, then load
// the missing tiles
if (!notRenderedTiles.isEmpty()) {
if (monitor.isCanceled()) {
setState(CANCELLED);
if (testing) {
System.out.println("monitor CANCELED!!!: " + thisid); //$NON-NLS-1$
}
return;
}
// set the listener on the tile range
range.addListener(listener);
// load the missing tiles by sending requests for them
range.loadTiles(monitor);
// block until all the missing tiles have come through (and draw them
// as they are added to the blocking queue
while( !notRenderedTiles.isEmpty() ) {
// check that the rendering is not canceled
if (monitor.isCanceled()) {
setState(CANCELLED);
if (testing) {
System.out.println("monitor CANCELED!!!: " + thisid); //$NON-NLS-1$
}
tilesToDraw_queue.clear();
return;
}
if (testing) {
System.out.println("BLOCKED: " + thisid); //$NON-NLS-1$
System.out.println("waiting on: " + notRenderedTiles.size() + " tiles"); //$NON-NLS-1$ //$NON-NLS-2$
}
Tile tile = null;
try {
tile = (Tile) tilesToDraw_queue.take(); // blocks until a tile is ready to
// take
if (testing) {
System.out.println("removed from queue: " + tile.getId()); //$NON-NLS-1$
}
} catch (InterruptedException ex) {
if (testing) {
System.out.println("InterruptedException trying to take: " + ex); //$NON-NLS-1$
}
}
if (testing) {
System.out.println("UNBLOCKED!!!: " + thisid); //$NON-NLS-1$
}
// check that the rendering is not canceled again after block
if (monitor.isCanceled()) {
setState(CANCELLED);
if (testing) {
System.out.println("monitor CANCELED!!!: " + thisid); //$NON-NLS-1$
}
tilesToDraw_queue.clear();
return;
}
// check that the tile's bounds are within the current
// context's bounds (if it's not, don't bother drawing it) and also
// only draw tiles that haven't already been drawn (panning fast
// can result in listeners being notified the same tile is ready multiple
// times but we don't want to draw it more than once per render cycle)
// ReferencedEnvelope viewbounds = getContext().getViewportModel().getBounds();
ReferencedEnvelope viewbounds = getContext().getImageBounds();
if (tile != null && tile.getBufferedImage() != null && viewbounds != null
&& viewbounds.intersects(tile.getBounds())
&& !renderedTiles.contains(tile.getId())) {
renderTile(graphics, tile, tileset.getCoordinateReferenceSystem(), style);
renderedTiles.add(tile.getId());
monitor.worked(tileWorth); // inc the monitor work by 1 tile
setState(RENDERING); // tell renderer new data is ready
}