String bboxSR, String format, String layerDefs, String layers,
String transparent) {
byte[] result = null;
try {
MapService mapService = ServiceManager.getMapService(serviceName);
if (mapService != null) {
MapDesc mapDesc = mapService.getMapDesc();
MapServiceDesc mapServiceDesc = mapService.getMapServiceDesc();
if (mapServiceDesc.isNeedToken()) {
if (!TokenService.verifyToken(token)
&& !("html".equals(f) && bbox == null)) {
return TokenService.TOKEN_INVALID_TIP.getBytes();
}
}
// Handle SRs
String sr = mapDesc.getWkid();
if (bboxSR == null || "".equals(bboxSR)) {
bboxSR = sr;
}
if (imageSR == null || "".equals(imageSR)) {
imageSR = sr;
}
// Handle size
String[] sizes = size.split(",");
int width = Integer.parseInt(sizes[0]);
int height = Integer.parseInt(sizes[1]);
// Handle bbox
double xmin = 0, ymin = 0, xmax = 0, ymax = 0;
if (bbox != null && bbox.indexOf(",") > 0) {
String[] bboxs = bbox.split(",");
if (bbox.length() > 3) {
xmin = Double.parseDouble(bboxs[0]);
ymin = Double.parseDouble(bboxs[1]);
xmax = Double.parseDouble(bboxs[2]);
ymax = Double.parseDouble(bboxs[3]);
}
}
if ((xmax - xmin) * (ymax - ymin) == 0) {
Envelope extent = mapDesc.getInitialExtent();
xmin = extent.getMinX();
xmax = extent.getMaxX();
ymin = extent.getMinY();
ymax = extent.getMaxY();
}
CoordinateReferenceSystem bboxCRS = SpatialReferenceManager
.wkidToCRS(bboxSR, true);
ReferencedEnvelope env = new ReferencedEnvelope(xmin, xmax,
ymin, ymax, bboxCRS);
env = GeometryToolkit.project(env, imageSR);
env = MapUtil.adjustEnvelopeToSize(env,
width, height);
// Handle dpi
int nDPI = Integer.parseInt(dpi);
// Handle layerDefs
HashMap<String, String> mapLayerDefs = null;
if (layerDefs != null && !"".equals(layerDefs)) {
try {
mapLayerDefs = new HashMap<String, String>();
String[] defs = layerDefs.split(",");
for (int i = 0; i < defs.length; i++) {
String def = defs[i];
if (def.contains(":")) {
String[] deff = def.split(":");
if (deff.length > 1) {
mapLayerDefs.put(deff[0], deff[1]);
}
} else {
mapLayerDefs.put(String.valueOf(i), def);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// Handle layers
ArrayList<String> layerIds = new ArrayList<String>();
if (layers != null && !"".equals(layers) && !"*".equals(layers)) {
String[] strLayers = layers.split(",");
int layerCount = strLayers.length;
for (int i = 0; i < layerCount; i++) {
layerIds.add(strLayers[i]);
}
} else {
for (int i = 0, count = mapDesc.getLayerInfos().size(); i < count; i++) {
LayerInfo layerInfo = mapDesc.getLayerInfo(i);
if (layerInfo.isVisible()) {
layerIds.add(String.valueOf(i));
}
}
}
// Handle export
RenderParam renderParam = new RenderParam();
renderParam.setAntiAlias(mapDesc.isAntiAlias());
renderParam.setExtent(env);
renderParam.setImageHeight(height);
renderParam.setImageWidth(width);
renderParam.setLayerDefs(mapLayerDefs);
renderParam.setTransparent(Boolean.parseBoolean(transparent));
renderParam.setVisibleLayerIds(layerIds);
byte[] image = null;
double scale = 0;
MapServiceInstance instance = (MapServiceInstance) mapService
.getMapServicePool().checkoutIdleInstance();
try {
image = instance.export(renderParam, format, nDPI);
scale = instance.getMap().computeScale(env, width, height,
nDPI);
} finally {
if (instance != null) {
mapService.getMapServicePool().checkinIdelInstance(
instance);
}
}
// Various out format