-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6003 from ibi-group/trimet-vector-layer
Filter vector tiles stops by current service week
- Loading branch information
Showing
19 changed files
with
446 additions
and
89 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/LayerFiltersTest.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,42 @@ | ||
package org.opentripplanner.ext.vectortiles.layers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner.transit.model._data.PatternTestModel; | ||
import org.opentripplanner.transit.model._data.TransitModelForTest; | ||
import org.opentripplanner.transit.model.network.TripPattern; | ||
import org.opentripplanner.transit.model.site.RegularStop; | ||
|
||
class LayerFiltersTest { | ||
|
||
private static final RegularStop STOP = TransitModelForTest.of().stop("1").build(); | ||
private static final LocalDate DATE = LocalDate.of(2024, 9, 5); | ||
private static final TripPattern PATTERN = PatternTestModel.pattern(); | ||
|
||
@Test | ||
void includeStopWithinServiceWeek() { | ||
var predicate = LayerFilters.buildCurrentServiceWeekPredicate( | ||
s -> List.of(PATTERN), | ||
trip -> List.of(DATE), | ||
() -> DATE | ||
); | ||
|
||
assertTrue(predicate.test(STOP)); | ||
} | ||
|
||
@Test | ||
void excludeOutsideServiceWeek() { | ||
var inThreeWeeks = DATE.plusDays(21); | ||
var predicate = LayerFilters.buildCurrentServiceWeekPredicate( | ||
s -> List.of(PATTERN), | ||
trip -> List.of(inThreeWeeks), | ||
() -> DATE | ||
); | ||
|
||
assertFalse(predicate.test(STOP)); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/ext/java/org/opentripplanner/ext/vectortiles/layers/LayerFilters.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,73 @@ | ||
package org.opentripplanner.ext.vectortiles.layers; | ||
|
||
import java.time.DayOfWeek; | ||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalAdjusters; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
import org.opentripplanner.apis.gtfs.model.LocalDateRange; | ||
import org.opentripplanner.transit.model.network.TripPattern; | ||
import org.opentripplanner.transit.model.site.RegularStop; | ||
import org.opentripplanner.transit.model.timetable.Trip; | ||
import org.opentripplanner.transit.service.PatternByServiceDatesFilter; | ||
import org.opentripplanner.transit.service.TransitService; | ||
|
||
/** | ||
* Predicates for filtering elements of vector tile layers. Currently only contains predicates | ||
* for {@link RegularStop}. Once more types need to be filtered, this may need some refactoring. | ||
*/ | ||
public class LayerFilters { | ||
|
||
/** | ||
* No filter is applied: all stops are included in the result. | ||
*/ | ||
public static final Predicate<RegularStop> NO_FILTER = x -> true; | ||
|
||
/** | ||
* Returns a predicate which only includes stop which are visited by a pattern that is in the current | ||
* "service week", which lasts from Sunday to Sunday. | ||
*/ | ||
public static Predicate<RegularStop> buildCurrentServiceWeekPredicate( | ||
Function<RegularStop, Collection<TripPattern>> getPatternsForStop, | ||
Function<Trip, Collection<LocalDate>> getServiceDatesForTrip, | ||
Supplier<LocalDate> nowSupplier | ||
) { | ||
var serviceDate = nowSupplier.get(); | ||
var lastSunday = serviceDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY)); | ||
var nextSundayPlusOne = serviceDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).plusDays(1); | ||
|
||
var filter = new PatternByServiceDatesFilter( | ||
// reminder, the end of the date range is exclusive so it's the next Sunday plus one day | ||
new LocalDateRange(lastSunday, nextSundayPlusOne), | ||
// not used | ||
route -> List.of(), | ||
getServiceDatesForTrip | ||
); | ||
|
||
return regularStop -> { | ||
var patterns = getPatternsForStop.apply(regularStop); | ||
var patternsInCurrentWeek = filter.filterPatterns(patterns); | ||
return !patternsInCurrentWeek.isEmpty(); | ||
}; | ||
} | ||
|
||
public static Predicate<RegularStop> forType(FilterType type, TransitService transitService) { | ||
return switch (type) { | ||
case NONE -> NO_FILTER; | ||
case SUNDAY_TO_SUNDAY_SERVICE_WEEK -> buildCurrentServiceWeekPredicate( | ||
transitService::getPatternsForStop, | ||
trip -> | ||
transitService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId()), | ||
() -> LocalDate.now(transitService.getTimeZone()) | ||
); | ||
}; | ||
} | ||
|
||
public enum FilterType { | ||
NONE, | ||
SUNDAY_TO_SUNDAY_SERVICE_WEEK, | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.