view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java @ 5818:a4ff4167be1e

Request feature info on all layers and show it as html if the server does not return valid gml. Non queryable layers produce an error message when the request fails. This is good enough
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 24 Apr 2013 17:33:27 +0200
parents 3c7e7d47939f
children
line wrap: on
line source
package de.intevation.flys.artifacts.model;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.math.Linear;
import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
import de.intevation.flys.artifacts.model.extreme.ExtremeResult;

import org.apache.log4j.Logger;

/**
 * Facet of a Waterlevel (WQKms).
 */
public class WaterlevelFacet extends DataFacet {

    private static Logger logger = Logger.getLogger(WaterlevelFacet.class);

    public WaterlevelFacet(int index, String name, String description) {
        super(index, name, description, ComputeType.ADVANCE, null, null);
    }

    public WaterlevelFacet(
        int         index,
        String      name,
        String      description,
        ComputeType type,
        String      stateID,
        String      hash
    ) {
        super(index, name, description, type, hash, stateID);
    }

    public WaterlevelFacet() {
    }

    protected WQKms [] getWQKms(CalculationResult res) {
        if (res.getData() instanceof ExtremeResult)
            return ((ExtremeResult) res.getData()).getWQKms();
        else if (res.getData() instanceof WQKms[]) {
            return (WQKms []) res.getData();
        }
        else {
            logger.error("WaterlevelFacet got wrong data type " + res.getData());
            return null;
        }
    }

    /**
     * Get waterlevel data.
     * @return a WQKms at given index.
     */
    @Override
    public Object getData(Artifact artifact, CallContext context) {

        if (logger.isDebugEnabled()) {
            logger.debug("Get data for waterlevels at index: " + index +
                " /stateId: " + stateId);
        }

        if (artifact == null) {
            logger.error("WaterlevelFacet.getData: artifact is null");
            return null;
        }

        FLYSArtifact winfo = (FLYSArtifact) artifact;

        CalculationResult res = (CalculationResult)
            winfo.compute(context, hash, stateId, type, false);

        if (res == null) {
            logger.error("WaterlevelFacet.getData: null result");
            return null;
        }

        WQKms [] wqkms = getWQKms(res);
        Object KM = context.getContextValue("currentKm");

        // Interpolation.
        if (KM != null) {
            linearInterpolate(wqkms[index], (Double) KM);
        }
        else {
            logger.debug("Do not interpolate.");
        }

        return wqkms != null ? wqkms[index] : null;
    }


    /**
     * Linear interpolation of WQKms.
     * TODO rewrite.
     * @return [w, q, km]
     */
    public WQKms linearInterpolate(WQKms wqkms, double km) {
        logger.debug("interpolate at given km (" + km + ")");

        WQKms resultWQKms = new WQKms();
        int size = wqkms.size();
        boolean kmIncreasing = wqkms.getKm(0) < wqkms.getKm(size-1);
        int mod = kmIncreasing ? +1 : -1;
        int idx = 0;
        // Move idx to closest from one direction, check for match.
        if (!kmIncreasing) {
            while (idx < size && wqkms.getKm(idx) < km) {
                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
                    return resultWQKms;
                }
                idx++;
            }
        }
        else {
            idx = size-1;
            while (idx > 0 && wqkms.getKm(idx) > km) {
                if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
                    resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
                    return resultWQKms;
                }
                idx--;
            }
        }
        if (Math.abs(wqkms.getKm(idx) - km) < 0.01d) {
            resultWQKms.add(wqkms.getW(idx), wqkms.getQ(idx), wqkms.getKm(idx));
            return resultWQKms;
        }

        if ((idx != -1) && (idx < size) && (idx - mod != -1) && (idx - mod < size)) {
            double inW = Linear.linear(
                km,
                wqkms.getKm(idx), wqkms.getKm(idx - mod),
                wqkms.getW(idx), wqkms.getW(idx - mod));
            double inQ = Linear.linear(
                km,
                wqkms.getKm(idx), wqkms.getKm(idx - mod),
                wqkms.getQ(idx), wqkms.getQ(idx - mod));
            resultWQKms.add(inW, inQ, km);
        }
        else {
            logger.debug("waterlevelfacet stuff " + idx + " size " + size + " mod: " + mod);
        }

        return resultWQKms;
    }


    /** Copy deeply. */
    @Override
    public Facet deepCopy() {
        WaterlevelFacet copy = new WaterlevelFacet();
        copy.set(this);
        copy.type    = type;
        copy.hash    = hash;
        copy.stateId = stateId;
        return copy;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org