teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.exports; ingo@1774: teichmann@5831: import org.dive4elements.artifactdatabase.data.StateData; teichmann@5831: import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; teichmann@5831: import org.dive4elements.artifactdatabase.state.Facet; teichmann@5831: import org.dive4elements.artifactdatabase.state.Settings; teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; teichmann@5867: import org.dive4elements.river.artifacts.D4EArtifact; teichmann@5831: import org.dive4elements.river.artifacts.model.FacetTypes; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WMSDBLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WMSLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.model.map.WSPLGENLayerFacet; teichmann@5831: import org.dive4elements.river.artifacts.states.WaterlevelGroundDifferences; teichmann@5867: import org.dive4elements.river.collections.D4EArtifactCollection; teichmann@5831: import org.dive4elements.river.utils.ArtifactMapfileGenerator; teichmann@5831: import org.dive4elements.river.utils.GeometryUtils; teichmann@5831: import org.dive4elements.river.utils.ThemeUtil; ingo@1774: christian@3306: import java.io.File; christian@3306: import java.io.FileNotFoundException; christian@3306: import java.io.IOException; christian@3306: import java.io.OutputStream; christian@3306: import java.util.ArrayList; christian@3306: import java.util.List; christian@3306: christian@3306: import org.apache.log4j.Logger; christian@3306: import org.w3c.dom.Document; christian@3306: import org.w3c.dom.Element; christian@3306: christian@6170: import com.vividsolutions.jts.geom.Envelope; christian@6170: ingo@1774: ingo@1774: public class MapGenerator implements OutGenerator, FacetTypes { ingo@1774: ingo@1774: private static Logger logger = Logger.getLogger(MapGenerator.class); ingo@1774: teichmann@5867: protected D4EArtifactCollection collection; ingo@3422: ingo@1774: protected Artifact master; ingo@1774: ingo@2047: protected Settings settings; ingo@2047: ingo@1774: protected Document request; ingo@1774: ingo@1774: protected OutputStream out; ingo@1774: ingo@1774: protected CallContext context; ingo@1774: ingo@1774: protected List layers; ingo@1774: ingo@1774: protected Envelope maxExtent; ingo@1774: protected Envelope initialExtent; ingo@1774: ingo@1774: protected String srid; ingo@1774: ingo@1774: ingo@1774: ingo@1774: @Override ingo@1774: public void init(Document request, OutputStream out, CallContext context) { ingo@1774: logger.debug("MapGenerator.init"); ingo@1774: ingo@1774: this.request = request; ingo@1774: this.out = out; ingo@1774: this.context = context; ingo@1774: ingo@1774: this.layers = new ArrayList(); ingo@1774: ingo@1774: this.maxExtent = null; ingo@1774: this.initialExtent = null; ingo@1774: } ingo@1774: ingo@1774: ingo@1774: @Override ingo@1774: public void setMasterArtifact(Artifact master) { ingo@1774: logger.debug("MapGenerator.setMasterArtifact"); ingo@1774: this.master = master; ingo@1774: } ingo@1774: ingo@3422: @Override teichmann@5867: public void setCollection(D4EArtifactCollection collection) { ingo@3422: this.collection = collection; ingo@3422: } ingo@1774: ingo@1774: @Override ingo@1774: public void doOut( felix@1944: ArtifactAndFacet artifactFacet, felix@1944: Document attr, felix@1944: boolean visible) ingo@1774: { felix@1944: String name = artifactFacet.getFacetName(); ingo@1774: felix@1944: logger.debug("MapGenerator.doOut: " + christian@6170: artifactFacet.getArtifact().identifier() + " | " + name); teichmann@5867: D4EArtifact flys = (D4EArtifact) artifactFacet.getArtifact(); ingo@1774: felix@1944: Facet nativeFacet = artifactFacet.getFacet(); ingo@1774: ingo@1774: if (nativeFacet instanceof WMSLayerFacet) { ingo@1774: WMSLayerFacet wms = (WMSLayerFacet) nativeFacet; ingo@3918: Envelope extent = wms.getOriginalExtent(); ingo@1774: ingo@1774: layers.add(wms); ingo@1774: ingo@1774: setMaxExtent(extent); ingo@1774: setSrid(wms.getSrid()); ingo@1774: ingo@1775: if (FLOODMAP_WSPLGEN.equals(name)) { ingo@2091: setInitialExtent(extent); ingo@2616: createWSPLGENLayer(flys, wms, attr); ingo@1775: } raimund@2639: else if (FLOODMAP_USERSHAPE.equals(name)) { raimund@2639: createUserShapeLayer(flys, wms); raimund@2639: } ingo@1792: else { christian@4864: logger.debug("doOut: createDatabaseLayer for facet name: " + name); ingo@1793: createDatabaseLayer(flys, wms, attr); ingo@1792: } ingo@1774: } ingo@1774: else { ingo@1774: logger.warn("Facet not supported: " + nativeFacet.getClass()); ingo@1774: } ingo@1775: } ingo@1774: ingo@1775: ingo@2616: protected void createWSPLGENLayer( teichmann@5867: D4EArtifact flys, ingo@2616: WMSLayerFacet wms, ingo@2616: Document attr ingo@2616: ) { ingo@1776: try { raimund@2638: if(wms instanceof WSPLGENLayerFacet) { christian@5655: // Retrieve waterlevel ground differences from artifact christian@5655: StateData dFrom = flys.getData(WaterlevelGroundDifferences.LOWER_FIELD); christian@5655: StateData dTo = flys.getData(WaterlevelGroundDifferences.UPPER_FIELD); christian@5655: StateData dStep = flys.getData(WaterlevelGroundDifferences.DIFF_FIELD); christian@5655: christian@5655: String fromStr = dFrom != null ? (String) dFrom.getValue() : null; christian@5655: String toStr = dTo != null ? (String) dTo.getValue() : null; christian@5655: String stepStr = dStep != null ? (String) dStep.getValue() : null; christian@5655: christian@5655: float from = Float.parseFloat(fromStr); christian@5655: float to = Float.parseFloat(toStr); christian@5655: float step = Float.parseFloat(stepStr); christian@5655: christian@4702: ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); raimund@2638: mfg.createUeskLayer( raimund@2638: flys, raimund@2638: (WSPLGENLayerFacet) wms, christian@6170: ThemeUtil.createDynamicMapserverStyle(attr, from, to, step, context.getMeta()), raimund@2638: context); raimund@2638: } raimund@2638: else { raimund@2638: logger.warn("Cannot create WSPLGEN layer from: " + christian@6170: wms.getClass()); raimund@2638: } ingo@1776: } ingo@1776: catch (IOException ioe) { ingo@1776: logger.error(ioe, ioe); ingo@1776: } ingo@1775: } ingo@1775: ingo@1775: teichmann@5867: protected void createUserShapeLayer(D4EArtifact flys, WMSLayerFacet wms) { christian@4702: ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); raimund@2639: raimund@2639: try { raimund@2639: mfg.createUserShapeLayer(flys, wms); raimund@2639: } raimund@2639: catch (FileNotFoundException fnfe) { raimund@2639: logger.error(fnfe, fnfe); raimund@2639: } raimund@2639: catch (IOException ioe) { raimund@2639: logger.error(ioe, ioe); raimund@2639: } raimund@2639: } raimund@2639: raimund@2639: ingo@1793: protected void createDatabaseLayer( teichmann@5867: D4EArtifact flys, ingo@1793: WMSLayerFacet wms, ingo@1793: Document attr ingo@1793: ) { ingo@1792: logger.debug("createDatabaseLayer for facet: " + wms.getName()); ingo@1792: christian@4702: ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); ingo@1792: ingo@1792: try { ingo@1792: File baseDir = mfg.getShapefileBaseDir(); ingo@1792: File artDir = new File(baseDir, flys.identifier()); ingo@1792: ingo@1792: if (artDir != null && !artDir.exists()) { ingo@1792: logger.debug("Create new directory: " + artDir.getPath()); ingo@1792: artDir.mkdir(); ingo@1792: } ingo@1792: ingo@1792: if (wms instanceof WMSDBLayerFacet) { ingo@1793: mfg.createDatabaseLayer( christian@6170: flys, christian@6170: (WMSDBLayerFacet) wms, christian@6170: ThemeUtil.createMapserverStyle(attr)); ingo@1792: } ingo@1792: else { ingo@1792: logger.warn("Cannot create DB layer from: " + wms.getClass()); ingo@1792: } ingo@1792: } ingo@1792: catch (FileNotFoundException fnfe) { ingo@1792: logger.error(fnfe, fnfe); ingo@1792: } ingo@1792: catch (IOException ioe) { ingo@1792: logger.error(ioe, ioe); ingo@1792: } ingo@1792: } ingo@1792: ingo@1792: ingo@1774: @Override ingo@1774: public void generate() christian@6170: throws IOException ingo@1774: { ingo@1774: logger.debug("MapGenerator.generate"); ingo@1774: christian@4702: ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); christian@4654: mfg.generate(); ingo@1781: ingo@1774: Document response = XMLUtils.newDocument(); ingo@1774: ElementCreator c = new ElementCreator( christian@6170: response, christian@6170: ArtifactNamespaceContext.NAMESPACE_URI, christian@6170: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@1774: ingo@1774: Element root = c.create("floodmap"); ingo@1774: Element layers = c.create("layers"); ingo@1774: ingo@1774: response.appendChild(root); ingo@1774: root.appendChild(layers); ingo@1774: ingo@1774: appendLayers(layers); ingo@1774: appendMapInformation(root, c); ingo@1774: ingo@1774: XMLUtils.toStream(response, out); christian@6170: } ingo@1774: ingo@1774: ingo@1774: protected void appendLayers(Element parent) { ingo@1774: for (WMSLayerFacet facet: layers) { ingo@1774: parent.appendChild(facet.toXML(parent.getOwnerDocument())); ingo@1774: } ingo@1774: } ingo@1774: ingo@1774: ingo@1774: protected void setMaxExtent(Envelope maxExtent) { ingo@1774: if (maxExtent == null) { ingo@1774: return; ingo@1774: } ingo@1774: ingo@1774: if (this.maxExtent == null) { ingo@1774: logger.debug("Set max extent to: " + maxExtent); ingo@2091: this.maxExtent = new Envelope(maxExtent); ingo@1774: return; ingo@1774: } ingo@1774: ingo@1774: this.maxExtent.expandToInclude(maxExtent); ingo@1774: } ingo@1774: ingo@1774: ingo@1774: protected void setInitialExtent(Envelope initialExtent) { ingo@2091: if (this.initialExtent == null && initialExtent != null) { ingo@1774: logger.debug("Set initial extent to: " + initialExtent); ingo@2091: this.initialExtent = new Envelope(initialExtent); ingo@1774: } ingo@1774: } ingo@1774: ingo@1774: ingo@1774: protected void setSrid(String srid) { ingo@1774: if (srid == null || srid.length() == 0) { ingo@1774: return; ingo@1774: } ingo@1774: ingo@1774: this.srid = srid; ingo@1774: } ingo@1774: ingo@1774: ingo@1774: protected void appendMapInformation(Element parent, ElementCreator c) { ingo@1774: String mE = GeometryUtils.jtsBoundsToOLBounds(this.maxExtent); ingo@1774: ingo@1774: Element maxExtent = c.create("maxExtent"); ingo@1774: maxExtent.setTextContent(mE); ingo@1774: raimund@2082: if(this.initialExtent != null) { raimund@2082: String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent); raimund@2082: Element initExtent = c.create("initialExtent"); raimund@2082: initExtent.setTextContent(iE); raimund@2082: parent.appendChild(initExtent); raimund@2082: } ingo@1774: ingo@1774: Element srid = c.create("srid"); ingo@1774: srid.setTextContent(this.srid); ingo@1774: ingo@1774: // TODO zoom levels ingo@1774: // TODO resolutation ingo@1774: ingo@1774: parent.appendChild(maxExtent); ingo@1774: parent.appendChild(srid); ingo@1774: } ingo@1979: ingo@1979: ingo@1979: /** ingo@1979: * Returns an instance of EmptySettings currently! ingo@1979: * ingo@1979: * @return an instance of EmptySettings. ingo@1979: */ ingo@2047: @Override ingo@1979: public Settings getSettings() { ingo@1979: return new EmptySettings(); ingo@1979: } ingo@2047: ingo@2047: ingo@2047: @Override ingo@2047: public void setSettings(Settings settings) { ingo@2047: this.settings = settings; ingo@2047: } ingo@1774: } ingo@1774: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :