Skip to content

Commit

Permalink
Filter stops by current service week
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Aug 8, 2024
1 parent 15a5bd2 commit 5f3cd3a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.opentripplanner.apis.support.TileJson;
import org.opentripplanner.ext.vectortiles.layers.areastops.AreaStopsLayerBuilder;
import org.opentripplanner.ext.vectortiles.layers.stations.StationsLayerBuilder;
import org.opentripplanner.ext.vectortiles.layers.stops.Predicates;
import org.opentripplanner.ext.vectortiles.layers.stops.StopsLayerBuilder;
import org.opentripplanner.ext.vectortiles.layers.vehicleparkings.VehicleParkingGroupsLayerBuilder;
import org.opentripplanner.ext.vectortiles.layers.vehicleparkings.VehicleParkingsLayerBuilder;
Expand Down Expand Up @@ -122,7 +123,18 @@ private static LayerBuilder<?> createLayerBuilder(
OtpServerRequestContext context
) {
return switch (layerParameters.type()) {
case Stop -> new StopsLayerBuilder(context.transitService(), layerParameters, locale);
case Stop -> new StopsLayerBuilder(
context.transitService(),
layerParameters,
locale,
Predicates.NO_FILTER
);
case CurrentServiceWeekStop -> new StopsLayerBuilder(
context.transitService(),
layerParameters,
locale,
Predicates.currentServiceWeek(context.transitService())
);
case Station -> new StationsLayerBuilder(context.transitService(), layerParameters, locale);
case AreaStop -> new AreaStopsLayerBuilder(context.transitService(), layerParameters, locale);
case VehicleRental -> new VehicleRentalPlacesLayerBuilder(
Expand Down Expand Up @@ -154,6 +166,7 @@ private static LayerBuilder<?> createLayerBuilder(

public enum LayerType {
Stop,
CurrentServiceWeekStop,
Station,
AreaStop,
VehicleRental,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.opentripplanner.ext.vectortiles.layers.stops;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.function.Predicate;
import org.opentripplanner.apis.gtfs.PatternByServiceDatesFilter;
import org.opentripplanner.apis.gtfs.model.LocalDateRange;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.service.TransitService;

public class Predicates {

public static final Predicate<RegularStop> NO_FILTER = x -> true;

public static Predicate<RegularStop> currentServiceWeek(TransitService transitService) {
var serviceDate = LocalDate.now(transitService.getTimeZone());
var lastSunday = serviceDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
var nextSunday = serviceDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).plusDays(1);
var filter = new PatternByServiceDatesFilter(
new LocalDateRange(lastSunday, nextSunday),
transitService
);

return regularStop -> {
var patterns = transitService.getPatternsForStop(regularStop);
var patternsInCurrentWeek = filter.filterPatterns(patterns);
return !patternsInCurrentWeek.isEmpty();
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.function.Predicate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.opentripplanner.apis.support.mapping.PropertyMapper;
import org.opentripplanner.ext.vectortiles.VectorTilesResource;
import org.opentripplanner.inspector.vector.LayerBuilder;
import org.opentripplanner.inspector.vector.LayerParameters;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.service.TransitService;

public class StopsLayerBuilder<T> extends LayerBuilder<T> {
public class StopsLayerBuilder extends LayerBuilder<RegularStop> {

static Map<MapperType, BiFunction<TransitService, Locale, PropertyMapper<RegularStop>>> mappers = Map.of(
MapperType.Digitransit,
DigitransitStopPropertyMapper::create
);
private final TransitService transitService;
private final Predicate<RegularStop> filter;

public StopsLayerBuilder(
TransitService transitService,
LayerParameters<VectorTilesResource.LayerType> layerParameters,
Locale locale
Locale locale,
Predicate<RegularStop> filter
) {
super(
(PropertyMapper<T>) Map
Map
.ofEntries(
entry(MapperType.Digitransit, new DigitransitStopPropertyMapper(transitService, locale)),
entry(
Expand All @@ -43,20 +39,22 @@ public StopsLayerBuilder(
layerParameters.expansionFactor()
);
this.transitService = transitService;
this.filter = filter;
}

protected List<Geometry> getGeometries(Envelope query) {
return transitService
.findRegularStops(query)
.stream()
.filter(filter)
.map(stop -> {
Geometry point = stop.getGeometry();

point.setUserData(stop);

return point;
})
.collect(Collectors.toList());
.toList();
}

enum MapperType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public PatternByServiceDatesFilter(
);
}

public PatternByServiceDatesFilter(LocalDateRange range, TransitService transitService) {
this(
range,
transitService::getPatternsForRoute,
trip -> transitService.getCalendarService().getServiceDatesForServiceId(trip.getServiceId())
);
}

/**
* Filter the patterns by the service dates that it operates on.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import java.util.Optional;
import javax.annotation.Nullable;
import org.opentripplanner.ext.vectortiles.VectorTilesResource;
import org.opentripplanner.ext.vectortiles.VectorTilesResource.LayerType;
import org.opentripplanner.inspector.vector.LayerParameters;
import org.opentripplanner.standalone.config.framework.json.NodeAdapter;

public class VectorTileConfig
implements VectorTilesResource.LayersParameters<VectorTilesResource.LayerType> {
public class VectorTileConfig implements VectorTilesResource.LayersParameters<LayerType> {

public static final VectorTileConfig DEFAULT = new VectorTileConfig(List.of(), null, null);
private final List<LayerParameters<VectorTilesResource.LayerType>> layers;
private final List<LayerParameters<LayerType>> layers;

@Nullable
private final String basePath;
Expand All @@ -28,7 +28,7 @@ public class VectorTileConfig
private final String attribution;

VectorTileConfig(
Collection<? extends LayerParameters<VectorTilesResource.LayerType>> layers,
Collection<? extends LayerParameters<LayerType>> layers,
@Nullable String basePath,
@Nullable String attribution
) {
Expand All @@ -38,7 +38,7 @@ public class VectorTileConfig
}

@Override
public List<LayerParameters<VectorTilesResource.LayerType>> layers() {
public List<LayerParameters<LayerType>> layers() {
return layers;
}

Expand Down

0 comments on commit 5f3cd3a

Please sign in to comment.