55import java .io .IOException ;
66import java .net .MalformedURLException ;
77import java .util .Arrays ;
8+ import java .util .HashMap ;
89import java .util .LinkedList ;
910import java .util .List ;
11+ import java .util .Map ;
1012import java .util .Optional ;
1113
1214import org .eqasim .core .components .transit .events .PublicTransitEvent ;
1517import org .eqasim .core .scenario .cutter .extent .ShapeScenarioExtent ;
1618import org .matsim .api .core .v01 .Id ;
1719import org .matsim .api .core .v01 .Scenario ;
20+ import org .matsim .api .core .v01 .events .ActivityEndEvent ;
1821import org .matsim .api .core .v01 .events .GenericEvent ;
22+ import org .matsim .api .core .v01 .events .handler .ActivityEndEventHandler ;
1923import org .matsim .api .core .v01 .events .handler .GenericEventHandler ;
2024import org .matsim .api .core .v01 .population .Leg ;
2125import org .matsim .api .core .v01 .population .Person ;
2832import org .matsim .core .events .MatsimEventsReader ;
2933import org .matsim .core .population .io .PopulationReader ;
3034import org .matsim .core .router .TripStructureUtils ;
35+ import org .matsim .core .router .TripStructureUtils .Trip ;
3136import org .matsim .core .scenario .ScenarioUtils ;
3237import org .matsim .core .utils .io .IOUtils ;
3338import org .matsim .pt .routes .TransitPassengerRoute ;
@@ -66,37 +71,38 @@ static public void main(String[] args) throws ConfigurationException, MalformedU
6671 new PopulationReader (scenario ).readFile (cmd .getOptionStrict ("population-path" ));
6772
6873 for (Person person : scenario .getPopulation ().getPersons ().values ()) {
69- for (Leg leg : TripStructureUtils .getLegs (person .getSelectedPlan ())) {
70- if (leg .getRoute () instanceof TransitPassengerRoute route ) {
71- routes .add (new IndividualRoute (route .getLineId (), route .getRouteId (), route .getAccessStopId (),
72- route .getEgressStopId (), person .getId (), route .getBoardingTime ().seconds ()));
74+ int tripIndex = 0 ;
75+
76+ for (Trip trip : TripStructureUtils .getTrips (person .getSelectedPlan ())) {
77+ int legIndex = 0 ;
78+
79+ for (Leg leg : trip .getLegsOnly ()) {
80+ if (leg .getRoute () instanceof TransitPassengerRoute route ) {
81+ routes .add (new IndividualRoute (route .getLineId (), route .getRouteId (),
82+ route .getAccessStopId (),
83+ route .getEgressStopId (), person .getId (), tripIndex , legIndex ,
84+ route .getBoardingTime ().seconds ()));
85+ }
7386 }
87+
88+ tripIndex ++;
7489 }
7590 }
7691 } else {
7792 EventsManager eventsManager = EventsUtils .createEventsManager ();
7893
79- eventsManager .addHandler (new GenericEventHandler () {
80- @ Override
81- public void handleEvent (GenericEvent event ) {
82- if (event instanceof PublicTransitEvent pt ) {
83- routes .add (new IndividualRoute (pt .getTransitLineId (), pt .getTransitRouteId (),
84- pt .getAccessStopId (), pt .getEgressStopId (), pt .getPersonId (),
85- pt .getVehicleDepartureTime ()));
86- }
87- }
88- });
94+ Handler handler = new Handler (routes );
95+ eventsManager .addHandler (handler );
8996
9097 MatsimEventsReader reader = new MatsimEventsReader (eventsManager );
9198 reader .addCustomEventMapper (PublicTransitEvent .TYPE , new PublicTransitEventMapper ());
9299 reader .readFile (cmd .getOptionStrict ("events-path" ));
93100 }
94101
95102 BufferedWriter writer = IOUtils .getBufferedWriter (cmd .getOptionStrict ("output-path" ));
96- int routeIndex = 0 ;
97103
98104 writer .write (String .join (";" , Arrays .asList ( //
99- "route_index " , "person_id " , "stop_id" , "area_id" , "is_access" , "is_egress" , "transit_mode" ,
105+ "person_id " , "trip_index" , "leg_index " , "stop_id" , "area_id" , "is_access" , "is_egress" , "transit_mode" ,
100106 "arrival_time" , "departure_time" )) + "\n " );
101107
102108 for (IndividualRoute route : routes ) {
@@ -139,8 +145,9 @@ public void handleEvent(GenericEvent event) {
139145
140146 if (extent == null || extent .isInside (stopFacility .getCoord ())) {
141147 writer .write (String .join (";" , Arrays .asList ( //
142- String .valueOf (routeIndex ), //
143148 route .personId .toString (), //
149+ String .valueOf (route .tripIndex ), //
150+ String .valueOf (route .legIndex ), //
144151 stop .getStopFacility ().getId ().toString (), //
145152 stop .getStopFacility ().getStopAreaId ().toString (), //
146153 String .valueOf (index == accessIndex ), //
@@ -151,15 +158,61 @@ public void handleEvent(GenericEvent event) {
151158 )) + "\n " );
152159 }
153160 }
154-
155- routeIndex ++;
156161 }
157162
158163 writer .close ();
159164 }
160165
161166 private record IndividualRoute (Id <TransitLine > transitLineId , Id <TransitRoute > transitRouteId ,
162167 Id <TransitStopFacility > accessStopId , Id <TransitStopFacility > egressStopId , Id <Person > personId ,
163- double referenceTime ) {
168+ int tripIndex , int legIndex , double referenceTime ) {
169+ }
170+
171+ private static class Handler implements GenericEventHandler , ActivityEndEventHandler {
172+ private final Map <Id <Person >, Integer > tripIndex = new HashMap <>();
173+ private final Map <Id <Person >, Integer > legIndex = new HashMap <>();
174+
175+ List <IndividualRoute > routes ;
176+
177+ Handler (List <IndividualRoute > routes ) {
178+ this .routes = routes ;
179+ }
180+
181+ @ Override
182+ public void handleEvent (ActivityEndEvent event ) {
183+ Integer localLegIndex = legIndex .get (event .getPersonId ());
184+
185+ if (localLegIndex == null ) {
186+ localLegIndex = 0 ;
187+ } else {
188+ localLegIndex = localLegIndex + 1 ;
189+ }
190+
191+ Integer personTripIndex = tripIndex .get (event .getPersonId ());
192+
193+ if (!TripStructureUtils .isStageActivityType (event .getActType ())) {
194+ if (personTripIndex == null ) {
195+ personTripIndex = 0 ;
196+ } else {
197+ personTripIndex = personTripIndex + 1 ;
198+ }
199+ }
200+
201+ tripIndex .put (event .getPersonId (), personTripIndex );
202+ legIndex .put (event .getPersonId (), localLegIndex );
203+ }
204+
205+ @ Override
206+ public void handleEvent (GenericEvent event ) {
207+ if (event instanceof PublicTransitEvent pt ) {
208+ int localTripIndex = tripIndex .getOrDefault (pt .getPersonId (), 0 );
209+ int localLegIndex = legIndex .getOrDefault (pt .getPersonId (), 0 );
210+
211+ routes .add (new IndividualRoute (pt .getTransitLineId (), pt .getTransitRouteId (),
212+ pt .getAccessStopId (), pt .getEgressStopId (), pt .getPersonId (), localTripIndex ,
213+ localLegIndex ,
214+ pt .getVehicleDepartureTime ()));
215+ }
216+ }
164217 }
165218}
0 commit comments