use of org.opentripplanner.routing.alertpatch.Alert in project OpenTripPlanner by opentripplanner.
the class AlertsUpdateHandler method handleAlert.
private void handleAlert(String id, GtfsRealtime.Alert alert) {
Alert alertText = new Alert();
alertText.alertDescriptionText = deBuffer(alert.getDescriptionText());
alertText.alertHeaderText = deBuffer(alert.getHeaderText());
alertText.alertUrl = deBuffer(alert.getUrl());
ArrayList<TimePeriod> periods = new ArrayList<TimePeriod>();
if (alert.getActivePeriodCount() > 0) {
long bestStartTime = Long.MAX_VALUE;
long lastEndTime = Long.MIN_VALUE;
for (TimeRange activePeriod : alert.getActivePeriodList()) {
final long realStart = activePeriod.hasStart() ? activePeriod.getStart() : 0;
final long start = activePeriod.hasStart() ? realStart - earlyStart : 0;
if (realStart > 0 && realStart < bestStartTime) {
bestStartTime = realStart;
}
final long end = activePeriod.hasEnd() ? activePeriod.getEnd() : Long.MAX_VALUE;
if (end > lastEndTime) {
lastEndTime = end;
}
periods.add(new TimePeriod(start, end));
}
if (bestStartTime != Long.MAX_VALUE) {
alertText.effectiveStartDate = new Date(bestStartTime * 1000);
}
if (lastEndTime != Long.MIN_VALUE) {
alertText.effectiveEndDate = new Date(lastEndTime * 1000);
}
} else {
// Per the GTFS-rt spec, if an alert has no TimeRanges, than it should always be shown.
periods.add(new TimePeriod(0, Long.MAX_VALUE));
}
for (EntitySelector informed : alert.getInformedEntityList()) {
if (fuzzyTripMatcher != null && informed.hasTrip()) {
TripDescriptor trip = fuzzyTripMatcher.match(feedId, informed.getTrip());
informed = informed.toBuilder().setTrip(trip).build();
}
String patchId = createId(id, informed);
String routeId = null;
if (informed.hasRouteId()) {
routeId = informed.getRouteId();
}
int direction;
if (informed.hasTrip() && informed.getTrip().hasDirectionId()) {
direction = informed.getTrip().getDirectionId();
} else {
direction = -1;
}
// TODO: The other elements of a TripDescriptor are ignored...
String tripId = null;
if (informed.hasTrip() && informed.getTrip().hasTripId()) {
tripId = informed.getTrip().getTripId();
}
String stopId = null;
if (informed.hasStopId()) {
stopId = informed.getStopId();
}
String agencyId = informed.getAgencyId();
if (informed.hasAgencyId()) {
agencyId = informed.getAgencyId().intern();
}
AlertPatch patch = new AlertPatch();
patch.setFeedId(feedId);
if (routeId != null) {
patch.setRoute(new AgencyAndId(feedId, routeId));
// Makes no sense to set direction if we don't have a route
if (direction != -1) {
patch.setDirectionId(direction);
}
}
if (tripId != null) {
patch.setTrip(new AgencyAndId(feedId, tripId));
}
if (stopId != null) {
patch.setStop(new AgencyAndId(feedId, stopId));
}
if (agencyId != null && routeId == null && tripId == null && stopId == null) {
patch.setAgencyId(agencyId);
}
patch.setTimePeriods(periods);
patch.setAlert(alertText);
patch.setId(patchId);
patchIds.add(patchId);
alertPatchService.apply(patch);
}
}
use of org.opentripplanner.routing.alertpatch.Alert in project OpenTripPlanner by opentripplanner.
the class AlertPatchServiceImplTest method setup.
@Before
public void setup() {
alerts = new TestAlertPatch[] { new TestAlertPatch(), new TestAlertPatch(), new TestAlertPatch(), new TestAlertPatch() };
alerts[0].setRoute(testRoute);
alerts[0].setStop(testStop);
alerts[0].setAlert(new Alert());
alerts[1].setAlert(new Alert());
alerts[2].setAlert(new Alert());
alerts[3].setAlert(new Alert());
alerts[0].setId("0");
alerts[1].setId("1");
alerts[2].setId("2");
alerts[3].setId("3");
}
use of org.opentripplanner.routing.alertpatch.Alert in project OpenTripPlanner by opentripplanner.
the class ShapefileStreetModule method buildGraph.
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
try {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = _featureSourceFactory.getFeatureSource();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem worldCRS = factory.createCoordinateReferenceSystem("EPSG:4326");
Query query = new Query();
query.setCoordinateSystem(sourceCRS);
query.setCoordinateSystemReproject(worldCRS);
FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(query);
features = featureSource.getFeatures(query);
HashMap<String, HashMap<Coordinate, Integer>> intersectionNameToId = new HashMap<String, HashMap<Coordinate, Integer>>();
SimpleFeatureConverter<String> streetIdConverter = _schema.getIdConverter();
SimpleFeatureConverter<String> streetNameConverter = _schema.getNameConverter();
SimpleFeatureConverter<P2<StreetTraversalPermission>> permissionConverter = _schema.getPermissionConverter();
SimpleFeatureConverter<String> noteConverter = _schema.getNoteConverter();
HashMap<Coordinate, IntersectionVertex> intersectionsByLocation = new HashMap<Coordinate, IntersectionVertex>();
SimpleFeatureConverter<P2<Double>> safetyConverter = _schema.getBicycleSafetyConverter();
SimpleFeatureConverter<Boolean> slopeOverrideCoverter = _schema.getSlopeOverrideConverter();
SimpleFeatureConverter<Boolean> featureSelector = _schema.getFeatureSelector();
// Keep track of features that are duplicated so we don't have duplicate streets
Set<Object> seen = new HashSet<Object>();
List<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
FeatureIterator<SimpleFeature> it2 = features.features();
while (it2.hasNext()) {
SimpleFeature feature = it2.next();
if (featureSelector != null && !featureSelector.convert(feature)) {
continue;
}
featureList.add(feature);
}
it2.close();
it2 = null;
HashMap<Coordinate, TreeSet<String>> coordinateToStreetNames = getCoordinatesToStreetNames(featureList);
for (SimpleFeature feature : featureList) {
if (feature.getDefaultGeometry() == null) {
log.warn("feature has no geometry: " + feature.getIdentifier());
continue;
}
LineString geom = toLineString((Geometry) feature.getDefaultGeometry());
Object o = streetIdConverter.convert(feature);
String label = "" + o;
if (o != null && seen.contains(label)) {
continue;
}
seen.add(label);
String name = streetNameConverter.convert(feature);
Coordinate[] coordinates = geom.getCoordinates();
if (coordinates.length < 2) {
// not a real linestring
log.warn("Bad geometry for street with label " + label + " name " + name);
continue;
}
// this rounding is a total hack, to work around
// http://jira.codehaus.org/browse/GEOT-2811
Coordinate startCoordinate = new Coordinate(Math.round(coordinates[0].x * 1048576) / 1048576.0, Math.round(coordinates[0].y * 1048576) / 1048576.0);
Coordinate endCoordinate = new Coordinate(Math.round(coordinates[coordinates.length - 1].x * 1048576) / 1048576.0, Math.round(coordinates[coordinates.length - 1].y * 1048576) / 1048576.0);
String startIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, startCoordinate);
if (startIntersectionName == "null") {
log.warn("No intersection name for " + name);
}
String endIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, endCoordinate);
IntersectionVertex startIntersection = intersectionsByLocation.get(startCoordinate);
if (startIntersection == null) {
startIntersection = new IntersectionVertex(graph, startIntersectionName, startCoordinate.x, startCoordinate.y, new NonLocalizedString(startIntersectionName));
intersectionsByLocation.put(startCoordinate, startIntersection);
}
IntersectionVertex endIntersection = intersectionsByLocation.get(endCoordinate);
if (endIntersection == null) {
endIntersection = new IntersectionVertex(graph, endIntersectionName, endCoordinate.x, endCoordinate.y, new NonLocalizedString(endIntersectionName));
intersectionsByLocation.put(endCoordinate, endIntersection);
}
double length = 0;
for (int i = 0; i < coordinates.length - 1; ++i) {
length += JTS.orthodromicDistance(coordinates[i], coordinates[i + 1], worldCRS);
}
P2<StreetTraversalPermission> permissions = permissionConverter.convert(feature);
// TODO Set appropriate car speed from shapefile source.
StreetEdge street = edgeFactory.createEdge(startIntersection, endIntersection, geom, new NonLocalizedString(name), length, permissions.first, false);
LineString reversed = (LineString) geom.reverse();
StreetEdge backStreet = edgeFactory.createEdge(endIntersection, startIntersection, reversed, new NonLocalizedString(name), length, permissions.second, true);
backStreet.shareData(street);
if (noteConverter != null) {
String note = noteConverter.convert(feature);
if (note != null && note.length() > 0) {
Alert noteAlert = Alert.createSimpleAlerts(note);
graph.streetNotesService.addStaticNote(street, noteAlert, StreetNotesService.ALWAYS_MATCHER);
graph.streetNotesService.addStaticNote(backStreet, noteAlert, StreetNotesService.ALWAYS_MATCHER);
}
}
boolean slopeOverride = slopeOverrideCoverter.convert(feature);
street.setSlopeOverride(slopeOverride);
backStreet.setSlopeOverride(slopeOverride);
if (safetyConverter != null) {
P2<Double> safetyFactors = safetyConverter.convert(feature);
if (safetyFactors != null) {
street.setBicycleSafetyFactor(safetyFactors.first.floatValue());
backStreet.setBicycleSafetyFactor(safetyFactors.second.floatValue());
}
}
}
} catch (Exception ex) {
throw new IllegalStateException("error loading shapefile street data", ex);
} finally {
_featureSourceFactory.cleanup();
}
}
use of org.opentripplanner.routing.alertpatch.Alert in project OpenTripPlanner by opentripplanner.
the class NoteProperties method generateNote.
public T2<Alert, NoteMatcher> generateNote(OSMWithTags way) {
Alert note = new Alert();
// TODO: this could probably be made without patternMatch for {} since all notes (at least currently) have {note} as notePattern
if (patternMatcher.matcher(notePattern).matches()) {
// This gets language -> translation of notePattern and all tags (which can have translations name:en for example)
Map<String, String> noteText = TemplateLibrary.generateI18N(notePattern, way);
note.alertHeaderText = TranslatedString.getI18NString(noteText);
} else {
note.alertHeaderText = new LocalizedString(notePattern, way);
}
return new T2<>(note, noteMatcher);
}
use of org.opentripplanner.routing.alertpatch.Alert in project OpenTripPlanner by opentripplanner.
the class GraphPathToTripPlanConverter method addModeAndAlerts.
/**
* Add mode and alerts fields to a {@link Leg}.
*
* @param leg The leg to add the mode and alerts to
* @param states The states that go with the leg
*/
private static void addModeAndAlerts(Graph graph, Leg leg, State[] states, boolean disableAlertFiltering, Locale requestedLocale) {
for (State state : states) {
TraverseMode mode = state.getBackMode();
Set<Alert> alerts = graph.streetNotesService.getNotes(state);
Edge edge = state.getBackEdge();
if (mode != null) {
leg.mode = mode.toString();
}
if (alerts != null) {
for (Alert alert : alerts) {
leg.addAlert(alert, requestedLocale);
}
}
for (AlertPatch alertPatch : graph.getAlertPatches(edge)) {
if (disableAlertFiltering || alertPatch.displayDuring(state)) {
if (alertPatch.hasTrip()) {
// this leg.
if (alertPatch.getTrip().equals(leg.tripId)) {
leg.addAlert(alertPatch.getAlert(), requestedLocale);
}
} else {
// If we are not matching a particular trip add all known alerts for this trip pattern.
leg.addAlert(alertPatch.getAlert(), requestedLocale);
}
}
}
}
}
Aggregations