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: d@9619: import java.io.File; d@9619: import java.io.FileNotFoundException; d@9619: import java.io.IOException; d@9619: import java.io.OutputStream; d@9619: import java.util.ArrayList; d@9619: import java.util.List; d@9619: d@9619: import org.apache.log4j.Logger; 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; 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: 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: d@9619: private String legend; d@9619: teichmann@7037: @Override d@9619: public void setup(final Object config) { teichmann@8202: log.debug("MapGenerator.setup"); teichmann@7037: } ingo@1774: ingo@1774: @Override d@9619: public void init(final String outName, final Document request, final OutputStream out, final CallContext context) { teichmann@8202: log.debug("MapGenerator.init"); ingo@1774: d@9619: this.outName = outName; d@9619: this.request = request; d@9619: this.out = out; d@9619: this.context = context; ingo@1774: d@9619: this.layers = new ArrayList<>(); ingo@1774: ingo@1774: this.maxExtent = null; ingo@1774: this.initialExtent = null; ingo@1774: } ingo@1774: ingo@1774: @Override d@9619: public void setMasterArtifact(final Artifact master) { teichmann@8202: log.debug("MapGenerator.setMasterArtifact"); ingo@1774: this.master = master; ingo@1774: } ingo@1774: ingo@3422: @Override d@9619: public void setCollection(final D4EArtifactCollection collection) { ingo@3422: this.collection = collection; ingo@3422: } ingo@1774: ingo@1774: @Override d@9619: public void doOut(final ArtifactAndFacet artifactFacet, final ThemeDocument attr, final boolean visible) { d@9619: final String name = artifactFacet.getFacetName(); ingo@1774: d@9619: log.debug("MapGenerator.doOut: " + artifactFacet.getArtifact().identifier() + " | " + name); d@9619: final D4EArtifact flys = (D4EArtifact) artifactFacet.getArtifact(); ingo@1774: d@9619: final Facet nativeFacet = artifactFacet.getFacet(); ingo@1774: ingo@1774: if (nativeFacet instanceof WMSLayerFacet) { d@9619: final WMSLayerFacet wms = (WMSLayerFacet) nativeFacet; d@9619: final Envelope extent = wms.getOriginalExtent(); ingo@1774: d@9619: this.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 d@9619: // else if (FLOODMAP_USERSHAPE.equals(name)) { d@9619: // createUserShapeLayer(flys, wms); d@9619: // } ingo@1792: else { teichmann@8202: log.debug("doOut: createDatabaseLayer for facet name: " + name); ingo@1793: createDatabaseLayer(flys, wms, attr); ingo@1792: } d@9619: } else { teichmann@8202: log.warn("Facet not supported: " + nativeFacet.getClass()); ingo@1774: } ingo@1775: } ingo@1774: d@9619: protected void createWSPLGENLayer(final D4EArtifact flys, final WMSLayerFacet wms, final ThemeDocument attr) { ingo@1776: try { d@9619: if (wms instanceof WSPLGENLayerFacet) { christian@5655: // Retrieve waterlevel ground differences from artifact d@9619: final StateData dFrom = flys.getData(WaterlevelGroundDifferences.LOWER_FIELD); d@9619: final StateData dTo = flys.getData(WaterlevelGroundDifferences.UPPER_FIELD); d@9619: final StateData dStep = flys.getData(WaterlevelGroundDifferences.DIFF_FIELD); christian@5655: d@9619: final String fromStr = dFrom != null ? (String) dFrom.getValue() : null; d@9619: final String toStr = dTo != null ? (String) dTo.getValue() : null; d@9619: final String stepStr = dStep != null ? (String) dStep.getValue() : null; christian@5655: d@9619: final float from = Float.parseFloat(fromStr); d@9619: final float to = Float.parseFloat(toStr); d@9619: final float step = Float.parseFloat(stepStr); christian@5655: d@9619: final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); d@9619: mfg.createUeskLayer(flys, (WSPLGENLayerFacet) wms, attr.createDynamicMapserverStyle(from, to, step, this.context.getMeta()), this.context); d@9619: } else { d@9619: log.warn("Cannot create WSPLGEN layer from: " + wms.getClass()); raimund@2638: } ingo@1776: } d@9619: catch (final IOException ioe) { teichmann@8202: log.error(ioe, ioe); ingo@1776: } ingo@1775: } ingo@1775: d@9619: protected void createUserShapeLayer(final D4EArtifact flys, final WMSLayerFacet wms) { d@9619: final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); raimund@2639: raimund@2639: try { raimund@2639: mfg.createUserShapeLayer(flys, wms); raimund@2639: } d@9619: catch (final FileNotFoundException fnfe) { teichmann@8202: log.error(fnfe, fnfe); raimund@2639: } d@9619: catch (final IOException ioe) { teichmann@8202: log.error(ioe, ioe); raimund@2639: } raimund@2639: } raimund@2639: d@9619: protected void createDatabaseLayer(final D4EArtifact flys, final WMSLayerFacet wms, final ThemeDocument attr) { teichmann@8202: log.debug("createDatabaseLayer for facet: " + wms.getName()); ingo@1792: d@9619: final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); ingo@1792: ingo@1792: try { d@9619: final File baseDir = mfg.getShapefileBaseDir(); d@9619: final 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) { d@9619: mfg.createDatabaseLayer(flys, (WMSDBLayerFacet) wms, attr.createMapserverStyle()); d@9619: } else { teichmann@8202: log.warn("Cannot create DB layer from: " + wms.getClass()); ingo@1792: } ingo@1792: } d@9619: catch (final FileNotFoundException fnfe) { teichmann@8202: log.error(fnfe, fnfe); ingo@1792: } d@9619: catch (final IOException ioe) { teichmann@8202: log.error(ioe, ioe); ingo@1792: } ingo@1792: } ingo@1792: ingo@1774: @Override d@9619: public void generate() throws IOException { teichmann@8202: log.debug("MapGenerator.generate"); ingo@1774: d@9619: final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); christian@4654: mfg.generate(); ingo@1781: d@9619: final Document response = XMLUtils.newDocument(); d@9619: final ElementCreator c = new ElementCreator(response, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@1774: d@9619: final Element root = c.create("floodmap"); d@9619: final 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: d@9619: XMLUtils.toStream(response, this.out); tom@8816: } ingo@1774: d@9619: protected void appendLayers(final Element parent) { d@9619: for (final WMSLayerFacet facet : this.layers) { ingo@1774: parent.appendChild(facet.toXML(parent.getOwnerDocument())); ingo@1774: } ingo@1774: } ingo@1774: d@9619: protected void setMaxExtent(final 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: d@9619: protected void setInitialExtent(final 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: d@9619: protected void setSrid(final 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: d@9619: protected void appendMapInformation(final Element parent, final 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: d@9619: final Element maxExtent = c.create("maxExtent"); ingo@1774: maxExtent.setTextContent(mE); ingo@1774: d@9619: if (this.initialExtent != null) { d@9619: final String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent); d@9619: final Element initExtent = c.create("initialExtent"); raimund@2082: initExtent.setTextContent(iE); raimund@2082: parent.appendChild(initExtent); raimund@2082: } ingo@1774: d@9619: final 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: * 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: @Override d@9619: public void setSettings(final 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 :