MATSIM
CountsModule.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * * project: org.matsim.*
4  * * CountSimComparisonModule.java
5  * * *
6  * * *********************************************************************** *
7  * * *
8  * * copyright : (C) 2014 by the members listed in the COPYING, *
9  * * LICENSE and WARRANTY file. *
10  * * email : info at matsim dot org *
11  * * *
12  * * *********************************************************************** *
13  * * *
14  * * This program is free software; you can redistribute it and/or modify *
15  * * it under the terms of the GNU General Public License as published by *
16  * * the Free Software Foundation; either version 2 of the License, or *
17  * * (at your option) any later version. *
18  * * See also COPYING, LICENSE and WARRANTY file *
19  * * *
20  * * ***********************************************************************
21  */
22 
23 package org.matsim.counts;
24 
25 import com.google.inject.Provides;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Scenario;
34 
35 import jakarta.inject.Inject;
36 import jakarta.inject.Singleton;
37 
38 public class CountsModule extends AbstractModule {
39  private static final Logger log = LogManager.getLogger( CountsModule.class );
40 
41  @Override
42  public void install() {
43  addControlerListenerBinding().to(CountsControlerListener.class);
44  bind(CountsInitializer.class).asEagerSingleton();
45  }
46 
47  private static class CountsInitializer {
48  @Inject
49  CountsInitializer(Counts<Link> counts, Scenario scenario) {
50  Counts<Link> scenarioCounts = (Counts<Link>) scenario.getScenarioElement(Counts.ELEMENT_NAME);
51  if (scenarioCounts == null) {
52  scenario.addScenarioElement(Counts.ELEMENT_NAME, counts);
53  } else {
54  if (counts != scenarioCounts) {
55  throw new RuntimeException();
56  }
57  }
58  }
59  }
60 
61  @Provides
62  @Singleton
63  Counts<Link> provideLinkCounts(Scenario scenario, CountsConfigGroup config) {
65  if (counts != null) {
66  return counts;
67  } else {
68  counts = new Counts<>();
69  if (config.getCountsFileName() != null) {
70  final String inputCRS = config.getInputCRS();
71  final String internalCRS = scenario.getConfig().global().getCoordinateSystem();
72 
73  MatsimCountsReader counts_parser;
74  if (inputCRS == null) {
75  counts_parser = new MatsimCountsReader(counts);
76  }
77  else {
78  log.info( "re-projecting counts from "+inputCRS+" to "+internalCRS+" for import" );
79 
80  counts_parser = new MatsimCountsReader( inputCRS, internalCRS, counts );
81  }
82  counts_parser.parse(config.getCountsFileURL(scenario.getConfig().getContext()));
83  }
84  return counts;
85  }
86  }
87 
88 }
void addScenarioElement(String name, Object o)
Object getScenarioElement(String name)
final LinkedBindingBuilder< ControlerListener > addControlerListenerBinding()
static final String ELEMENT_NAME
Definition: Counts.java:23
final GlobalConfigGroup global()
Definition: Config.java:395