view flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java @ 5645:696d710470f5

flys/issue1077: Show loads as step line, therefore transform data in SedimentLoadFacet to stretch as in the measurement stations bounds. Deal with this new kind of data in the Generator.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 10 Apr 2013 09:35:07 +0200
parents dc0d37715e16
children a9243df307b1 53a2ceeae9a3
line wrap: on
line source
package de.intevation.flys.exports;

import com.vividsolutions.jts.geom.Envelope;

import de.intevation.artifactdatabase.state.ArtifactAndFacet;
import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.Settings;
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.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
import de.intevation.flys.artifacts.model.map.WMSLayerFacet;
import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet;
import de.intevation.flys.collections.FLYSArtifactCollection;
import de.intevation.flys.utils.ArtifactMapfileGenerator;
import de.intevation.flys.utils.GeometryUtils;
import de.intevation.flys.utils.ThemeUtil;

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.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class MapGenerator implements OutGenerator, FacetTypes {

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

    protected FLYSArtifactCollection collection;

    protected Artifact master;

    protected Settings settings;

    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 setCollection(FLYSArtifactCollection collection) {
        this.collection = collection;
    }

    @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.getOriginalExtent();

            layers.add(wms);

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

            if (FLOODMAP_WSPLGEN.equals(name)) {
                setInitialExtent(extent);
                createWSPLGENLayer(flys, wms, attr);
            }
            else if (FLOODMAP_BARRIERS.equals(name)) {
                createBarriersLayer(flys, wms);
            }
            else if (FLOODMAP_USERSHAPE.equals(name)) {
                createUserShapeLayer(flys, wms);
            }
            else {
                logger.debug("doOut: createDatabaseLayer for facet name: " + name);
                createDatabaseLayer(flys, wms, attr);
            }
        }
        else {
            logger.warn("Facet not supported: " + nativeFacet.getClass());
        }
    }


    protected void createWSPLGENLayer(
        FLYSArtifact  flys,
        WMSLayerFacet wms,
        Document      attr
    ) {
        try {
            if(wms instanceof WSPLGENLayerFacet) {
                ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
                mfg.createUeskLayer(
                    flys,
                    (WSPLGENLayerFacet) wms,
                    ThemeUtil.createDynamicMapserverStyle(attr),
                    context);
            }
            else {
                logger.warn("Cannot create WSPLGEN layer from: " +
                            wms.getClass());
            }
        }
        catch (IOException ioe) {
            logger.error(ioe, ioe);
        }
    }


    protected void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) {
        ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();

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


    protected void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms) {
        ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();

        try {
            mfg.createUserShapeLayer(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());

        ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();

        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");

        ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
        mfg.generate();

        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 = new Envelope(maxExtent);
            return;
        }

        this.maxExtent.expandToInclude(maxExtent);
    }


    protected void setInitialExtent(Envelope initialExtent) {
        if (this.initialExtent == null && initialExtent != null) {
            logger.debug("Set initial extent to: " + initialExtent);
            this.initialExtent = new Envelope(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);

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

        if(this.initialExtent != null) {
            String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent);
            Element initExtent = c.create("initialExtent");
            initExtent.setTextContent(iE);
            parent.appendChild(initExtent);
        }

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

        // TODO zoom levels
        // TODO resolutation

        parent.appendChild(maxExtent);
        parent.appendChild(srid);
    }


    /**
     * Returns an instance of <i>EmptySettings</i> currently!
     *
     * @return an instance of <i>EmptySettings</i>.
     */
    @Override
    public Settings getSettings() {
        return new EmptySettings();
    }


    @Override
    public void setSettings(Settings settings) {
        this.settings = settings;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org