MATSIM
CountsComparisonAlgorithm.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * CountsComparisonAlgorithm.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.counts.algorithms;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
26 import org.matsim.api.core.v01.Coord;
27 import org.matsim.api.core.v01.Id;
28 import org.matsim.api.core.v01.IdMap;
33 import org.matsim.counts.Count;
36 import org.matsim.counts.Counts;
37 import org.matsim.counts.Volume;
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 
51 
52  public static interface VolumesForId {
53  double[] getVolumesForStop(Id<TransitStopFacility> locationId);
54  }
55 
56  public static interface DistanceFilter {
57  boolean isInRange(Count<Link> count);
58  }
59 
61 
62  private final Counts<Link> counts;
63 
64  private final List<CountSimComparison> result;
65 
67 
68  @Override
69  public boolean isInRange(Count<Link> count) {
70  return true;
71  }
72 
73  };
74 
75  private final Network network;
76 
77  private double countsScaleFactor;
78 
79  private final static Logger log = LogManager.getLogger(CountsComparisonAlgorithm.class);
80 
81  public CountsComparisonAlgorithm(final VolumesAnalyzer volumes, final Counts<Link> counts, final Network network, final double countsScaleFactor) {
82  this.counts = counts;
83  this.result = new ArrayList<CountSimComparison>();
84  this.network = network;
85  this.countsScaleFactor = countsScaleFactor;
86  this.volumesPerLinkPerHour = new VolumesForId() {
87 
88  @Override
89  public double[] getVolumesForStop(Id<TransitStopFacility> locationId) {
90  return volumes.getVolumesPerHourForLink(Id.create(locationId, Link.class));
91  }
92 
93  };
94  }
95 
96  public CountsComparisonAlgorithm(final IdMap<Link, double[]> volumesPerLinkPerHour, final Counts<Link> counts, final Network network, final double countsScaleFactor) {
97  this.volumesPerLinkPerHour = new VolumesForId() {
98 
99  @Override
100  public double[] getVolumesForStop(Id<TransitStopFacility> locationId) {
101  return volumesPerLinkPerHour.get(Id.create(locationId, Link.class));
102  }
103 
104  };
105  this.counts = counts;
106  this.result = new ArrayList<>();
107  this.network = network;
108  this.countsScaleFactor = countsScaleFactor;
109  }
110 
111  public CountsComparisonAlgorithm(VolumesForId volumesPerLinkPerHour, final Counts<Link> counts, final Network network, final double countsScaleFactor) {
112  this.volumesPerLinkPerHour = volumesPerLinkPerHour;
113  this.counts = counts;
114  this.result = new ArrayList<>();
115  this.network = network;
116  this.countsScaleFactor = countsScaleFactor;
117  }
118 
123  private void compare() {
124  for (Count<Link> count : this.counts.getCounts().values()) {
125  if (!distanceFilter.isInRange(count)) {
126  continue;
127  }
128  double[] volumes = this.volumesPerLinkPerHour.getVolumesForStop(Id.create(count.getId(), TransitStopFacility.class));
129  if (volumes == null || volumes.length == 0) {
130  log.warn("No volumes for count location: " + count.getId().toString());
131  continue;
132  }
133  for (int hour = 1; hour <= 24; hour++) {
134  Volume volume = count.getVolume(hour);
135  if (volume != null) {
136  double countValue = volume.getValue();
137  double simValue=volumes[hour-1];
138  simValue *= this.countsScaleFactor;
139  this.result.add(new CountSimComparisonImpl(count.getId(), count.getCsLabel(), hour, countValue, simValue));
140  }
141  }
142  }
143  }
144 
149  public List<CountSimComparison> getComparison() {
150  return this.result;
151  }
152 
153  public void run() {
154  this.compare();
155  }
156 
163  public void setDistanceFilter(final Double distance, final String nodeId) {
164  final Coord centerCoord = network.getNodes().get(Id.create(nodeId, Node.class)).getCoord();
165  this.distanceFilter = new DistanceFilter() {
166 
167  @Override
168  public boolean isInRange(Count<Link> count) {
169  Link l = network.getLinks().get(count.getId());
170  if (l == null) {
171  log.warn("Cannot find requested link: " + count.getId().toString());
172  return false;
173  }
174  double dist = CoordUtils.calcEuclideanDistance(l.getCoord(), centerCoord);
175  return dist < distance;
176  }
177 
178  };
179  }
180 
181  public void setCountCoordUsingDistanceFilter(final Double distance, final String nodeId) {
182  final Coord centerCoord = network.getNodes().get(Id.create(nodeId, Node.class)).getCoord();
183  this.distanceFilter = new CountsComparisonAlgorithm.DistanceFilter() {
184 
185  @Override
186  public boolean isInRange(Count<Link> count) {
187  double dist = CoordUtils.calcEuclideanDistance(count.getCoord(), centerCoord);
188  return dist < distance;
189  }
190  };
191  }
192 
193  public void setDistanceFilter(DistanceFilter distanceFilter) {
194  this.distanceFilter = distanceFilter;
195  }
196 
197  public void setCountsScaleFactor(final double countsScaleFactor) {
198  this.countsScaleFactor = countsScaleFactor;
199  }
200 
201 }
double [] getVolumesPerHourForLink(final Id< Link > linkId)
Map< Id< Node >, ? extends Node > getNodes()
static double calcEuclideanDistance(Coord coord, Coord other)
final TreeMap< Id< T >, Count< T > > getCounts()
Definition: Counts.java:125
CountsComparisonAlgorithm(final IdMap< Link, double[]> volumesPerLinkPerHour, final Counts< Link > counts, final Network network, final double countsScaleFactor)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
final double getValue()
Definition: Volume.java:54
final Id< T > getId()
Definition: Count.java:98
double [] getVolumesForStop(Id< TransitStopFacility > locationId)
Map< Id< Link >, ? extends Link > getLinks()
void setDistanceFilter(final Double distance, final String nodeId)
void setCountCoordUsingDistanceFilter(final Double distance, final String nodeId)
CountsComparisonAlgorithm(final VolumesAnalyzer volumes, final Counts< Link > counts, final Network network, final double countsScaleFactor)
CountsComparisonAlgorithm(VolumesForId volumesPerLinkPerHour, final Counts< Link > counts, final Network network, final double countsScaleFactor)