view flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java @ 1944:21a4d2c677a1

Changed doOut signature, side effect from blackboard feature (to come). flys-artifacts/trunk@3334 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 30 Nov 2011 10:10:42 +0000
parents 1636686070f7
children a7c437c9547e
line wrap: on
line source
package de.intevation.flys.exports;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;

import com.vividsolutions.jts.geom.Envelope;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;

import de.intevation.artifacts.common.ArtifactNamespaceContext;
import de.intevation.artifacts.common.utils.XMLUtils;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;
import de.intevation.artifactdatabase.state.Facet;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
import de.intevation.flys.artifacts.model.WMSLayerFacet;
import de.intevation.flys.utils.GeometryUtils;
import de.intevation.flys.utils.MapfileGenerator;
import de.intevation.flys.utils.ThemeUtil;


public class MapGenerator implements OutGenerator, FacetTypes {

    private static Logger logger = Logger.getLogger(MapGenerator.class);

    protected Artifact master;

    protected Document request;

    protected OutputStream out;

    protected CallContext context;

    protected List<WMSLayerFacet> layers;

    protected Envelope maxExtent;
    protected Envelope initialExtent;

    protected String srid;



    @Override
    public void init(Document request, OutputStream out, CallContext context) {
        logger.debug("MapGenerator.init");

        this.request  = request;
        this.out      = out;
        this.context  = context;

        this.layers = new ArrayList<WMSLayerFacet>();

        this.maxExtent = null;
        this.initialExtent = null;
    }


    @Override
    public void setMasterArtifact(Artifact master) {
        logger.debug("MapGenerator.setMasterArtifact");
        this.master = master;
    }


    @Override
    public void doOut(
        ArtifactAndFacet artifactFacet,
        Document         attr,
        boolean          visible)
    {
        String name = artifactFacet.getFacetName();

        logger.debug("MapGenerator.doOut: " +
            artifactFacet.getArtifact().identifier() + " | " + name);
        FLYSArtifact flys = (FLYSArtifact) artifactFacet.getArtifact();

        Facet nativeFacet = artifactFacet.getFacet();

        if (nativeFacet instanceof WMSLayerFacet) {
            WMSLayerFacet wms = (WMSLayerFacet) nativeFacet;
            Envelope   extent = wms.getExtent();

            layers.add(wms);

            setMaxExtent(extent);
            setSrid(wms.getSrid());

            if (FLOODMAP_WSPLGEN.equals(name)) {
                if (initialExtent == null) {
                    setInitialExtent(extent);
                }

                createWSPLGENLayer(flys, wms);
            }
            else if (FLOODMAP_BARRIERS.equals(name)) {
                createBarriersLayer(flys, wms);
            }
            else {
                createDatabaseLayer(flys, wms, attr);
            }
        }
        else {
            logger.warn("Facet not supported: " + nativeFacet.getClass());
        }
    }


    protected void createWSPLGENLayer(FLYSArtifact flys, WMSLayerFacet wms) {
        try {
            MapfileGenerator mfg = MapfileGenerator.getInstance();
            mfg.createUeskLayer(flys, wms);
        }
        catch (IOException ioe) {
            logger.error(ioe, ioe);
        }
    }


    protected void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) {
        MapfileGenerator mfg = MapfileGenerator.getInstance();

        try {
            mfg.createBarriersLayer(flys, wms);
        }
        catch (FileNotFoundException fnfe) {
            logger.error(fnfe, fnfe);
        }
        catch (IOException ioe) {
            logger.error(ioe, ioe);
        }
    }


    protected void createDatabaseLayer(
        FLYSArtifact  flys,
        WMSLayerFacet wms,
        Document      attr
    ) {
        logger.debug("createDatabaseLayer for facet: " + wms.getName());

        MapfileGenerator mfg = MapfileGenerator.getInstance();

        try {
            File baseDir = mfg.getShapefileBaseDir();
            File artDir  = new File(baseDir, flys.identifier());

            if (artDir != null && !artDir.exists()) {
                logger.debug("Create new directory: " + artDir.getPath());
                artDir.mkdir();
            }

            if (wms instanceof WMSDBLayerFacet) {
                mfg.createDatabaseLayer(
                    flys,
                    (WMSDBLayerFacet) wms,
                    ThemeUtil.createMapserverStyle(attr));
            }
            else {
                logger.warn("Cannot create DB layer from: " + wms.getClass());
            }
        }
        catch (FileNotFoundException fnfe) {
            logger.error(fnfe, fnfe);
        }
        catch (IOException ioe) {
            logger.error(ioe, ioe);
        }
    }


    @Override
    public void generate()
    throws IOException
    {
        logger.debug("MapGenerator.generate");

        MapfileGenerator.getInstance().update();

        Document response = XMLUtils.newDocument();
        ElementCreator c  = new ElementCreator(
            response,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root   = c.create("floodmap");
        Element layers = c.create("layers");

        response.appendChild(root);
        root.appendChild(layers);

        appendLayers(layers);
        appendMapInformation(root, c);

        XMLUtils.toStream(response, out);
    }


    protected void appendLayers(Element parent) {
        for (WMSLayerFacet facet: layers) {
            parent.appendChild(facet.toXML(parent.getOwnerDocument()));
        }
    }


    protected void setMaxExtent(Envelope maxExtent) {
        if (maxExtent == null) {
            return;
        }

        if (this.maxExtent == null) {
            logger.debug("Set max extent to: " + maxExtent);
            this.maxExtent = maxExtent;
            return;
        }

        logger.debug("Expand max extent by: " + maxExtent);
        logger.debug("Max extent before expanding: " + this.maxExtent);
        this.maxExtent.expandToInclude(maxExtent);
        logger.debug("Max extent after expanding: " + this.maxExtent);
    }


    protected void setInitialExtent(Envelope initialExtent) {
        if (initialExtent == null) {
            return;
        }

        if (this.initialExtent == null) {
            logger.debug("Set initial extent to: " + initialExtent);
            this.initialExtent = initialExtent;
            return;
        }

        logger.debug("Set initial extent to: " + initialExtent);
        this.initialExtent = initialExtent;
    }


    protected void setSrid(String srid) {
        if (srid == null || srid.length() == 0) {
            return;
        }

        this.srid = srid;
    }


    protected void appendMapInformation(Element parent, ElementCreator c) {
        String mE = GeometryUtils.jtsBoundsToOLBounds(this.maxExtent);
        logger.debug("BUILD MAX EXTENT OF:" + this.maxExtent);
        logger.debug("BUILD MAX EXTENT:" + mE);

        Element maxExtent = c.create("maxExtent");
        maxExtent.setTextContent(mE);

        String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent);
        logger.debug("BUILD INITIAL EXTENT OF: " + this.initialExtent);
        logger.debug("BUILD INITIAL EXTENT: " + iE);
        Element initExtent = c.create("initialExtent");
        initExtent.setTextContent(iE);

        Element srid = c.create("srid");
        srid.setTextContent(this.srid);

        // TODO zoom levels
        // TODO resolutation

        parent.appendChild(maxExtent);
        parent.appendChild(initExtent);
        parent.appendChild(srid);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org