view backend/src/main/java/org/dive4elements/river/model/CrossSectionLine.java @ 8755:30b1ddadf275

(issue1801) Unify reference gauge finding code The basic way as described in the method comment of the determineRefGauge method is now used in the WINFOArtifact, MainValuesService and RiverUtils.getGauge method. RiverUtils.getGauge previously just returned the first gauge found. While this is now a behavior change I believe that it is always more correct then the undeterministic behavior of the previous implmenentation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 24 Jun 2015 14:07:26 +0200
parents 9d2e69f971f5
children 8abe94270f32
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.model;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;

import java.awt.geom.Point2D;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.SequenceGenerator;
import javax.persistence.GenerationType;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;

import org.apache.log4j.Logger;

@Entity
@Table(name = "cross_section_lines")
public class CrossSectionLine
implements   Serializable
{
    private static Logger log = Logger.getLogger(CrossSectionLine.class);

    public static final double EPSILON   = 1e-4;

    public static final double TOO_SMALL = 0.2;
    public static final double TOO_BIG   = 2500;

    private Integer                 id;
    private Double                  km;
    private CrossSection            crossSection;

    private List<CrossSectionPoint> points;

    public static final Comparator<CrossSectionPoint> COL_POS_CMP =
        new Comparator<CrossSectionPoint>() {
            @Override
            public int compare(CrossSectionPoint a, CrossSectionPoint b) {
                double xa = a.getX().doubleValue();
                double xb = b.getX().doubleValue();
                double d = xa - xb;
                if (d < -EPSILON) return -1;
                if (d > +EPSILON) return +1;
                int diff = a.getColPos() - b.getColPos();
                return diff < 0 ? -1 : diff > 0 ? +1 : 0;
            }
        };


    public static final boolean isValid(double x) {
        x = Math.abs(x);
        return x > TOO_SMALL && x < TOO_BIG;
    }

    public static final boolean isValid(Point2D p) {
        return isValid(p.getX()) && isValid(p.getY());
    }


    public CrossSectionLine() {
    }

    public CrossSectionLine(CrossSection crossSection, Double km) {
        this.crossSection = crossSection;
        this.km           = km;
    }

    @Id
    @SequenceGenerator(
        name           = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ",
        sequenceName   = "CROSS_SECTION_LINES_ID_SEQ",
        allocationSize = 1)
    @GeneratedValue(
        strategy  = GenerationType.SEQUENCE,
        generator = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ")
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "km")
    public Double getKm() {
        return km;
    }

    public void setKm(Double km) {
        this.km = km;
    }

    @OneToOne
    @JoinColumn(name = "cross_section_id")
    public CrossSection getCrossSection() {
        return crossSection;
    }

    public void setCrossSection(CrossSection crossSection) {
        this.crossSection = crossSection;
    }

    @OneToMany
    @JoinColumn(name="cross_section_line_id")
    public List<CrossSectionPoint> getPoints() {
        return points;
    }

    public void setPoints(List<CrossSectionPoint> points) {
        this.points = points;
    }


    public List<Point2D> fetchCrossSectionLinesPoints() {

        List<CrossSectionPoint> linePoints =
            new ArrayList<CrossSectionPoint>(getPoints());

        Collections.sort(linePoints, COL_POS_CMP);

        List<Point2D> points = new ArrayList<Point2D>(linePoints.size());
        for (CrossSectionPoint p: linePoints) {
            double x = p.getX().doubleValue();
            double y = p.getY().doubleValue();
            if (isValid(x) && isValid(y)) {
                points.add(new Point2D.Double(x, y));
            }
        }

        return points;
    }

    public double [][] fetchCrossSectionProfile() {
        return fetchCrossSectionProfile(fetchCrossSectionLinesPoints());
    }

    /** double[][] from List<Point2D> */
    public static double [][] fetchCrossSectionProfile(List<Point2D> points) {

        int P = points.size();

        double [] xs = new double[P];
        double [] ys = new double[P];

        if (P > 0) {
            xs[0] = points.get(0).getX();
            ys[0] = points.get(0).getY();

            for (int i = 1; i < P; i++) {
                Point2D p = points.get(i);
                double x = p.getX();
                double y = p.getY();

                if (x <= xs[i-1]) {
                    x = xs[i-1] + EPSILON;
                }

                xs[i] = x;
                ys[i] = y;
            }
        }

        return new double [][] { xs, ys };
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org