WMSMapContent createMapContext(QName layer, String style) throws Exception {

    // create a map context
    WMSMapContent mapContent = new WMSMapContent();
    mapContent.addLayer(createMapLayer(layer, style));
    mapContent.setMapHeight(256);
    mapContent.setMapWidth(256);

    GetMapRequest getMapRequest = createGetMapRequest(new QName[] {layer});
    getMapRequest.setWidth(256);
    getMapRequest.setHeight(256);

    mapContent.setRequest(getMapRequest);
    mapContent
        .getViewport()
        .setBounds(new ReferencedEnvelope(-180, 180, -90, 90, DefaultGeographicCRS.WGS84));
    return mapContent;
  }
  @Test
  public void testExternalImageSize() throws Exception {
    GetMapRequest req = createGetMapRequest(MockData.STREAMS);
    req.setWidth(256);
    req.setHeight(256);

    WMSMapContent mapContent = new WMSMapContent(req);
    mapContent.addLayer(createMapLayer(MockData.STREAMS, "big-local-image"));

    mapContent
        .getViewport()
        .setBounds(new ReferencedEnvelope(-180, 0, -90, 90, DefaultGeographicCRS.WGS84));
    mapContent.setMapHeight(256);
    mapContent.setMapWidth(256);

    KMLMapOutputFormat of = new KMLMapOutputFormat(getWMS());
    KMLMap map = of.produceMap(mapContent);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    new KMLEncoder().encode(map.getKml(), bout, null);

    Document document = dom(new ByteArrayInputStream(bout.toByteArray()));

    assertEquals("kml", document.getDocumentElement().getNodeName());
    assertEquals(1, document.getElementsByTagName("Style").getLength());

    XMLAssert.assertXpathExists("//kml:IconStyle/kml:scale", document);

    XPath xPath = XPathFactory.newInstance().newXPath();
    initXPath(xPath);

    Double scale =
        (Double)
            xPath.evaluate(
                "//kml:IconStyle/kml:scale", document.getDocumentElement(), XPathConstants.NUMBER);
    assertEquals(42d / 16d, scale, 0.01);
  }