view backend/src/main/java/org/dive4elements/river/model/CrossSection.java @ 6229:3fea9701d58d

Fix gauge determination. If we look upstream ( a > b ) we still have to compare the start value of our range against the gauge and not the end value. The start value is always the relevant value as we handle the direction by checking this against A or B of the gauge. Also: I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again. I will never compare doubles with == again.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 06 Jun 2013 18:23:17 +0200
parents 4c3ccf2b0304
children 5b1cc816fcdb
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.List;
import java.util.ArrayList;

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.OrderBy;
import javax.persistence.JoinColumn;

import java.math.MathContext;
import java.math.BigDecimal;

import org.hibernate.Session;
import org.hibernate.SQLQuery;
import org.hibernate.Query;

import org.hibernate.type.StandardBasicTypes;

import org.dive4elements.river.backend.SessionHolder;

@Entity
@Table(name = "cross_sections")
public class CrossSection
implements   Serializable
{
    public static final MathContext PRECISION = new MathContext(6);

    public static final String SQL_FAST_CROSS_SECTION_LINES =
        "SELECT km, x, y, csl.id AS csl_id " +
        "FROM cross_section_lines csl JOIN cross_section_points csp " +
        "ON csp.cross_section_line_id = csl.id " +
        "WHERE csl.cross_section_id = :cs_id AND " +
        "km between :from_km AND :to_km " +
        "ORDER BY csl.id, csp.col_pos";

    private Integer                id;
    private River                  river;
    private TimeInterval           timeInterval;
    private String                 description;
    private List<CrossSectionLine> lines;

    public CrossSection() {
    }

    public CrossSection(
        River        river,
        TimeInterval timeInterval,
        String       description
    ) {
        this.river        = river;
        this.timeInterval = timeInterval;
        this.description  = description;
    }

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

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

    @OneToOne
    @JoinColumn(name = "river_id")
    public River getRiver() {
        return river;
    }

    public void setRiver(River river) {
        this.river = river;
    }

    @OneToOne
    @JoinColumn(name = "time_interval_id")
    public TimeInterval getTimeInterval() {
        return timeInterval;
    }

    public void setTimeInterval(TimeInterval timeInterval) {
        this.timeInterval = timeInterval;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @OneToMany
    @OrderBy("km")
    @JoinColumn(name="cross_section_id")
    public List<CrossSectionLine> getLines() {
        return lines;
    }

    public void setLines(List<CrossSectionLine> lines) {
        this.lines = lines;
    }

    public List<CrossSectionLine> getLines(double startKm, double endKm) {
        Session session = SessionHolder.HOLDER.get();
        Query query = session.createQuery(
            "from CrossSectionLine where crossSection=:crossSection " +
            "and km between :startKm and :endKm order by km");
        query.setParameter("crossSection", this);
        query.setParameter("startKm", new BigDecimal(startKm, PRECISION));
        query.setParameter("endKm", new BigDecimal(endKm, PRECISION));

        return query.list();
    }

    public List<FastCrossSectionLine> getFastLines(
        double startKm,
        double endKm
    ) {
        Session session = SessionHolder.HOLDER.get();

        SQLQuery sqlQuery = session.createSQLQuery(SQL_FAST_CROSS_SECTION_LINES)
            .addScalar("km",     StandardBasicTypes.DOUBLE)
            .addScalar("x",      StandardBasicTypes.DOUBLE)
            .addScalar("y",      StandardBasicTypes.DOUBLE)
            .addScalar("csl_id", StandardBasicTypes.INTEGER);

        sqlQuery
            .setInteger("cs_id",  getId())
            .setDouble("from_km", startKm)
            .setDouble("to_km",   endKm);

        List<Object []> results = sqlQuery.list();

        ArrayList<Point2D> points = new ArrayList<Point2D>(500);
        ArrayList<FastCrossSectionLine> lines =
            new ArrayList<FastCrossSectionLine>();

        Integer lastId = null;
        Double  lastKm = null;

        for (Object [] result: results) {
            Double  km = (Double)result[0];
            Double  x  = (Double)result[1];
            Double  y  = (Double)result[2];
            Integer id = (Integer)result[3];

            if (lastId != null && !lastId.equals(id)) {
                points.trimToSize();
                FastCrossSectionLine line =
                    new FastCrossSectionLine(lastKm, points);
                lines.add(line);
                points = new ArrayList<Point2D>(500);
            }

            Point2D p = new Point2D.Double(x, y);

            if (CrossSectionLine.isValid(p)) {
                points.add(p);
            }

            lastKm = km;
            lastId = id;
        }

        if (lastId != null) {
            points.trimToSize();
            FastCrossSectionLine line =
                new FastCrossSectionLine(lastKm, points);
            lines.add(line);
        }

        lines.trimToSize();

        return lines;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org