Skip to content

Commit

Permalink
fix: properly reading PublicTransitEvent objects from events file dur…
Browse files Browse the repository at this point in the history
…ing analysis

Now adding a CustomEventMapper to the event readers that converts GenericEvent objects to PublicTransitEvent ones. This is done by the new convert method in the latter class.
  • Loading branch information
Tarek Chouaki committed Dec 15, 2023
1 parent f7b51f0 commit 97115ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;

import org.eqasim.core.components.transit.events.PublicTransitEvent;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.MatsimEventsReader;
Expand All @@ -16,7 +17,9 @@ public LegReaderFromEvents(LegListener legListener) {
public Collection<LegItem> readLegs(String eventsPath) {
EventsManager eventsManager = EventsUtils.createEventsManager();
eventsManager.addHandler(legListener);
new MatsimEventsReader(eventsManager).readFile(eventsPath);
MatsimEventsReader matsimEventsReader = new MatsimEventsReader(eventsManager);
matsimEventsReader.addCustomEventMapper(PublicTransitEvent.TYPE, PublicTransitEvent::convert);
matsimEventsReader.readFile(eventsPath);
return legListener.getLegItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;

import org.eqasim.core.components.transit.events.PublicTransitEvent;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.MatsimEventsReader;
Expand All @@ -16,7 +17,9 @@ public TripReaderFromEvents(TripListener tripListener) {
public Collection<TripItem> readTrips(String eventsPath) {
EventsManager eventsManager = EventsUtils.createEventsManager();
eventsManager.addHandler(tripListener);
new MatsimEventsReader(eventsManager).readFile(eventsPath);
MatsimEventsReader matsimEventsReader = new MatsimEventsReader(eventsManager);
matsimEventsReader.addCustomEventMapper(PublicTransitEvent.TYPE, PublicTransitEvent::convert);
matsimEventsReader.readFile(eventsPath);
return tripListener.getTripItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@ public Map<String, String> getAttributes() {
attributes.put("travelDistance", String.valueOf(travelDistance));
return attributes;
}

public static PublicTransitEvent convert(GenericEvent genericEvent) {
if(!TYPE.equals(genericEvent.getEventType())) {
return null;
}
Map<String, String> attributes = genericEvent.getAttributes();
Id<Person> personId = Id.createPersonId(attributes.get("person"));
Id<TransitLine> transitLineId = Id.create(attributes.get("line"), TransitLine.class);
Id<TransitRoute> transitRouteId = Id.create(attributes.get("route"), TransitRoute.class);
Id<TransitStopFacility> accessStopId = Id.create(attributes.get("accessStop"), TransitStopFacility.class);
Id<TransitStopFacility> egressStopId = Id.create(attributes.get("egressStop"), TransitStopFacility.class);
double vehicleDepartureTime = Double.parseDouble(attributes.get("vehicleDepartureTime"));
double travelDistance = Double.parseDouble(attributes.get("travelDistance"));

return new PublicTransitEvent(genericEvent.getTime(), personId, transitLineId, transitRouteId, accessStopId, egressStopId, vehicleDepartureTime, travelDistance);
}
}

0 comments on commit 97115ce

Please sign in to comment.