view flys-artifacts/src/main/java/de/intevation/flys/artifacts/map/PrintMap.java @ 5779:ebec12def170

Datacage: Add a pool of builders to make it multi threadable. XML DOM is not thread safe. Therefore the old implementation only allowed one thread to use the builder at a time. As the complexity of the configuration has increased over time this has become a bottleneck of the whole application because it took quiet some time to build a result. Furthermore the builder code path is visited very frequent. So many concurrent requests were piled up resulting in long waits for the users. To mitigate this problem a round robin pool of builders is used now. Each of the pooled builders has an independent copy of the XML template and can be run in parallel. The number of builders is determined by the system property 'flys.datacage.pool.size'. It defaults to 4.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 21 Apr 2013 12:48:09 +0200
parents 4a76da133144
children
line wrap: on
line source
package de.intevation.flys.artifacts.map;

import java.awt.Color;
import java.awt.Rectangle;

import java.io.File;
import java.io.IOException;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import java.util.List;

import java.net.URL;
import java.net.MalformedURLException;

import javax.imageio.ImageIO;


import org.geotools.data.ows.Layer;
import org.geotools.data.ows.WMSCapabilities;
import org.geotools.data.wms.WebMapServer;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.MapContext;
import org.geotools.map.WMSMapLayer;
import org.geotools.ows.ServiceException;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.renderer.GTRenderer;


public class PrintMap {

    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";
    public static final String DEFAULT_OUTFILE = "~/map.jpeg";

    public static final String MAPSERVER = System.getProperty("wms", DEFAULT_WMS);
    public static final String MAP_IMAGE = System.getProperty("outfile", DEFAULT_OUTFILE);


    public static void main(String[] args) {
        System.out.println("-> start PrintMap");
        System.out.println("  -> Print layers of WMS: " + MAPSERVER);

        try {
            WebMapServer    server = getMapserver();
            WMSMapLayer[] wmsLayer = getWMSLayers(server);

            MapContext mapcontent = new MapContext( wmsLayer );
            mapcontent.setTitle(" NEW MAP CONTENT TITLE ");

            printMap(mapcontent);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("-> finished PrintMap");
    }


    public static void printMap(MapContext map) throws Exception {
        int imageWidth = 600;

        GTRenderer renderer = new StreamingRenderer();
        renderer.setContext(map);

        Rectangle imageBounds = null;
        ReferencedEnvelope mapBounds = null;

        try {
            mapBounds = map.getLayerBounds();
            double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
            imageBounds = new Rectangle(
                0, 0,
                imageWidth,
                (int) Math.round(imageWidth * heightToWidth));

        }
        catch (Exception e) {
            // failed to access map layers
            throw new RuntimeException(e);
        }

        BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D gr = image.createGraphics();
        gr.setPaint(Color.WHITE);
        gr.fill(imageBounds);

        try {
            renderer.paint(gr, imageBounds, mapBounds);
            File fileToSave = new File(MAP_IMAGE);
            ImageIO.write(image, "jpeg", fileToSave);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static WebMapServer getMapserver() throws MalformedURLException, IOException, ServiceException {
        return new WebMapServer(getServerUrl());
    }


    public static URL getServerUrl() throws MalformedURLException {
        return new URL(MAPSERVER);
    }


    public static WMSMapLayer[] getWMSLayers(WebMapServer server) {
        if (server == null) {
            System.out.println("WebMapServer == null");
            throw new RuntimeException("WebMapServer == null");
        }

        WMSCapabilities capabilities = server.getCapabilities();

        List<Layer> layers = capabilities.getLayerList();
        WMSMapLayer[] wmslayers = new WMSMapLayer[layers.size()];

        for (int i = 0, L = layers.size(); i < L; i++) {
            Layer l = layers.get(i);

            System.out.println("   -> add layer: " + l);
            wmslayers[i] = new WMSMapLayer(server, l);
        }

        return wmslayers;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org