Skip to content

Commit

Permalink
Merge pull request #19 from SchweizerischeBundesbahnen/fix/gtfs-format
Browse files Browse the repository at this point in the history
Fix/gtfs format
  • Loading branch information
munterfi authored Dec 3, 2024
2 parents 8eb9e9f + 3a87cee commit 32b70f4
Show file tree
Hide file tree
Showing 13 changed files with 572 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import ch.sbb.pfi.netzgrafikeditor.converter.util.time.ServiceDayTime;

import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -29,29 +28,15 @@

public class GtfsSupplyBuilder extends BaseSupplyBuilder<GtfsSchedule> {

public static final int ROUTE_TYPE = 0;
private static final Agency AGENCY = Agency.builder()
.agencyId("nge")
.agencyName("Netzgrafik Editor")
.agencyTimeZone("UTC")
.agencyUrl("https://github.com/SchweizerischeBundesbahnen/netzgrafik-editor-frontend")
.build();
private static final Calendar CALENDAR = Calendar.builder()
.serviceId("always")
.monday(Calendar.Type.AVAILABLE)
.tuesday(Calendar.Type.AVAILABLE)
.wednesday(Calendar.Type.AVAILABLE)
.thursday(Calendar.Type.AVAILABLE)
.friday(Calendar.Type.AVAILABLE)
.startDate(LocalDate.MAX)
.endDate(LocalDate.MAX)
.build();
public static final int ROUTE_TYPE = 2; // rail

private final List<Stop> stops = new ArrayList<>();
private final List<Route> routes = new ArrayList<>();
private final List<Trip> trips = new ArrayList<>();
private final List<StopTime> stopTimes = new ArrayList<>();

private final Set<String> createdRoutes = new HashSet<>();
private final Map<String, Integer> tripCounts = new HashMap<>();
private final Map<String, List<RouteElement>> routeElements = new HashMap<>();

public GtfsSupplyBuilder(InfrastructureRepository infrastructureRepository, VehicleCircuitsPlanner vehicleCircuitsPlanner) {
Expand All @@ -74,36 +59,47 @@ protected void buildTransitRoute(TransitRouteContainer transitRouteContainer) {
// store route elements for stop time creation
routeElements.put(transitRouteContainer.transitRouteInfo().getId(), transitRouteContainer.routeElements());

// build transit route names
String routeShortName = String.format("%s - %s",
transitRouteContainer.routeElements().getFirst().getStopFacilityInfo().getId(),
transitRouteContainer.routeElements().getLast().getStopFacilityInfo().getId());
String routeLongName = String.format("%s: %s",
transitRouteContainer.transitRouteInfo().getTransitLineInfo().getCategory(), routeShortName);

// create and add GTFS route (transit line in the context of the supply builder) if not yet added
String routeId = transitRouteContainer.transitRouteInfo().getTransitLineInfo().getId();
if (!createdRoutes.contains(routeId)) {
routes.add(Route.builder()
.routeId(routeId)
.agencyId(AGENCY.getAgencyId())
.routeLongName(routeId)
.routeShortName(routeId)
.agencyId(Agency.DEFAULT_ID)
.routeLongName(routeLongName)
.routeShortName(routeShortName)
.routeType(ROUTE_TYPE)
.build());
createdRoutes.add(routeId);
}

// create and add trip
trips.add(Trip.builder()
.routeId(routeId)
.serviceId(CALENDAR.getServiceId())
.tripId(transitRouteContainer.transitRouteInfo().getId())
.tripHeadsign(transitRouteContainer.routeElements().getLast().getStopFacilityInfo().getId())
.build());
}

@Override
protected void buildDeparture(VehicleAllocation vehicleAllocation) {

String tripId = vehicleAllocation.getDepartureInfo().getTransitRouteInfo().getId();
String routeId = vehicleAllocation.getDepartureInfo().getTransitRouteInfo().getId();
String tripId = String.format("%s_%d", routeId, tripCounts.merge(routeId, 1, Integer::sum));
List<RouteElement> currentRouteElements = routeElements.get(routeId);

// create and add trip
trips.add(Trip.builder()
.routeId(vehicleAllocation.getDepartureInfo().getTransitRouteInfo().getTransitLineInfo().getId())
.serviceId(Calendar.DEFAULT_ID)
.tripId(tripId)
.tripHeadsign(currentRouteElements.getLast().getStopFacilityInfo().getId())
.build());

// create and add stop times: gtfs stop time sequence starts with 1 not 0
final int[] count = {1};
final ServiceDayTime[] time = {vehicleAllocation.getDepartureInfo().getTime()};
final int[] count = {0};

for (RouteElement routeElement : routeElements.get(tripId)) {
for (RouteElement routeElement : currentRouteElements) {
routeElement.accept(new RouteElementVisitor() {

@Override
Expand All @@ -112,7 +108,7 @@ public void visit(RouteStop routeStop) {
Duration dwellTime = routeStop.getDwellTime();

// set time to arrival time if at start of stop time sequence
if (count[0] == 0) {
if (count[0] == 1) {
time[0] = time[0].minus(dwellTime);
}

Expand Down Expand Up @@ -142,13 +138,6 @@ public void visit(RoutePass routePass) {

@Override
protected GtfsSchedule getResult() {
return GtfsSchedule.builder()
.agencies(List.of(AGENCY))
.stops(stops)
.routes(routes)
.trips(trips)
.stopTimes(stopTimes)
.calendars(List.of(CALENDAR))
.build();
return GtfsSchedule.builder().stops(stops).routes(routes).trips(trips).stopTimes(stopTimes).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
@Builder
public class Agency {

String agencyId;
public static final String DEFAULT_ID = "nge";

String agencyName;
@Builder.Default
String agencyId = DEFAULT_ID;

String agencyUrl;
@Builder.Default
String agencyName = "Netzgrafik Editor";

String agencyTimeZone;
@Builder.Default
String agencyUrl = "https://github.com/SchweizerischeBundesbahnen/netzgrafik-editor-frontend";

@Builder.Default
String agencyTimezone = "UTC";

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,44 @@

import java.time.LocalDate;

import static ch.sbb.pfi.netzgrafikeditor.converter.adapter.gtfs.model.FeedInfo.DEFAULT_END_DATE;
import static ch.sbb.pfi.netzgrafikeditor.converter.adapter.gtfs.model.FeedInfo.DEFAULT_START_DATE;

@Value
@Builder
public class Calendar {

String serviceId;
public static final String DEFAULT_ID = "always";

@Builder.Default
String serviceId = DEFAULT_ID;

Type monday;
@Builder.Default
Type monday = Type.AVAILABLE;

Type tuesday;
@Builder.Default
Type tuesday = Type.AVAILABLE;

Type wednesday;
@Builder.Default
Type wednesday = Type.AVAILABLE;

Type thursday;
@Builder.Default
Type thursday = Type.AVAILABLE;

Type friday;
@Builder.Default
Type friday = Type.AVAILABLE;

Type saturday;
@Builder.Default
Type saturday = Type.AVAILABLE;

Type sunday;
@Builder.Default
Type sunday = Type.AVAILABLE;

LocalDate startDate;
@Builder.Default
LocalDate startDate = DEFAULT_START_DATE;

LocalDate endDate;
@Builder.Default
LocalDate endDate = DEFAULT_END_DATE;

public enum Type {
AVAILABLE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ch.sbb.pfi.netzgrafikeditor.converter.adapter.gtfs.model;

import lombok.Builder;
import lombok.Value;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Value
@Builder
public class FeedInfo {

public static final LocalDate DEFAULT_START_DATE = LocalDate.of(1970, 1, 1);
public static final LocalDate DEFAULT_END_DATE = LocalDate.of(2099, 12, 31);

@Builder.Default
String feedPublisherName = "Netzgrafik Editor Converter";

@Builder.Default
String feedPublisherUrl = "https://github.com/SchweizerischeBundesbahnen/netzgrafik-editor-converter";

@Builder.Default
String feedLang = "en";

@Builder.Default
LocalDate feedStartDate = DEFAULT_START_DATE;

@Builder.Default
LocalDate feedEndDate = DEFAULT_END_DATE;

@Builder.Default
LocalDateTime feedVersion = LocalDateTime.now();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
@Builder
public class GtfsSchedule {

List<Agency> agencies;
@Builder.Default
FeedInfo feedInfo = FeedInfo.builder().build();

@Builder.Default
List<Agency> agencies = List.of(Agency.builder().build());

List<Stop> stops;

Expand All @@ -19,6 +23,7 @@ public class GtfsSchedule {

List<StopTime> stopTimes;

List<Calendar> calendars;
@Builder.Default
List<Calendar> calendars = List.of(Calendar.builder().build());

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void convert(Path networkGraphicFile, Path outputDirectory, NetworkGraphi
case GTFS -> {
SupplyBuilder<GtfsSchedule> builder = new GtfsSupplyBuilder(new NoInfrastructureRepository(),
new NoVehicleCircuitsPlanner(new NoRollingStockRepository()));
ConverterSink<GtfsSchedule> sink = new GtfsScheduleWriter(outputDirectory);
ConverterSink<GtfsSchedule> sink = new GtfsScheduleWriter(outputDirectory, true);

yield new NetworkGraphicConverter<>(config, source, builder, sink);
}
Expand Down
Loading

0 comments on commit 32b70f4

Please sign in to comment.