Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nav 104 simplify reduce return value for iso line rest api endpoint #75

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/main/java/ch/naviqore/app/controller/RoutingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ch.naviqore.app.dto.Connection;
import ch.naviqore.app.dto.DtoMapper;
import ch.naviqore.app.dto.EarliestArrival;
import ch.naviqore.app.dto.StopConnection;
import ch.naviqore.app.dto.TimeType;
import ch.naviqore.service.PublicTransitService;
import ch.naviqore.service.Stop;
Expand Down Expand Up @@ -129,15 +129,16 @@ public List<Connection> getConnections(@RequestParam(required = false) String so
}

@GetMapping("/isolines")
public List<EarliestArrival> getIsolines(@RequestParam(required = false) String sourceStopId,
@RequestParam(required = false, defaultValue = "-91") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-181") double sourceLongitude,
@RequestParam(required = false) LocalDateTime dateTime,
@RequestParam(required = false, defaultValue = "DEPARTURE") TimeType timeType,
@RequestParam(required = false, defaultValue = "2147483647") int maxWalkingDuration,
@RequestParam(required = false, defaultValue = "2147483647") int maxTransferNumber,
@RequestParam(required = false, defaultValue = "2147483647") int maxTravelTime,
@RequestParam(required = false, defaultValue = "0") int minTransferTime) {
public List<StopConnection> getIsolines(@RequestParam(required = false) String sourceStopId,
@RequestParam(required = false, defaultValue = "-91") double sourceLatitude,
@RequestParam(required = false, defaultValue = "-181") double sourceLongitude,
@RequestParam(required = false) LocalDateTime dateTime,
@RequestParam(required = false, defaultValue = "DEPARTURE") TimeType timeType,
@RequestParam(required = false, defaultValue = "2147483647") int maxWalkingDuration,
@RequestParam(required = false, defaultValue = "2147483647") int maxTransferNumber,
@RequestParam(required = false, defaultValue = "2147483647") int maxTravelTime,
@RequestParam(required = false, defaultValue = "0") int minTransferTime,
@RequestParam(required = false, defaultValue = "false") boolean returnConnections) {

GeoCoordinate sourceCoordinate = null;
if (sourceStopId == null) {
Expand All @@ -163,12 +164,12 @@ public List<EarliestArrival> getIsolines(@RequestParam(required = false) String
connections = service.getIsoLines(sourceCoordinate, dateTime, map(timeType), config);
}

List<EarliestArrival> arrivals = new ArrayList<>();
List<StopConnection> arrivals = new ArrayList<>();

for (Map.Entry<Stop, ch.naviqore.service.Connection> entry : connections.entrySet()) {
Stop stop = entry.getKey();
ch.naviqore.service.Connection connection = entry.getValue();
arrivals.add(map(stop, connection));
arrivals.add(new StopConnection(stop, connection, timeType, returnConnections));
}

return arrivals;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/ch/naviqore/app/dto/DtoMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public static Connection map(ch.naviqore.service.Connection connection) {
return new Connection(legs);
}

public static EarliestArrival map(ch.naviqore.service.Stop stop, ch.naviqore.service.Connection connection) {
return new EarliestArrival(map(stop), connection.getArrivalTime(), map(connection));
}

private static class LegVisitorImpl implements LegVisitor<Leg> {
@Override
public Leg visit(PublicTransitLeg publicTransitLeg) {
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/ch/naviqore/app/dto/EarliestArrival.java

This file was deleted.

119 changes: 119 additions & 0 deletions src/main/java/ch/naviqore/app/dto/StopConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package ch.naviqore.app.dto;

import lombok.*;

import java.time.LocalDateTime;
import java.util.List;

/**
* This class represents a connection between a stop and a spawn source (iso-line) in a transportation network. It
* contains information about the stop, the leg closest to the target stop, and the connection itself.
*/
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@EqualsAndHashCode
@ToString
@Getter
public class StopConnection {

private final Stop stop;
private Leg connectingLeg;
private Connection connection;

/**
* Constructs a new StopConnection object.
*
* @param serviceStop The stop from the service.
* @param serviceConnection The connection from the service.
* @param timeType The type of time (DEPARTURE or ARRIVAL), needed to construct the connecting leg.
* @param returnConnections A boolean indicating whether to return connections and trip stop times.
*/
public StopConnection(ch.naviqore.service.Stop serviceStop, ch.naviqore.service.Connection serviceConnection,
TimeType timeType, boolean returnConnections) {

this.stop = DtoMapper.map(serviceStop);
this.connection = DtoMapper.map(serviceConnection);
if (timeType == TimeType.DEPARTURE) {
prepareDepartureConnectingLeg();
} else {
prepareArrivalConnectingLeg();
}
if (!returnConnections) {
reduceData();
}
}

/**
* Finds the index of a stop time in a trip for a given stop and time.
*
* @param trip The trip to search in.
* @param stop The stop to find.
* @param time The time to match.
* @param timeType The type of time to match (DEPARTURE or ARRIVAL).
* @return The index of the stop time in the trip.
*/
private static int findStopTimeIndexInTrip(Trip trip, Stop stop, LocalDateTime time, TimeType timeType) {
List<StopTime> stopTimes = trip.getStopTimes();
for (int i = 0; i < stopTimes.size(); i++) {
StopTime stopTime = stopTimes.get(i);
if (stopTime.getStop().equals(stop)) {
if (timeType == TimeType.DEPARTURE && stopTime.getDepartureTime().equals(time)) {
return i;
} else if (timeType == TimeType.ARRIVAL && stopTime.getArrivalTime().equals(time)) {
return i;
}
}
}
throw new IllegalStateException("Stop time not found in trip.");
}

/**
* Prepares the connecting leg for a departure connection (i.e. builds a leg from the second last to the last stop
* in the connection).
*/
private void prepareDepartureConnectingLeg() {
connectingLeg = this.connection.getLegs().getLast();
if (connectingLeg.getTrip() == null) {
return;
}
int stopTimeIndex = findStopTimeIndexInTrip(connectingLeg.getTrip(), connectingLeg.getToStop(),
connectingLeg.getArrivalTime(), TimeType.ARRIVAL);
StopTime sourceStopTime = connectingLeg.getTrip().getStopTimes().get(stopTimeIndex - 1);
connectingLeg = new Leg(connectingLeg.getType(), sourceStopTime.getStop().getCoordinates(),
connectingLeg.getTo(), sourceStopTime.getStop(), connectingLeg.getToStop(),
sourceStopTime.getDepartureTime(), connectingLeg.getArrivalTime(), connectingLeg.getTrip());
}

/**
* Prepares the connecting leg for an arrival connection (i.e. builds a leg from the first to the second stop in the
* connection).
*/
private void prepareArrivalConnectingLeg() {
connectingLeg = this.connection.getLegs().getFirst();
if (connectingLeg.getTrip() == null) {
return;
}
int stopTimeIndex = findStopTimeIndexInTrip(connectingLeg.getTrip(), connectingLeg.getFromStop(),
connectingLeg.getDepartureTime(), TimeType.DEPARTURE);
StopTime targetStopTime = connectingLeg.getTrip().getStopTimes().get(stopTimeIndex + 1);
connectingLeg = new Leg(connectingLeg.getType(), connectingLeg.getFrom(),
targetStopTime.getStop().getCoordinates(), connectingLeg.getFromStop(), targetStopTime.getStop(),
connectingLeg.getDepartureTime(), targetStopTime.getArrivalTime(), connectingLeg.getTrip());
}

/**
* Reduces the data of the StopConnection object by setting the connection to null and nullifying the stop times in
* the trip of the connecting leg.
*/
private void reduceData() {
connection = null;
if (connectingLeg.getTrip() == null) {
return;
}
Trip reducedTrip = new Trip(connectingLeg.getTrip().getHeadSign(), connectingLeg.getTrip().getRoute(), null);
connectingLeg = new Leg(connectingLeg.getType(), connectingLeg.getFrom(), connectingLeg.getTo(),
connectingLeg.getFromStop(), connectingLeg.getToStop(), connectingLeg.getDepartureTime(),
connectingLeg.getArrivalTime(), reducedTrip);
}

}

1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ logging.level.root=${LOG_LEVEL:INFO}
# value is a file path, the GTFS is loaded from the file and the update interval is ignored. Examples:
# - gtfs.static.uri=benchmark/input/switzerland.zip
# - gtfs.static.uri=https://opentransportdata.swiss/en/dataset/timetable-2024-gtfs2020/permalink
# - gtfs.static.uri=https://connolly.ch/zuerich-trams.zip
gtfs.static.uri=${GTFS_STATIC_URI:src/test/resources/ch/naviqore/gtfs/schedule/sample-feed-1.zip}
# Cron expression for updating the static GTFS feed from the provided URL. Public transit agencies update their static
# GTFS data regularly. Set this interval to match the agency's publish schedule. Default is to update the schedule
Expand Down
15 changes: 9 additions & 6 deletions src/main/resources/ch.naviqore.app/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ paths:
schema:
type: integer
description: The minimum transfer time between trips in seconds. Defaults to `0`.
- name: returnConnections
in: query
schema:
type: boolean
description: Whether to return the connections for each reachable stop, else the connection field will be null. Defaults to `false`.
responses:
'200':
description: A list of stop and fastest connection pairs for each reachable stop.
Expand All @@ -280,7 +285,7 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/EarliestArrival'
$ref: '#/components/schemas/StopConnection'
'400':
description: Invalid input parameters
'404':
Expand Down Expand Up @@ -346,14 +351,13 @@ components:
format: date-time
trip:
$ref: '#/components/schemas/Trip'
EarliestArrival:
StopConnection:
type: object
properties:
stop:
$ref: '#/components/schemas/Stop'
arrivalTime:
type: string
format: date-time
connectingLeg:
$ref: '#/components/schemas/Leg'
connection:
$ref: '#/components/schemas/Connection'
Coordinate:
Expand Down Expand Up @@ -400,7 +404,6 @@ components:
- STARTS_WITH
- CONTAINS
- ENDS_WITH
- FUZZY
TIME_TYPE:
type: string
enum:
Expand Down
Loading