Subversion Repositories hibernate-spatial

Rev

Blame | Last modification | View Log | RSS feed

package org.hibernatespatial.jts.linearref;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineSegment;
import org.hibernatespatial.jts.geom.MeasuredCoordinate;

/**
 * @author Karel Maesen, Geovise BVBA
 *         creation-date: Dec 1, 2010
 */
public class MeasuredLineSegment {

    private final LineSegment segment;
    private final MeasuredCoordinate start;
    private final MeasuredCoordinate end;

    MeasuredLineSegment(MeasuredCoordinate start, MeasuredCoordinate end) {
        this.start = start;
        this.end = end;
        this.segment = new LineSegment(start.getCoordinate(), end.getCoordinate());
    }

    public double getStartM() {
        return start.getM();
    }

    public double getEndM() {
        return end.getM();
    }

    public double measureAlong(double factor) {
        return getStartM() + factor * (getEndM() - getStartM());
    }

    public double segmentFraction(double measure) {
        double fraction = (measure - getStartM()) / (getEndM() - getStartM());

        if (fraction < 0d) return 0d;
        if (fraction > 1d) return 1d;
        if (Double.isNaN(fraction)) return 1d; //ensures that if measure=startM and endM == startM, the end measure is selected
        return fraction;
    }

    public MeasuredCoordinate getMeasuredCoordinate(double measure) {
        double fraction = this.segmentFraction(measure);
        return measuredPointAlong(fraction);
    }

    public MeasuredCoordinate measuredPointAlong(double fraction) {
        Coordinate co = this.pointAlong(fraction);
        double measure = this.measureAlong(fraction);
        return new MeasuredCoordinate(co, measure);
    }

    public boolean measureInSegment(double measure) {
        return (getStartM() <= measure && measure <= getEndM()) ||
                getStartM() >= measure && measure >= getEndM();
    }


    //Delegated methods.

    public Coordinate intersection(LineSegment line) {
        return segment.intersection(line);
    }

    public double getLength() {
        return segment.getLength();
    }

    public boolean isHorizontal() {
        return segment.isHorizontal();
    }

    public boolean isVertical() {
        return segment.isVertical();
    }

    public int orientationIndex(LineSegment seg) {
        return segment.orientationIndex(seg);
    }

    public int orientationIndex(Coordinate p) {
        return segment.orientationIndex(p);
    }

    public void reverse() {
        segment.reverse();
    }

    public void normalize() {
        segment.normalize();
    }

    public double angle() {
        return segment.angle();
    }

    public Coordinate midPoint() {
        return segment.midPoint();
    }

    public double distance(LineSegment ls) {
        return segment.distance(ls);
    }

    public double distance(Coordinate p) {
        return segment.distance(p);
    }

    public double distancePerpendicular(Coordinate p) {
        return segment.distancePerpendicular(p);
    }

    public Coordinate pointAlong(double segmentLengthFraction) {
        return segment.pointAlong(segmentLengthFraction);
    }

    public Coordinate pointAlongOffset(double segmentLengthFraction, double offsetDistance) {
        return segment.pointAlongOffset(segmentLengthFraction, offsetDistance);
    }

    public double projectionFactor(Coordinate p) {
        return segment.projectionFactor(p);
    }

    public double segmentFraction(Coordinate inputPt) {
        return segment.segmentFraction(inputPt);
    }

    public Coordinate project(Coordinate p) {
        return segment.project(p);
    }

    public LineSegment project(LineSegment seg) {
        return segment.project(seg);
    }

    public Coordinate closestPoint(Coordinate p) {
        return segment.closestPoint(p);
    }

    public Coordinate[] closestPoints(LineSegment line) {
        return segment.closestPoints(line);
    }
}