long totalTimeout = DOWNLOAD_TIMEOUT + DOWNLOAD_TIMEOUT_ONE_TILE * tiles.size();
log.debug("=== total timeout (millis): {}", totalTimeout);
ExecutorService service = Executors.newFixedThreadPool(DOWNLOAD_MAX_THREADS);
List<Future<ImageResult>> futures = service.invokeAll(callables, totalTimeout, TimeUnit.MILLISECONDS);
// determine the pixel bounds of the mosaic
Bbox pixelBounds = getPixelBounds(tiles);
// create the images for the mosaic
List<RenderedImage> images = new ArrayList<RenderedImage>();
for (Future<ImageResult> future : futures) {
if (future.isDone()) {
try {
ImageResult result;
result = future.get();
// create a rendered image
RenderedImage image = JAI.create("stream", new ByteArraySeekableStream(result.getImage()));
// convert to common direct colormodel (some images have their own indexed color model)
RenderedImage colored = toDirectColorModel(image);
// translate to the correct position in the tile grid
double xOffset = result.getRasterImage().getCode().getX() * tileWidth - pixelBounds.getX();
double yOffset;
// TODO: in some cases, the y-index is up (e.g. WMS), should be down for
// all layers !!!!
if (isYIndexUp(tiles)) {
yOffset = result.getRasterImage().getCode().getY() * tileHeight - pixelBounds.getY();
} else {
yOffset = (pixelBounds.getMaxY() - (result.getRasterImage().getCode().getY() + 1)
* tileHeight);
}
log.debug("adding to(" + xOffset + "," + yOffset + "), url = "
+ result.getRasterImage().getUrl());
RenderedImage translated = TranslateDescriptor.create(colored, (float) xOffset,
(float) yOffset, new InterpolationNearest(), null);
images.add(translated);
} catch (ExecutionException e) {
addLoadError(graphics, (ImageException) (e.getCause()), viewport);
} catch (InterruptedException e) {
log.warn("missing tile in mosaic " + e.getMessage());
} catch (MalformedURLException e) {
log.warn("missing tile in mosaic " + e.getMessage());
} catch (IOException e) {
log.warn("missing tile in mosaic " + e.getMessage());
}
}
}
if (images.size() > 0) {
ImageLayout imageLayout = new ImageLayout(0, 0, (int) pixelBounds.getWidth(),
(int) pixelBounds.getHeight());
imageLayout.setTileWidth(tileWidth);
imageLayout.setTileHeight(tileHeight);
// create the mosaic image
ParameterBlock pbMosaic = new ParameterBlock();