|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.mobilitydata.gtfsvalidator.validator; |
| 18 | + |
| 19 | +import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.*; |
| 20 | +import static org.mobilitydata.gtfsvalidator.util.S2Earth.getDistanceMeters; |
| 21 | + |
| 22 | +import com.google.common.geometry.S2Point; |
| 23 | +import javax.inject.Inject; |
| 24 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; |
| 25 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 26 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 27 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 28 | +import org.mobilitydata.gtfsvalidator.table.*; |
| 29 | +import org.mobilitydata.gtfsvalidator.util.StopUtil; |
| 30 | + |
| 31 | +/** |
| 32 | + * Validates that the transfer distance between two stops is not too large. |
| 33 | + * |
| 34 | + * <p>Generated notice: {@link TransferDistanceTooLargeNotice}. |
| 35 | + * |
| 36 | + * <p>Generated notice: {@link TransferDistanceAbove_2KmNotice}. |
| 37 | + */ |
| 38 | +@GtfsValidator |
| 39 | +public class TransferDistanceValidator extends FileValidator { |
| 40 | + private final GtfsTransferTableContainer transferTableContainer; |
| 41 | + private final GtfsStopTableContainer stopTableContainer; |
| 42 | + |
| 43 | + @Inject |
| 44 | + TransferDistanceValidator( |
| 45 | + GtfsTransferTableContainer transferTableContainer, |
| 46 | + GtfsStopTableContainer stopTableContainer) { |
| 47 | + this.transferTableContainer = transferTableContainer; |
| 48 | + this.stopTableContainer = stopTableContainer; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void validate(NoticeContainer noticeContainer) { |
| 53 | + for (GtfsTransfer transfer : transferTableContainer.getEntities()) { |
| 54 | + if (transfer.hasFromStopId() && transfer.hasToStopId()) { |
| 55 | + S2Point fromCoordinates = |
| 56 | + StopUtil.getStopOrParentLatLng(stopTableContainer, transfer.fromStopId()).toPoint(); |
| 57 | + S2Point toCoordinates = |
| 58 | + StopUtil.getStopOrParentLatLng(stopTableContainer, transfer.toStopId()).toPoint(); |
| 59 | + double distanceMeters = getDistanceMeters(fromCoordinates, toCoordinates); |
| 60 | + if (distanceMeters > 10_000) { |
| 61 | + noticeContainer.addValidationNotice( |
| 62 | + new TransferDistanceTooLargeNotice(transfer, distanceMeters / 1_000)); |
| 63 | + |
| 64 | + } else if (distanceMeters > 2_000) { |
| 65 | + noticeContainer.addValidationNotice( |
| 66 | + new TransferDistanceAbove_2KmNotice(transfer, distanceMeters / 1_000)); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean shouldCallValidate() { |
| 74 | + return transferTableContainer != null |
| 75 | + && stopTableContainer != null |
| 76 | + && transferTableContainer.hasColumn(GtfsTransfer.FROM_STOP_ID_FIELD_NAME) |
| 77 | + && transferTableContainer.hasColumn(GtfsTransfer.TO_STOP_ID_FIELD_NAME); |
| 78 | + } |
| 79 | + |
| 80 | + /** The transfer distance from stop to stop in `transfers.txt` is larger than 2 km. */ |
| 81 | + @GtfsValidationNotice(severity = INFO) |
| 82 | + public static class TransferDistanceAbove_2KmNotice extends ValidationNotice { |
| 83 | + |
| 84 | + /** The row number from `transfers.txt` for the faulty entry. */ |
| 85 | + private final int csvRowNumber; |
| 86 | + |
| 87 | + /** The ID of the stop in `from_stop_id`. */ |
| 88 | + private final String fromStopId; |
| 89 | + |
| 90 | + /** The ID of the stop in `to_stop_id`. */ |
| 91 | + private final String toStopId; |
| 92 | + |
| 93 | + /** The distance between the two stops in km. */ |
| 94 | + private final double distanceKm; |
| 95 | + |
| 96 | + public TransferDistanceAbove_2KmNotice(GtfsTransfer transfer, double distanceKm) { |
| 97 | + this.csvRowNumber = transfer.csvRowNumber(); |
| 98 | + this.fromStopId = transfer.fromStopId(); |
| 99 | + this.toStopId = transfer.toStopId(); |
| 100 | + this.distanceKm = distanceKm; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** The transfer distance from stop to stop in `transfers.txt` is larger than 10 km. */ |
| 105 | + @GtfsValidationNotice(severity = WARNING) |
| 106 | + public static class TransferDistanceTooLargeNotice extends ValidationNotice { |
| 107 | + |
| 108 | + /** The row number from `transfers.txt` for the faulty entry. */ |
| 109 | + private final int csvRowNumber; |
| 110 | + |
| 111 | + /** The ID of the stop in `from_stop_id`. */ |
| 112 | + private final String fromStopId; |
| 113 | + |
| 114 | + /** The ID of the stop in `to_stop_id`. */ |
| 115 | + private final String toStopId; |
| 116 | + |
| 117 | + /** The distance between the two stops in km. */ |
| 118 | + private final double distanceKm; |
| 119 | + |
| 120 | + public TransferDistanceTooLargeNotice(GtfsTransfer transfer, double distanceKm) { |
| 121 | + this.csvRowNumber = transfer.csvRowNumber(); |
| 122 | + this.fromStopId = transfer.fromStopId(); |
| 123 | + this.toStopId = transfer.toStopId(); |
| 124 | + this.distanceKm = distanceKm; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments