26
26
import org .matsim .core .trafficmonitoring .FreeSpeedTravelTime ;
27
27
import org .matsim .core .utils .geometry .CoordinateTransformation ;
28
28
import org .matsim .core .utils .geometry .geotools .MGC ;
29
+ import org .matsim .utils .objectattributes .attributable .Attributes ;
29
30
import picocli .CommandLine ;
30
31
31
32
import java .io .FileWriter ;
@@ -40,6 +41,7 @@ public class ExtractRelevantFreightTrips implements MATSimAppCommand {
40
41
// This script will extract the relevant freight trips within the given shape
41
42
// file from the German wide freight traffic.
42
43
private static final Logger log = LogManager .getLogger (ExtractRelevantFreightTrips .class );
44
+ public static final String GEOGRAPHICAL_TRIP_TYPE = "geographical_Trip_Type" ;
43
45
44
46
/**
45
47
* Enum for the type of trips to be extracted. The following types are available:
@@ -74,9 +76,12 @@ private enum TripType {
74
76
@ CommandLine .Option (names = "--tripType" , description = "Set the tripType: OUTGOING, INCOMING, TRANSIT, INTERNAL, ALL" , defaultValue = "ALL" )
75
77
private TripType tripType ;
76
78
77
- @ CommandLine .Option (names = "--LegMode " , description = "Set leg mode for long distance freight legs." , defaultValue = "freight" )
79
+ @ CommandLine .Option (names = "--legMode " , description = "Set leg mode for long distance freight legs." , defaultValue = "freight" )
78
80
private String legMode ;
79
81
82
+ @ CommandLine .Option (names = "--subpopulation" , description = "Set subpopulation for the extracted freight trips" , defaultValue = "freight" )
83
+ private String subpopulation ;
84
+
80
85
private final SplittableRandom rnd = new SplittableRandom (4711 );
81
86
82
87
private final List <Coord > fromCoords = new ArrayList <>();
@@ -139,17 +144,17 @@ public Integer call() throws Exception {
139
144
int generated = 0 ;
140
145
int processed = 0 ;
141
146
log .info ("Start creating the modified plans: there are in total {} persons to be processed" ,
142
- originalPlans .getPersons ().keySet (). size ());
147
+ originalPlans .getPersons ().size ());
143
148
144
149
for (Person person : originalPlans .getPersons ().values ()) {
145
150
146
151
processed += 1 ;
147
152
if (processed % 10000 == 0 ) {
148
- log .info ("Processing: {} persons of {} persons have been processed" , processed , originalPlans .getPersons ().keySet (). size ());
153
+ log .info ("Processing: {} persons of {} persons have been processed" , processed , originalPlans .getPersons ().size ());
149
154
}
150
155
151
156
Plan plan = person .getSelectedPlan ();
152
- // String goodType = (String) person.getAttributes().getAttribute("type_of_good"); // TODO keep all the attribute from original population
157
+ Attributes attributes = person .getAttributes ();
153
158
// By default, the plan of each freight person consist of only 3 elements:
154
159
// startAct, leg, endAct
155
160
Activity startActivity = (Activity ) plan .getPlanElements ().get (0 );
@@ -169,33 +174,35 @@ public Integer call() throws Exception {
169
174
170
175
switch (tripType ) {
171
176
case ALL -> {
172
- createActivitiesForInternalTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , act1 , endCoord );
177
+ createActivitiesForInternalTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , act1 , endCoord , attributes );
173
178
createActivitiesForOutgoingTrip (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
174
179
startLink , endLink ,
175
- linksOnTheBoundary , act1 , endCoord );
180
+ linksOnTheBoundary , act1 , endCoord , attributes );
176
181
createActivitiesForIncomingTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
177
- startLink , endLink , linksOnTheBoundary , act1 , endCoord );
182
+ startLink , endLink , linksOnTheBoundary , act1 , endCoord , attributes );
178
183
createActivitiesForTransitTrip (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
179
- startLink , endLink , linksOnTheBoundary , act1 , endCoord );
184
+ startLink , endLink , linksOnTheBoundary , act1 , endCoord , attributes );
180
185
}
181
186
case INTERNAL ->
182
- createActivitiesForInternalTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , act1 , endCoord );
187
+ createActivitiesForInternalTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , act1 , endCoord ,
188
+ attributes );
183
189
case OUTGOING ->
184
190
createActivitiesForOutgoingTrip (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
185
- startLink , endLink , linksOnTheBoundary , act1 , endCoord );
191
+ startLink , endLink , linksOnTheBoundary , act1 , endCoord , attributes );
186
192
case INCOMING ->
187
193
createActivitiesForIncomingTrips (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
188
- startLink , endLink , linksOnTheBoundary , act1 , endCoord );
194
+ startLink , endLink , linksOnTheBoundary , act1 , endCoord , attributes );
189
195
case TRANSIT ->
190
196
createActivitiesForTransitTrip (originIsInside , destinationIsInside , act0 , ct , startCoord , departureTime , router , network ,
191
- startLink , endLink , linksOnTheBoundary , act1 , endCoord );
197
+ startLink , endLink , linksOnTheBoundary , act1 , endCoord , attributes );
192
198
default -> throw new IllegalStateException ("Unexpected value: " + tripType );
193
199
}
194
200
195
201
// Add new freight person to the output plans if trips is relevant
196
202
if (act0 .getEndTime ().orElse (86400 ) < 86400 ) {
197
203
Person freightPerson = populationFactory .createPerson (Id .create ("freight_" + generated , Person .class ));
198
- freightPerson .getAttributes ().putAttribute ("subpopulation" , "freight" );
204
+ attributes .getAsMap ().forEach (freightPerson .getAttributes ()::putAttribute );
205
+ freightPerson .getAttributes ().putAttribute ("subpopulation" , subpopulation );
199
206
Plan freightPersonPlan = populationFactory .createPlan ();
200
207
freightPersonPlan .addActivity (act0 );
201
208
freightPersonPlan .addLeg (leg );
@@ -246,7 +253,7 @@ public Integer call() throws Exception {
246
253
*/
247
254
private void createActivitiesForTransitTrip (boolean originIsInside , boolean destinationIsInside , Activity act0 , CoordinateTransformation ct , Coord startCoord ,
248
255
double departureTime , LeastCostPathCalculator router , Network network , Id <Link > startLink , Id <Link > endLink ,
249
- List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord ) {
256
+ List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord , Attributes attributes ) {
250
257
if (!originIsInside && !destinationIsInside ) {
251
258
double timeSpent = 0 ;
252
259
boolean vehicleIsInside = false ;
@@ -283,6 +290,7 @@ private void createActivitiesForTransitTrip(boolean originIsInside, boolean dest
283
290
}
284
291
}
285
292
}
293
+ attributes .putAttribute (GEOGRAPHICAL_TRIP_TYPE , "transit" );
286
294
}
287
295
}
288
296
@@ -291,7 +299,7 @@ private void createActivitiesForTransitTrip(boolean originIsInside, boolean dest
291
299
*/
292
300
private void createActivitiesForIncomingTrips (boolean originIsInside , boolean destinationIsInside , Activity act0 , CoordinateTransformation ct , Coord startCoord ,
293
301
double departureTime , LeastCostPathCalculator router , Network network , Id <Link > startLink , Id <Link > endLink ,
294
- List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord ) {
302
+ List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord , Attributes attributes ) {
295
303
if (!originIsInside && destinationIsInside ) {
296
304
if (cutOnBoundary ) {
297
305
boolean isCoordSet = false ;
@@ -324,6 +332,7 @@ private void createActivitiesForIncomingTrips(boolean originIsInside, boolean de
324
332
act0 .setEndTime (departureTime );
325
333
}
326
334
act1 .setCoord (ct .transform (endCoord ));
335
+ attributes .putAttribute (GEOGRAPHICAL_TRIP_TYPE , "incoming" );
327
336
}
328
337
}
329
338
@@ -332,7 +341,7 @@ private void createActivitiesForIncomingTrips(boolean originIsInside, boolean de
332
341
*/
333
342
private void createActivitiesForOutgoingTrip (boolean originIsInside , boolean destinationIsInside , Activity act0 , CoordinateTransformation ct , Coord startCoord ,
334
343
double departureTime , LeastCostPathCalculator router , Network network , Id <Link > startLink , Id <Link > endLink ,
335
- List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord ) {
344
+ List <Id <Link >> linksOnTheBoundary , Activity act1 , Coord endCoord , Attributes attributes ) {
336
345
if (originIsInside && !destinationIsInside ) {
337
346
act0 .setCoord (ct .transform (startCoord ));
338
347
act0 .setEndTime (departureTime );
@@ -359,19 +368,20 @@ private void createActivitiesForOutgoingTrip(boolean originIsInside, boolean des
359
368
} else {
360
369
act1 .setCoord (ct .transform (endCoord ));
361
370
}
362
-
371
+ attributes . putAttribute ( GEOGRAPHICAL_TRIP_TYPE , "outgoing" );
363
372
}
364
373
}
365
374
366
375
/**
367
376
* Create activities if the trip is an internal trip
368
377
*/
369
378
private static void createActivitiesForInternalTrips (boolean originIsInside , boolean destinationIsInside , Activity act0 , CoordinateTransformation ct , Coord startCoord ,
370
- double departureTime , Activity act1 , Coord endCoord ) {
379
+ double departureTime , Activity act1 , Coord endCoord , Attributes attributes ) {
371
380
if (originIsInside && destinationIsInside ) {
372
381
act0 .setCoord (ct .transform (startCoord ));
373
382
act0 .setEndTime (departureTime );
374
383
act1 .setCoord (ct .transform (endCoord ));
384
+ attributes .putAttribute (GEOGRAPHICAL_TRIP_TYPE , "internal" );
375
385
}
376
386
}
377
387
}
0 commit comments