view artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationState.java @ 9563:3955ecc1a516

Restrict inundation map to selected river range
author gernotbelger
date Mon, 29 Oct 2018 17:57:30 +0100
parents bf6b63208f34
children 63bbd5e45839
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.uinfo.inundationduration;

import java.util.List;

import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.ChartArtifact;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.MapArtifact.MapState;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.DataFacet;
import org.dive4elements.river.artifacts.model.EmptyFacet;
import org.dive4elements.river.artifacts.model.ReportFacet;
import org.dive4elements.river.artifacts.model.map.WMSLayerFacet;
import org.dive4elements.river.artifacts.uinfo.UINFOArtifact;
import org.dive4elements.river.artifacts.uinfo.inundationduration.InundationDurationCalculationResult.WmsLayer;
import org.dive4elements.river.model.RiverAxisKm;
import org.dive4elements.river.utils.GeometryUtils;

import com.vividsolutions.jts.geom.Envelope;

/**
 * @author Domenico Nardi Tironi
 */
public class InundationDurationState extends MapState {

    /// ** The log that is used in this state. */
    // private static Logger log = Logger.getLogger(FlowDepthState.class);

    private static final long serialVersionUID = 1L;

    private static final String LABEL_URL_SEPARATOR = ";LABEL_URL_SEPARATOR;";// always sync with client (ExportPanel)

    /**
     * From this state can only be continued trivially.
     */
    @Override
    protected String getUIProvider() {
        return "continue";
    }

    @Override
    public Object computeFeed(final D4EArtifact artifact, final String hash, final CallContext context, final List<Facet> facets, final Object old) {
        // FIXME: why is this necessary?
        if (artifact instanceof ChartArtifact) {
            facets.add(new EmptyFacet());
            return null;
        }

        return compute((UINFOArtifact) artifact, context, ComputeType.FEED, hash, facets, old);
    }

    @Override
    public Object computeAdvance(final D4EArtifact artifact, final String hash, final CallContext context, final List<Facet> facets, final Object old) {
        if (artifact instanceof ChartArtifact) {
            facets.add(new EmptyFacet());
            return null;
        }

        if (facets != null)
            super.computeAdvance(artifact, hash, context, facets, old);

        return compute((UINFOArtifact) artifact, context, ComputeType.ADVANCE, hash, facets, old);

    }

    /**
     * Compute result or returned object from cache, create facets.
     *
     * @param old
     *            Object that was cached.
     */
    private Object compute(final UINFOArtifact sinfo, final CallContext context, final ComputeType type, final String hash, final List<Facet> facets,
            final Object old) {

        final CalculationResult res = doCompute(sinfo, context, old);

        if (facets == null)
            return res;

        final InundationDurationCalculationResult result = (InundationDurationCalculationResult) res.getData();
        final List<WmsLayer> layers = result.getLayers();

        int index = 1; // 1 because super.computeAdvance adds the river theme with index 0
        for (final WmsLayer layer : layers) {

            final String label = layer.getLabel();
            final String url = layer.getUrl();

            final WMSLayerFacet wmsFacet = new WMSLayerFacet(index, FLOODMAP_EXTERNAL_WMS_INUNDATIONDUR + index, label, type, getID(), hash, url);
            wmsFacet.addLayer(layer.getLayer());

            facets.add(wmsFacet);
            // TODO:
            // wmsFacet.setExtent(getExtent(false));
            // wmsFacet.setOriginalExtent(getExtent(true));
            wmsFacet.setSrid(getSrid());

            if (layer.isShowLayerLink())
                facets.add(new DataFacet("wms_url", label + LABEL_URL_SEPARATOR + url, ComputeType.ADVANCE, hash, this.id));

            index++; // because super.computeAdvance adds the river theme with index 0
        }

        final Calculation report = res.getReport();
        if (report.hasProblems())
            facets.add(new ReportFacet(ComputeType.ADVANCE, hash, this.id));

        return res;
    }

    private CalculationResult doCompute(final UINFOArtifact artifact, final CallContext context, final Object old) {
        if (old instanceof CalculationResult)
            return (CalculationResult) old;

        return new InundationDurationCalculation(context).calculate(artifact);
    }

    /**
     * The extent is restricted to the selected station range
     */
    @Override
    protected Envelope getExtent(final boolean reproject) {

        final InundationDurationAccess access = new InundationDurationAccess((UINFOArtifact) this.artifact);

        final double lowerKm = access.getLowerKm();
        final double upperKm = access.getUpperKm();

        final List<RiverAxisKm> axisKms = RiverAxisKm.getRiverAxisKms(getRiverId(), lowerKm, upperKm);

        Envelope max = null;

        for (final RiverAxisKm ax : axisKms) {
            final Envelope env = ax.getGeom().getEnvelopeInternal();

            if (max == null)
                max = env;
            else
                max.expandToInclude(env);
        }

        return max != null && reproject ? GeometryUtils.transform(max, getSrid()) : max;
    }
}

http://dive4elements.wald.intevation.org