org.geolatte.geom.LineString Maven / Gradle / Ivy
Show all versions of geolatte-geom Show documentation
/*
* This file is part of the GeoLatte project.
*
* GeoLatte is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeoLatte is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with GeoLatte. If not, see .
*
* Copyright (C) 2010 - 2011 and Ownership of code is shared by:
* Qmino bvba - Romeinsestraat 18 - 3001 Heverlee (http://www.qmino.com)
* Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
*/
package org.geolatte.geom;
import org.geolatte.geom.crs.CoordinateReferenceSystem;
/**
* A LineString is a 1-dimensional Geometry
consisting of the LineSegment
s defined by
* consecutive pairs of Point
s of a PointSequence
.
*
* @author Karel Maesen, Geovise BVBA, 2011
*/
public class LineString extends Geometry
implements Linear
, Simple {
public LineString(CoordinateReferenceSystem
crs) {
super(crs);
}
/**
* This constructor has been added to speed up object creation of LinearRings
*
* @param base
*/
protected LineString(LineString
base) {
super(base.getPositions(), base.getCoordinateReferenceSystem());
}
/**
* Constructs a LineString
from the specified positions
* and CoordinateReferenceSystem
.
*
* @param positions the {@code PositionSequence} that determines the geometry
* @params crs the {@code CoordinateReferenceSystem} for the geometry
* @throws IllegalArgumentException if the passed PointSequence
is non-empty and of size less than 2
*/
public LineString(PositionSequence
positions, CoordinateReferenceSystem
crs) {
super(positions, crs);
if (positions.size() != 0 && positions.size() < 2) {
throw new IllegalArgumentException("Require at least two points for a linestring");
}
}
/**
* Returns the first Position
of this LineString
.
*
* @return the first Position
of this LineString
.
*/
public P getStartPosition() {
return getPositionN(0);
}
/**
* Returns the last Position
of this LineString
.
*
* @return the last Position
of this LineString
.
*/
public P getEndPosition() {
return getPositionN(getNumPositions() - 1);
}
/**
* Checks whether this LineString
is closed, i.e. the first Point
equals the last.
*
* @return true if this LineString
is closed.
*/
public boolean isClosed() {
return !isEmpty() && getStartPosition().equals(getEndPosition());
}
/**
* Checks whether this LineString
is a ring, i.e. is closed and non-empty.
*
*
Note that this implementation allows rings thate are non-simple.
*
* @return true if this LineString
is a ring.
*/
public boolean isRing() {
return !isEmpty() && isClosed();
}
@Override
public int getDimension() {
return 1;
}
@Override
public GeometryType getGeometryType() {
return GeometryType.LINESTRING;
}
@Override
public void accept(GeometryVisitor visitor) {
visitor.visit(this);
}
/**
* Returns the {@code LineSegments} that define this instance.
*
* @return an Iterable of this instance's {@code LineSegment}s
*/
Iterable> lineSegments() {
return new LineSegments(getPositions());
}
@Override
@SuppressWarnings("unchecked")
public LineString as(Class castToType){
checkCast(castToType);
return (LineString)this;
}
}