9
9
import org .matsim .core .population .algorithms .PersonAlgorithm ;
10
10
import org .matsim .core .population .algorithms .TripsToLegsAlgorithm ;
11
11
import org .matsim .core .router .RoutingModeMainModeIdentifier ;
12
+ import org .matsim .core .router .TripStructureUtils ;
12
13
import picocli .CommandLine ;
13
14
14
15
import java .nio .file .Files ;
15
16
import java .nio .file .Path ;
17
+ import java .util .List ;
18
+ import java .util .Optional ;
19
+ import java .util .Set ;
16
20
17
21
/**
18
- * Removes information from a popoulation file.
22
+ * Removes information from a population file.
19
23
*
20
24
* @author rakow
21
25
*/
@@ -38,11 +42,17 @@ public class CleanPopulation implements MATSimAppCommand, PersonAlgorithm {
38
42
@ CommandLine .Option (names = "--remove-activity-location" , description = "Remove link and facility from activities" , defaultValue = "false" )
39
43
private boolean rmActivityLocations ;
40
44
45
+ @ CommandLine .Option (names = "--remove-activity-facilities" , description = "Remove facility ids from activities" , defaultValue = "false" )
46
+ private boolean rmActivityFacilities ;
47
+
41
48
@ CommandLine .Option (names = "--remove-routes" , description = "Remove route information" , defaultValue = "false" )
42
49
private boolean rmRoutes ;
43
50
44
- @ CommandLine .Option (names = "--trips-to-legs" , description = "Convert trips in older plan format to legs first (Removes routes implicitly)." , defaultValue = "false" )
45
- private boolean tripsToLegs ;
51
+ @ CommandLine .Option (names = "--trips-to-legs" , description = "Convert trips to single legs (Removes all routing and individual legs)." , defaultValue = "false" )
52
+ private boolean tripsToLegs ;
53
+
54
+ @ CommandLine .Option (names = "--remove-legs" , description = "Remove the legs of trips for given modes, e.g. pt" , split = "," , defaultValue = "" )
55
+ private Set <String > rmLegs ;
46
56
47
57
@ CommandLine .Option (names = "--output" , description = "Output file name" , required = true )
48
58
private Path output ;
@@ -60,7 +70,7 @@ public Integer call() throws Exception {
60
70
61
71
Population population = PopulationUtils .readPopulation (plans .toString ());
62
72
63
- if (!rmRoutes && !rmActivityLocations && !rmUnselected && !tripsToLegs ) {
73
+ if (!rmRoutes && !rmActivityLocations && !rmUnselected && !tripsToLegs && ! rmActivityFacilities && ( rmLegs == null || rmLegs . isEmpty ()) ) {
64
74
log .error ("None of the 'remove' commands specified." );
65
75
return 2 ;
66
76
}
@@ -87,6 +97,10 @@ public void run(Person person) {
87
97
if (tripsToLegs )
88
98
trips2Legs .run (plan );
89
99
100
+ if (rmLegs != null && !rmLegs .isEmpty ()) {
101
+ removeLegs (plan , rmLegs );
102
+ }
103
+
90
104
for (PlanElement el : plan .getPlanElements ()) {
91
105
if (rmRoutes ) {
92
106
removeRouteFromLeg (el );
@@ -95,10 +109,44 @@ public void run(Person person) {
95
109
if (rmActivityLocations ) {
96
110
removeActivityLocation (el );
97
111
}
112
+
113
+ if (rmActivityFacilities && el instanceof Activity act ) {
114
+ act .setFacilityId (null );
115
+ }
98
116
}
99
117
}
100
118
}
101
119
120
+ /**
121
+ * Remove the legs of trips that contain any of the given modes.
122
+ */
123
+ public static void removeLegs (Plan plan , Set <String > modes ) {
124
+
125
+ final List <PlanElement > planElements = plan .getPlanElements ();
126
+
127
+ // Remove all pt trips
128
+ for (TripStructureUtils .Trip trip : TripStructureUtils .getTrips (plan )) {
129
+
130
+ // Check if any of the modes is in the trip
131
+ Optional <Leg > cleanLeg = trip .getLegsOnly ().stream ().filter (l -> modes .contains (l .getMode ())).findFirst ();
132
+
133
+ if (cleanLeg .isEmpty ())
134
+ continue ;
135
+
136
+ // Replaces all trip elements and inserts single leg
137
+ final List <PlanElement > fullTrip =
138
+ planElements .subList (
139
+ planElements .indexOf (trip .getOriginActivity ()) + 1 ,
140
+ planElements .indexOf (trip .getDestinationActivity ()));
141
+
142
+ fullTrip .clear ();
143
+
144
+ Leg leg = PopulationUtils .createLeg (cleanLeg .get ().getMode ());
145
+ TripStructureUtils .setRoutingMode (leg , cleanLeg .get ().getMode ());
146
+ fullTrip .add (leg );
147
+ }
148
+ }
149
+
102
150
/**
103
151
* Remove link and facility information from activity.
104
152
*/
@@ -128,4 +176,6 @@ public static void removeUnselectedPlans(Person person) {
128
176
person .removePlan (plan );
129
177
}
130
178
}
179
+
180
+
131
181
}
0 commit comments