use of org.onebusaway.gtfs.model.Agency in project onebusaway-application-modules by camsys.
the class TripEntriesFactoryTest method test.
@Test
public void test() {
GtfsRelationalDao gtfsDao = Mockito.mock(GtfsRelationalDao.class);
Agency agency = new Agency();
agency.setId("1");
agency.setTimezone("America/Los_Angeles");
// gtfsDao.saveEntity(agency);
Route route = new Route();
route.setId(new AgencyAndId("1", "routeA"));
route.setAgency(agency);
Mockito.when(gtfsDao.getAllRoutes()).thenReturn(Arrays.asList(route));
AgencyAndId shapeId = new AgencyAndId("1", "shapeId");
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "tripA"));
trip.setRoute(route);
trip.setServiceId(new AgencyAndId("1", "serviceId"));
trip.setShapeId(shapeId);
Mockito.when(gtfsDao.getTripsForRoute(route)).thenReturn(Arrays.asList(trip));
Stop stopA = new Stop();
stopA.setId(aid("stopA"));
StopTime stA = new StopTime();
stA.setId(100);
stA.setArrivalTime(time(9, 00));
stA.setDepartureTime(time(9, 05));
stA.setStopSequence(100);
stA.setStop(stopA);
stA.setTrip(trip);
Stop stopB = new Stop();
stopB.setId(aid("stopB"));
StopTime stB = new StopTime();
stB.setId(101);
stB.setArrivalTime(time(10, 00));
stB.setDepartureTime(time(10, 05));
stB.setStopSequence(102);
stB.setStop(stopB);
stB.setTrip(trip);
Mockito.when(gtfsDao.getStopTimesForTrip(trip)).thenReturn(Arrays.asList(stA, stB));
TransitGraphImpl graph = new TransitGraphImpl();
graph.putStopEntry(stop("stopA", 47.672207391799056, -122.387855896286));
graph.putStopEntry(stop("stopB", 47.66852277218285, -122.3853882639923));
RouteEntryImpl routeEntry = route("routeA");
graph.putRouteEntry(routeEntry);
graph.initialize();
ShapePointsFactory shapePointsFactory = new ShapePointsFactory();
shapePointsFactory.addPoint(47.673840100841396, -122.38756621771239);
shapePointsFactory.addPoint(47.668667271970484, -122.38756621771239);
shapePointsFactory.addPoint(47.66868172192725, -122.3661729186096);
ShapePoints shapePoints = shapePointsFactory.create();
ShapePointHelper shapePointHelper = Mockito.mock(ShapePointHelper.class);
Mockito.when(shapePointHelper.getShapePointsForShapeId(shapeId)).thenReturn(shapePoints);
TripEntriesFactory factory = new TripEntriesFactory();
factory.setGtfsDao(gtfsDao);
factory.setShapePointHelper(shapePointHelper);
factory.setUniqueService(new UniqueServiceImpl());
StopTimeEntriesFactory stopTimeEntriesFactory = new StopTimeEntriesFactory();
stopTimeEntriesFactory.setDistanceAlongShapeLibrary(new DistanceAlongShapeLibrary());
factory.setStopTimeEntriesFactory(stopTimeEntriesFactory);
factory.processTrips(graph);
TripEntryImpl entry = graph.getTripEntryForId(trip.getId());
assertEquals(trip.getId(), entry.getId());
assertEquals(route.getId(), entry.getRoute().getId());
assertEquals(lsid("serviceId"), entry.getServiceId());
assertEquals(trip.getShapeId(), entry.getShapeId());
assertEquals(2177.1, entry.getTotalTripDistance(), 0.1);
List<StopTimeEntry> stopTimes = entry.getStopTimes();
assertEquals(2, stopTimes.size());
for (StopTimeEntry stopTime : stopTimes) {
assertSame(entry, stopTime.getTrip());
}
}
use of org.onebusaway.gtfs.model.Agency in project onebusaway-application-modules by camsys.
the class AgencyEntriesFactory method processAgencies.
public void processAgencies(TransitGraphImpl graph) {
Collection<Agency> agencies = _gtfsDao.getAllAgencies();
for (Agency agency : agencies) {
AgencyEntryImpl agencyEntry = new AgencyEntryImpl();
agencyEntry.setId(_uniqueService.unique(agency.getId()));
graph.putAgencyEntry(agencyEntry);
}
graph.refreshAgencyMapping();
}
use of org.onebusaway.gtfs.model.Agency in project gtfs-realtime-validator by CUTR-at-USF.
the class BatchProcessor method processFeeds.
/**
* Process the GTFS and GTFS-realtime feeds provided in the constructor. If setReturnStatistics() is set to true,
* the method will return a list of IterationStatistics (one per GTFS-rt file) for performance in the batch
* validation. By default this method will return null to avoid memory issues with extremely large batch processes.
*
* @return If setReturnStatistics() is set to true, it will return a list of IterationStatistics (one per GTFS-rt
* file) for performance in the batch validation. By default this method will return null to avoid memory issues
* when processing an extremely large number of files.
* @throws NoSuchAlgorithmException If the MD5 hash algorithm (used to determine feed uniqueness) is not available on the machine executing the code
* @throws IOException If the GTFS or GTFS-realtime files cannot be read or the results cannot be written to disk
*/
public List<IterationStatistics> processFeeds() throws NoSuchAlgorithmException, IOException {
// Read GTFS data into a GtfsDaoImpl
_log.info("Starting batch processor...");
if (mReturnStatistics) {
mIterationStatistics = new ArrayList<>();
}
String timeZoneText = null;
double gtfsReadTime = readGtfsData();
Collection<Agency> agencies = mGtfsData.getAllAgencies();
for (Agency agency : agencies) {
timeZoneText = agency.getTimezone();
break;
}
mGtfsMetadata = new GtfsMetadata(mPathToGtfsFile.getAbsolutePath(), TimeZone.getTimeZone(timeZoneText), mGtfsData, mIgnoreShapes);
// Initialize validation rules
synchronized (mValidationRules) {
if (mValidationRules.isEmpty()) {
mValidationRules.add(new CrossFeedDescriptorValidator());
mValidationRules.add(new VehicleValidator());
mValidationRules.add(new TimestampValidator());
mValidationRules.add(new StopTimeUpdateValidator());
mValidationRules.add(new TripDescriptorValidator());
mValidationRules.add(new StopValidator());
mValidationRules.add(new FrequencyTypeZeroValidator());
mValidationRules.add(new FrequencyTypeOneValidator());
mValidationRules.add(new HeaderValidator());
}
}
// Configure output
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
_log.info("Sorting GTFS-rt files by " + mSortBy.name() + "...");
// Read GTFS-rt protobuf files from provided directory
List<Path> paths = Files.walk(Paths.get(mPathToGtfsRealtime)).filter(Files::isRegularFile).sorted((o1, o2) -> {
if (mSortBy.equals(SortBy.DATE_MODIFIED)) {
try {
// Sort by date modified (ascending) (it seems more consistent cross-platform than "date created")
return SortUtils.compareByDateModified(o1, o2);
} catch (IOException e) {
_log.error("Can't sort GTFS-rt files by date - assuming dates are equal: " + e);
}
// Assume file dates are equal if we get an exception
return 0;
} else {
// Sort by name (ascending)
return SortUtils.compareByFileName(o1, o2);
}
}).collect(Collectors.toList());
MessageDigest md = MessageDigest.getInstance("MD5");
GtfsRealtime.FeedMessage prevMessage = null;
byte[] prevHash = null;
for (Path path : paths) {
IterationStatistics stats = null;
if (mReturnStatistics) {
stats = new IterationStatistics();
stats.setGtfsReadTime(gtfsReadTime);
}
long startTimeNanos = System.nanoTime();
long startToByteArray = System.nanoTime();
byte[] protobuf;
try {
protobuf = IOUtils.toByteArray(Files.newInputStream(path));
} catch (IOException e) {
_log.error("Error reading GTFS-rt file to byte array, skipping to next file: " + e);
continue;
}
double toByteArray = getElapsedTime(startToByteArray, System.nanoTime());
_log.info("Read " + path.getFileName() + " to byte array in " + getElapsedTimeString(toByteArray));
if (mReturnStatistics) {
stats.setToByteArrayTime(toByteArray);
}
byte[] currentHash = md.digest(protobuf);
if (MessageDigest.isEqual(currentHash, prevHash)) {
// This feed file is a duplicate of the last one - skip to next file
continue;
}
long timestamp;
if (mSortBy.equals(SortBy.DATE_MODIFIED)) {
// Use file last modified date as "current" timestamp
timestamp = Files.getLastModifiedTime(path).toMillis();
} else {
// Use time parsed from file name as "current" timestamp
try {
timestamp = TimestampUtils.getTimestampFromFileName(path.toFile().getName());
} catch (DateTimeParseException | StringIndexOutOfBoundsException e) {
_log.error("Couldn't parse timestamp from file name '" + path.toFile().getName() + "' - using date modified instead: " + e);
timestamp = Files.getLastModifiedTime(path).toMillis();
}
}
long startProtobufDecode = System.nanoTime();
GtfsRealtime.FeedMessage message;
try {
message = GtfsRealtime.FeedMessage.parseFrom(protobuf);
} catch (InvalidProtocolBufferException e) {
_log.error("Error reading GTFS-rt message from byte array, skipping to next file: " + e);
continue;
}
double pbDecode = getElapsedTime(startProtobufDecode, System.nanoTime());
_log.info("Decoded " + path.getFileName() + " protobuf in " + getElapsedTimeString(pbDecode));
if (mReturnStatistics) {
stats.setDecodeProtobufTime(pbDecode);
}
GtfsRealtime.FeedMessage combinedMessage = null;
// See if more than one entity type exists in this feed
if (GtfsUtils.isCombinedFeed(message)) {
// Run CrossFeedDescriptorValidator on this message
combinedMessage = message;
}
List<ErrorListHelperModel> allErrorLists = new ArrayList<>();
StringBuilder consoleOutput = new StringBuilder();
List<RuleStatistics> ruleStatistics = null;
if (mReturnStatistics) {
ruleStatistics = new ArrayList<>();
}
for (FeedEntityValidator rule : mValidationRules) {
long startRuleNanos = System.nanoTime();
List<ErrorListHelperModel> errorLists = rule.validate(timestamp, mGtfsData, mGtfsMetadata, message, prevMessage, combinedMessage);
allErrorLists.addAll(errorLists);
double ruleExecutionTime = getElapsedTime(startRuleNanos, System.nanoTime());
consoleOutput.append("\n" + rule.getClass().getSimpleName() + " - rule = " + getElapsedTimeString(ruleExecutionTime));
if (mReturnStatistics) {
RuleStatistics ruleStat = new RuleStatistics();
ruleStat.setRuleExecutionTime(ruleExecutionTime);
ruleStat.setValidator(rule.getClass().getSimpleName());
ruleStatistics.add(ruleStat);
}
}
double totalIterationTime = getElapsedTime(startTimeNanos, System.nanoTime());
consoleOutput.append("\nProcessed " + path.getFileName() + " in " + getElapsedTimeString(totalIterationTime));
consoleOutput.append("\n---------------------");
_log.info(consoleOutput.toString());
if (mReturnStatistics) {
stats.setRuleStatistics(ruleStatistics);
stats.setTotalIterationTime(totalIterationTime);
}
// Write validation results for this file to JSON
writeResults(mapper, path, allErrorLists);
if (mPlainTextExtension != null) {
// Write plain text version of protocol buffer
writePlainText(message, mapper, path);
}
if (mReturnStatistics) {
mIterationStatistics.add(stats);
}
prevHash = currentHash;
prevMessage = message;
}
return mIterationStatistics;
}
use of org.onebusaway.gtfs.model.Agency in project OpenTripPlanner by opentripplanner.
the class AgencyAdapter method unmarshal.
@Override
public Agency unmarshal(AgencyType arg) throws Exception {
if (arg == null) {
return null;
}
Agency a = new Agency();
a.setId(arg.id);
a.setName(arg.name);
a.setUrl(arg.url);
a.setTimezone(arg.timezone);
a.setLang(arg.lang);
a.setPhone(arg.phone);
a.setFareUrl(arg.fareUrl);
return new Agency(a);
}
use of org.onebusaway.gtfs.model.Agency in project OpenTripPlanner by opentripplanner.
the class Graph method getTimeZone.
/**
* Returns the time zone for the first agency in this graph. This is used to interpret times in API requests. The JVM default time zone cannot be
* used because we support multiple graphs on one server via the routerId. Ideally we would want to interpret times in the time zone of the
* geographic location where the origin/destination vertex or board/alight event is located. This may become necessary when we start making graphs
* with long distance train, boat, or air services.
*/
public TimeZone getTimeZone() {
if (timeZone == null) {
Collection<Agency> agencies = null;
if (agenciesForFeedId.entrySet().size() > 0) {
agencies = agenciesForFeedId.entrySet().iterator().next().getValue();
}
if (agencies == null || agencies.size() == 0) {
timeZone = TimeZone.getTimeZone("GMT");
LOG.warn("graph contains no agencies (yet); API request times will be interpreted as GMT.");
} else {
CalendarService cs = this.getCalendarService();
for (Agency agency : agencies) {
TimeZone tz = cs.getTimeZoneForAgencyId(agency.getId());
if (timeZone == null) {
LOG.debug("graph time zone set to {}", tz);
timeZone = tz;
} else if (!timeZone.equals(tz)) {
LOG.error("agency time zone differs from graph time zone: {}", tz);
}
}
}
}
return timeZone;
}
Aggregations