} else {
file = new File(createTempDir(storage.getStorage()), name + ".mbtiles");
}
// Create the MBTile file
MBTilesFile mbtile = new MBTilesFile(file, true);
try {
// Initialize the MBTile file in order to avoid exceptions when accessing the geoPackage file
mbtile.init();
// Create the GetMap request to use
GetMapRequest request = new GetMapRequest();
// Create the layers map for the request
ArrayList<MapLayerInfo> layers = new ArrayList<MapLayerInfo>();
// Get the layers from the catalog
for (String layername : layerz) {
LayerInfo layerInfo = catalog.getLayerByName(layername);
// Ensure the Layer is present
if (layerInfo == null) {
throw new ServiceException("Layer not found: " + layername);
}
layers.add(new MapLayerInfo(layerInfo));
}
request.setLayers(layers);
// Generate the bounding box if not present
if (boundingbox == null) {
try {
// generate one from requests layers
ReferencedEnvelope bbox = null;
for (MapLayerInfo l : request.getLayers()) {
ResourceInfo r = l.getResource();
// use native bbox
ReferencedEnvelope b = r.getNativeBoundingBox();
if (bbox != null) {
// transform
b = b.transform(bbox.getCoordinateReferenceSystem(), true);
}
if (bbox != null) {
bbox.include(b);
} else {
bbox = b;
}
}
request.setBbox(bbox);
} catch (Exception e) {
String msg = "Must specify bbox, unable to derive from requested layers";
throw new RuntimeException(msg, e);
}
} else {
request.setBbox(boundingbox);
}
// Extract CRS
CoordinateReferenceSystem crs = boundingbox.getCoordinateReferenceSystem();
// Set the request CRS
if (crs == null) {
// use crs of the layer
ResourceInfo r = request.getLayers().iterator().next().getResource();
crs = r.getCRS();
request.setCrs(crs);
} else {
request.setCrs(crs);
}
// Set the request SRS
request.setSRS(CRS.toSRS(crs));
// Set Background color and Transparency
if (bgColor != null && !bgColor.isEmpty()) {
request.setBgColor(Color.decode(bgColor));
}
request.setTransparent(transparency == null ? false : transparency);
// Add a style
if (stylePath != null) {
request.setStyleUrl(stylePath);
} else if (styleBody != null && !styleBody.isEmpty()) {
request.setStyleBody(styleBody);
} else {
request.setStyles(new ArrayList<Style>());
if (styleNames != null && !styleNames.isEmpty()) {
for (String styleName : styleNames) {
StyleInfo info = catalog.getStyleByName(styleName);
if (info != null) {
request.getStyles().add(info.getStyle());
} else {
request.getStyles().add(null);
}
}
}
if (request.getStyles().isEmpty()) {
for (MapLayerInfo info : request.getLayers()) {
request.getStyles().add(info.getDefaultStyle());
}
}
}
// Set the format of the mbtiles images
request.setFormat("none");
Map formatOptions = new HashMap();
formatOptions.put("format", format);
// Configure zoom levels if present
if (minZoom != null) {
formatOptions.put("min_zoom", minZoom);
}
if (maxZoom != null) {
formatOptions.put("max_zoom", maxZoom);
}
if (minColumn != null) {
formatOptions.put("min_column", minColumn);
}
if (maxColumn != null) {
formatOptions.put("max_column", maxColumn);
}
if (minRow != null) {
formatOptions.put("min_row", minRow);
}
if (maxRow != null) {
formatOptions.put("max_row", maxRow);
}
// Set the gridSet name
formatOptions.put(GRIDSET_NAME, EPSG_900913);
// Add the format options to the request
request.setFormatOptions(formatOptions);
// Execute the requests
mapOutput.addTiles(mbtile, request, name);
} catch (Exception e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
throw new ProcessException(e);
} finally {
// Close the connection
if (mbtile != null) {
try {
mbtile.close();
} catch (Exception e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}