Skip to content

Commit

Permalink
Merge branch 'via_coordinate' into test_via_perfomance
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Feb 26, 2025
2 parents f3139b9 + 43e6882 commit c7fe1dd
Show file tree
Hide file tree
Showing 101 changed files with 2,028 additions and 1,137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.opentripplanner.ext.restapi.model.ElevationMetadata;
import org.opentripplanner.ext.restapi.model.TripPlannerResponse;
import org.opentripplanner.framework.application.OTPRequestTimeoutException;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.response.RoutingResponse;
import org.opentripplanner.routing.error.RoutingValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -62,14 +60,12 @@ public Response plan(@Context UriInfo uriInfo, @Context Request grizzlyRequest)

// Create response object, containing a copy of all request parameters. Maybe they should be in the debug section of the response.
TripPlannerResponse response = new TripPlannerResponse(uriInfo);
RouteRequest request = null;
RoutingResponse res = null;
try {
/* Fill in request fields from query parameters via shared superclass method, catching any errors. */
request = super.buildRequest(uriInfo.getQueryParameters());
var request = super.buildRequest(uriInfo.getQueryParameters());

// Route
res = serverContext.routingService().route(request);
var res = serverContext.routingService().route(request);

// Map to API
// TODO VIA (Leonard) - we should store the default showIntermediateStops somewhere
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
import org.opentripplanner.apis.transmodel.model.framework.CoordinateInputType;
import org.opentripplanner.apis.transmodel.model.plan.TripQuery;
import org.opentripplanner.apis.transmodel.model.plan.ViaLocationInputType;
import org.opentripplanner.apis.transmodel.support.OneOfInputValidator;
import org.opentripplanner.framework.geometry.WgsCoordinate;
import org.opentripplanner.routing.api.request.via.PassThroughViaLocation;
import org.opentripplanner.routing.api.request.via.ViaLocation;
import org.opentripplanner.routing.api.request.via.VisitViaLocation;
import org.opentripplanner.transit.model.framework.FeedScopedId;

@SuppressWarnings("unchecked")
class TripViaLocationMapper {

static List<ViaLocation> mapToViaLocations(final List<Map<String, Object>> via) {
Expand Down Expand Up @@ -57,7 +60,8 @@ private static VisitViaLocation mapVisitViaLocation(Map<String, Object> inputMap
var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL);
var minimumWaitTime = (Duration) inputMap.get(ViaLocationInputType.FIELD_MINIMUM_WAIT_TIME);
var stopLocationIds = mapStopLocationIds(inputMap);
return new VisitViaLocation(label, minimumWaitTime, stopLocationIds, List.of());
var coordinate = mapCoordinate(inputMap);
return new VisitViaLocation(label, minimumWaitTime, stopLocationIds, coordinate);
}

private static PassThroughViaLocation mapPassThroughViaLocation(Map<String, Object> inputMap) {
Expand All @@ -68,14 +72,14 @@ private static PassThroughViaLocation mapPassThroughViaLocation(Map<String, Obje

private static List<FeedScopedId> mapStopLocationIds(Map<String, Object> map) {
var c = (Collection<String>) map.get(ViaLocationInputType.FIELD_STOP_LOCATION_IDS);
return c == null ? List.of() : c.stream().map(TransitIdMapper::mapIDToDomain).toList();
}

// When coordinates are added, we need to accept null here...
if (c == null) {
throw new IllegalArgumentException(
"'" + ViaLocationInputType.FIELD_STOP_LOCATION_IDS + "' is not set!"
);
}
return c.stream().map(TransitIdMapper::mapIDToDomain).toList();
private static List<WgsCoordinate> mapCoordinate(Map<String, Object> map) {
return CoordinateInputType
.mapToWsgCoordinate(ViaLocationInputType.FIELD_COORDINATE, map)
.map(List::of)
.orElseGet(List::of);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,58 @@
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLNonNull;
import java.util.Map;
import java.util.Optional;
import org.opentripplanner.framework.geometry.WgsCoordinate;

@SuppressWarnings("unchecked")
public class CoordinateInputType {

/* Constants are package local to be used in unit-tests */
static final String LATITUDE = "latitude";
static final String LONGITUDE = "longitude";

public static final GraphQLInputObjectType INPUT_TYPE = GraphQLInputObjectType
.newInputObject()
.name("InputCoordinates")
.description("Input type for coordinates in the WGS84 system")
.field(
GraphQLInputObjectField
.newInputObjectField()
.name("latitude")
.name(LATITUDE)
.description("The latitude of the place.")
.type(new GraphQLNonNull(Scalars.GraphQLFloat))
.build()
)
.field(
GraphQLInputObjectField
.newInputObjectField()
.name("longitude")
.name(LONGITUDE)
.description("The longitude of the place.")
.type(new GraphQLNonNull(Scalars.GraphQLFloat))
.build()
)
.build();

public static Optional<WgsCoordinate> mapToWsgCoordinate(
String fieldName,
Map<String, Object> input
) {
Map<String, Object> coordinate = (Map<String, Object>) input.get(fieldName);

if (coordinate == null) {
return Optional.empty();
}

return Optional.of(
new WgsCoordinate((Double) coordinate.get(LATITUDE), (Double) coordinate.get(LONGITUDE))
);
}

public static Map<String, Object> mapForTest(WgsCoordinate coordinate) {
return Map.ofEntries(
Map.entry(LATITUDE, coordinate.latitude()),
Map.entry(LONGITUDE, coordinate.longitude())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import java.time.Duration;
import org.opentripplanner.apis.transmodel.model.framework.CoordinateInputType;
import org.opentripplanner.apis.transmodel.model.framework.TransmodelScalars;

public class ViaLocationInputType {
Expand All @@ -25,8 +26,6 @@ public class ViaLocationInputType {
on-board visit does not count, the traveler must alight or board at the given stop for it to to
be accepted. To visit a coordinate, the traveler must walk(bike or drive) to the closest point
in the street network from a stop and back to another stop to join the transit network.
NOTE! Coordinates are NOT supported yet.
""";
private static final String DOC_PASS_THROUGH_VIA_LOCATION =
"""
Expand All @@ -44,9 +43,8 @@ be accepted. To visit a coordinate, the traveler must walk(bike or drive) to the
public static final String FIELD_LABEL = "label";
public static final String FIELD_MINIMUM_WAIT_TIME = "minimumWaitTime";
public static final String FIELD_STOP_LOCATION_IDS = "stopLocationIds";
public static final String FIELD_COORDINATE = "coordinate";

// TODO : Add coordinates
//private static final String FIELD_COORDINATES = "coordinates";
public static final String FIELD_VISIT = "visit";
public static final String DOC_FIELD_VISIT =
"Board or alight at a stop location or visit a coordinate.";
Expand All @@ -68,6 +66,7 @@ be accepted. To visit a coordinate, the traveler must walk(bike or drive) to the
stop place or a group of stop places. It is enough to visit ONE of the locations
listed.
""";
private static final String DOC_COORDINATE = "A coordinate to route through.";

static final GraphQLInputObjectType VISIT_VIA_LOCATION_INPUT = GraphQLInputObjectType
.newInputObject()
Expand All @@ -85,11 +84,11 @@ be accepted. To visit a coordinate, the traveler must walk(bike or drive) to the
b
.name(FIELD_STOP_LOCATION_IDS)
.description(DOC_STOP_LOCATION_IDS)
.type(requiredListOfNonNullStrings())
.type(optionalListOfNonNullStrings())
)
.field(b ->
b.name(FIELD_COORDINATE).description(DOC_COORDINATE).type(CoordinateInputType.INPUT_TYPE)
)
/*
TODO: Add support for coordinates
*/
.build();

static final GraphQLInputObjectType PASS_THROUGH_VIA_LOCATION_INPUT = GraphQLInputObjectType
Expand Down Expand Up @@ -121,6 +120,10 @@ be accepted. To visit a coordinate, the traveler must walk(bike or drive) to the
.build();

private static GraphQLInputType requiredListOfNonNullStrings() {
return new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString)));
return new GraphQLNonNull(optionalListOfNonNullStrings());
}

private static GraphQLInputType optionalListOfNonNullStrings() {
return new GraphQLList(new GraphQLNonNull(GraphQLString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.index.StreetIndex;
import org.opentripplanner.routing.linking.LinkingDirection;
import org.opentripplanner.routing.linking.VertexLinker;
import org.opentripplanner.service.osminfo.OsmInfoGraphBuildService;
import org.opentripplanner.service.osminfo.model.Platform;
Expand All @@ -27,6 +26,7 @@
import org.opentripplanner.street.model.edge.AreaEdge;
import org.opentripplanner.street.model.edge.BoardingLocationToStopLink;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.edge.LinkingDirection;
import org.opentripplanner.street.model.edge.StreetEdge;
import org.opentripplanner.street.model.edge.StreetEdgeBuilder;
import org.opentripplanner.street.model.edge.StreetTransitStopLink;
Expand Down Expand Up @@ -212,7 +212,7 @@ private boolean connectVertexToWay(TransitStopVertex ts, StreetIndex index) {
for (var vertex : linker.linkToSpecificStreetEdgesPermanently(
boardingLocation,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
platformEdgeList.getValue().stream().map(StreetEdge.class::cast).collect(Collectors.toSet())
)) {
linkBoardingLocationToStop(ts, stop.getCode(), vertex);
Expand Down Expand Up @@ -243,7 +243,7 @@ private boolean connectVertexToNode(TransitStopVertex ts, StreetIndex index) {
linker.linkVertexPermanently(
boardingLocation,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(osmBoardingLocationVertex, splitVertex) ->
getConnectingEdges(boardingLocation, osmBoardingLocationVertex, splitVertex)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import org.opentripplanner.graph_builder.issues.ParkAndRideEntranceRemoved;
import org.opentripplanner.graph_builder.model.GraphBuilderModule;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.linking.LinkingDirection;
import org.opentripplanner.service.vehicleparking.VehicleParkingRepository;
import org.opentripplanner.service.vehicleparking.model.VehicleParking;
import org.opentripplanner.service.vehicleparking.model.VehicleParkingHelper;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.edge.LinkingDirection;
import org.opentripplanner.street.model.edge.StreetStationCentroidLink;
import org.opentripplanner.street.model.edge.StreetTransitEntranceLink;
import org.opentripplanner.street.model.edge.StreetTransitStopLink;
Expand Down Expand Up @@ -160,7 +160,7 @@ private void linkStopToStreetNetwork(TransitStopVertex tStop, StopLinkType linkT
.linkVertexPermanently(
tStop,
WALK_ONLY,
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(transitVertex, streetVertex) -> {
var linkEdges = createStopLinkEdges((TransitStopVertex) transitVertex, streetVertex);

Expand All @@ -187,7 +187,7 @@ private void linkToDriveableEdge(TransitStopVertex tStop) {
.linkVertexPermanently(
tStop,
CAR_ONLY,
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(transitVertex, streetVertex) ->
createStopLinkEdges((TransitStopVertex) transitVertex, streetVertex)
);
Expand All @@ -213,7 +213,7 @@ private static void linkVehicleParkingWithLinker(
.linkVertexPermanently(
vehicleParkingVertex,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(vertex, streetVertex) ->
List.of(
StreetVehicleParkingLink.createStreetVehicleParkingLink(
Expand All @@ -234,7 +234,7 @@ private static void linkVehicleParkingWithLinker(
.linkVertexPermanently(
vehicleParkingVertex,
new TraverseModeSet(TraverseMode.CAR),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(vertex, streetVertex) ->
List.of(
StreetVehicleParkingLink.createStreetVehicleParkingLink(
Expand All @@ -258,7 +258,7 @@ private void linkTransitEntrances(Graph graph) {
.linkVertexPermanently(
tEntrance,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
(vertex, streetVertex) ->
List.of(
StreetTransitEntranceLink.createStreetTransitEntranceLink(
Expand Down Expand Up @@ -296,7 +296,7 @@ private void linkStationCentroids(Graph graph) {
.linkVertexPermanently(
station,
new TraverseModeSet(TraverseMode.WALK),
LinkingDirection.BOTH_WAYS,
LinkingDirection.BIDIRECTIONAL,
stationAndStreetVertexLinker
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public StreetLeg(StreetLegBuilder builder) {
this.generalizedCost = builder.getGeneralizedCost();
this.elevationProfile = builder.getElevationProfile();
this.legGeometry = builder.getGeometry();
this.walkSteps = builder.getWalkSteps();
this.walkSteps = Objects.requireNonNull(builder.getWalkSteps());
this.streetNotes = Set.copyOf(builder.getStreetNotes());
this.walkingBike = builder.getWalkingBike();
this.rentedVehicle = builder.getRentedVehicle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.locationtech.jts.geom.LineString;
import org.opentripplanner.street.model.note.StreetNote;
Expand All @@ -19,7 +20,7 @@ public class StreetLegBuilder {
private int generalizedCost;
private LineString geometry;
private ElevationProfile elevationProfile;
private List<WalkStep> walkSteps;
private List<WalkStep> walkSteps = List.of();
private Boolean walkingBike;
private Boolean rentedVehicle;
private String vehicleRentalNetwork;
Expand Down Expand Up @@ -157,7 +158,7 @@ public StreetLegBuilder withElevationProfile(ElevationProfile elevationProfile)
}

public StreetLegBuilder withWalkSteps(List<WalkStep> walkSteps) {
this.walkSteps = walkSteps;
this.walkSteps = Objects.requireNonNull(walkSteps);
return this;
}

Expand Down
Loading

0 comments on commit c7fe1dd

Please sign in to comment.