MATSIM
LinkPaxVolumesWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * LinkPaxVolumesWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2021 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.analysis.linkpaxvolumes;
22 
23 import org.apache.commons.csv.CSVFormat;
24 import org.apache.commons.csv.CSVPrinter;
25 import org.matsim.api.core.v01.Id;
28 import org.matsim.core.utils.io.IOUtils;
30 
31 import java.io.IOException;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
34 
35 public class LinkPaxVolumesWriter {
37  private final Network network;
38  private final String columnSeparator;
39  private final int numberOfHours;
40 
41  public LinkPaxVolumesWriter(LinkPaxVolumesAnalysis linkPaxVolumesAnalysis, Network network, String sep) {
42  this.linkPaxVolumesAnalysis = linkPaxVolumesAnalysis;
43  this.network = network;
44  this.columnSeparator = sep;
45  this.numberOfHours = linkPaxVolumesAnalysis.getNumberOfHours();
46  }
47 
48  public void writeLinkVehicleAndPaxVolumesAllPerDayCsv(String fileName) {
49  // have results sorted
50  SortedSet<Id<Link>> linkIdsSorted = new TreeSet(network.getLinks().keySet());
51 
52  String[] header = {"link", "vehicles", "passengersInclDriver"};
53 
54  try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(fileName),
55  CSVFormat.DEFAULT.withDelimiter(columnSeparator.charAt(0)).withHeader(header))
56  ) {
57  for (Id<Link> linkId : linkIdsSorted) {
58  printer.print(linkId);
59  printer.print(calcDailyValueOrZero(linkPaxVolumesAnalysis.getVehicleVolumesForLink(linkId)));
60  printer.print(calcDailyValueOrZero(linkPaxVolumesAnalysis.getPaxVolumesForLink(linkId)));
61  printer.println();
62  }
63  } catch (IOException e) {
64  e.printStackTrace();
65  }
66  }
67 
69  // have results sorted
70  SortedSet<Id<Link>> linkIdsSorted = new TreeSet(network.getLinks().keySet());
71  SortedSet<String> networkModesSorted = new TreeSet(linkPaxVolumesAnalysis.getNetworkModes());
72 
73  String[] header = {"link", "networkMode", "hour", "vehicles", "passengersInclDriver"};
74 
75  try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(fileName),
76  CSVFormat.DEFAULT.withDelimiter(columnSeparator.charAt(0)).withHeader(header))
77  ) {
78  for (Id<Link> linkId : linkIdsSorted) {
79  for (String networkMode : networkModesSorted) {
80  int[] hourlyVehicleVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
81  getVehicleVolumesForLinkPerNetworkMode(linkId, networkMode));
82  int[] hourlyPassengerVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
83  getPaxVolumesForLinkPerNetworkMode(linkId, networkMode));
84  for (int hour = 0; hour < numberOfHours; hour++) {
85  printer.print(linkId);
86  printer.print(networkMode);
87  printer.print(hour);
88  printer.print(hourlyVehicleVolumes[hour]);
89  printer.print(hourlyPassengerVolumes[hour]);
90  printer.println();
91  }
92  }
93  }
94  } catch (IOException e) {
95  e.printStackTrace();
96  }
97  }
98 
99  /*
100  * Please note that the same vehicle is counted for each passenger mode it is serving here. E.g. a pt vehicle
101  * will be counted with the driver's passenger mode and with the passenger's passenger mode, i.e. multiple times.
102  */
104  // have results sorted
105  SortedSet<Id<Link>> linkIdsSorted = new TreeSet(network.getLinks().keySet());
106  SortedSet<String> passengerModesSorted = new TreeSet(linkPaxVolumesAnalysis.getPassengerModes());
107 
108  String[] header = {"link", "passengerMode", "hour", "vehicles", "passengersPossiblyInclDriver"};
109 
110  try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(fileName),
111  CSVFormat.DEFAULT.withDelimiter(columnSeparator.charAt(0)).withHeader(header))
112  ) {
113  for (Id<Link> linkId : linkIdsSorted) {
114  for (String passengerMode : passengerModesSorted) {
115  int[] hourlyVehicleVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
116  getVehicleVolumesForLinkPerPassengerMode(linkId, passengerMode));
117  int[] hourlyPassengerVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
118  getPaxVolumesForLinkPerPassengerMode(linkId, passengerMode));
119  for (int hour = 0; hour < numberOfHours; hour++) {
120  printer.print(linkId);
121  printer.print(passengerMode);
122  printer.print(hour);
123  printer.print(hourlyVehicleVolumes[hour]);
124  printer.print(hourlyPassengerVolumes[hour]);
125  printer.println();
126  }
127  }
128  }
129  } catch (IOException e) {
130  e.printStackTrace();
131  }
132  }
133 
135  // have results sorted
136  SortedSet<Id<Link>> linkIdsSorted = new TreeSet(network.getLinks().keySet());
137  SortedSet<Id<VehicleType>> vehicleIdsSorted = new TreeSet(linkPaxVolumesAnalysis.getVehicleTypes());
138 
139  String[] header = {"link", "vehicleType", "hour", "vehicles", "passengersInclDriver"};
140 
141  try (CSVPrinter printer = new CSVPrinter(IOUtils.getBufferedWriter(fileName),
142  CSVFormat.DEFAULT.withDelimiter(columnSeparator.charAt(0)).withHeader(header))
143  ) {
144  for (Id<Link> linkId : linkIdsSorted) {
145  for (Id<VehicleType> vehicleTypeId : vehicleIdsSorted) {
146  int[] hourlyVehicleVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
147  getVehicleVolumesForLinkPerVehicleType(linkId, vehicleTypeId));
148  int[] hourlyPassengerVolumes = calcHourlyValuesOrZero(linkPaxVolumesAnalysis.
149  getPaxVolumesForLinkPerVehicleType(linkId, vehicleTypeId));
150  for (int hour = 0; hour < numberOfHours; hour++) {
151  printer.print(linkId);
152  printer.print(vehicleTypeId);
153  printer.print(hour);
154  printer.print(hourlyVehicleVolumes[hour]);
155  printer.print(hourlyPassengerVolumes[hour]);
156  printer.println();
157  }
158  }
159  }
160  } catch (IOException e) {
161  e.printStackTrace();
162  }
163  }
164 
165  private int[] calcHourlyValuesOrZero(int[] volumes) {
166  if (volumes != null) {
167  return linkPaxVolumesAnalysis.getVolumePerHourFromTimeBinArray(volumes);
168  } else {
169  return new int[numberOfHours];
170  }
171  }
172 
173  private int calcDailyValueOrZero(int[] volumes) {
174  if (volumes != null) {
175  return linkPaxVolumesAnalysis.getVolumePerDayFromTimeBinArray(volumes);
176  } else {
177  return 0;
178  }
179  }
180 }
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
Map< Id< Link >, ? extends Link > getLinks()