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

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

package org.opentripplanner.gtfs.mapping;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.onebusaway.gtfs.model.Stop;
import org.opentripplanner.transit.model.site.Pathway;
import org.opentripplanner.transit.model.site.PathwayBuilder;
import org.opentripplanner.transit.model.site.StationElement;
import org.opentripplanner.util.MapUtils;

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

  private final StopMapper stopMapper;

  private final EntranceMapper entranceMapper;

  private final PathwayNodeMapper nodeMapper;

  private final BoardingAreaMapper boardingAreaMapper;

  private final Map mappedPathways = new HashMap<>();

  PathwayMapper(
    StopMapper stopMapper,
    EntranceMapper entranceMapper,
    PathwayNodeMapper nodeMapper,
    BoardingAreaMapper boardingAreaMapper
  ) {
    this.stopMapper = stopMapper;
    this.entranceMapper = entranceMapper;
    this.nodeMapper = nodeMapper;
    this.boardingAreaMapper = boardingAreaMapper;
  }

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

  /** Map from GTFS to OTP model, {@code null} safe. */
  Pathway map(org.onebusaway.gtfs.model.Pathway orginal) {
    return orginal == null ? null : mappedPathways.computeIfAbsent(orginal, this::doMap);
  }

  private Pathway doMap(org.onebusaway.gtfs.model.Pathway rhs) {
    PathwayBuilder pathway = Pathway
      .of(AgencyAndIdMapper.mapAgencyAndId(rhs.getId()))
      .withPathwayMode(PathwayModeMapper.map(rhs.getPathwayMode()))
      .withFromStop(mapStationElement(rhs.getFromStop()))
      .withToStop(mapStationElement(rhs.getToStop()))
      .withName(rhs.getSignpostedAs())
      .withReversedName(rhs.getReversedSignpostedAs())
      .withIsBidirectional(rhs.getIsBidirectional() == 1);

    if (rhs.isTraversalTimeSet()) {
      pathway.withTraversalTime(rhs.getTraversalTime());
    }
    if (rhs.isLengthSet()) {
      pathway.withLength(rhs.getLength());
    }
    if (rhs.isStairCountSet()) {
      pathway.withStairCount(rhs.getStairCount());
    }
    if (rhs.isMaxSlopeSet()) {
      pathway.withSlope(rhs.getMaxSlope());
    }

    return pathway.build();
  }

  private StationElement mapStationElement(Stop stop) {
    if (stop != null) {
      switch (stop.getLocationType()) {
        case org.onebusaway.gtfs.model.Stop.LOCATION_TYPE_STOP:
          return stopMapper.map(stop);
        case org.onebusaway.gtfs.model.Stop.LOCATION_TYPE_ENTRANCE_EXIT:
          return entranceMapper.map(stop);
        case org.onebusaway.gtfs.model.Stop.LOCATION_TYPE_NODE:
          return nodeMapper.map(stop);
        case org.onebusaway.gtfs.model.Stop.LOCATION_TYPE_BOARDING_AREA:
          return boardingAreaMapper.map(stop);
      }
    }
    // Stop was missing (null) or locationType was not valid (e.g. it was a station or missing)
    return null;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy