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@6905: import org.dive4elements.river.themes.ThemeDocument; teichmann@5831: import org.dive4elements.river.utils.ArtifactMapfileGenerator; teichmann@5831: import org.dive4elements.river.utils.GeometryUtils; teichmann@6831: teichmann@6831: import java.io.File; teichmann@6831: import java.io.FileNotFoundException; teichmann@6831: import java.io.IOException; teichmann@6831: import java.io.OutputStream; teichmann@6831: import java.util.ArrayList; teichmann@6831: import java.util.List; teichmann@6831: teichmann@6831: import org.apache.log4j.Logger; christian@3306: import org.w3c.dom.Document; christian@3306: import org.w3c.dom.Element; christian@3306: teichmann@6831: import com.vividsolutions.jts.geom.Envelope; teichmann@6831: ingo@1774: ingo@1774: public class MapGenerator implements OutGenerator, FacetTypes { ingo@1774: teichmann@8202: private static Logger log = 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: teichmann@7077: protected String outName; teichmann@7077: teichmann@7037: @Override teichmann@7087: public void setup(Object config) { teichmann@8202: log.debug("MapGenerator.setup"); teichmann@7037: } ingo@1774: ingo@1774: @Override tom@8856: public void init( tom@8856: String outName, tom@8856: Document request, tom@8856: OutputStream out, tom@8856: CallContext context tom@8856: ) { teichmann@8202: log.debug("MapGenerator.init"); ingo@1774: teichmann@7077: this.outName = outName; 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) { teichmann@8202: log.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, teichmann@6905: ThemeDocument attr, felix@1944: boolean visible) ingo@1774: { felix@1944: String name = artifactFacet.getFacetName(); ingo@1774: teichmann@8202: log.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: } christian@6833: // FIXME: Already generated by HWSBarrierState christian@6833: // wms has a wrong SRID which would break that layer christian@6833: //else if (FLOODMAP_USERSHAPE.equals(name)) { christian@6833: // createUserShapeLayer(flys, wms); christian@6833: //} ingo@1792: else { teichmann@8202: log.debug("doOut: createDatabaseLayer for facet name: " + name); ingo@1793: createDatabaseLayer(flys, wms, attr); ingo@1792: } ingo@1774: } ingo@1774: else { teichmann@8202: log.warn("Facet not supported: " + nativeFacet.getClass()); ingo@1774: } ingo@1775: } ingo@1774: ingo@1775: ingo@2616: protected void createWSPLGENLayer( teichmann@6905: D4EArtifact flys, ingo@2616: WMSLayerFacet wms, teichmann@6905: ThemeDocument attr ingo@2616: ) { ingo@1776: try { raimund@2638: if(wms instanceof WSPLGENLayerFacet) { christian@5655: // Retrieve waterlevel ground differences from artifact tom@8856: StateData dFrom = tom@8856: flys.getData(WaterlevelGroundDifferences.LOWER_FIELD); tom@8856: StateData dTo = tom@8856: flys.getData(WaterlevelGroundDifferences.UPPER_FIELD); tom@8856: StateData dStep = tom@8856: flys.getData(WaterlevelGroundDifferences.DIFF_FIELD); christian@5655: tom@8856: String fromStr = dFrom != null tom@8856: ? (String) dFrom.getValue() tom@8856: : null; tom@8856: String toStr = dTo != null tom@8856: ? (String) dTo.getValue() tom@8856: : null; tom@8856: String stepStr = dStep != null tom@8856: ? (String) dStep.getValue() tom@8856: : 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, teichmann@6905: attr.createDynamicMapserverStyle( teichmann@6905: from, to, step, context.getMeta()), raimund@2638: context); raimund@2638: } raimund@2638: else { teichmann@8202: log.warn("Cannot create WSPLGEN layer from: " + christian@6170: wms.getClass()); raimund@2638: } ingo@1776: } ingo@1776: catch (IOException ioe) { teichmann@8202: log.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) { teichmann@8202: log.error(fnfe, fnfe); raimund@2639: } raimund@2639: catch (IOException ioe) { teichmann@8202: log.error(ioe, ioe); raimund@2639: } raimund@2639: } raimund@2639: raimund@2639: ingo@1793: protected void createDatabaseLayer( teichmann@6905: D4EArtifact flys, ingo@1793: WMSLayerFacet wms, teichmann@6905: ThemeDocument attr ingo@1793: ) { teichmann@8202: log.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()) { teichmann@8202: log.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, teichmann@6905: attr.createMapserverStyle()); ingo@1792: } ingo@1792: else { teichmann@8202: log.warn("Cannot create DB layer from: " + wms.getClass()); ingo@1792: } ingo@1792: } ingo@1792: catch (FileNotFoundException fnfe) { teichmann@8202: log.error(fnfe, fnfe); ingo@1792: } ingo@1792: catch (IOException ioe) { teichmann@8202: log.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: { teichmann@8202: log.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); tom@8816: } 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) { teichmann@8202: log.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) { teichmann@8202: log.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) { andre@8527: String mE; andre@8527: if (this.maxExtent != null) { andre@8527: mE = GeometryUtils.jtsBoundsToOLBounds(this.maxExtent); andre@8527: } else { andre@8527: log.error("Layer without extent. Probably no geometry at all."); tom@8746: mE = "0 0 1 1"; andre@8527: } 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 :