Skip to content
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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}
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;
Copy link
Collaborator

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 that agency_id be included even when there is only one agency.

}
for (GtfsFareAttribute fareAttribute : fareAttributeTable.getEntities()) {
if (!fareAttribute.hasAgencyId()) {
noticeContainer.addValidationNotice(
new MissingRequiredFieldNotice(GtfsFareAttribute.FILENAME, fareAttribute.csvRowNumber(),
GtfsFareAttribute.AGENCY_ID_FIELD_NAME));
}
}
}
}
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);
}
}
}
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();
}
}
}
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);
}
}
Loading