use of com.graphhopper.storage.NodeAccess in project graphhopper by graphhopper.
the class LocationIndexTreeTest method testFindingWayGeometry.
@Test
public void testFindingWayGeometry() {
Graph g = createGHStorage(encodingManager);
NodeAccess na = g.getNodeAccess();
na.setNode(10, 51.2492152, 9.4317166);
na.setNode(20, 52, 9);
na.setNode(30, 51.2, 9.4);
na.setNode(50, 49, 10);
g.edge(20, 50, 1, true).setWayGeometry(Helper.createPointList(51.25, 9.43));
g.edge(10, 20, 1, true);
g.edge(20, 30, 1, true);
LocationIndex index = createIndex(g, 2000);
assertEquals(20, findID(index, 51.25, 9.43));
}
use of com.graphhopper.storage.NodeAccess in project graphhopper by graphhopper.
the class LocationIndexTreeTest method createTestGraph2.
// see testgraph2.jpg
Graph createTestGraph2() {
Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false);
NodeAccess na = graph.getNodeAccess();
na.setNode(8, 49.94553, 11.57214);
na.setNode(9, 49.94553, 11.57314);
na.setNode(10, 49.94553, 11.57414);
na.setNode(11, 49.94553, 11.57514);
na.setNode(12, 49.94553, 11.57614);
na.setNode(13, 49.94553, 11.57714);
na.setNode(0, 49.94653, 11.57114);
na.setNode(1, 49.94653, 11.57214);
na.setNode(2, 49.94653, 11.57314);
na.setNode(3, 49.94653, 11.57414);
na.setNode(4, 49.94653, 11.57514);
na.setNode(5, 49.94653, 11.57614);
na.setNode(6, 49.94653, 11.57714);
na.setNode(7, 49.94653, 11.57814);
na.setNode(14, 49.94753, 11.57214);
na.setNode(15, 49.94753, 11.57314);
na.setNode(16, 49.94753, 11.57614);
na.setNode(17, 49.94753, 11.57814);
na.setNode(18, 49.94853, 11.57114);
na.setNode(19, 49.94853, 11.57214);
na.setNode(20, 49.94853, 11.57814);
na.setNode(21, 49.94953, 11.57214);
na.setNode(22, 49.94953, 11.57614);
na.setNode(23, 49.95053, 11.57114);
na.setNode(24, 49.95053, 11.57214);
na.setNode(25, 49.95053, 11.57314);
na.setNode(26, 49.95053, 11.57514);
na.setNode(27, 49.95053, 11.57614);
na.setNode(28, 49.95053, 11.57714);
na.setNode(29, 49.95053, 11.57814);
na.setNode(30, 49.95153, 11.57214);
na.setNode(31, 49.95153, 11.57314);
na.setNode(32, 49.95153, 11.57514);
na.setNode(33, 49.95153, 11.57614);
na.setNode(34, 49.95153, 11.57714);
na.setNode(34, 49.95153, 11.57714);
// to create correct bounds
// bottom left
na.setNode(100, 49.941, 11.56614);
// top right
na.setNode(101, 49.96053, 11.58814);
graph.edge(0, 1, 10, true);
graph.edge(1, 2, 10, true);
graph.edge(2, 3, 10, true);
graph.edge(3, 4, 10, true);
graph.edge(4, 5, 10, true);
graph.edge(6, 7, 10, true);
graph.edge(8, 2, 10, true);
graph.edge(9, 2, 10, true);
graph.edge(10, 3, 10, true);
graph.edge(11, 4, 10, true);
graph.edge(12, 5, 10, true);
graph.edge(13, 6, 10, true);
graph.edge(1, 14, 10, true);
graph.edge(2, 15, 10, true);
graph.edge(5, 16, 10, true);
graph.edge(14, 15, 10, true);
graph.edge(16, 17, 10, true);
graph.edge(16, 20, 10, true);
graph.edge(16, 25, 10, true);
graph.edge(18, 14, 10, true);
graph.edge(18, 19, 10, true);
graph.edge(18, 21, 10, true);
graph.edge(19, 21, 10, true);
graph.edge(21, 24, 10, true);
graph.edge(23, 24, 10, true);
graph.edge(24, 25, 10, true);
graph.edge(26, 27, 10, true);
graph.edge(27, 28, 10, true);
graph.edge(28, 29, 10, true);
graph.edge(24, 30, 10, true);
graph.edge(24, 31, 10, true);
graph.edge(26, 32, 10, true);
graph.edge(27, 33, 10, true);
graph.edge(28, 34, 10, true);
return graph;
}
use of com.graphhopper.storage.NodeAccess in project graphhopper by graphhopper.
the class LocationIndexTreeTest method testFindNClosest.
// 0---1---2
// | | |
// |10 | |
// | | | |
// 3-9-4---5
// | | |
// 6---7---8
@Test
public void testFindNClosest() {
Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false);
NodeAccess na = graph.getNodeAccess();
na.setNode(0, 0.0010, 0.0000);
na.setNode(1, 0.0010, 0.0005);
na.setNode(2, 0.0010, 0.0010);
na.setNode(3, 0.0005, 0.0000);
na.setNode(4, 0.0005, 0.0005);
na.setNode(5, 0.0005, 0.0010);
na.setNode(6, 0.0000, 0.0000);
na.setNode(7, 0.0000, 0.0005);
na.setNode(8, 0.0000, 0.0010);
na.setNode(9, 0.0005, 0.0002);
na.setNode(10, 0.0007, 0.0002);
graph.edge(0, 1);
graph.edge(1, 2);
graph.edge(0, 3);
graph.edge(1, 4);
graph.edge(2, 5);
graph.edge(3, 9);
graph.edge(9, 4);
EdgeIteratorState edge4_5 = graph.edge(4, 5);
graph.edge(10, 9);
graph.edge(3, 6);
EdgeIteratorState edge4_7 = graph.edge(4, 7);
graph.edge(5, 8);
graph.edge(6, 7);
graph.edge(7, 8);
LocationIndexTree index = createIndexNoPrepare(graph, 500);
index.prepareIndex();
// query node 4 => get at least 4-5, 4-7
List<QueryResult> result = index.findNClosest(0.0004, 0.0006, EdgeFilter.ALL_EDGES, 15);
List<Integer> ids = new ArrayList<Integer>();
for (QueryResult qr : result) {
ids.add(qr.getClosestEdge().getEdge());
}
Collections.sort(ids);
assertEquals("edge ids do not match", Arrays.asList(edge4_5.getEdge(), edge4_7.getEdge()), ids);
}
use of com.graphhopper.storage.NodeAccess in project graphhopper by graphhopper.
the class LocationIndexTreeTest method testCrossBoundaryNetwork_issue667.
// 0--1--2--3, the "cross boundary" edges are 1-2 and 5-6
// | | | |
// 4--5--6--7
@Test
public void testCrossBoundaryNetwork_issue667() {
Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false);
NodeAccess na = graph.getNodeAccess();
na.setNode(0, 0.1, 179.5);
na.setNode(1, 0.1, 179.9);
na.setNode(2, 0.1, -179.8);
na.setNode(3, 0.1, -179.5);
na.setNode(4, 0, 179.5);
na.setNode(5, 0, 179.9);
na.setNode(6, 0, -179.8);
na.setNode(7, 0, -179.5);
// just use 1 as distance which is incorrect but does not matter in this unit case
graph.edge(0, 1, 1, true);
graph.edge(0, 4, 1, true);
graph.edge(1, 5, 1, true);
graph.edge(4, 5, 1, true);
graph.edge(2, 3, 1, true);
graph.edge(2, 6, 1, true);
graph.edge(3, 7, 1, true);
graph.edge(6, 7, 1, true);
// as last edges: create cross boundary edges
// See #667 where the recommendation is to adjust the import and introduce two pillar nodes
// where the connection is cross boundary and would be okay if ignored as real length is 0
graph.edge(1, 2, 1, true).setWayGeometry(Helper.createPointList(0, 180, 0, -180));
// but this unit test succeeds even without this adjusted import:
graph.edge(5, 6, 1, true);
LocationIndexTree index = createIndexNoPrepare(graph, 500);
index.prepareIndex();
assertTrue(graph.getNodes() > 0);
for (int i = 0; i < graph.getNodes(); i++) {
QueryResult qr = index.findClosest(na.getLat(i), na.getLon(i), EdgeFilter.ALL_EDGES);
assertEquals(i, qr.getClosestNode());
}
}
use of com.graphhopper.storage.NodeAccess in project graphhopper by graphhopper.
the class RealtimeFeed method fromProtobuf.
public static RealtimeFeed fromProtobuf(Graph graph, GtfsStorage staticGtfs, PtFlagEncoder encoder, GtfsRealtime.FeedMessage feedMessage) {
String feedKey = "gtfs_0";
GTFSFeed feed = staticGtfs.getGtfsFeeds().get(feedKey);
// TODO: Require configuration of feed and agency this realtime feed is for.
// Realtime feeds are always specific to an agency.
Agency agency = feed.agency.values().iterator().next();
final IntHashSet blockedEdges = new IntHashSet();
final IntLongHashMap delaysForBoardEdges = new IntLongHashMap();
final IntLongHashMap delaysForAlightEdges = new IntLongHashMap();
final LinkedList<VirtualEdgeIteratorState> additionalEdges = new LinkedList<>();
final Graph overlayGraph = new Graph() {
int nNodes = 0;
int firstEdge = graph.getAllEdges().getMaxId() + 1;
final NodeAccess nodeAccess = new NodeAccess() {
IntIntHashMap additionalNodeFields = new IntIntHashMap();
@Override
public int getAdditionalNodeField(int nodeId) {
return 0;
}
@Override
public void setAdditionalNodeField(int nodeId, int additionalValue) {
additionalNodeFields.put(nodeId, additionalValue);
}
@Override
public boolean is3D() {
return false;
}
@Override
public int getDimension() {
return 0;
}
@Override
public void ensureNode(int nodeId) {
}
@Override
public void setNode(int nodeId, double lat, double lon) {
}
@Override
public void setNode(int nodeId, double lat, double lon, double ele) {
}
@Override
public double getLatitude(int nodeId) {
return 0;
}
@Override
public double getLat(int nodeId) {
return 0;
}
@Override
public double getLongitude(int nodeId) {
return 0;
}
@Override
public double getLon(int nodeId) {
return 0;
}
@Override
public double getElevation(int nodeId) {
return 0;
}
@Override
public double getEle(int nodeId) {
return 0;
}
};
@Override
public Graph getBaseGraph() {
return graph;
}
@Override
public int getNodes() {
return graph.getNodes() + nNodes;
}
@Override
public NodeAccess getNodeAccess() {
return nodeAccess;
}
@Override
public BBox getBounds() {
return null;
}
@Override
public EdgeIteratorState edge(int a, int b) {
return null;
}
@Override
public EdgeIteratorState edge(int a, int b, double distance, boolean bothDirections) {
int edge = firstEdge++;
final VirtualEdgeIteratorState newEdge = new VirtualEdgeIteratorState(-1, edge, a, b, distance, 0, "", new PointList());
final VirtualEdgeIteratorState reverseNewEdge = new VirtualEdgeIteratorState(-1, edge, b, a, distance, 0, "", new PointList());
newEdge.setReverseEdge(reverseNewEdge);
reverseNewEdge.setReverseEdge(newEdge);
additionalEdges.push(newEdge);
return newEdge;
}
@Override
public EdgeIteratorState getEdgeIteratorState(int edgeId, int adjNode) {
return null;
}
@Override
public AllEdgesIterator getAllEdges() {
return null;
}
@Override
public EdgeExplorer createEdgeExplorer(EdgeFilter filter) {
return null;
}
@Override
public EdgeExplorer createEdgeExplorer() {
return graph.createEdgeExplorer();
}
@Override
public Graph copyTo(Graph g) {
return null;
}
@Override
public GraphExtension getExtension() {
throw new RuntimeException();
}
};
Map<Integer, String> routes = new HashMap<>();
Map<GtfsStorage.Validity, Integer> operatingDayPatterns = new HashMap<>(staticGtfs.getOperatingDayPatterns());
Map<Integer, byte[]> tripDescriptors = new HashMap<>();
Map<Integer, Integer> stopSequences = new HashMap<>();
Map<String, int[]> boardEdgesForTrip = new HashMap<>();
Map<String, int[]> alightEdgesForTrip = new HashMap<>();
Map<GtfsStorage.FeedIdWithTimezone, Integer> writableTimeZones = new HashMap<>();
GtfsStorageI gtfsStorage = new GtfsStorageI() {
@Override
public Map<String, Fare> getFares() {
return null;
}
@Override
public Map<GtfsStorage.Validity, Integer> getOperatingDayPatterns() {
return operatingDayPatterns;
}
@Override
public Map<GtfsStorage.FeedIdWithTimezone, Integer> getWritableTimeZones() {
return writableTimeZones;
}
@Override
public Map<Integer, byte[]> getTripDescriptors() {
return tripDescriptors;
}
@Override
public Map<Integer, Integer> getStopSequences() {
return stopSequences;
}
@Override
public Map<String, int[]> getBoardEdgesForTrip() {
return boardEdgesForTrip;
}
@Override
public Map<String, int[]> getAlightEdgesForTrip() {
return alightEdgesForTrip;
}
@Override
public Map<String, GTFSFeed> getGtfsFeeds() {
HashMap<String, GTFSFeed> stringGTFSFeedHashMap = new HashMap<>();
stringGTFSFeedHashMap.put(feedKey, feed);
return stringGTFSFeedHashMap;
}
@Override
public Map<String, Transfers> getTransfers() {
return staticGtfs.getTransfers();
}
@Override
public Map<String, Integer> getStationNodes() {
return staticGtfs.getStationNodes();
}
@Override
public Map<Integer, String> getRoutes() {
return routes;
}
};
final GtfsReader gtfsReader = new GtfsReader(feedKey, overlayGraph, gtfsStorage, encoder, null);
Instant timestamp = Instant.ofEpochSecond(feedMessage.getHeader().getTimestamp());
// FIXME
LocalDate dateToChange = timestamp.atZone(ZoneId.of(agency.agency_timezone)).toLocalDate();
BitSet validOnDay = new BitSet();
LocalDate startDate = feed.calculateStats().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 -> {
String key = GtfsStorage.tripKey(tripUpdate.getTrip().getTripId(), tripUpdate.getTrip().getStartTime());
final int[] boardEdges = staticGtfs.getBoardEdgesForTrip().get(key);
final int[] leaveEdges = staticGtfs.getAlightEdgesForTrip().get(key);
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, agency, 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 = graph.getEdgeIteratorState(boardEdge, Integer.MIN_VALUE).getAdjNode();
int timeOffset = tripUpdate.getTrip().hasStartTime() ? LocalTime.parse(tripUpdate.getTrip().getStartTime()).toSecondOfDay() : 0;
int delayedBoardEdge = gtfsReader.addDelayedBoardEdge(ZoneId.of(agency.agency_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(ZoneId.of(agency.agency_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(ZoneId.of(agency.agency_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(ZoneId.of(agency.agency_timezone), 0, new ArrayList<>(), tripWithStopTimes, tripUpdate.getTrip());
});
gtfsReader.wireUpStops();
return new RealtimeFeed(staticGtfs, feed, agency, feedMessage, blockedEdges, delaysForBoardEdges, delaysForAlightEdges, additionalEdges, tripDescriptors, stopSequences, operatingDayPatterns);
}
Aggregations