reader.setEntityStore(store);
reader.run();
Logger.info("GtfsImporter: importing agencies...");
GtfsSnapshotMergeTask agencyTask = new GtfsSnapshotMergeTask(snapshotMerge);
agencyTask.startTask();
List<Agency> agencies = Agency.findAll();
for (Agency agency : agencies)
{
agencyIdMap.put(agency.gtfsAgencyId, new BigInteger(agency.id.toString()));
}
BigInteger primaryAgencyId = null;
for (org.onebusaway.gtfs.model.Agency gtfsAgency : reader.getAgencies()) {
if(!agencyIdMap.containsKey(gtfsAgency.getId()))
{
Agency agency = new Agency(gtfsAgency);
agency.save();
agencyIdMap.put(agency.gtfsAgencyId, BigInteger.valueOf(agency.id));
primaryAgencyId = BigInteger.valueOf(agency.id);
agencyCount++;
}
else
primaryAgencyId = agencyIdMap.get(gtfsAgency.getId());
}
Agency primaryAgency = Agency.findById(primaryAgencyId.longValue());
agencyTask.completeTask("Imported " + agencyCount + " agencies.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info("Agencies loaded: " + agencyCount.toString());
Logger.info("GtfsImporter: importing routes...");
GtfsSnapshotMergeTask routeTask = new GtfsSnapshotMergeTask(snapshotMerge);
routeTask.startTask();
for (org.onebusaway.gtfs.model.Route gtfsRoute : store.getAllRoutes()) {
BigInteger agencyId = agencyIdMap.get(gtfsRoute.getAgency().getId());
BigInteger routeId = Route.nativeInsert(snapshotMerge.em(), gtfsRoute, agencyId);
routeIdMap.put(gtfsRoute.getId().toString(), routeId );
routeCount++;
}
if(agencyCount > 1)
primaryAgencyId = null;
routeTask.completeTask("Imported " + routeCount + " routes.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info("Routes loaded:" + routeCount.toString());
Logger.info("GtfsImporter: importing stops...");
GtfsSnapshotMergeTask stopTask = new GtfsSnapshotMergeTask(snapshotMerge);
stopTask.startTask();
for (org.onebusaway.gtfs.model.Stop gtfsStop : store.getAllStops()) {
BigInteger stopId = Stop.nativeInsert(snapshotMerge.em(), gtfsStop, primaryAgencyId);
stopIdMap.put(gtfsStop.getId().toString(), stopId );
stopCount++;
}
stopTask.completeTask("Imported " + stopCount + " stops.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info("Stops loaded: " + stopCount);
Logger.info("GtfsImporter: importing Shapes...");
Logger.info("Calculating agency centroid for stops...");
Point centroid = Stop.findCentroid(primaryAgencyId);
Logger.info("Center: " + centroid.getCoordinate().y + ", " + centroid.getCoordinate().x);
primaryAgency.defaultLat = centroid.getCoordinate().y;
primaryAgency.defaultLon = centroid.getCoordinate().x;
primaryAgency.save();
GtfsSnapshotMergeTask tripShapeTask = new GtfsSnapshotMergeTask(snapshotMerge);
tripShapeTask.startTask();
// import points
for (org.onebusaway.gtfs.model.ShapePoint shapePoint : store.getAllShapePoints()) {
List<org.onebusaway.gtfs.model.ShapePoint> shapePoints = shapePointIdMap.get(shapePoint.getShapeId().toString());
if(shapePoints != null)
{
shapePoints.add(shapePoint);
}
else
{
shapePoints = new ArrayList<org.onebusaway.gtfs.model.ShapePoint>();
shapePoints.add(shapePoint);
shapePointIdMap.put(shapePoint.getShapeId().toString(), shapePoints);
}
shapePointCount++;
}
// sort/load points
for(String gtfsShapeId : shapePointIdMap.keySet())
{
List<org.onebusaway.gtfs.model.ShapePoint> shapePoints = shapePointIdMap.get(gtfsShapeId);
Collections.sort(shapePoints);
Double describedDistance = new Double(0);
List<String> points = new ArrayList<String>();
for(org.onebusaway.gtfs.model.ShapePoint shapePoint : shapePoints)
{
describedDistance += shapePoint.getDistTraveled();
points.add(new Double(shapePoint.getLon()).toString() + " " + new Double(shapePoint.getLat()).toString());
}
String linestring = "LINESTRING(" + StringUtils.join(points, ", ") + ")";
BigInteger tripShapeId = TripShape.nativeInsert(snapshotMerge.em(), gtfsShapeId, linestring, describedDistance);
tripShapeIdMap.put(gtfsShapeId, tripShapeId);
shapeCount++;
}
Logger.info("Shape points loaded: " + shapePointCount.toString());
Logger.info("Shapes loaded: " + shapeCount.toString());
tripShapeTask.completeTask("Imported " + shapePointCount + " points in " + shapeCount + " shapes.", GtfsSnapshotMergeTaskStatus.SUCCESS);
GtfsSnapshotMergeTask serviceCalendarsTask = new GtfsSnapshotMergeTask(snapshotMerge);
serviceCalendarsTask.startTask();
Logger.info("GtfsImporter: importing Service Calendars...");
for (org.onebusaway.gtfs.model.ServiceCalendar gtfsService : store.getAllCalendars()) {
BigInteger serviceId = ServiceCalendar.nativeInsert(snapshotMerge.em(), gtfsService, primaryAgencyId);
serviceIdMap.put(gtfsService.getServiceId().toString(), serviceId);
serviceCalendarCount++;
}
Logger.info("Service calendars loaded: " + serviceCalendarCount);
serviceCalendarsTask.completeTask("Imported " + serviceCalendarCount.toString() + " Service calendars.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info("GtfsImporter: importing Service Calendar dates...");
GtfsSnapshotMergeTask serviceCalendarDatesTask = new GtfsSnapshotMergeTask(snapshotMerge);
serviceCalendarDatesTask.startTask();
for (org.onebusaway.gtfs.model.ServiceCalendarDate gtfsServiceDate : store.getAllCalendarDates()) {
BigInteger serviceDateId = ServiceCalendarDate.nativeInsert(snapshotMerge.em(), gtfsServiceDate);
serviceDateIdMap.put(gtfsServiceDate.getServiceId().toString(), serviceDateId);
serviceCalendarDateCount++;
}
serviceCalendarDatesTask.completeTask("Imported " + serviceCalendarDateCount.toString() + " Service calendar dates.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info(" loaded: " + serviceCalendarDateCount);
Logger.info("GtfsImporter: importing trips...");
GtfsSnapshotMergeTask tripsTask = new GtfsSnapshotMergeTask(snapshotMerge);
tripsTask.startTask();
for (org.onebusaway.gtfs.model.Trip gtfsTrip : store.getAllTrips()) {
BigInteger routeId = routeIdMap.get(gtfsTrip.getRoute().getId().toString());
BigInteger shapeId = null;
if(gtfsTrip.getShapeId() != null)
shapeId = tripShapeIdMap.get(gtfsTrip.getShapeId().toString());
BigInteger serviceId = serviceIdMap.containsKey(gtfsTrip.getServiceId().toString()) ? serviceIdMap.get(gtfsTrip.getServiceId().toString()) : null;
BigInteger serviceDateId = serviceDateIdMap.containsKey(gtfsTrip.getServiceId().toString()) ? serviceDateIdMap.get(gtfsTrip.getServiceId().toString()) : null;
BigInteger tripId = Trip.nativeInsert(snapshotMerge.em(), gtfsTrip, routeId, shapeId, serviceId, serviceDateId);
tripIdMap.put(gtfsTrip.getId().toString(), tripId);
tripCount++;
}
Logger.info("Trips loaded: " + tripCount);
tripsTask.completeTask("Imported " + tripCount.toString() + " trips.", GtfsSnapshotMergeTaskStatus.SUCCESS);
Logger.info("GtfsImporter: importing stopTimes...");
GtfsSnapshotMergeTask stopTimesTask = new GtfsSnapshotMergeTask(snapshotMerge);
stopTimesTask.startTask();
for (org.onebusaway.gtfs.model.StopTime gtfsStopTime : store.getAllStopTimes()) {
BigInteger stopId = stopIdMap.get(gtfsStopTime.getStop().getId().toString());
BigInteger tripId = tripIdMap.get(gtfsStopTime.getTrip().getId().toString());
BigInteger stopTimeId = StopTime.nativeInsert(snapshotMerge.em(), gtfsStopTime, tripId, stopId);
StopSequence stopSequence = new StopSequence(stopId, gtfsStopTime.getStopSequence());
if(!tripStopTimeMap.containsKey(tripId))
tripStopTimeMap.put(tripId, new ArrayList<StopSequence>());
tripStopTimeMap.get(tripId).add(stopSequence);
stopTimeCount++;
}
Logger.info("StopTimes loaded: " + stopTimeCount.toString());
stopTimesTask.completeTask("Imported " + stopTimeCount.toString() + " stop times.", GtfsSnapshotMergeTaskStatus.SUCCESS);
String mergeDescription = new String("Imported GTFS file: " + agencyCount + " agencies; " + routeCount + " routes;" + stopCount + " stops; " + stopTimeCount + " stopTimes; " + tripCount + " trips;" + shapePointCount + " shapePoints");
snapshotMerge.complete(mergeDescription);