Search in sources :

Example 1 with Trip

use of com.graphhopper.Trip in project graphhopper by graphhopper.

the class TripFromLabel method parsePartitionToLegs.

// We are parsing a string of edges into a hierarchical trip.
// One could argue that one should never write a parser
// by hand, because it is always ugly, but use a parser library.
// The code would then read like a specification of what paths through the graph mean.
private List<Trip.Leg> parsePartitionToLegs(List<Label.Transition> path, Graph graph, Weighting weighting, Translation tr, List<String> requestedPathDetails) {
    if (path.size() <= 1) {
        return Collections.emptyList();
    }
    if (GtfsStorage.EdgeType.ENTER_PT == path.get(1).edge.getType()) {
        String feedId = path.get(1).edge.getPlatformDescriptor().feed_id;
        List<Trip.Leg> result = new ArrayList<>();
        long boardTime = -1;
        List<Label.Transition> partition = null;
        for (int i = 1; i < path.size(); i++) {
            Label.Transition transition = path.get(i);
            GraphExplorer.MultiModalEdge edge = path.get(i).edge;
            if (edge.getType() == GtfsStorage.EdgeType.BOARD) {
                boardTime = transition.label.currentTime;
                partition = new ArrayList<>();
            }
            if (partition != null) {
                partition.add(path.get(i));
            }
            if (EnumSet.of(GtfsStorage.EdgeType.TRANSFER, GtfsStorage.EdgeType.LEAVE_TIME_EXPANDED_NETWORK).contains(edge.getType())) {
                GtfsRealtime.TripDescriptor tripDescriptor = partition.get(0).edge.getTripDescriptor();
                final StopsFromBoardHopDwellEdges stopsFromBoardHopDwellEdges = new StopsFromBoardHopDwellEdges(feedId, tripDescriptor);
                partition.stream().filter(e -> EnumSet.of(GtfsStorage.EdgeType.HOP, GtfsStorage.EdgeType.BOARD, GtfsStorage.EdgeType.DWELL).contains(e.edge.getType())).forEach(stopsFromBoardHopDwellEdges::next);
                stopsFromBoardHopDwellEdges.finish();
                List<Trip.Stop> stops = stopsFromBoardHopDwellEdges.stops;
                result.add(new Trip.PtLeg(feedId, partition.get(0).edge.getTransfers() == 0, tripDescriptor.getTripId(), tripDescriptor.getRouteId(), Optional.ofNullable(gtfsStorage.getGtfsFeeds().get(feedId).trips.get(tripDescriptor.getTripId())).map(t -> t.trip_headsign).orElse("extra"), stops, partition.stream().mapToDouble(t -> t.edge.getDistance()).sum(), path.get(i - 1).label.currentTime - boardTime, geometryFactory.createLineString(stops.stream().map(s -> s.geometry.getCoordinate()).toArray(Coordinate[]::new))));
                partition = null;
                if (edge.getType() == GtfsStorage.EdgeType.TRANSFER) {
                    feedId = edge.getPlatformDescriptor().feed_id;
                    int[] skippedEdgesForTransfer = gtfsStorage.getSkippedEdgesForTransfer().get(edge.getId());
                    if (skippedEdgesForTransfer != null) {
                        List<Trip.Leg> legs = parsePartitionToLegs(transferPath(skippedEdgesForTransfer, weighting, path.get(i - 1).label.currentTime), graph, weighting, tr, requestedPathDetails);
                        result.add(legs.get(0));
                    }
                }
            }
        }
        return result;
    } else {
        InstructionList instructions = new InstructionList(tr);
        InstructionsFromEdges instructionsFromEdges = new InstructionsFromEdges(graph, weighting, weighting.getFlagEncoder(), instructions);
        int prevEdgeId = -1;
        for (int i = 1; i < path.size(); i++) {
            if (path.get(i).edge.getType() != GtfsStorage.EdgeType.HIGHWAY) {
                throw new IllegalStateException("Got a transit edge where I think I must be on a road.");
            }
            EdgeIteratorState edge = graph.getEdgeIteratorState(path.get(i).edge.getId(), path.get(i).label.node.streetNode);
            instructionsFromEdges.next(edge, i, prevEdgeId);
            prevEdgeId = edge.getEdge();
        }
        instructionsFromEdges.finish();
        Path pathh = new Path(graph);
        for (Label.Transition transition : path) {
            if (transition.edge != null)
                pathh.addEdge(transition.edge.getId());
        }
        pathh.setFromNode(path.get(0).label.node.streetNode);
        pathh.setEndNode(path.get(path.size() - 1).label.node.streetNode);
        pathh.setFound(true);
        Map<String, List<PathDetail>> pathDetails = PathDetailsFromEdges.calcDetails(pathh, ((GraphHopperStorage) this.graph.getBaseGraph()).getEncodingManager(), weighting, requestedPathDetails, pathDetailsBuilderFactory, 0);
        final Instant departureTime = Instant.ofEpochMilli(path.get(0).label.currentTime);
        final Instant arrivalTime = Instant.ofEpochMilli(path.get(path.size() - 1).label.currentTime);
        return Collections.singletonList(new Trip.WalkLeg("Walk", Date.from(departureTime), lineStringFromInstructions(instructions), edges(path).mapToDouble(edgeLabel -> edgeLabel.getDistance()).sum(), instructions, pathDetails, Date.from(arrivalTime)));
    }
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) Stop(com.conveyal.gtfs.model.Stop) LoggerFactory(org.slf4j.LoggerFactory) LocalDateTime(java.time.LocalDateTime) Coordinate(org.locationtech.jts.geom.Coordinate) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Lists(com.google.common.collect.Lists) ResponsePath(com.graphhopper.ResponsePath) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Duration(java.time.Duration) Graph(com.graphhopper.storage.Graph) Fares(com.graphhopper.gtfs.fare.Fares) StreamSupport(java.util.stream.StreamSupport) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) SECONDS(java.time.temporal.ChronoUnit.SECONDS) com.graphhopper.util(com.graphhopper.util) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) GTFSFeed(com.conveyal.gtfs.GTFSFeed) Path(com.graphhopper.routing.Path) Instant(java.time.Instant) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) PathDetailsFromEdges(com.graphhopper.util.details.PathDetailsFromEdges) Stream(java.util.stream.Stream) StopTime(com.conveyal.gtfs.model.StopTime) Weighting(com.graphhopper.routing.weighting.Weighting) Trip(com.graphhopper.Trip) Geometry(org.locationtech.jts.geom.Geometry) PathDetail(com.graphhopper.util.details.PathDetail) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Stop(com.conveyal.gtfs.model.Stop) ResponsePath(com.graphhopper.ResponsePath) Path(com.graphhopper.routing.Path) Trip(com.graphhopper.Trip) Instant(java.time.Instant) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Coordinate(org.locationtech.jts.geom.Coordinate)

Example 2 with Trip

use of com.graphhopper.Trip in project graphhopper by graphhopper.

the class TripFromLabel method parsePathIntoLegs.

// We are parsing a string of edges into a hierarchical trip.
// One could argue that one should never write a parser
// by hand, because it is always ugly, but use a parser library.
// The code would then read like a specification of what paths through the graph mean.
private List<Trip.Leg> parsePathIntoLegs(List<Label.Transition> path, GraphExplorer graph, Weighting weighting, Translation tr) {
    if (path.size() <= 1) {
        return Collections.emptyList();
    }
    if (GtfsStorage.EdgeType.ENTER_PT == path.get(1).edge.edgeType) {
        final GtfsStorage.FeedIdWithTimezone feedIdWithTimezone = gtfsStorage.getTimeZones().get(path.get(1).edge.timeZoneId);
        List<Trip.Leg> result = new ArrayList<>();
        long boardTime = -1;
        List<Label.Transition> partition = null;
        for (int i = 1; i < path.size(); i++) {
            Label.Transition transition = path.get(i);
            Label.EdgeLabel edge = path.get(i).edge;
            if (edge.edgeType == GtfsStorage.EdgeType.BOARD) {
                boardTime = transition.label.currentTime;
                partition = new ArrayList<>();
            }
            if (partition != null) {
                partition.add(path.get(i));
            }
            if (EnumSet.of(GtfsStorage.EdgeType.TRANSFER, GtfsStorage.EdgeType.LEAVE_TIME_EXPANDED_NETWORK).contains(edge.edgeType)) {
                Geometry lineString = lineStringFromEdges(partition);
                GtfsRealtime.TripDescriptor tripDescriptor;
                try {
                    tripDescriptor = GtfsRealtime.TripDescriptor.parseFrom(realtimeFeed.getTripDescriptor(partition.get(0).edge.edgeIteratorState.getEdge()));
                } catch (InvalidProtocolBufferException e) {
                    throw new RuntimeException(e);
                }
                final StopsFromBoardHopDwellEdges stopsFromBoardHopDwellEdges = new StopsFromBoardHopDwellEdges(feedIdWithTimezone.feedId, tripDescriptor);
                partition.stream().filter(e -> EnumSet.of(GtfsStorage.EdgeType.HOP, GtfsStorage.EdgeType.BOARD, GtfsStorage.EdgeType.DWELL).contains(e.edge.edgeType)).forEach(stopsFromBoardHopDwellEdges::next);
                stopsFromBoardHopDwellEdges.finish();
                List<Trip.Stop> stops = stopsFromBoardHopDwellEdges.stops;
                result.add(new Trip.PtLeg(feedIdWithTimezone.feedId, partition.get(0).edge.nTransfers == 0, tripDescriptor.getTripId(), tripDescriptor.getRouteId(), edges(partition).map(edgeLabel -> edgeLabel.edgeIteratorState).collect(Collectors.toList()), stops, partition.stream().mapToDouble(t -> t.edge.distance).sum(), path.get(i - 1).label.currentTime - boardTime, lineString));
                partition = null;
            }
        }
        return result;
    } else {
        InstructionList instructions = new InstructionList(tr);
        InstructionsFromEdges instructionsFromEdges = new InstructionsFromEdges(path.get(1).edge.edgeIteratorState.getBaseNode(), graph.getGraph(), weighting, weighting.getFlagEncoder(), graph.getNodeAccess(), tr, instructions);
        int prevEdgeId = -1;
        for (int i = 1; i < path.size(); i++) {
            EdgeIteratorState edge = path.get(i).edge.edgeIteratorState;
            instructionsFromEdges.next(edge, i, prevEdgeId);
            prevEdgeId = edge.getEdge();
        }
        instructionsFromEdges.finish();
        final Instant departureTime = Instant.ofEpochMilli(path.get(0).label.currentTime);
        final Instant arrivalTime = Instant.ofEpochMilli(path.get(path.size() - 1).label.currentTime);
        return Collections.singletonList(new Trip.WalkLeg("Walk", Date.from(departureTime), edges(path).map(edgeLabel -> edgeLabel.edgeIteratorState).collect(Collectors.toList()), lineStringFromEdges(path), edges(path).mapToDouble(edgeLabel -> edgeLabel.distance).sum(), instructions, Date.from(arrivalTime)));
    }
}
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) InstructionsFromEdges(com.graphhopper.routing.InstructionsFromEdges) Stop(com.conveyal.gtfs.model.Stop) Trip(com.graphhopper.Trip) Instant(java.time.Instant) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GtfsRealtime(com.google.transit.realtime.GtfsRealtime) Geometry(com.vividsolutions.jts.geom.Geometry)

Aggregations

GTFSFeed (com.conveyal.gtfs.GTFSFeed)2 Stop (com.conveyal.gtfs.model.Stop)2 StopTime (com.conveyal.gtfs.model.StopTime)2 GtfsRealtime (com.google.transit.realtime.GtfsRealtime)2 Trip (com.graphhopper.Trip)2 Fares (com.graphhopper.gtfs.fare.Fares)2 InstructionsFromEdges (com.graphhopper.routing.InstructionsFromEdges)2 Weighting (com.graphhopper.routing.weighting.Weighting)2 com.graphhopper.util (com.graphhopper.util)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 LocalDateTime (java.time.LocalDateTime)2 SECONDS (java.time.temporal.ChronoUnit.SECONDS)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 StreamSupport (java.util.stream.StreamSupport)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Iterables (com.google.common.collect.Iterables)1