Skip to content

Commit

Permalink
Stop using FlagEncoder for Pt
Browse files Browse the repository at this point in the history
  • Loading branch information
michaz committed Oct 7, 2019
1 parent bcc6d83 commit 0dee7ab
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 250 deletions.
10 changes: 0 additions & 10 deletions api/src/main/java/com/graphhopper/util/Parameters.java
Expand Up @@ -214,14 +214,4 @@ public static final class Details {
public static final String DISTANCE = "distance";
}

public static final class PT {
public static final String EARLIEST_DEPARTURE_TIME = "pt.earliest_departure_time";
public static final String PROFILE_QUERY = "pt.profile";
public static final String ARRIVE_BY = "pt.arrive_by";
public static final String IGNORE_TRANSFERS = "pt.ignore_transfers";
public static final String WALK_SPEED = "pt.walk_speed";
public static final String MAX_WALK_DISTANCE_PER_LEG = "pt.max_walk_distance_per_leg";
public static final String LIMIT_SOLUTIONS = "pt.limit_solutions";
public static final String BLOCKED_ROUTE_TYPES = "pt.blocked_route_types";
}
}
Expand Up @@ -25,15 +25,14 @@
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.transit.realtime.GtfsRealtime;
import com.graphhopper.reader.gtfs.GtfsStorage;
import com.graphhopper.reader.gtfs.PtFlagEncoder;
import com.graphhopper.reader.gtfs.PtEncodedValues;
import com.graphhopper.reader.gtfs.RealtimeFeed;
import com.graphhopper.storage.GraphHopperStorage;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.glassfish.hk2.api.Factory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
Expand All @@ -48,16 +47,14 @@ public class RealtimeFeedLoadingCache implements Factory<RealtimeFeed> {
private final HttpClient httpClient;
private final GraphHopperStorage graphHopperStorage;
private final GtfsStorage gtfsStorage;
private final PtFlagEncoder ptFlagEncoder;
private final RealtimeBundleConfiguration bundleConfiguration;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final LoadingCache<String, RealtimeFeed> cache;

@Inject
RealtimeFeedLoadingCache(GraphHopperStorage graphHopperStorage, GtfsStorage gtfsStorage, PtFlagEncoder ptFlagEncoder, HttpClient httpClient, RealtimeBundleConfiguration bundleConfiguration) {
RealtimeFeedLoadingCache(GraphHopperStorage graphHopperStorage, GtfsStorage gtfsStorage, HttpClient httpClient, RealtimeBundleConfiguration bundleConfiguration) {
this.graphHopperStorage = graphHopperStorage;
this.gtfsStorage = gtfsStorage;
this.ptFlagEncoder = ptFlagEncoder;
this.bundleConfiguration = bundleConfiguration;
this.httpClient = httpClient;
this.cache = CacheBuilder.newBuilder()
Expand Down Expand Up @@ -102,7 +99,7 @@ private RealtimeFeed fetchFeedsAndCreateGraph() {
throw new RuntimeException(e);
}
}
return RealtimeFeed.fromProtobuf(graphHopperStorage, gtfsStorage, ptFlagEncoder, feedMessageMap);
return RealtimeFeed.fromProtobuf(graphHopperStorage, gtfsStorage, PtEncodedValues.fromEncodingManager(graphHopperStorage.getEncodingManager()), feedMessageMap);
}

}
Expand Up @@ -29,7 +29,9 @@
import java.time.Instant;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand All @@ -38,7 +40,7 @@
public final class GraphExplorer {

private final EdgeExplorer edgeExplorer;
private final PtFlagEncoder flagEncoder;
private final PtEncodedValues flagEncoder;
private final GtfsStorage gtfsStorage;
private final RealtimeFeed realtimeFeed;
private final boolean reverse;
Expand All @@ -47,12 +49,12 @@ public final class GraphExplorer {
private double walkSpeedKmH;


public GraphExplorer(Graph graph, Weighting accessEgressWeighting, PtFlagEncoder flagEncoder, GtfsStorage gtfsStorage, RealtimeFeed realtimeFeed, boolean reverse, boolean walkOnly, double walkSpeedKmh) {
public GraphExplorer(Graph graph, Weighting accessEgressWeighting, PtEncodedValues flagEncoder, GtfsStorage gtfsStorage, RealtimeFeed realtimeFeed, boolean reverse, boolean walkOnly, double walkSpeedKmh) {
this.accessEgressWeighting = accessEgressWeighting;
DefaultEdgeFilter accessEgressIn = DefaultEdgeFilter.inEdges(accessEgressWeighting.getFlagEncoder());
DefaultEdgeFilter accessEgressOut = DefaultEdgeFilter.outEdges(accessEgressWeighting.getFlagEncoder());
DefaultEdgeFilter ptIn = DefaultEdgeFilter.inEdges(flagEncoder);
DefaultEdgeFilter ptOut = DefaultEdgeFilter.outEdges(flagEncoder);
DefaultEdgeFilter ptIn = DefaultEdgeFilter.inEdges(flagEncoder.getAccessEnc());
DefaultEdgeFilter ptOut = DefaultEdgeFilter.outEdges(flagEncoder.getAccessEnc());
EdgeFilter in = edgeState -> accessEgressIn.accept(edgeState) || ptIn.accept(edgeState);
EdgeFilter out = edgeState -> accessEgressOut.accept(edgeState) || ptOut.accept(edgeState);
this.edgeExplorer = graph.createEdgeExplorer(reverse ? in : out);
Expand Down
Expand Up @@ -60,13 +60,13 @@ public final class GraphHopperGtfs {

public static class Factory {
private final TranslationMap translationMap;
private final PtFlagEncoder flagEncoder;
private final PtEncodedValues ptEncodedValues;
private final GraphHopperStorage graphHopperStorage;
private final LocationIndex locationIndex;
private final GtfsStorage gtfsStorage;

private Factory(PtFlagEncoder flagEncoder, TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage) {
this.flagEncoder = flagEncoder;
private Factory(TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage) {
this.ptEncodedValues = PtEncodedValues.fromEncodingManager(graphHopperStorage.getEncodingManager());
this.translationMap = translationMap;
this.graphHopperStorage = graphHopperStorage;
this.locationIndex = locationIndex;
Expand All @@ -76,20 +76,20 @@ private Factory(PtFlagEncoder flagEncoder, TranslationMap translationMap, GraphH
public GraphHopperGtfs createWith(GtfsRealtime.FeedMessage realtimeFeed) {
Map<String, GtfsRealtime.FeedMessage> realtimeFeeds = new HashMap<>();
realtimeFeeds.put("gtfs_0", realtimeFeed);
return new GraphHopperGtfs(flagEncoder, translationMap, graphHopperStorage, locationIndex, gtfsStorage, RealtimeFeed.fromProtobuf(graphHopperStorage, gtfsStorage, flagEncoder, realtimeFeeds));
return new GraphHopperGtfs(translationMap, graphHopperStorage, locationIndex, gtfsStorage, RealtimeFeed.fromProtobuf(graphHopperStorage, gtfsStorage, ptEncodedValues, realtimeFeeds));
}

public GraphHopperGtfs createWithoutRealtimeFeed() {
return new GraphHopperGtfs(flagEncoder, translationMap, graphHopperStorage, locationIndex, gtfsStorage, RealtimeFeed.empty(gtfsStorage));
return new GraphHopperGtfs(translationMap, graphHopperStorage, locationIndex, gtfsStorage, RealtimeFeed.empty(gtfsStorage));
}
}

public static Factory createFactory(PtFlagEncoder flagEncoder, TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage) {
return new Factory(flagEncoder, translationMap, graphHopperStorage, locationIndex, gtfsStorage);
public static Factory createFactory(TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage) {
return new Factory(translationMap, graphHopperStorage, locationIndex, gtfsStorage);
}

private final TranslationMap translationMap;
private final PtFlagEncoder flagEncoder;
private final PtEncodedValues ptEncodedValues;
private final Weighting accessEgressWeighting;
private final GraphHopperStorage graphHopperStorage;
private final LocationIndex locationIndex;
Expand Down Expand Up @@ -193,8 +193,8 @@ private QueryResult findClosest(GHPoint point, int indexForErrorMessage) {
if (!source.isValid()) {
throw new PointNotFoundException("Cannot find point: " + point, indexForErrorMessage);
}
if (source.getClosestEdge().get(flagEncoder.getTypeEnc()) != GtfsStorage.EdgeType.HIGHWAY) {
throw new RuntimeException(source.getClosestEdge().get(flagEncoder.getTypeEnc()).name());
if (source.getClosestEdge().get(ptEncodedValues.getTypeEnc()) != GtfsStorage.EdgeType.HIGHWAY) {
throw new RuntimeException(source.getClosestEdge().get(ptEncodedValues.getTypeEnc()).name());
}
return source;
}
Expand All @@ -214,10 +214,10 @@ private void parseSolutionsAndAddToResponse(List<List<Label.Transition>> solutio

private List<List<Label.Transition>> findPaths(int startNode, int destNode) {
StopWatch stopWatch = new StopWatch().start();
final GraphExplorer accessEgressGraphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, flagEncoder, gtfsStorage, realtimeFeed, !arriveBy, true, walkSpeedKmH);
final GraphExplorer accessEgressGraphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, ptEncodedValues, gtfsStorage, realtimeFeed, !arriveBy, true, walkSpeedKmH);
boolean reverse = !arriveBy;
GtfsStorage.EdgeType edgeType = reverse ? GtfsStorage.EdgeType.EXIT_PT : GtfsStorage.EdgeType.ENTER_PT;
MultiCriteriaLabelSetting stationRouter = new MultiCriteriaLabelSetting(accessEgressGraphExplorer, flagEncoder, reverse, maxWalkDistancePerLeg, false, false, false, maxVisitedNodesForRequest, new ArrayList<>());
MultiCriteriaLabelSetting stationRouter = new MultiCriteriaLabelSetting(accessEgressGraphExplorer, ptEncodedValues, reverse, maxWalkDistancePerLeg, false, false, false, maxVisitedNodesForRequest, new ArrayList<>());
stationRouter.setBetaWalkTime(betaWalkTime);
Iterator<Label> stationIterator = stationRouter.calcLabels(destNode, startNode, initialTime, blockedRouteTypes).iterator();
List<Label> stationLabels = new ArrayList<>();
Expand All @@ -226,7 +226,7 @@ private List<List<Label.Transition>> findPaths(int startNode, int destNode) {
if (label.adjNode == startNode) {
stationLabels.add(label);
break;
} else if (label.edge != -1 && queryGraph.getEdgeIteratorState(label.edge, label.parent.adjNode).get(flagEncoder.getTypeEnc()) == edgeType) {
} else if (label.edge != -1 && queryGraph.getEdgeIteratorState(label.edge, label.parent.adjNode).get(ptEncodedValues.getTypeEnc()) == edgeType) {
stationLabels.add(label);
}
}
Expand All @@ -237,10 +237,10 @@ private List<List<Label.Transition>> findPaths(int startNode, int destNode) {
reverseSettledSet.put(stationLabel.adjNode, stationLabel);
}

GraphExplorer graphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, flagEncoder, gtfsStorage, realtimeFeed, arriveBy, false, walkSpeedKmH);
GraphExplorer graphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, ptEncodedValues, gtfsStorage, realtimeFeed, arriveBy, false, walkSpeedKmH);
List<Label> discoveredSolutions = new ArrayList<>();
final long smallestStationLabelWeight;
MultiCriteriaLabelSetting router = new MultiCriteriaLabelSetting(graphExplorer, flagEncoder, arriveBy, maxWalkDistancePerLeg, true, !ignoreTransfers, profileQuery, maxVisitedNodesForRequest, discoveredSolutions);
MultiCriteriaLabelSetting router = new MultiCriteriaLabelSetting(graphExplorer, ptEncodedValues, arriveBy, maxWalkDistancePerLeg, true, !ignoreTransfers, profileQuery, maxVisitedNodesForRequest, discoveredSolutions);
router.setBetaTransfers(betaTransfers);
router.setBetaWalkTime(betaWalkTime);
if (!stationLabels.isEmpty()) {
Expand Down Expand Up @@ -278,7 +278,7 @@ private List<List<Label.Transition>> findPaths(int startNode, int destNode) {

List<List<Label.Transition>> pathsToStations = discoveredSolutions.stream()
.map(originalSolutions::get)
.map(l -> tripFromLabel.getTransitions(arriveBy, flagEncoder, queryGraph, l)).collect(Collectors.toList());
.map(l -> tripFromLabel.getTransitions(arriveBy, ptEncodedValues, queryGraph, l)).collect(Collectors.toList());

List<List<Label.Transition>> paths = pathsToStations.stream().map(p -> {
if (arriveBy) {
Expand Down Expand Up @@ -316,13 +316,13 @@ private List<List<Label.Transition>> findPaths(int startNode, int destNode) {
}

private List<Label.Transition> pathFromStation(Label l) {
return tripFromLabel.getTransitions(!arriveBy, flagEncoder, queryGraph, l);
return tripFromLabel.getTransitions(!arriveBy, ptEncodedValues, queryGraph, l);
}
}

@Inject
public GraphHopperGtfs(PtFlagEncoder flagEncoder, TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage, RealtimeFeed realtimeFeed) {
this.flagEncoder = flagEncoder;
public GraphHopperGtfs(TranslationMap translationMap, GraphHopperStorage graphHopperStorage, LocationIndex locationIndex, GtfsStorage gtfsStorage, RealtimeFeed realtimeFeed) {
this.ptEncodedValues = PtEncodedValues.fromEncodingManager(graphHopperStorage.getEncodingManager());
this.accessEgressWeighting = new FastestWeighting(graphHopperStorage.getEncodingManager().getEncoder("foot"));
this.translationMap = translationMap;
this.graphHopperStorage = graphHopperStorage;
Expand All @@ -332,7 +332,7 @@ public GraphHopperGtfs(PtFlagEncoder flagEncoder, TranslationMap translationMap,
this.tripFromLabel = new TripFromLabel(this.gtfsStorage, this.realtimeFeed);
}

public static GraphHopperStorage createOrLoad(GHDirectory directory, EncodingManager encodingManager, PtFlagEncoder ptFlagEncoder, GtfsStorage gtfsStorage, Collection<String> gtfsFiles, Collection<String> osmFiles) {
public static GraphHopperStorage createOrLoad(GHDirectory directory, EncodingManager encodingManager, GtfsStorage gtfsStorage, Collection<String> gtfsFiles, Collection<String> osmFiles) {
GraphHopperStorage graphHopperStorage = new GraphHopperStorage(directory, encodingManager, false, new GraphExtension.NoOpExtension());
if (graphHopperStorage.loadExisting()) {
return graphHopperStorage;
Expand Down Expand Up @@ -364,10 +364,10 @@ public static GraphHopperStorage createOrLoad(GHDirectory directory, EncodingMan
} else {
walkNetworkIndex = new EmptyLocationIndex();
}
GraphHopperGtfs graphHopperGtfs = new GraphHopperGtfs(ptFlagEncoder, new TranslationMap().doImport(), graphHopperStorage, walkNetworkIndex, gtfsStorage, RealtimeFeed.empty(gtfsStorage));
GraphHopperGtfs graphHopperGtfs = new GraphHopperGtfs(new TranslationMap().doImport(), graphHopperStorage, walkNetworkIndex, gtfsStorage, RealtimeFeed.empty(gtfsStorage));
for (int i = 0; i < id; i++) {
GTFSFeed gtfsFeed = gtfsStorage.getGtfsFeeds().get("gtfs_" + i);
GtfsReader gtfsReader = new GtfsReader("gtfs_" + i, graphHopperStorage, gtfsStorage, ptFlagEncoder, walkNetworkIndex);
GtfsReader gtfsReader = new GtfsReader("gtfs_" + i, graphHopperStorage, graphHopperStorage.getEncodingManager(), gtfsStorage, walkNetworkIndex);
gtfsReader.connectStopsToStreetNetwork();
graphHopperGtfs.getType0TransferWithTimes(gtfsFeed)
.forEach(t -> {
Expand Down Expand Up @@ -400,20 +400,20 @@ public static LocationIndex createOrLoadIndex(GHDirectory directory, GraphHopper
@GET
@Produces(MediaType.APPLICATION_JSON)
public ObjectNode route(@QueryParam("point") List<GHLocation> requestPoints,
@QueryParam(Parameters.PT.EARLIEST_DEPARTURE_TIME) String departureTimeString,
@QueryParam("pt.earliest_departure_time") String departureTimeString,
@QueryParam("locale") String localeStr,
@QueryParam(Parameters.PT.IGNORE_TRANSFERS) Boolean ignoreTransfers,
@QueryParam(Parameters.PT.PROFILE_QUERY) Boolean profileQuery,
@QueryParam(Parameters.PT.LIMIT_SOLUTIONS) Integer limitSolutions) {
@QueryParam("pt.ignore_transfers") Boolean ignoreTransfers,
@QueryParam("pt.profile") Boolean profileQuery,
@QueryParam("pt.limit_solutions") Integer limitSolutions) {

if (departureTimeString == null) {
throw new BadRequestException(String.format(Locale.ROOT, "Illegal value for required parameter %s: [%s]", Parameters.PT.EARLIEST_DEPARTURE_TIME, departureTimeString));
throw new BadRequestException(String.format(Locale.ROOT, "Illegal value for required parameter %s: [%s]", "pt.earliest_departure_time", departureTimeString));
}
Instant departureTime;
try {
departureTime = Instant.parse(departureTimeString);
} catch (DateTimeParseException e) {
throw new BadRequestException(String.format(Locale.ROOT, "Illegal value for required parameter %s: [%s]", Parameters.PT.EARLIEST_DEPARTURE_TIME, departureTimeString));
throw new BadRequestException(String.format(Locale.ROOT, "Illegal value for required parameter %s: [%s]", "pt.earliest_departure_time", departureTimeString));
}

Request request = new Request(requestPoints, departureTime);
Expand Down Expand Up @@ -453,9 +453,9 @@ private Stream<TransferWithTime> getType0TransferWithTimes(GTFSFeed gtfsFeed) {
points.add(graphHopperStorage.getNodeAccess().getLat(tonode), graphHopperStorage.getNodeAccess().getLon(tonode));

QueryGraph queryGraph = QueryGraph.lookup(graphHopperStorage, Collections.emptyList());
final GraphExplorer graphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, flagEncoder, gtfsStorage, realtimeFeed, false, true, 5.0);
final GraphExplorer graphExplorer = new GraphExplorer(queryGraph, accessEgressWeighting, ptEncodedValues, gtfsStorage, realtimeFeed, false, true, 5.0);

MultiCriteriaLabelSetting router = new MultiCriteriaLabelSetting(graphExplorer, flagEncoder, false, Double.MAX_VALUE, false, false, false, Integer.MAX_VALUE, new ArrayList<>());
MultiCriteriaLabelSetting router = new MultiCriteriaLabelSetting(graphExplorer, ptEncodedValues, false, Double.MAX_VALUE, false, false, false, Integer.MAX_VALUE, new ArrayList<>());
Iterator<Label> iterator = router.calcLabels(fromnode, tonode, Instant.ofEpochMilli(0), 0).iterator();
Label solution = null;
while (iterator.hasNext()) {
Expand Down

0 comments on commit 0dee7ab

Please sign in to comment.