org.opentripplanner.routing.edgetype.TemporaryPartialStreetEdge Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
package org.opentripplanner.routing.edgetype;
import java.util.Collection;
import javax.annotation.Nonnull;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.LineString;
import org.opentripplanner.common.TurnRestriction;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.util.ElevationUtils;
import org.opentripplanner.routing.vertextype.StreetVertex;
import org.opentripplanner.routing.vertextype.TemporaryVertex;
import org.opentripplanner.util.I18NString;
final public class TemporaryPartialStreetEdge extends StreetWithElevationEdge implements TemporaryEdge {
private static final long serialVersionUID = 1L;
/**
* The edge on which this lies.
*/
private final StreetEdge parentEdge;
// An explicit geometry is stored, so that it may still be retrieved after this edge is removed
// from the graph and the from/to vertices are set to null.
private final LineString geometry;
/**
* Create a new partial street edge along the given 'parentEdge' from 'v1' to 'v2'.
* If the length is negative, a new length is calculated from the geometry.
* The elevation data is calculated using the 'parentEdge' and given 'length'.
*/
public TemporaryPartialStreetEdge(StreetEdge parentEdge, StreetVertex v1, StreetVertex v2,
LineString geometry, I18NString name, double length) {
super(v1, v2, geometry, name, length, parentEdge.getPermission(), false);
this.parentEdge = parentEdge;
this.geometry = super.getGeometry();
setCarSpeed(parentEdge.getCarSpeed());
setElevationProfileUsingParents();
}
/**
* Create a new partial street edge along the given 'parentEdge' from 'v1' to 'v2'.
* The length is calculated using the provided geometry.
* The elevation data is calculated using the 'parentEdge' and the calculated 'length'.
*/
TemporaryPartialStreetEdge(StreetEdge parentEdge, StreetVertex v1, StreetVertex v2,
LineString geometry, I18NString name) {
super(v1, v2, geometry, name, parentEdge.getPermission(), false);
this.parentEdge = parentEdge;
this.geometry = super.getGeometry();
setCarSpeed(parentEdge.getCarSpeed());
setElevationProfileUsingParents();
}
@Override
public LineString getGeometry() {
return geometry;
}
/**
* Partial edges are always partial.
*/
@Override
public boolean isPartial() {
return true;
}
/**
* Have the inbound angle of their parent.
*/
@Override
public int getInAngle() {
return parentEdge.getInAngle();
}
/**
* Have the outbound angle of their parent.
*/
@Override
public int getOutAngle() {
return parentEdge.getInAngle();
}
@Nonnull
@Override
public Collection getTurnRestrictions() {
return parentEdge.getTurnRestrictions();
}
/**
* This implementation makes it so that TurnRestrictions on the parent edge are applied to this edge as well.
*/
@Override
public boolean isEquivalentTo(Edge e) {
return (e == this || e == parentEdge);
}
@Override
public boolean isReverseOf(Edge e) {
Edge other = e;
if (e instanceof TemporaryPartialStreetEdge) {
other = ((TemporaryPartialStreetEdge) e).parentEdge;
}
// TODO(flamholz): is there a case where a partial edge has a reverse of its own?
return parentEdge.isReverseOf(other);
}
@Override
public boolean isRoundabout() {
return parentEdge.isRoundabout();
}
/**
* Returns true if this edge is trivial - beginning and ending at the same point.
*/
public boolean isTrivial() {
Coordinate fromCoord = this.getFromVertex().getCoordinate();
Coordinate toCoord = this.getToVertex().getCoordinate();
return fromCoord.equals(toCoord);
}
public StreetEdge getParentEdge() {
return parentEdge;
}
@Override
public String toString() {
return "TemporaryPartialStreetEdge(" + this.getDefaultName() + ", " + this.getFromVertex() + " -> "
+ this.getToVertex() + " length=" + this.getDistanceMeters() + " carSpeed="
+ this.getCarSpeed() + " parentEdge=" + parentEdge + ")";
}
private void assertEdgeIsNotDirectedAwayFromTemporaryEndVertex(StreetVertex v1) {
if(v1 instanceof TemporaryVertex) {
if (((TemporaryVertex)v1).isEndVertex()) {
throw new IllegalStateException("A temporary edge is directed away from an end vertex");
}
}
}
private void assertEdgeIsDirectedTowardsTemporaryEndVertex(StreetVertex v2) {
if(v2 instanceof TemporaryVertex) {
if (!((TemporaryVertex)v2).isEndVertex()) {
throw new IllegalStateException("A temporary edge is directed towards a start vertex");
}
}
}
private void setElevationProfileUsingParents() {
setElevationProfile(
ElevationUtils.getPartialElevationProfile(
getParentEdge().getElevationProfile(), 0, getDistanceMeters()
),
false
);
}
}