Skip to content

Commit

Permalink
Implement maxDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Mar 5, 2025
1 parent 530d808 commit 038b859
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import static org.opentripplanner.transit.model.basic.TransitMode.BUS;
import static org.opentripplanner.transit.model.basic.TransitMode.FERRY;

import de.vdv.ojp20.IndividualTransportOptionStructure;
import de.vdv.ojp20.ItModesStructure;
import de.vdv.ojp20.LineDirectionFilterStructure;
import de.vdv.ojp20.ModeFilterStructure;
import de.vdv.ojp20.OJPStopEventRequestStructure;
import de.vdv.ojp20.PersonalModesEnumeration;
import de.vdv.ojp20.PlaceContextStructure;
import de.vdv.ojp20.StopEventParamStructure;
import de.vdv.ojp20.siri.LineDirectionStructure;
import de.vdv.ojp20.siri.LineRefStructure;
import de.vdv.ojp20.siri.VehicleModesOfTransportEnumeration;
import java.math.BigInteger;
import java.time.ZonedDateTime;
import java.util.Set;
import org.junit.jupiter.api.Test;
Expand All @@ -29,7 +33,6 @@ class OjpServiceTest {
private static final OjpService SERVICE = new OjpService(null, ID_RESOLVER, ZoneIds.BERLIN);

private static final FeedScopedId LINE_ID = id("line1");
private static final FeedScopedId AGENCY_ID = id("line1");

@Test
void defaultCase() {
Expand All @@ -40,6 +43,35 @@ void defaultCase() {
assertThat(params.excludedAgencies()).isEmpty();
assertThat(params.includedModes()).isEmpty();
assertThat(params.excludedModes()).isEmpty();
assertEquals(OjpService.DEFAULT_RADIUS_METERS, params.maximumWalkDistance());
assertEquals(OjpService.DEFAULT_NUM_DEPARTURES, params.numDepartures());
}

@Test
void maxDistance() {
var params = SERVICE.extractStopEventParams(
new OJPStopEventRequestStructure()
.withLocation(
new PlaceContextStructure()
.withDepArrTime(new XmlDateTime(ZDT))
.withIndividualTransportOption(
new IndividualTransportOptionStructure()
.withItModeAndModeOfOperation(
new ItModesStructure().withPersonalMode(PersonalModesEnumeration.FOOT)
)
.withMaxDistance(BigInteger.TEN)
)
)
);
assertEquals(10, params.maximumWalkDistance());
}

@Test
void numDepartures() {
var params = SERVICE.extractStopEventParams(
stopEvent(new StopEventParamStructure().withNumberOfResults(BigInteger.TWO))
);
assertEquals(2, params.numDepartures());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<siri:StopPointRef>8507000</siri:StopPointRef>
</ojp:PlaceRef>
<ojp:DepArrTime>2025-02-13T13:03:36</ojp:DepArrTime>
<ojp:IndividualTransportOption>
<ojp:ItModeAndModeOfOperation>
<ojp:PersonalMode>foot</ojp:PersonalMode>
</ojp:ItModeAndModeOfOperation>
<ojp:MaxDistance>999</ojp:MaxDistance>
</ojp:IndividualTransportOption>
</ojp:Location>
<ojp:Params>
<ojp:ModeFilter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<StopPointRef>8507000</StopPointRef>
</LocationRef>
<DepArrTime>2025-02-13T13:03:36</DepArrTime>
<IndividualTransportOptions>
<Mode>walk</Mode>
<MaxDistance>999</MaxDistance>
</IndividualTransportOptions>
</Location>
<Params>
<PtModeFilter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public List<CallAtStop> findTripTimesOnDate(
StopEventRequestParams params
) {
var calls = finder
.findClosestStops(coordinate.asJtsCoordinate(), 1000)
.findClosestStops(coordinate.asJtsCoordinate(), params.maximumWalkDistance)
.stream()
.flatMap(nearbyStop ->
this.findTripTimesOnDate(nearbyStop.stop, params)
Expand Down Expand Up @@ -108,6 +108,7 @@ public record StopEventRequestParams(
Instant time,
ArrivalDeparture arrivalDeparture,
Duration timeWindow,
int maximumWalkDistance,
int numDepartures,
Set<FeedScopedId> includedAgencies,
Set<FeedScopedId> includedRoutes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import de.vdv.ojp20.OJP;
import de.vdv.ojp20.OJPStopEventRequestStructure;
import de.vdv.ojp20.OperatorFilterStructure;
import de.vdv.ojp20.PersonalModesEnumeration;
import de.vdv.ojp20.PlaceContextStructure;
import de.vdv.ojp20.PlaceRefStructure;
import de.vdv.ojp20.StopEventParamStructure;
import de.vdv.ojp20.StopEventTypeEnumeration;
import de.vdv.ojp20.UseRealtimeDataEnumeration;
import de.vdv.ojp20.siri.StopPointRefStructure;
import java.math.BigInteger;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand All @@ -37,6 +39,8 @@

public class OjpService {

public static final int DEFAULT_RADIUS_METERS = 1000;
public static final int DEFAULT_NUM_DEPARTURES = 1;
private static final Duration DEFAULT_TIME_WINDOW = Duration.ofHours(2);
private final VdvService vdvService;
private final IdResolver idResolver;
Expand Down Expand Up @@ -76,7 +80,10 @@ protected VdvService.StopEventRequestParams extractStopEventParams(
var time = Optional.ofNullable(ser.getLocation().getDepArrTime().atZone(zoneId)).orElse(
ZonedDateTime.now(zoneId)
);
int numResults = params(ser).map(s -> s.getNumberOfResults()).map(i -> i.intValue()).orElse(1);
int numResults = params(ser)
.map(s -> s.getNumberOfResults())
.map(i -> i.intValue())
.orElse(DEFAULT_NUM_DEPARTURES);

var arrivalDeparture = arrivalDeparture(ser);
var timeWindow = timeWindow(ser);
Expand All @@ -86,11 +93,25 @@ protected VdvService.StopEventRequestParams extractStopEventParams(
Set<FeedScopedId> excludedRoutes = lineFilter(ser, f -> isExclude(f.isExclude()));
Set<TransitMode> includedModes = modeFilter(ser, m -> !isExclude(m.isExclude()));
Set<TransitMode> excludedModes = modeFilter(ser, m -> isExclude(m.isExclude()));
int maxWalkDistance = Optional.ofNullable(ser.getLocation())
.flatMap(l ->
l
.getIndividualTransportOption()
.stream()
.filter(
o -> o.getItModeAndModeOfOperation().getPersonalMode() == PersonalModesEnumeration.FOOT
)
.findFirst()
.flatMap(o -> Optional.ofNullable(o.getMaxDistance()))
)
.map(BigInteger::intValue)
.orElse(DEFAULT_RADIUS_METERS);

return new VdvService.StopEventRequestParams(
time.toInstant(),
arrivalDeparture,
timeWindow,
maxWalkDistance,
numResults,
selectedAgencies,
selectedRoutes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
<xsl:value-of select="//trias:DepArrTime"/>
</xsl:element>
</xsl:if>
<xsl:if test="//trias:IndividualTransportOptions">
<IndividualTransportOption>
<ItModeAndModeOfOperation>
<PersonalMode>foot</PersonalMode>
</ItModeAndModeOfOperation>
<MaxDistance>
<xsl:value-of select="//trias:MaxDistance"/>
</MaxDistance>
</IndividualTransportOption>
</xsl:if>
</Location>
<Params>
<xsl:if test="//trias:NumberOfResults">
Expand Down

0 comments on commit 038b859

Please sign in to comment.