Skip to content

Commit

Permalink
Merge pull request #242 from vsperez/tc_issue_241
Browse files Browse the repository at this point in the history
FIX issue #241
  • Loading branch information
scrudden authored Jul 30, 2021
2 parents 5347c07 + 77972b9 commit 9c08d8f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ public class StopPath implements Serializable, Lifecycle {
@Transient
private List<VectorWithHeading> vectors = null;

/**
* This is used just to get better location of the bustop
* where bestmatch is done.
*/
@Transient
private Double shapeDistanceTraveled;

// Because Hibernate requires objects with composite IDs to be Serializable
private static final long serialVersionUID = 8170734640228933095L;

Expand All @@ -149,6 +156,7 @@ public class StopPath implements Serializable, Lifecycle {
* @param waitStop
* @param scheduleAdherenceStop
* @param breakTime
* @param shapeDistanceTraveled
*/
public StopPath(int configRev,
String pathId,
Expand All @@ -161,7 +169,7 @@ public StopPath(int configRev,
boolean scheduleAdherenceStop,
Integer breakTime,
Double maxDistance,
Double maxSpeed) {
Double maxSpeed, Double shapeDistanceTraveled) {
this.configRev = configRev;
this.stopPathId = pathId;
this.stopId = stopId;
Expand All @@ -177,6 +185,7 @@ public StopPath(int configRev,
this.breakTime = breakTime;
this.maxDistance = maxDistance;
this.maxSpeed = maxSpeed;
this.shapeDistanceTraveled=shapeDistanceTraveled;
}

/**
Expand All @@ -199,6 +208,7 @@ private StopPath() {
this.maxDistance = null;
this.maxSpeed = null;


}

/**
Expand Down Expand Up @@ -235,7 +245,13 @@ public static String determinePathId(String previousStopId, String stopId) {
return previousStopId + "_to_" + stopId;
}
}

public Double getShapeDistanceTraveled() {
return shapeDistanceTraveled;
}

public void setShapeDistanceTraveled(Double shapeDistanceTraveled) {
this.shapeDistanceTraveled = shapeDistanceTraveled;
}
/**
* Returns the distance to travel along the path. Summation of
* all of the path segments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ private List<ScheduleTime> getScheduleTimesForTrip(Trip trip) {
StopPath path = new StopPath(revs.getConfigRev(), pathId,
stopId, gtfsStopTime.getStopSequence(), lastStopInTrip,
trip.getRouteId(), layoverStop, waitStop,
scheduleAdherenceStop, gtfsRoute.getBreakTime(), gtfsStopTime.getMaxDistance(), gtfsStopTime.getMaxSpeed());
scheduleAdherenceStop, gtfsRoute.getBreakTime(), gtfsStopTime.getMaxDistance(), gtfsStopTime.getMaxSpeed(),gtfsStopTime.getShapeDistTraveled());
paths.add(path);

previousStopId = stopId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,15 @@ private static class BestMatch {
double stopToShapeDistance;
double distanceAlongShape;
Location matchLocation;

double distanceAlongPattern;
@Override
public String toString() {
return "BestMatch ["
+ "shapeIndex=" + shapeIndex
+ ", stopToShapeDistance="
+ Geo.distanceFormat(stopToShapeDistance)
+ ", distanceAlongShape="
+ Geo.distanceFormat(distanceAlongShape)
+ ", matchLocation=" + matchLocation
+ "]";
return "BestMatch [shapeIndex=" + shapeIndex + ", stopToShapeDistance=" + stopToShapeDistance
+ ", distanceAlongShape=" + distanceAlongShape + ", matchLocation=" + matchLocation
+ ", distanceAlongPattern=" + distanceAlongPattern + "]";
}


}
/**
* Determines and returns the best match of the stop to a shape.
Expand All @@ -247,14 +244,16 @@ public String toString() {
*/
private BestMatch determineBestMatch(TripPattern tripPattern,
int stopIndex, int previousShapeIndex,
double previousDistanceAlongShape, List<Location> shapeLocs) {
double previousDistanceAlongShape, List<Location> shapeLocs,double previousDistanceAlongPattern) {
// Value to be returned
BestMatch bestMatch = null;

// Determine the stop for the trip pattern
String stopId = tripPattern.getStopId(stopIndex);
Stop stop = stopsMap.get(stopId);

StopPath stopPath= tripPattern.getStopPath(stopIndex);
//if(stopId.startsWith("GLN") )
// System.out.println("AQUI COMINEZA "+stopId);
// Determine the previous stop for the trip pattern (can be null)
Stop previousStop = null;
if (stopIndex > 0) {
Expand Down Expand Up @@ -282,6 +281,7 @@ private BestMatch determineBestMatch(TripPattern tripPattern,
// when add in length of current vector it indeed shows how
// far along shapes been looking to match current stop.
double distanceAlongShapesExamined = -previousDistanceAlongShape;
double dististanceOverPattern = previousDistanceAlongPattern;
for (int shapeIndex = previousShapeIndex;
shapeIndex < shapeLocs.size()-1;
++shapeIndex) {
Expand All @@ -290,6 +290,17 @@ private BestMatch determineBestMatch(TripPattern tripPattern,
Location loc1 = shapeLocs.get(shapeIndex+1);
Vector shapeVector = new Vector(loc0, loc1);


dististanceOverPattern+=shapeVector.length();
//This is the same segment than the previous one
//so we should only add the part form the last part
if(previousShapeIndex==shapeIndex)
{
dististanceOverPattern-=previousDistanceAlongShape;
}


// stop.get
// Determine distance of stop to the current shape
double stopToShapeDistance = stop.getLoc().distance(shapeVector);
// If this is the best fit so far, but
Expand Down Expand Up @@ -322,9 +333,16 @@ private BestMatch determineBestMatch(TripPattern tripPattern,
bestMatch.matchLocation =
shapeVector
.locAlongVector(bestMatch.distanceAlongShape);

bestMatch.distanceAlongPattern=dististanceOverPattern-(shapeVector.length()-bestMatch.distanceAlongShape);
}
}

if(stopPath.getShapeDistanceTraveled()!=null && bestMatch!=null)
{

if(Math.abs(bestMatch.distanceAlongPattern-stopPath.getShapeDistanceTraveled())<10)
break;
}
// Keep track of how far along the shapes have examined. If
// have looked for much further than the distance between the
// stops then have looked far enough. Don't want to look too
Expand Down Expand Up @@ -402,7 +420,8 @@ private BestMatch determineBestMatch(TripPattern tripPattern,
tripPattern.toStringListingTripIds() );
}
}

logger.debug(" bestMatch {} expected distanceAlongPattern {}",bestMatch,stopPath.getShapeDistanceTraveled());
System.out.println("bestMatch " +bestMatch+" "+stopPath.getShapeDistanceTraveled());
// Return results
return bestMatch;
}
Expand All @@ -428,14 +447,14 @@ private void determinePathSegmentsMatchingStopsToShapes(
double previousDistanceAlongShape = 0.0;
Location previousLocation = null;
int numberOfStopsTooFarAway = 0;

double previousDistanceAlongPattern=0.0;
// For each stop for the trip pattern...
for (int stopIndex = 0;
stopIndex < tripPattern.getStopPaths().size();
++stopIndex) {
// Determine which shape the stop matches to
BestMatch bestMatch = determineBestMatch(tripPattern, stopIndex,
previousShapeIndex, previousDistanceAlongShape, shapeLocs);
previousShapeIndex, previousDistanceAlongShape, shapeLocs,previousDistanceAlongPattern);
// Keep track of how many stops too far away from path so can log
// the number for the entire system
if (bestMatch.stopToShapeDistance > maxStopToPathDistance) {
Expand Down Expand Up @@ -510,6 +529,7 @@ private void determinePathSegmentsMatchingStopsToShapes(
// Prepare for looking at next stop
previousShapeIndex = bestMatch.shapeIndex;
previousDistanceAlongShape = bestMatch.distanceAlongShape;
previousDistanceAlongPattern= bestMatch.distanceAlongPattern;
} // End of for each stop for the trip pattern

// If there errors with stops being too far away from the stopPaths then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public GtfsStopTime(CSVRecord record, boolean supplemental, String fileName)
dropOffType = getOptionalValue(record, "drop_off_type");

String shapeDistTraveledStr = getOptionalValue(record, "shape_dist_traveled");
shapeDistTraveled = shapeDistTraveledStr == null ?
shapeDistTraveled = shapeDistTraveledStr == null || shapeDistTraveledStr.trim().isEmpty() ?
null : Double.parseDouble(shapeDistTraveledStr);

timepointStop = getOptionalBooleanValue(record, "timepoint");
Expand Down

0 comments on commit 9c08d8f

Please sign in to comment.