Search in sources :

Example 6 with GTFSFeed

use of com.conveyal.gtfs.GTFSFeed in project graphhopper by graphhopper.

the class GtfsStorage method loadExisting.

@Override
public boolean loadExisting() {
    this.data = DBMaker.newFileDB(new File(dir.getLocation() + "/transit_schedule")).transactionDisable().mmapFileEnable().readOnly().make();
    init();
    for (String gtfsFeedId : this.gtfsFeedIds) {
        try {
            GTFSFeed feed = new GTFSFeed(dir.getLocation() + "/" + gtfsFeedId);
            this.gtfsFeeds.put(gtfsFeedId, feed);
            this.transfers.put(gtfsFeedId, new Transfers(feed));
        } catch (IOException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) ZipFile(java.util.zip.ZipFile) GTFSFeed(com.conveyal.gtfs.GTFSFeed)

Example 7 with GTFSFeed

use of com.conveyal.gtfs.GTFSFeed in project graphhopper by graphhopper.

the class TripFromLabel method createPathWrapper.

PathWrapper createPathWrapper(Translation tr, PointList waypoints, List<Trip.Leg> legs) {
    if (legs.size() > 1 && legs.get(0) instanceof Trip.WalkLeg) {
        final Trip.WalkLeg accessLeg = (Trip.WalkLeg) legs.get(0);
        legs.set(0, new Trip.WalkLeg(accessLeg.departureLocation, new Date(legs.get(1).getDepartureTime().getTime() - (accessLeg.getArrivalTime().getTime() - accessLeg.getDepartureTime().getTime())), accessLeg.edges, accessLeg.geometry, accessLeg.distance, accessLeg.instructions, legs.get(1).getDepartureTime()));
    }
    if (legs.size() > 1 && legs.get(legs.size() - 1) instanceof Trip.WalkLeg) {
        final Trip.WalkLeg egressLeg = (Trip.WalkLeg) legs.get(legs.size() - 1);
        legs.set(legs.size() - 1, new Trip.WalkLeg(egressLeg.departureLocation, legs.get(legs.size() - 2).getArrivalTime(), egressLeg.edges, egressLeg.geometry, egressLeg.distance, egressLeg.instructions, new Date(legs.get(legs.size() - 2).getArrivalTime().getTime() + (egressLeg.getArrivalTime().getTime() - egressLeg.getDepartureTime().getTime()))));
    }
    PathWrapper path = new PathWrapper();
    path.setWaypoints(waypoints);
    path.getLegs().addAll(legs);
    final InstructionList instructions = getInstructions(tr, path.getLegs());
    path.setInstructions(instructions);
    PointList pointsList = new PointList();
    for (Instruction instruction : instructions) {
        pointsList.add(instruction.getPoints());
    }
    path.setPoints(pointsList);
    path.setDistance(path.getLegs().stream().mapToDouble(Trip.Leg::getDistance).sum());
    path.setTime((legs.get(legs.size() - 1).getArrivalTime().toInstant().toEpochMilli() - legs.get(0).getDepartureTime().toInstant().toEpochMilli()));
    path.setNumChanges((int) path.getLegs().stream().filter(l -> l instanceof Trip.PtLeg).filter(l -> !((Trip.PtLeg) l).isInSameVehicleAsPrevious).count() - 1);
    com.graphhopper.gtfs.fare.Trip faresTrip = new com.graphhopper.gtfs.fare.Trip();
    path.getLegs().stream().filter(leg -> leg instanceof Trip.PtLeg).map(leg -> (Trip.PtLeg) leg).findFirst().ifPresent(firstPtLeg -> {
        LocalDateTime firstPtDepartureTime = GtfsHelper.localDateTimeFromDate(firstPtLeg.getDepartureTime());
        path.getLegs().stream().filter(leg -> leg instanceof Trip.PtLeg).map(leg -> (Trip.PtLeg) leg).map(ptLeg -> {
            final GTFSFeed gtfsFeed = gtfsStorage.getGtfsFeeds().get(ptLeg.feed_id);
            return new com.graphhopper.gtfs.fare.Trip.Segment(ptLeg.route_id, Duration.between(firstPtDepartureTime, GtfsHelper.localDateTimeFromDate(ptLeg.getDepartureTime())).getSeconds(), gtfsFeed.stops.get(ptLeg.stops.get(0).stop_id).zone_id, gtfsFeed.stops.get(ptLeg.stops.get(ptLeg.stops.size() - 1).stop_id).zone_id, ptLeg.stops.stream().map(s -> gtfsFeed.stops.get(s.stop_id).zone_id).collect(Collectors.toSet()));
        }).forEach(faresTrip.segments::add);
        Fares.cheapestFare(gtfsStorage.getFares(), faresTrip).ifPresent(amount -> path.setFare(amount.getAmount()));
    });
    return path;
}
Also used : java.util(java.util) Stop(com.conveyal.gtfs.model.Stop) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Duration(java.time.Duration) Geometry(com.vividsolutions.jts.geom.Geometry) Fares(com.graphhopper.gtfs.fare.Fares) StreamSupport(java.util.stream.StreamSupport) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Coordinate(com.vividsolutions.jts.geom.Coordinate) SECONDS(java.time.temporal.ChronoUnit.SECONDS) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Label.reverseEdges(com.graphhopper.reader.gtfs.Label.reverseEdges) Fun(org.mapdb.Fun) Stream(java.util.stream.Stream) StopTime(com.conveyal.gtfs.model.StopTime) PathWrapper(com.graphhopper.PathWrapper) Weighting(com.graphhopper.routing.weighting.Weighting) Trip(com.graphhopper.Trip) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) LocalDateTime(java.time.LocalDateTime) Trip(com.graphhopper.Trip) PathWrapper(com.graphhopper.PathWrapper) GTFSFeed(com.conveyal.gtfs.GTFSFeed)

Example 8 with GTFSFeed

use of com.conveyal.gtfs.GTFSFeed in project graphhopper by graphhopper.

the class FareTest method parseFares.

private static Map<String, Fare> parseFares(String fareAttributes, String fareRules) {
    GTFSFeed feed = new GTFSFeed();
    HashMap<String, Fare> fares = new HashMap<>();
    new FixedFareAttributeLoader(feed, fares) {

        void load(String input) {
            reader = new CsvReader(new StringReader(input));
            reader.setHeaders(new String[] { "fare_id", "price", "currency_type", "payment_method", "transfers", "transfer_duration" });
            try {
                while (reader.readRecord()) {
                    loadOneRow();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.load(fareAttributes);
    new FareRule.Loader(feed, fares) {

        void load(String input) {
            reader = new CsvReader(new StringReader(input));
            reader.setHeaders(new String[] { "fare_id", "route_id", "origin_id", "destination_id", "contains_id" });
            try {
                while (reader.readRecord()) {
                    loadOneRow();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.load(fareRules);
    return fares;
}
Also used : CsvReader(com.csvreader.CsvReader) FareRule(com.conveyal.gtfs.model.FareRule) StringReader(java.io.StringReader) IOException(java.io.IOException) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Fare(com.conveyal.gtfs.model.Fare)

Aggregations

GTFSFeed (com.conveyal.gtfs.GTFSFeed)8 StopTime (com.conveyal.gtfs.model.StopTime)3 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)3 Fun (org.mapdb.Fun)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 Fare (com.conveyal.gtfs.model.Fare)2 Trip (com.conveyal.gtfs.model.Trip)2 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ZonedDateTime (java.time.ZonedDateTime)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 ExecutionException (java.util.concurrent.ExecutionException)2 IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)1 IntLongHashMap (com.carrotsearch.hppc.IntLongHashMap)1 Agency (com.conveyal.gtfs.model.Agency)1