Search in sources :

Example 11 with GTFSFeed

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

the class GtfsStorage method loadGtfsFromFile.

void loadGtfsFromFile(String id, ZipFile zip) {
    try {
        GTFSFeed feed = new GTFSFeed(dir.getLocation() + "/" + id);
        feed.loadFromFile(zip);
        fixFares(feed, zip);
        this.gtfsFeeds.put(id, feed);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    this.gtfsFeedIds.add(id);
}
Also used : GTFSFeed(com.conveyal.gtfs.GTFSFeed) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 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 13 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 14 with GTFSFeed

use of com.conveyal.gtfs.GTFSFeed in project OpenTripPlanner by opentripplanner.

the class FakeGraph method addTransit.

/**
 * Add transit (not just stops) to a Columbus graph
 */
public static void addTransit(Graph gg) throws Exception {
    // using conveyal GTFS lib to build GTFS so a lot of code does not have to be rewritten later
    // once we're using the conveyal GTFS lib for everything we ought to be able to do this
    // without even writing out the GTFS to a file.
    GTFSFeed feed = new GTFSFeed();
    Agency a = createDummyAgency("agency", "Agency", "America/New_York");
    feed.agency.put("agency", a);
    Route r = new Route();
    r.route_short_name = "1";
    r.route_long_name = "High Street";
    r.route_type = 3;
    r.agency = a;
    r.route_id = "route";
    feed.routes.put(r.route_id, r);
    Service s = createDummyService();
    feed.services.put(s.service_id, s);
    com.conveyal.gtfs.model.Stop s1 = new com.conveyal.gtfs.model.Stop();
    s1.stop_id = s1.stop_name = "s1";
    s1.stop_lat = 40.2182;
    s1.stop_lon = -83.0889;
    feed.stops.put(s1.stop_id, s1);
    com.conveyal.gtfs.model.Stop s2 = new com.conveyal.gtfs.model.Stop();
    s2.stop_id = s2.stop_name = "s2";
    s2.stop_lat = 39.9621;
    s2.stop_lon = -83.0007;
    feed.stops.put(s2.stop_id, s2);
    // make timetabled trips
    for (int departure = 7 * 3600; departure < 20 * 3600; departure += FREQUENCY) {
        Trip t = new Trip();
        t.trip_id = "trip" + departure;
        t.service = s;
        t.route = r;
        feed.trips.put(t.trip_id, t);
        StopTime st1 = new StopTime();
        st1.trip_id = t.trip_id;
        st1.arrival_time = departure;
        st1.departure_time = departure;
        st1.stop_id = s1.stop_id;
        st1.stop_sequence = 1;
        feed.stop_times.put(new Fun.Tuple2(st1.trip_id, st1.stop_sequence), st1);
        StopTime st2 = new StopTime();
        st2.trip_id = t.trip_id;
        st2.arrival_time = departure + TRAVEL_TIME;
        st2.departure_time = departure + TRAVEL_TIME;
        st2.stop_sequence = 2;
        st2.stop_id = s2.stop_id;
        feed.stop_times.put(new Fun.Tuple2(st2.trip_id, st2.stop_sequence), st2);
    }
    File tempFile = File.createTempFile("gtfs", ".zip");
    feed.toFile(tempFile.getAbsolutePath());
    // phew. load it into the graph.
    GtfsModule gtfs = new GtfsModule(Arrays.asList(new GtfsBundle(tempFile)));
    gtfs.buildGraph(gg, new HashMap<>());
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) com.conveyal.gtfs.model(com.conveyal.gtfs.model) GTFSFeed(com.conveyal.gtfs.GTFSFeed) File(java.io.File) Fun(org.mapdb.Fun)

Example 15 with GTFSFeed

use of com.conveyal.gtfs.GTFSFeed in project OpenTripPlanner by opentripplanner.

the class FakeGraph method addMultiplePatterns.

/**
 * Add a transit line with multiple patterns to a Columbus graph. Most trips serve stops s1, s2, s3 but some serve only s1, s3
 */
public static void addMultiplePatterns(Graph gg) throws Exception {
    // using conveyal GTFS lib to build GTFS so a lot of code does not have to be rewritten later
    // once we're using the conveyal GTFS lib for everything we ought to be able to do this
    // without even writing out the GTFS to a file.
    GTFSFeed feed = new GTFSFeed();
    Agency a = createDummyAgency("agency", "Agency", "America/New_York");
    feed.agency.put("agency", a);
    Route r = new Route();
    r.route_short_name = "1";
    r.route_long_name = "High Street";
    r.route_type = 3;
    r.agency = a;
    r.route_id = "route";
    feed.routes.put(r.route_id, r);
    Service s = createDummyService();
    feed.services.put(s.service_id, s);
    com.conveyal.gtfs.model.Stop s1 = new com.conveyal.gtfs.model.Stop();
    s1.stop_id = s1.stop_name = "s1";
    s1.stop_lat = 40.2182;
    s1.stop_lon = -83.0889;
    feed.stops.put(s1.stop_id, s1);
    com.conveyal.gtfs.model.Stop s2 = new com.conveyal.gtfs.model.Stop();
    s2.stop_id = s2.stop_name = "s2";
    s2.stop_lat = 39.9621;
    s2.stop_lon = -83.0007;
    feed.stops.put(s2.stop_id, s2);
    com.conveyal.gtfs.model.Stop s3 = new com.conveyal.gtfs.model.Stop();
    s3.stop_id = s3.stop_name = "s3";
    s3.stop_lat = 39.9510;
    s3.stop_lon = -83.0007;
    feed.stops.put(s3.stop_id, s3);
    // make timetabled trips
    for (int departure = 7 * 3600, dcount = 0; departure < 20 * 3600; departure += FREQUENCY) {
        Trip t = new Trip();
        t.trip_id = "trip" + departure;
        t.service = s;
        t.route = r;
        feed.trips.put(t.trip_id, t);
        StopTime st1 = new StopTime();
        st1.trip_id = t.trip_id;
        st1.arrival_time = departure;
        st1.departure_time = departure;
        st1.stop_id = s1.stop_id;
        st1.stop_sequence = 1;
        feed.stop_times.put(new Fun.Tuple2(st1.trip_id, st1.stop_sequence), st1);
        // occasionally skip second stop
        boolean secondStop = dcount++ % 10 != 0;
        if (secondStop) {
            StopTime st2 = new StopTime();
            st2.trip_id = t.trip_id;
            st2.arrival_time = departure + TRAVEL_TIME;
            st2.departure_time = departure + TRAVEL_TIME;
            st2.stop_sequence = 2;
            st2.stop_id = s2.stop_id;
            feed.stop_times.put(new Fun.Tuple2(st2.trip_id, st2.stop_sequence), st2);
        }
        StopTime st3 = new StopTime();
        st3.trip_id = t.trip_id;
        st3.arrival_time = departure + (secondStop ? 2 : 1) * TRAVEL_TIME;
        st3.departure_time = departure + (secondStop ? 2 : 1) * TRAVEL_TIME;
        st3.stop_sequence = 3;
        st3.stop_id = s3.stop_id;
        feed.stop_times.put(new Fun.Tuple2(st3.trip_id, st3.stop_sequence), st3);
    }
    File tempFile = File.createTempFile("gtfs", ".zip");
    feed.toFile(tempFile.getAbsolutePath());
    // phew. load it into the graph.
    GtfsModule gtfs = new GtfsModule(Arrays.asList(new GtfsBundle(tempFile)));
    gtfs.buildGraph(gg, new HashMap<>());
}
Also used : TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Stop(org.onebusaway.gtfs.model.Stop) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) com.conveyal.gtfs.model(com.conveyal.gtfs.model) GTFSFeed(com.conveyal.gtfs.GTFSFeed) File(java.io.File) Fun(org.mapdb.Fun)

Aggregations

GTFSFeed (com.conveyal.gtfs.GTFSFeed)25 Fun (org.mapdb.Fun)12 StopTime (com.conveyal.gtfs.model.StopTime)8 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)8 File (java.io.File)8 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 IntHashSet (com.carrotsearch.hppc.IntHashSet)6 Trip (com.conveyal.gtfs.model.Trip)6 Stream (java.util.stream.Stream)6 StreamSupport (java.util.stream.StreamSupport)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 com.conveyal.gtfs.model (com.conveyal.gtfs.model)5 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)5 Stop (org.onebusaway.gtfs.model.Stop)5 GtfsBundle (org.opentripplanner.graph_builder.model.GtfsBundle)5 TransitStop (org.opentripplanner.routing.vertextype.TransitStop)5 IntArrayList (com.carrotsearch.hppc.IntArrayList)4 IntLongHashMap (com.carrotsearch.hppc.IntLongHashMap)4