-
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
Add a list of validators that meet the official GTFS requirements #1315
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f85c737
Add a list of validators that meet the official GTFS requirements
aababilov 7da9c4e
Update comments and remove validation of the required stop_name
aababilov 37228f9
Remove StopTimeInSequenceOrderValidator
aababilov efcc7d2
Merge branch 'master' into many-validators
isabelle-dr 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
127 changes: 127 additions & 0 deletions
127
.../java/org/mobilitydata/gtfsvalidator/validator/BlockTripsWithConsistentTypeValidator.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,127 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 com.google.common.collect.Iterables; | ||
import com.google.common.collect.Multimaps; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRoute; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRouteTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsRouteType; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsTrip; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsTripTableContainer; | ||
|
||
/** | ||
* Validates that trips that belong to the same block have consistent route types (e.g., bus does | ||
* not transfer to rail). | ||
* | ||
* <p> Generated notice: {@link BlockTripsWithInconsistentRouteTypesNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class BlockTripsWithConsistentTypeValidator extends FileValidator { | ||
private final GtfsTripTableContainer tripTable; | ||
private final GtfsRouteTableContainer routeTable; | ||
|
||
@Inject | ||
BlockTripsWithConsistentTypeValidator( | ||
GtfsTripTableContainer tripTable, GtfsRouteTableContainer routeTable) { | ||
this.tripTable = tripTable; | ||
this.routeTable = routeTable; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
for (List<GtfsTrip> tripsInBlock : Multimaps.asMap(tripTable.byBlockIdMap()).values()) { | ||
GtfsTrip firstTrip = tripsInBlock.get(0); | ||
// We don't check trips without a block id. | ||
if (!firstTrip.hasBlockId()) { | ||
continue; | ||
} | ||
|
||
Optional<GtfsRoute> firstRoute = routeTable.byRouteId(firstTrip.routeId()); | ||
if (firstRoute.isEmpty()) { | ||
continue; | ||
} | ||
GtfsRouteType firstRouteType = firstRoute.get().routeType(); | ||
for (GtfsTrip otherTrip : Iterables.skip(tripsInBlock, 1)) { | ||
if (firstTrip.routeId().equals(otherTrip.routeId())) { | ||
// Both trips are compatible since they belong to the same route. | ||
continue; | ||
} | ||
Optional<GtfsRoute> otherRoute = routeTable.byRouteId(otherTrip.routeId()); | ||
if (otherRoute.isPresent() | ||
&& !routesAreCompatibleThroughBlockTransfer( | ||
firstRouteType, otherRoute.get().routeType())) { | ||
noticeContainer.addValidationNotice(new BlockTripsWithInconsistentRouteTypesNotice( | ||
firstTrip, firstRoute.get(), otherTrip, otherRoute.get())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** Tells if route types are compatible for block transfer. */ | ||
private static boolean routesAreCompatibleThroughBlockTransfer( | ||
GtfsRouteType routeType1, GtfsRouteType routeType2) { | ||
if (routeType1.equals(routeType2)) { | ||
return true; | ||
} | ||
|
||
// The difference beservice_datestween rail and subway (metro) may be fuzzy, so we allow | ||
// transfers between them. | ||
return isRailOrSubway(routeType1) && isRailOrSubway(routeType2); | ||
} | ||
|
||
private static boolean isRailOrSubway(GtfsRouteType routeType) { | ||
return routeType.equals(GtfsRouteType.RAIL) || routeType.equals(GtfsRouteType.SUBWAY); | ||
} | ||
|
||
/** | ||
* Describes two trips that belong to the same block and have inconsistent route types (e.g., bus | ||
* vs train). | ||
*/ | ||
static class BlockTripsWithInconsistentRouteTypesNotice extends ValidationNotice { | ||
private final long csvRowNumber1; | ||
private final String tripId1; | ||
private final int routeType1; | ||
private final String routeTypeName1; | ||
private final long csvRowNumber2; | ||
private final String tripId2; | ||
private final int routeType2; | ||
private final String routeTypeName2; | ||
private final String blockId; | ||
|
||
BlockTripsWithInconsistentRouteTypesNotice( | ||
GtfsTrip firstTrip, GtfsRoute firstRoute, GtfsTrip otherTrip, GtfsRoute otherRoute) { | ||
super(SeverityLevel.WARNING); | ||
this.csvRowNumber1 = firstTrip.csvRowNumber(); | ||
this.tripId1 = firstTrip.tripId(); | ||
this.routeType1 = firstRoute.routeType().getNumber(); | ||
this.routeTypeName1 = firstRoute.routeType().toString(); | ||
this.csvRowNumber2 = otherTrip.csvRowNumber(); | ||
this.tripId2 = otherTrip.tripId(); | ||
this.routeType2 = otherRoute.routeType().getNumber(); | ||
this.routeTypeName2 = otherRoute.routeType().toString(); | ||
this.blockId = firstTrip.blockId(); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rc/main/java/org/mobilitydata/gtfsvalidator/validator/FareAttributeAgencyIdValidator.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,59 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsAgencyTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareAttribute; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFareAttributeTableContainer; | ||
|
||
/** | ||
* Checks that {@code fare_attributes.agency_id} field is defined for every row if there is more | ||
* than 1 agency in the feed. | ||
* | ||
* <p>Generated notice: {@link MissingRequiredFieldNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class FareAttributeAgencyIdValidator extends FileValidator { | ||
private final GtfsFareAttributeTableContainer fareAttributeTable; | ||
private final GtfsAgencyTableContainer agencyTable; | ||
|
||
@Inject | ||
FareAttributeAgencyIdValidator( | ||
GtfsFareAttributeTableContainer fareAttributeTable, GtfsAgencyTableContainer agencyTable) { | ||
this.fareAttributeTable = fareAttributeTable; | ||
this.agencyTable = agencyTable; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
if (agencyTable.entityCount() <= 1) { | ||
// fare_attributes.agency_id is not required when there is a single agency. | ||
return; | ||
} | ||
for (GtfsFareAttribute fareAttribute : fareAttributeTable.getEntities()) { | ||
if (!fareAttribute.hasAgencyId()) { | ||
noticeContainer.addValidationNotice( | ||
new MissingRequiredFieldNotice(GtfsFareAttribute.FILENAME, fareAttribute.csvRowNumber(), | ||
GtfsFareAttribute.AGENCY_ID_FIELD_NAME)); | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
main/src/main/java/org/mobilitydata/gtfsvalidator/validator/FeedHasLanguageValidator.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,70 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsAgency; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsAgencyTableContainer; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsFeedInfoTableContainer; | ||
|
||
/** | ||
* Checks that a feed has language defined in either {@code feed_info.feed_lang} or {@code | ||
* agency.agency_lang}. | ||
* | ||
* <p>Note that {@code feed_lang} is a required field: if there is a {@code feed_info} entity, | ||
* it must have{@code feed_lang} set. {@code agency_lang}, instead, is optional. | ||
* The recommended way is to provide {@code feed_lang} and omit {@code agency_lang}. | ||
* | ||
* <p> Generated notice: {@link FeedHasNoLanguageNotice}. | ||
*/ | ||
@GtfsValidator | ||
public class FeedHasLanguageValidator extends FileValidator { | ||
private final GtfsFeedInfoTableContainer feedInfoTable; | ||
private final GtfsAgencyTableContainer agencyTable; | ||
|
||
@Inject | ||
FeedHasLanguageValidator( | ||
GtfsAgencyTableContainer agencyTable, GtfsFeedInfoTableContainer feedInfoTable) { | ||
this.feedInfoTable = feedInfoTable; | ||
this.agencyTable = agencyTable; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
if (feedInfoTable.getEntities().size() == 1 && feedInfoTable.getSingleEntity().isPresent()) { | ||
// The language is defined in feed_info.feed_lang field. | ||
return; | ||
} | ||
for (GtfsAgency agency : agencyTable.getEntities()) { | ||
if (agency.hasAgencyLang()) { | ||
// The language is defined in agency.agency_lang field. | ||
return; | ||
} | ||
} | ||
noticeContainer.addValidationNotice(new FeedHasNoLanguageNotice()); | ||
} | ||
|
||
static class FeedHasNoLanguageNotice extends ValidationNotice { | ||
FeedHasNoLanguageNotice() { | ||
super(SeverityLevel.ERROR); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
main/src/main/java/org/mobilitydata/gtfsvalidator/validator/StationUsageValidator.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,70 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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 javax.inject.Inject; | ||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.SeverityLevel; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsLocationType; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsStop; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsStopTableContainer; | ||
|
||
/** | ||
* Checks that every station ({@code location_type=1}) in {@code stops.txt} | ||
* has child platforms ({@code location_type=0}). | ||
* | ||
* <p>Note that a station that has child entrances ({@code location_type=2}) | ||
* or generic nodes ({@code location_type=3}) is also reported. | ||
* | ||
* <p> Generated notice: {@link StationWithoutPlatformsNotice} | ||
*/ | ||
@GtfsValidator | ||
public class StationUsageValidator extends FileValidator { | ||
private final GtfsStopTableContainer stopTable; | ||
|
||
@Inject | ||
StationUsageValidator(GtfsStopTableContainer stopTable) { | ||
this.stopTable = stopTable; | ||
} | ||
|
||
@Override | ||
public void validate(NoticeContainer noticeContainer) { | ||
for (GtfsStop parent : stopTable.getEntities()) { | ||
if (parent.locationType().equals(GtfsLocationType.STATION) | ||
&& stopTable.byParentStation(parent.stopId()) | ||
.stream() | ||
.noneMatch(s -> s.locationType().equals(GtfsLocationType.STOP))) { | ||
noticeContainer.addValidationNotice(new StationWithoutPlatformsNotice(parent)); | ||
} | ||
} | ||
} | ||
|
||
static class StationWithoutPlatformsNotice extends ValidationNotice { | ||
private final long csvRowNumber; | ||
private final String stopId; | ||
private final String stopName; | ||
|
||
StationWithoutPlatformsNotice(GtfsStop location) { | ||
super(SeverityLevel.WARNING); | ||
this.csvRowNumber = location.csvRowNumber(); | ||
this.stopId = location.stopId(); | ||
this.stopName = location.stopName(); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
main/src/main/java/org/mobilitydata/gtfsvalidator/validator/StopLatLngRequiredValidator.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,62 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* 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.table.GtfsLocationType.ENTRANCE; | ||
import static org.mobilitydata.gtfsvalidator.table.GtfsLocationType.STATION; | ||
import static org.mobilitydata.gtfsvalidator.table.GtfsLocationType.STOP; | ||
|
||
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; | ||
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFieldNotice; | ||
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; | ||
import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsLocationType; | ||
import org.mobilitydata.gtfsvalidator.table.GtfsStop; | ||
|
||
/** | ||
* Checks that conditionally required fields {@code stop_lat} and {@code stop_lon} are set depending | ||
* on {@code location_type}. | ||
* | ||
* <p>{@code stop_lat} and {@code stop_lon} are required for stations ({@code location_type=0}), | ||
* stops ({@code location_type=1}) and entrances ({@code location_type=2}) and optional for other | ||
* types. | ||
* <p> | ||
* Generated notice: {@code MissingRequiredFieldError}. | ||
*/ | ||
@GtfsValidator | ||
public class StopLatLngRequiredValidator extends SingleEntityValidator<GtfsStop> { | ||
@Override | ||
public void validate(GtfsStop location, NoticeContainer noticeContainer) { | ||
GtfsLocationType locationType = location.locationType(); | ||
if (!(locationType == STOP || locationType == STATION || locationType == ENTRANCE)) { | ||
return; | ||
} | ||
if (!location.hasStopLat()) { | ||
noticeContainer.addValidationNotice( | ||
createMissingRequiredFieldNotice(location, GtfsStop.STOP_LAT_FIELD_NAME)); | ||
} | ||
if (!location.hasStopLon()) { | ||
noticeContainer.addValidationNotice( | ||
createMissingRequiredFieldNotice(location, GtfsStop.STOP_LON_FIELD_NAME)); | ||
} | ||
} | ||
|
||
private static ValidationNotice createMissingRequiredFieldNotice( | ||
GtfsStop location, String fieldName) { | ||
return new MissingRequiredFieldNotice(GtfsStop.FILENAME, location.csvRowNumber(), fieldName); | ||
} | ||
} |
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.
@isabelle-dr @aababilov I've added similar logic as part of #1318 and added a best practice
MissingRecommendedFieldNotice
warning to recommend thatagency_id
be included even when there is only one agency.