Search in sources :

Example 16 with GTFSFeed

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

the class FakeGraph method addTransitMultipleLines.

/**
 * Add many transit lines to a lot of stops
 */
public static void addTransitMultipleLines(Graph g) 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);
    int stopIdx = 0;
    while (stopIdx < 10000) {
        com.conveyal.gtfs.model.Stop s1 = new com.conveyal.gtfs.model.Stop();
        s1.stop_id = s1.stop_name = "s" + stopIdx++;
        s1.stop_lat = 39.9354 + (stopIdx % 100) * 1e-3;
        s1.stop_lon = -83.0589 + (stopIdx / 100) * 1e-3;
        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 = "s" + stopIdx++;
        s2.stop_lat = 39.9354 + (stopIdx % 100) * 1e-3;
        s2.stop_lon = -83.0589 + (stopIdx / 100) * 1e-3;
        feed.stops.put(s2.stop_id, s2);
        // make timetabled trips
        int departure = 8 * 3600;
        Trip t = new Trip();
        t.trip_id = "trip" + departure + "_" + stopIdx;
        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 + 500;
        st2.departure_time = departure + 500;
        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(g, 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 17 with GTFSFeed

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

the class FareTest method parseFares.

public static Map<String, Fare> parseFares(String feedId, String fareAttributes, String fareRules) {
    GTFSFeed feed = new GTFSFeed();
    feed.feedId = feedId;
    HashMap<String, Fare> fares = new HashMap<>();
    new FareAttribute.Loader(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 : FareAttribute(com.conveyal.gtfs.model.FareAttribute) 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)

Example 18 with GTFSFeed

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

the class RealtimeFeedLoadingCache method start.

@Override
public void start() {
    this.transfers = new HashMap<>();
    for (Map.Entry<String, GTFSFeed> entry : this.gtfsStorage.getGtfsFeeds().entrySet()) {
        this.transfers.put(entry.getKey(), new Transfers(entry.getValue()));
    }
    this.executor = Executors.newSingleThreadExecutor();
    this.cache = CacheBuilder.newBuilder().maximumSize(1).refreshAfterWrite(1, TimeUnit.MINUTES).build(new CacheLoader<String, RealtimeFeed>() {

        public RealtimeFeed load(String key) {
            return fetchFeedsAndCreateGraph();
        }

        @Override
        public ListenableFuture<RealtimeFeed> reload(String key, RealtimeFeed oldValue) {
            ListenableFutureTask<RealtimeFeed> task = ListenableFutureTask.create(() -> fetchFeedsAndCreateGraph());
            executor.execute(task);
            return task;
        }
    });
}
Also used : RealtimeFeed(com.graphhopper.gtfs.RealtimeFeed) Transfers(com.graphhopper.gtfs.Transfers) CacheLoader(com.google.common.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) GTFSFeed(com.conveyal.gtfs.GTFSFeed)

Example 19 with GTFSFeed

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

the class RealtimeFeed method fromProtobuf.

public static RealtimeFeed fromProtobuf(GraphHopperStorage graphHopperStorage, GtfsStorage staticGtfs, Map<String, Transfers> transfers, Map<String, GtfsRealtime.FeedMessage> feedMessages) {
    final IntHashSet blockedEdges = new IntHashSet();
    final IntLongHashMap delaysForBoardEdges = new IntLongHashMap();
    final IntLongHashMap delaysForAlightEdges = new IntLongHashMap();
    final LinkedList<PtGraph.PtEdge> additionalEdges = new LinkedList<>();
    final GtfsReader.PtGraphOut overlayGraph = new GtfsReader.PtGraphOut() {

        int nextEdge = staticGtfs.getPtGraph().getEdgeCount();

        int nextNode = staticGtfs.getPtGraph().getNodeCount();

        @Override
        public int createEdge(int src, int dest, PtEdgeAttributes attrs) {
            int edgeId = nextEdge++;
            additionalEdges.add(new PtGraph.PtEdge(edgeId, src, dest, attrs));
            return edgeId;
        }

        @Override
        public int createNode() {
            return nextNode++;
        }
    };
    feedMessages.forEach((feedKey, feedMessage) -> {
        GTFSFeed feed = staticGtfs.getGtfsFeeds().get(feedKey);
        ZoneId timezone = ZoneId.of(feed.agency.values().stream().findFirst().get().agency_timezone);
        PtGraph ptGraphNodesAndEdges = staticGtfs.getPtGraph();
        final GtfsReader gtfsReader = new GtfsReader(feedKey, graphHopperStorage, ptGraphNodesAndEdges, overlayGraph, staticGtfs, null, transfers.get(feedKey), null);
        Instant timestamp = Instant.ofEpochSecond(feedMessage.getHeader().getTimestamp());
        // FIXME
        LocalDate dateToChange = timestamp.atZone(timezone).toLocalDate();
        BitSet validOnDay = new BitSet();
        LocalDate startDate = feed.getStartDate();
        validOnDay.set((int) DAYS.between(startDate, dateToChange));
        feedMessage.getEntityList().stream().filter(GtfsRealtime.FeedEntity::hasTripUpdate).map(GtfsRealtime.FeedEntity::getTripUpdate).filter(tripUpdate -> tripUpdate.getTrip().getScheduleRelationship() == GtfsRealtime.TripDescriptor.ScheduleRelationship.SCHEDULED).forEach(tripUpdate -> {
            Collection<Frequency> frequencies = feed.getFrequencies(tripUpdate.getTrip().getTripId());
            int timeOffset = (tripUpdate.getTrip().hasStartTime() && !frequencies.isEmpty()) ? LocalTime.parse(tripUpdate.getTrip().getStartTime()).toSecondOfDay() : 0;
            final int[] boardEdges = findBoardEdgesForTrip(staticGtfs, feedKey, feed, tripUpdate);
            final int[] leaveEdges = findLeaveEdgesForTrip(staticGtfs, feedKey, feed, tripUpdate);
            if (boardEdges == null || leaveEdges == null) {
                logger.warn("Trip not found: {}", tripUpdate.getTrip());
                return;
            }
            tripUpdate.getStopTimeUpdateList().stream().filter(stopTimeUpdate -> stopTimeUpdate.getScheduleRelationship() == SKIPPED).mapToInt(GtfsRealtime.TripUpdate.StopTimeUpdate::getStopSequence).forEach(skippedStopSequenceNumber -> {
                blockedEdges.add(boardEdges[skippedStopSequenceNumber]);
                blockedEdges.add(leaveEdges[skippedStopSequenceNumber]);
            });
            GtfsReader.TripWithStopTimes tripWithStopTimes = toTripWithStopTimes(feed, tripUpdate);
            tripWithStopTimes.stopTimes.forEach(stopTime -> {
                if (stopTime.stop_sequence > leaveEdges.length - 1) {
                    logger.warn("Stop sequence number too high {} vs {}", stopTime.stop_sequence, leaveEdges.length);
                    return;
                }
                final StopTime originalStopTime = feed.stop_times.get(new Fun.Tuple2(tripUpdate.getTrip().getTripId(), stopTime.stop_sequence));
                int arrivalDelay = stopTime.arrival_time - originalStopTime.arrival_time;
                delaysForAlightEdges.put(leaveEdges[stopTime.stop_sequence], arrivalDelay * 1000);
                int departureDelay = stopTime.departure_time - originalStopTime.departure_time;
                if (departureDelay > 0) {
                    int boardEdge = boardEdges[stopTime.stop_sequence];
                    int departureNode = ptGraphNodesAndEdges.edge(boardEdge).getAdjNode();
                    int delayedBoardEdge = gtfsReader.addDelayedBoardEdge(timezone, tripUpdate.getTrip(), stopTime.stop_sequence, stopTime.departure_time + timeOffset, departureNode, validOnDay);
                    delaysForBoardEdges.put(delayedBoardEdge, departureDelay * 1000);
                }
            });
        });
        feedMessage.getEntityList().stream().filter(GtfsRealtime.FeedEntity::hasTripUpdate).map(GtfsRealtime.FeedEntity::getTripUpdate).filter(tripUpdate -> tripUpdate.getTrip().getScheduleRelationship() == GtfsRealtime.TripDescriptor.ScheduleRelationship.ADDED).forEach(tripUpdate -> {
            Trip trip = new Trip();
            trip.trip_id = tripUpdate.getTrip().getTripId();
            trip.route_id = tripUpdate.getTrip().getRouteId();
            final List<StopTime> stopTimes = tripUpdate.getStopTimeUpdateList().stream().map(stopTimeUpdate -> {
                final StopTime stopTime = new StopTime();
                stopTime.stop_sequence = stopTimeUpdate.getStopSequence();
                stopTime.stop_id = stopTimeUpdate.getStopId();
                stopTime.trip_id = trip.trip_id;
                final ZonedDateTime arrival_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
                stopTime.arrival_time = (int) Duration.between(arrival_time.truncatedTo(ChronoUnit.DAYS), arrival_time).getSeconds();
                final ZonedDateTime departure_time = Instant.ofEpochSecond(stopTimeUpdate.getArrival().getTime()).atZone(timezone);
                stopTime.departure_time = (int) Duration.between(departure_time.truncatedTo(ChronoUnit.DAYS), departure_time).getSeconds();
                return stopTime;
            }).collect(Collectors.toList());
            GtfsReader.TripWithStopTimes tripWithStopTimes = new GtfsReader.TripWithStopTimes(trip, stopTimes, validOnDay, Collections.emptySet(), Collections.emptySet());
            gtfsReader.addTrip(timezone, 0, new ArrayList<>(), tripWithStopTimes, tripUpdate.getTrip());
        });
        gtfsReader.wireUpAdditionalDeparturesAndArrivals(timezone);
    });
    return new RealtimeFeed(feedMessages, blockedEdges, delaysForBoardEdges, delaysForAlightEdges, additionalEdges);
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) java.time(java.time) NO_DATA(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA) IntArrayList(com.carrotsearch.hppc.IntArrayList) Frequency(com.conveyal.gtfs.model.Frequency) StreamSupport(java.util.stream.StreamSupport) IntLongHashMap(com.carrotsearch.hppc.IntLongHashMap) OutputStream(java.io.OutputStream) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Logger(org.slf4j.Logger) GTFSFeed(com.conveyal.gtfs.GTFSFeed) SKIPPED(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SKIPPED) IntHashSet(com.carrotsearch.hppc.IntHashSet) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Fun(org.mapdb.Fun) DAYS(java.time.temporal.ChronoUnit.DAYS) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) StopTime(com.conveyal.gtfs.model.StopTime) Trip(com.conveyal.gtfs.model.Trip) IntHashSet(com.carrotsearch.hppc.IntHashSet) IntLongHashMap(com.carrotsearch.hppc.IntLongHashMap) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Fun(org.mapdb.Fun) StopTime(com.conveyal.gtfs.model.StopTime) Trip(com.conveyal.gtfs.model.Trip) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Frequency(com.conveyal.gtfs.model.Frequency)

Example 20 with GTFSFeed

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

the class RealtimeFeed method findBoardEdgesForTrip.

private static int[] findBoardEdgesForTrip(GtfsStorage staticGtfs, String feedKey, GTFSFeed feed, GtfsRealtime.TripUpdate tripUpdate) {
    Trip trip = feed.trips.get(tripUpdate.getTrip().getTripId());
    StopTime next = feed.getOrderedStopTimesForTrip(trip.trip_id).iterator().next();
    int station = staticGtfs.getStationNodes().get(new GtfsStorage.FeedIdWithStopId(feedKey, next.stop_id));
    Optional<PtGraph.PtEdge> firstBoarding = StreamSupport.stream(staticGtfs.getPtGraph().edgesAround(station).spliterator(), false).flatMap(e -> StreamSupport.stream(staticGtfs.getPtGraph().edgesAround(e.getAdjNode()).spliterator(), false)).flatMap(e -> StreamSupport.stream(staticGtfs.getPtGraph().edgesAround(e.getAdjNode()).spliterator(), false)).filter(e -> e.getType() == GtfsStorage.EdgeType.BOARD).filter(e -> normalize(e.getAttrs().tripDescriptor).equals(tripUpdate.getTrip())).findAny();
    int n = firstBoarding.get().getAdjNode();
    Stream<PtGraph.PtEdge> boardEdges = evenIndexed(nodes(hopDwellChain(staticGtfs, n))).mapToObj(e -> boardForAdjNode(staticGtfs, e));
    return collectWithPadding(boardEdges);
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) java.time(java.time) NO_DATA(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.NO_DATA) IntArrayList(com.carrotsearch.hppc.IntArrayList) Frequency(com.conveyal.gtfs.model.Frequency) StreamSupport(java.util.stream.StreamSupport) IntLongHashMap(com.carrotsearch.hppc.IntLongHashMap) OutputStream(java.io.OutputStream) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Logger(org.slf4j.Logger) GTFSFeed(com.conveyal.gtfs.GTFSFeed) SKIPPED(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate.ScheduleRelationship.SKIPPED) IntHashSet(com.carrotsearch.hppc.IntHashSet) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Fun(org.mapdb.Fun) DAYS(java.time.temporal.ChronoUnit.DAYS) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) StopTime(com.conveyal.gtfs.model.StopTime) Trip(com.conveyal.gtfs.model.Trip) Trip(com.conveyal.gtfs.model.Trip) StopTime(com.conveyal.gtfs.model.StopTime)

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