All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.gtfs.mapping.TripMapper Maven / Gradle / Ivy

The newest version!
package org.opentripplanner.gtfs.mapping;

import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.opentripplanner.framework.collection.MapUtils;
import org.opentripplanner.framework.i18n.I18NString;
import org.opentripplanner.routing.api.request.framework.TimePenalty;
import org.opentripplanner.transit.model.timetable.Trip;

/** Responsible for mapping GTFS TripMapper into the OTP model. */
class TripMapper {

  private final RouteMapper routeMapper;
  private final DirectionMapper directionMapper;
  private final TranslationHelper translationHelper;

  private final Map mappedTrips = new HashMap<>();
  private final Map flexSafeTimePenalties = new HashMap<>();

  TripMapper(
    RouteMapper routeMapper,
    DirectionMapper directionMapper,
    TranslationHelper translationHelper
  ) {
    this.routeMapper = routeMapper;
    this.directionMapper = directionMapper;
    this.translationHelper = translationHelper;
  }

  Collection map(Collection trips) {
    return MapUtils.mapToList(trips, this::map);
  }

  Trip map(org.onebusaway.gtfs.model.Trip orginal) {
    return orginal == null ? null : mappedTrips.computeIfAbsent(orginal, this::doMap);
  }

  Collection getMappedTrips() {
    return mappedTrips.values();
  }

  /**
   * The map of flex duration factors per flex trip.
   */
  Map flexSafeTimePenalties() {
    return flexSafeTimePenalties;
  }

  private Trip doMap(org.onebusaway.gtfs.model.Trip rhs) {
    var lhs = Trip.of(AgencyAndIdMapper.mapAgencyAndId(rhs.getId()));

    lhs.withRoute(routeMapper.map(rhs.getRoute()));
    lhs.withServiceId(AgencyAndIdMapper.mapAgencyAndId(rhs.getServiceId()));
    lhs.withShortName(rhs.getTripShortName());
    I18NString tripHeadsign = null;
    if (rhs.getTripHeadsign() != null) {
      tripHeadsign =
        translationHelper.getTranslation(
          org.onebusaway.gtfs.model.Trip.class,
          "tripHeadsign",
          rhs.getId().getId(),
          rhs.getTripHeadsign()
        );
    }
    lhs.withHeadsign(tripHeadsign);
    lhs.withDirection(directionMapper.map(rhs.getDirectionId(), lhs.getId()));
    lhs.withGtfsBlockId(rhs.getBlockId());
    lhs.withShapeId(AgencyAndIdMapper.mapAgencyAndId(rhs.getShapeId()));
    lhs.withWheelchairBoarding(WheelchairAccessibilityMapper.map(rhs.getWheelchairAccessible()));
    lhs.withBikesAllowed(BikeAccessMapper.mapForTrip(rhs));

    var trip = lhs.build();
    mapSafeTimePenalty(rhs).ifPresent(f -> flexSafeTimePenalties.put(trip, f));
    return trip;
  }

  private Optional mapSafeTimePenalty(org.onebusaway.gtfs.model.Trip rhs) {
    if (rhs.getSafeDurationFactor() == null && rhs.getSafeDurationOffset() == null) {
      return Optional.empty();
    } else {
      var offset = Duration.ofMinutes(rhs.getSafeDurationOffset().longValue());
      return Optional.of(TimePenalty.of(offset, rhs.getSafeDurationFactor().doubleValue()));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy