}
tileEntryName = tileEntryName.substring(0, tileEntryName.length()-1);
}
// figure out the actual bounds of the tiles to be renderered
BoundingBox bbox = bbox(request);
GridSubset gridSubset = findBestGridSubset(request);
int[] minmax = findMinMaxZoom(gridSubset, request);
//ReferencedEnvelope bounds = new ReferencedEnvelope(findTileBounds(gridSubset, bbox,
// minmax[0]), getCoordinateReferenceSystem(map));
// create a prototype getmap request
GetMapRequest req = new GetMapRequest();
OwsUtils.copy(request, req, GetMapRequest.class);
req.setLayers(mapLayers);
String imageFormat = formatOpts.containsKey("format") ? parseFormatFromOpts(formatOpts)
: findBestFormat(request);
req.setFormat(imageFormat);
req.setWidth(gridSubset.getTileWidth());
req.setHeight(gridSubset.getTileHeight());
req.setCrs(getCoordinateReferenceSystem(request));
// store metadata
tiles.setMetadata(tileEntryName, bounds(request), imageFormat, srid(request), mapLayers, minmax,
gridSubset);
//column and row bounds
Integer minColumn = null, maxColumn = null, minRow = null, maxRow = null;
if (formatOpts.containsKey("min_column")) {
minColumn = Integer.parseInt(formatOpts.get("min_column").toString());
}
if (formatOpts.containsKey("max_column")) {
maxColumn = Integer.parseInt(formatOpts.get("max_column").toString());
}
if (formatOpts.containsKey("min_row")) {
minRow = Integer.parseInt(formatOpts.get("min_row").toString());
}
if (formatOpts.containsKey("max_row")) {
maxRow = Integer.parseInt(formatOpts.get("max_row").toString());
}
// flag determining if tile row indexes we store in database should be inverted
boolean flipy = Boolean.valueOf((String) formatOpts.get("flipy"));
for (int z = minmax[0]; z < minmax[1]; z++) {
long[] intersect = gridSubset.getCoverageIntersection(z, bbox);
long minX = minColumn == null? intersect[0] : Math.max(minColumn, intersect[0]);
long maxX = maxColumn == null? intersect[2] : Math.min(maxColumn, intersect[2]);
long minY = minRow == null? intersect[1] : Math.max(minRow, intersect[1]);
long maxY = maxRow == null? intersect[3] : Math.min(maxRow, intersect[3]);
for (long x = minX; x <= maxX; x++) {
for (long y = minY; y <= maxY; y++) {
BoundingBox box = gridSubset.boundsFromIndex(new long[] { x, y, z });
req.setBbox(new Envelope(box.getMinX(), box.getMaxX(), box.getMinY(), box.getMaxY()));
WebMap result = webMapService.getMap(req);
tiles.addTile(z, (int) x, (int) (flipy ? gridSubset.getNumTilesHigh(z)
- (y + 1) : y), toBytes(result));
// Cleanup
cleaner.finished(null);