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;
}
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;
}
use of com.conveyal.gtfs.GTFSFeed in project graphhopper by graphhopper.
the class FareTest method parseFares.
private static Map<String, Fare> parseFares(String fareAttributes, String fareRules) {
GTFSFeed feed = new GTFSFeed();
HashMap<String, Fare> fares = new HashMap<>();
new FixedFareAttributeLoader(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;
}
Aggregations