MATSIM
PlansDumpingImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * DumpPlans.java.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.controler.corelisteners;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
29 import org.matsim.core.config.Config;
35 
36 import com.google.inject.Inject;
37 import com.google.inject.Singleton;
40 
50 @Singleton
51 final class PlansDumpingImpl implements PlansDumping, BeforeMobsimListener {
52 
53  static final private Logger log = LogManager.getLogger(PlansDumpingImpl.class);
54 
55  @Inject private Config config;
56  @Inject private Network network;
57  @Inject private Population population;
58  @Inject private IterationStopWatch stopwatch;
59  @Inject private OutputDirectoryHierarchy controlerIO;
60  private int writePlansInterval ;
61 
62  private int writeMoreUntilIteration;
63 
64  @Inject
65  PlansDumpingImpl(ControllerConfigGroup config) {
66  this.writePlansInterval = config.getWritePlansInterval();
67  this.writeMoreUntilIteration = config.getWritePlansUntilIteration() ;
68  }
69 
70  @Override
71  public void notifyBeforeMobsim(final BeforeMobsimEvent event) {
72  final boolean writingPlansAtAll = writePlansInterval > 0;
73  final boolean regularWritePlans = writingPlansAtAll && (event.getIteration()>0 && event.getIteration() % writePlansInterval== 0);
74  final boolean earlyIteration = event.getIteration() <= writeMoreUntilIteration ;
75  if ( writingPlansAtAll && (regularWritePlans || earlyIteration) ) {
76  stopwatch.beginOperation("dump all plans");
77  log.info("dumping plans...");
78  final String inputCRS = config.plans().getInputCRS();
79  final String internalCRS = config.global().getCoordinateSystem();
80 
81  if ( inputCRS == null ) {
82  new PopulationWriter(population, network).write(controlerIO.getIterationFilename(event.getIteration(), Controler.DefaultFiles.population));
83  }
84  else {
85  log.info( "re-projecting population from "+internalCRS+" back to "+inputCRS+" for export" );
86 
87  final CoordinateTransformation transformation =
88  TransformationFactory.getCoordinateTransformation(
89  internalCRS,
90  inputCRS );
91 
92  new PopulationWriter(transformation, population, network).write(controlerIO.getIterationFilename(event.getIteration(), Controler.DefaultFiles.population));
93  }
94  log.info("finished plans dump.");
95  stopwatch.endOperation("dump all plans");
96  }
97  }
98 
99 }
static CoordinateTransformation getCoordinateTransformation(final String fromSystem, final String toSystem)