Skip to content

Commit 409e5b4

Browse files
authored
feat: implemented transfer distance validator (#1958)
1 parent 07a56a9 commit 409e5b4

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}

main/src/test/java/org/mobilitydata/gtfsvalidator/validator/NoticeFieldsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void testNoticeClassFieldNames() {
109109
"fileNameB",
110110
"filename",
111111
"firstIndex",
112+
"fromStopId",
112113
"geoDistanceToShape",
113114
"geoJsonType",
114115
"geographyId",
@@ -213,6 +214,7 @@ public void testNoticeClassFieldNames() {
213214
"tableName",
214215
"time",
215216
"timeframeGroupId",
217+
"toStopId",
216218
"transferCount",
217219
"tripCsvRowNumber",
218220
"tripFieldName",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.mobilitydata.gtfsvalidator.validator;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import com.google.common.collect.ImmutableList;
6+
import org.junit.Test;
7+
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
8+
import org.mobilitydata.gtfsvalidator.table.*;
9+
import org.mobilitydata.gtfsvalidator.table.GtfsTransfer.Builder;
10+
11+
public class TransferDistanceValidatorTest {
12+
13+
private final NoticeContainer noticeContainer = new NoticeContainer();
14+
15+
@Test
16+
public void testDistanceAbove10KmGeneratesWarning() {
17+
GtfsStopTableContainer stops =
18+
GtfsStopTableContainer.forEntities(
19+
ImmutableList.of(
20+
new GtfsStop.Builder().setStopId("s0").setStopLat(0.0).setStopLon(0.0).build(),
21+
new GtfsStop.Builder().setStopId("s1").setStopLat(0.0).setStopLon(0.1).build()),
22+
noticeContainer);
23+
GtfsTransferTableContainer transfers =
24+
GtfsTransferTableContainer.forEntities(
25+
ImmutableList.of(
26+
new Builder().setCsvRowNumber(1).setFromStopId("s0").setToStopId("s1").build()),
27+
noticeContainer);
28+
29+
new TransferDistanceValidator(transfers, stops).validate(noticeContainer);
30+
assertThat(noticeContainer.getValidationNotices())
31+
.containsExactly(
32+
new TransferDistanceValidator.TransferDistanceTooLargeNotice(
33+
transfers.getEntities().get(0), 11.119510117748394));
34+
}
35+
36+
@Test
37+
public void testDistanceAbove2KmGeneratesNotice() {
38+
GtfsStopTableContainer stops =
39+
GtfsStopTableContainer.forEntities(
40+
ImmutableList.of(
41+
new GtfsStop.Builder().setStopId("s0").setStopLat(0.0).setStopLon(0.0).build(),
42+
new GtfsStop.Builder().setStopId("s1").setStopLat(0.0).setStopLon(0.02).build()),
43+
noticeContainer);
44+
GtfsTransferTableContainer transfers =
45+
GtfsTransferTableContainer.forEntities(
46+
ImmutableList.of(
47+
new Builder().setCsvRowNumber(1).setFromStopId("s0").setToStopId("s1").build()),
48+
noticeContainer);
49+
50+
new TransferDistanceValidator(transfers, stops).validate(noticeContainer);
51+
assertThat(noticeContainer.getValidationNotices())
52+
.containsExactly(
53+
new TransferDistanceValidator.TransferDistanceAbove_2KmNotice(
54+
transfers.getEntities().get(0), 2.2239020235496785));
55+
}
56+
57+
@Test
58+
public void testDistanceBellow2KmYieldsNoNotice() {
59+
GtfsStopTableContainer stops =
60+
GtfsStopTableContainer.forEntities(
61+
ImmutableList.of(
62+
new GtfsStop.Builder().setStopId("s0").setStopLat(0.0).setStopLon(0.0).build(),
63+
new GtfsStop.Builder().setStopId("s1").setStopLat(0.0).setStopLon(0.01).build()),
64+
noticeContainer);
65+
GtfsTransferTableContainer transfers =
66+
GtfsTransferTableContainer.forEntities(
67+
ImmutableList.of(
68+
new Builder().setCsvRowNumber(1).setFromStopId("s0").setToStopId("s1").build()),
69+
noticeContainer);
70+
71+
new TransferDistanceValidator(transfers, stops).validate(noticeContainer);
72+
assertThat(noticeContainer.getValidationNotices()).isEmpty();
73+
}
74+
}

0 commit comments

Comments
 (0)