|
| 1 | +/* |
| 2 | + * Copyright 2025 MobilityData |
| 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.annotation.GtfsValidationNotice.SectionRef.FILE_REQUIREMENTS; |
| 20 | +import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.ERROR; |
| 21 | + |
| 22 | +import java.util.*; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
| 25 | +import javax.inject.Inject; |
| 26 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice; |
| 27 | +import org.mobilitydata.gtfsvalidator.annotation.GtfsValidator; |
| 28 | +import org.mobilitydata.gtfsvalidator.notice.NoticeContainer; |
| 29 | +import org.mobilitydata.gtfsvalidator.notice.ValidationNotice; |
| 30 | +import org.mobilitydata.gtfsvalidator.table.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Validates that the feature id from "locations.geojson" is not a duplicate of any stop_id from |
| 34 | + * "stops.txt" or location_group_id from "location_group_stops.txt" |
| 35 | + * |
| 36 | + * <p>Generated notice: {@link DuplicateGeographyIdNotice}. |
| 37 | + */ |
| 38 | +@GtfsValidator |
| 39 | +public class UniqueGeographyIdValidator extends FileValidator { |
| 40 | + private final GtfsStopTableContainer stopTable; |
| 41 | + private final GtfsLocationGroupsTableContainer locationGroupStopsTable; |
| 42 | + private final GtfsGeoJsonFeaturesContainer geoJsonFeatures; |
| 43 | + |
| 44 | + @Inject |
| 45 | + UniqueGeographyIdValidator( |
| 46 | + GtfsGeoJsonFeaturesContainer geoJsonFeatures, |
| 47 | + GtfsStopTableContainer stopTable, |
| 48 | + GtfsLocationGroupsTableContainer locationGroupTable) { |
| 49 | + this.geoJsonFeatures = geoJsonFeatures; |
| 50 | + this.stopTable = stopTable; |
| 51 | + this.locationGroupStopsTable = locationGroupTable; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void validate(NoticeContainer noticeContainer) { |
| 56 | + // Collect all ID entries from each file |
| 57 | + List<IdEntry> allEntries = |
| 58 | + Stream.concat( |
| 59 | + geoJsonFeatures.getEntities().stream() |
| 60 | + .map( |
| 61 | + f -> |
| 62 | + new IdEntry( |
| 63 | + f.featureId(), GtfsGeoJsonFeature.FILENAME, f.featureIndex())), |
| 64 | + Stream.concat( |
| 65 | + stopTable.getEntities().stream() |
| 66 | + .map(s -> new IdEntry(s.stopId(), GtfsStop.FILENAME, s.csvRowNumber())), |
| 67 | + locationGroupStopsTable.getEntities().stream() |
| 68 | + .map( |
| 69 | + g -> |
| 70 | + new IdEntry( |
| 71 | + g.locationGroupId(), |
| 72 | + GtfsLocationGroupStops.FILENAME, |
| 73 | + g.csvRowNumber())))) |
| 74 | + .collect(Collectors.toList()); |
| 75 | + |
| 76 | + // Group by ID and check for duplicates across files |
| 77 | + allEntries.stream() |
| 78 | + .collect(Collectors.groupingBy(IdEntry::id)) |
| 79 | + .forEach( |
| 80 | + (id, entries) -> { |
| 81 | + if (entries.size() > 1) { |
| 82 | + Set<String> uniqueFilenames = |
| 83 | + entries.stream().map(IdEntry::filename).collect(Collectors.toSet()); |
| 84 | + if (uniqueFilenames.size() == 1) return; |
| 85 | + noticeContainer.addValidationNotice( |
| 86 | + new DuplicateGeographyIdNotice( |
| 87 | + id, |
| 88 | + getRowNumber(entries, GtfsStop.FILENAME), |
| 89 | + getRowNumber(entries, GtfsLocationGroupStops.FILENAME), |
| 90 | + getRowNumber(entries, GtfsGeoJsonFeature.FILENAME))); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + // Utility method to extract row number by filename |
| 96 | + private Integer getRowNumber(List<IdEntry> entries, String filename) { |
| 97 | + return entries.stream() |
| 98 | + .filter(e -> e.filename().equals(filename)) |
| 99 | + .map(IdEntry::instanceIndex) |
| 100 | + .findFirst() |
| 101 | + .orElse(null); |
| 102 | + } |
| 103 | + |
| 104 | + // Helper record to hold ID entries |
| 105 | + private static class IdEntry { |
| 106 | + private final String id; |
| 107 | + private final String filename; |
| 108 | + private final int instanceIndex; |
| 109 | + |
| 110 | + public IdEntry(String id, String filename, int instanceIndex) { |
| 111 | + this.id = id; |
| 112 | + this.filename = filename; |
| 113 | + this.instanceIndex = instanceIndex; |
| 114 | + } |
| 115 | + |
| 116 | + public int instanceIndex() { |
| 117 | + return instanceIndex; |
| 118 | + } |
| 119 | + |
| 120 | + public String id() { |
| 121 | + return id; |
| 122 | + } |
| 123 | + |
| 124 | + public String filename() { |
| 125 | + return filename; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Geography id is duplicated across multiple files. |
| 131 | + * |
| 132 | + * <p>ID must be unique across all `stops.stop_id`, `locations.geojson` `id`, and |
| 133 | + * `location_groups.location_group_id` values. |
| 134 | + */ |
| 135 | + @GtfsValidationNotice( |
| 136 | + severity = ERROR, |
| 137 | + files = |
| 138 | + @GtfsValidationNotice.FileRefs({ |
| 139 | + GtfsLocationGroupsSchema.class, |
| 140 | + GtfsStopTimeSchema.class, |
| 141 | + GtfsLocationGroupsSchema.class |
| 142 | + }), |
| 143 | + sections = @GtfsValidationNotice.SectionRefs(FILE_REQUIREMENTS)) |
| 144 | + public static class DuplicateGeographyIdNotice extends ValidationNotice { |
| 145 | + |
| 146 | + /** The geography id that is duplicated. */ |
| 147 | + private final String geographyId; |
| 148 | + |
| 149 | + /** The csv row number in stops.txt */ |
| 150 | + private final Integer csvRowNumberA; |
| 151 | + |
| 152 | + /** The csv row number in location_group_stops.txt */ |
| 153 | + private final Integer csvRowNumberB; |
| 154 | + |
| 155 | + /** The feature index in locations.geojson */ |
| 156 | + private final Integer featureIndex; |
| 157 | + |
| 158 | + public DuplicateGeographyIdNotice( |
| 159 | + String geographyId, Integer csvRowNumberA, Integer csvRowNumberB, Integer featureIndex) { |
| 160 | + this.geographyId = geographyId; |
| 161 | + this.csvRowNumberA = csvRowNumberA; |
| 162 | + this.csvRowNumberB = csvRowNumberB; |
| 163 | + this.featureIndex = featureIndex; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments