ingo@2328: package de.intevation.flys.artifacts.map; ingo@2328: ingo@2328: import java.awt.Color; ingo@2328: import java.awt.Rectangle; ingo@2328: ingo@2328: import java.io.File; ingo@2328: import java.io.IOException; ingo@2328: ingo@2328: import java.awt.Graphics2D; ingo@2328: import java.awt.image.BufferedImage; ingo@2328: ingo@2328: import java.util.List; ingo@2328: ingo@2328: import java.net.URL; ingo@2328: import java.net.MalformedURLException; ingo@2328: ingo@2328: import javax.imageio.ImageIO; ingo@2328: ingo@2328: ingo@2328: import org.geotools.data.ows.Layer; ingo@2328: import org.geotools.data.ows.WMSCapabilities; ingo@2328: import org.geotools.data.wms.WebMapServer; ingo@2328: import org.geotools.geometry.jts.ReferencedEnvelope; ingo@2328: import org.geotools.map.MapContext; ingo@2328: import org.geotools.map.WMSMapLayer; ingo@2328: import org.geotools.ows.ServiceException; ingo@2328: import org.geotools.renderer.lite.StreamingRenderer; ingo@2328: import org.geotools.renderer.GTRenderer; ingo@2328: ingo@2328: ingo@2328: public class PrintMap { ingo@2328: ingo@2328: public static final String DEFAULT_WMS = "http://map1.naturschutz.rlp.de/service_lanis/mod_wms/wms_getmap.php?mapfile=group_gdide&REQUEST=GetCapabilities&SERVICE=WMS"; ingo@2328: public static final String DEFAULT_OUTFILE = "~/map.jpeg"; ingo@2328: ingo@2328: public static final String MAPSERVER = System.getProperty("wms", DEFAULT_WMS); ingo@2328: public static final String MAP_IMAGE = System.getProperty("outfile", DEFAULT_OUTFILE); ingo@2328: ingo@2328: ingo@2328: public static void main(String[] args) { ingo@2328: System.out.println("-> start PrintMap"); ingo@2328: System.out.println(" -> Print layers of WMS: " + MAPSERVER); ingo@2328: ingo@2328: try { ingo@2328: WebMapServer server = getMapserver(); ingo@2328: WMSMapLayer[] wmsLayer = getWMSLayers(server); ingo@2328: ingo@2328: MapContext mapcontent = new MapContext( wmsLayer ); ingo@2328: mapcontent.setTitle(" NEW MAP CONTENT TITLE "); ingo@2328: ingo@2328: printMap(mapcontent); ingo@2328: } ingo@2328: catch (Exception e) { ingo@2328: e.printStackTrace(); ingo@2328: } ingo@2328: ingo@2328: System.out.println("-> finished PrintMap"); ingo@2328: } ingo@2328: ingo@2328: ingo@2328: public static void printMap(MapContext map) throws Exception { ingo@2328: int imageWidth = 600; ingo@2328: ingo@2328: GTRenderer renderer = new StreamingRenderer(); ingo@2328: renderer.setContext(map); ingo@2328: ingo@2328: Rectangle imageBounds = null; ingo@2328: ReferencedEnvelope mapBounds = null; ingo@2328: ingo@2328: try { ingo@2328: mapBounds = map.getLayerBounds(); ingo@2328: double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0); ingo@2328: imageBounds = new Rectangle( ingo@2328: 0, 0, ingo@2328: imageWidth, ingo@2328: (int) Math.round(imageWidth * heightToWidth)); ingo@2328: ingo@2328: } ingo@2328: catch (Exception e) { ingo@2328: // failed to access map layers ingo@2328: throw new RuntimeException(e); ingo@2328: } ingo@2328: ingo@2328: BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB); ingo@2328: Graphics2D gr = image.createGraphics(); ingo@2328: gr.setPaint(Color.WHITE); ingo@2328: gr.fill(imageBounds); ingo@2328: ingo@2328: try { ingo@2328: renderer.paint(gr, imageBounds, mapBounds); ingo@2328: File fileToSave = new File(MAP_IMAGE); ingo@2328: ImageIO.write(image, "jpeg", fileToSave); ingo@2328: } ingo@2328: catch (IOException e) { ingo@2328: throw new RuntimeException(e); ingo@2328: } ingo@2328: } ingo@2328: ingo@2328: ingo@2328: public static WebMapServer getMapserver() throws MalformedURLException, IOException, ServiceException { ingo@2328: return new WebMapServer(getServerUrl()); ingo@2328: } ingo@2328: ingo@2328: ingo@2328: public static URL getServerUrl() throws MalformedURLException { ingo@2328: return new URL(MAPSERVER); ingo@2328: } ingo@2328: ingo@2328: ingo@2328: public static WMSMapLayer[] getWMSLayers(WebMapServer server) { ingo@2328: if (server == null) { ingo@2328: System.out.println("WebMapServer == null"); ingo@2328: throw new RuntimeException("WebMapServer == null"); ingo@2328: } ingo@2328: ingo@2328: WMSCapabilities capabilities = server.getCapabilities(); ingo@2328: ingo@2328: List layers = capabilities.getLayerList(); ingo@2328: WMSMapLayer[] wmslayers = new WMSMapLayer[layers.size()]; ingo@2328: sascha@3087: for (int i = 0, L = layers.size(); i < L; i++) { ingo@2328: Layer l = layers.get(i); ingo@2328: ingo@2328: System.out.println(" -> add layer: " + l); ingo@2328: wmslayers[i] = new WMSMapLayer(server, l); ingo@2328: } ingo@2328: ingo@2328: return wmslayers; ingo@2328: } ingo@2328: } sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :