-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: trip vs shape distance validation #1553
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bae0af9
feat: trip vs shape distance validation
cka-y 0f51e13
feat: updated severity and fixed tests
cka-y 54233bd
Merge branch 'master' into feat/163
davidgamez 6607d69
Merge branch 'master' into feat/163
davidgamez 61a41eb
Merge branch 'master' into feat/163
cka-y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
...src/main/java/org/mobilitydata/gtfsvalidator/validator/TripAndShapeDistanceValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2021 MobilityData IO | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.mobilitydata.gtfsvalidator.validator; | ||
|
||
import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.ERROR; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.FileRefs; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.*; | ||
|
||
/** | ||
* Validates that the distance traveled by a trip is lesser or equals the max length of its shape. | ||
* | ||
* <p>Generated notice: {@link TripDistanceExceedsShapeDistanceNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class TripAndShapeDistanceValidator extends FileValidator { | ||
|
||
private final GtfsTripTableContainer tripTable; | ||
|
||
private final GtfsStopTimeTableContainer stopTimeTable; | ||
|
||
private final GtfsShapeTableContainer shapeTable; | ||
|
||
@Inject | ||
TripAndShapeDistanceValidator( | ||
GtfsTripTableContainer tripTable, | ||
GtfsStopTimeTableContainer stopTimeTable, | ||
GtfsShapeTableContainer shapeTable) { | ||
this.tripTable = tripTable; | ||
this.stopTimeTable = stopTimeTable; | ||
this.shapeTable = shapeTable; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
List<String> uniqueShapeIds = | ||
shapeTable.getEntities().stream() | ||
.map(GtfsShape::shapeId) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
|
||
uniqueShapeIds.forEach( | ||
shapeId -> { | ||
double maxShapeDist = | ||
shapeTable.getEntities().stream() | ||
.filter(s -> s.shapeId().equals(shapeId)) | ||
.mapToDouble(GtfsShape::shapeDistTraveled) | ||
.max() | ||
.orElse(Double.NEGATIVE_INFINITY); | ||
|
||
tripTable | ||
.byShapeId(shapeId) | ||
.forEach( | ||
trip -> { | ||
double maxStopTimeDist = | ||
stopTimeTable.byTripId(trip.tripId()).stream() | ||
.mapToDouble(GtfsStopTime::shapeDistTraveled) | ||
.max() | ||
.orElse(Double.NEGATIVE_INFINITY); | ||
|
||
if (maxStopTimeDist > maxShapeDist) { | ||
noticeContainer.addValidationNotice( | ||
new TripDistanceExceedsShapeDistanceNotice( | ||
trip.tripId(), shapeId, maxStopTimeDist, maxShapeDist)); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** The distance traveled by a trip should be less or equal to the max length of its shape. */ | ||
@GtfsValidationNotice( | ||
severity = ERROR, | ||
files = @FileRefs({GtfsTrip.class, GtfsStopTime.class, GtfsShape.class})) | ||
static class TripDistanceExceedsShapeDistanceNotice extends ValidationNotice { | ||
|
||
/** The faulty record's trip id. */ | ||
private final String tripId; | ||
|
||
/** The faulty record's shape id. */ | ||
private final String shapeId; | ||
|
||
/** The faulty record's trip max distance traveled. */ | ||
private final double maxTripDistanceTraveled; | ||
|
||
/** The faulty record's shape max distance traveled. */ | ||
private final double maxShapeDistanceTraveled; | ||
|
||
TripDistanceExceedsShapeDistanceNotice( | ||
String tripId, | ||
String shapeId, | ||
double maxTripDistanceTraveled, | ||
double maxShapeDistanceTraveled) { | ||
this.tripId = tripId; | ||
this.shapeId = shapeId; | ||
this.maxShapeDistanceTraveled = maxShapeDistanceTraveled; | ||
this.maxTripDistanceTraveled = maxTripDistanceTraveled; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...test/java/org/mobilitydata/gtfsvalidator/validator/TripAndShapeDistanceValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.mobilitydata.gtfsvalidator.validator; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.*; | ||
|
||
@RunWith(JUnit4.class) | ||
public class TripAndShapeDistanceValidatorTest { | ||
|
||
private static List<GtfsTrip> createTripTable(int rows) { | ||
ArrayList<GtfsTrip> trips = new ArrayList<>(); | ||
for (int i = 0; i < rows; i++) { | ||
trips.add( | ||
new GtfsTrip.Builder() | ||
.setCsvRowNumber(i + 1) | ||
.setTripId("t" + i) | ||
.setServiceId("sr" + i) | ||
.setRouteId("r" + i) | ||
.setShapeId("s" + i) | ||
.build()); | ||
} | ||
return trips; | ||
} | ||
|
||
private static List<GtfsShape> createShapeTable(int rows, double shapeDistTraveled) { | ||
ArrayList<GtfsShape> shapes = new ArrayList<>(); | ||
for (int i = 0; i < rows; i++) { | ||
shapes.add( | ||
new GtfsShape.Builder() | ||
.setCsvRowNumber(i + 1) | ||
.setShapeId("s" + i) | ||
.setShapePtLat(1.0) | ||
.setShapePtLon(1.0) | ||
.setShapePtSequence(0) | ||
.setShapeDistTraveled(shapeDistTraveled + i) | ||
.build()); | ||
} | ||
return shapes; | ||
} | ||
|
||
private static List<GtfsStopTime> createStopTimesTable(int rows, double shapeDistTraveled) { | ||
ArrayList<GtfsStopTime> stopTimes = new ArrayList<>(); | ||
for (int i = 0; i < rows; i++) { | ||
stopTimes.add( | ||
new GtfsStopTime.Builder() | ||
.setCsvRowNumber(i + 1) | ||
.setTripId("t" + i) | ||
.setStopSequence(0) | ||
.setStopId("st" + i) | ||
.setShapeDistTraveled(shapeDistTraveled + i) | ||
.build()); | ||
} | ||
return stopTimes; | ||
} | ||
|
||
private static List<ValidationNotice> generateNotices( | ||
List<GtfsTrip> trips, List<GtfsStopTime> stopTimes, List<GtfsShape> shapes) { | ||
NoticeContainer noticeContainer = new NoticeContainer(); | ||
new TripAndShapeDistanceValidator( | ||
GtfsTripTableContainer.forEntities(trips, noticeContainer), | ||
GtfsStopTimeTableContainer.forEntities(stopTimes, noticeContainer), | ||
GtfsShapeTableContainer.forEntities(shapes, noticeContainer)) | ||
.validate(noticeContainer); | ||
return noticeContainer.getValidationNotices(); | ||
} | ||
|
||
@Test | ||
public void testTripDistanceExceedsShapeDistance() { | ||
assertThat( | ||
generateNotices( | ||
createTripTable(1), createStopTimesTable(1, 10.0), createShapeTable(1, 9.0))) | ||
.isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void testValidTripVsShapeDistance1() { | ||
assertThat( | ||
generateNotices( | ||
createTripTable(1), createStopTimesTable(1, 10.0), createShapeTable(1, 10.0))) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testValidTripVsShapeDistance2() { | ||
assertThat( | ||
generateNotices( | ||
createTripTable(1), createStopTimesTable(1, 9.0), createShapeTable(1, 10.0))) | ||
.isEmpty(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great addition!