diff flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java @ 1774:092e1e5020bc

Added a new MapGenerator which only returns a map configuration document at the moment (work still in progress). flys-artifacts/trunk@3095 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 28 Oct 2011 05:54:25 +0000
parents
children 0156105222c9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/MapGenerator.java	Fri Oct 28 05:54:25 2011 +0000
@@ -0,0 +1,208 @@
+package de.intevation.flys.exports;
+
+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.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+import de.intevation.flys.artifacts.model.FacetTypes;
+import de.intevation.flys.artifacts.model.WMSLayerFacet;
+import de.intevation.flys.utils.GeometryUtils;
+
+
+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(
+        Artifact artifact,
+        Facet    facet,
+        Document attr,
+        boolean  visible)
+    {
+        String name = facet.getName();
+
+        logger.debug("MapGenerator.doOut: " +artifact.identifier()+" | "+name);
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+
+        Facet nativeFacet = flys.getNativeFacet(facet);
+
+        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) && initialExtent == null) {
+                setInitialExtent(extent);
+            }
+        }
+        else {
+            logger.warn("Facet not supported: " + nativeFacet.getClass());
+        }
+
+    }
+
+
+    @Override
+    public void generate()
+    throws IOException
+    {
+        logger.debug("MapGenerator.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 = 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