MATSIM
PopulationWriter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansWriter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2012 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.population.io;
22 
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.UncheckedIOException;
26 import java.util.HashMap;
27 import java.util.Map;
28 
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
43 
44 public final class PopulationWriter extends AbstractMatsimWriter implements MatsimWriter {
45 
46  private final double write_person_fraction;
47 
50  private final Population population;
51  private final Network network;
52  private final Counter counter = new Counter("[" + this.getClass().getSimpleName() + "] dumped person # ");
53 
54  private final static Logger log = LogManager.getLogger(PopulationWriter.class);
55  private final Map<Class<?>,AttributeConverter<?>> converters = new HashMap<>();
56 
57 
58  public PopulationWriter(final Population population) {
59  this(population, null, 1.0);
60  }
61 
63  final CoordinateTransformation coordinateTransformation,
64  final Population population) {
65  this(coordinateTransformation , population, null, 1.0);
66  }
67 
76  public PopulationWriter(final Population population, final Network network) {
77  this(population, network, 1.0);
78  }
79 
81  final CoordinateTransformation coordinateTransformation,
82  final Population population,
83  final Network network) {
85  }
86 
98  final CoordinateTransformation coordinateTransformation,
99  final Population population,
100  final Network network,
101  final double fraction) {
102  this.coordinateTransformation = coordinateTransformation;
103  this.population = population;
104  this.network = network;
105  this.write_person_fraction = fraction;
106  if (FeatureFlags.useParallelIO()) {
107  this.handler = new ParallelPopulationWriterHandlerV6(coordinateTransformation);
108  } else {
109  this.handler = new PopulationWriterHandlerImplV6(coordinateTransformation);
110  }
111  }
112 
123  final Population population,
124  final Network network,
125  final double fraction) {
126  this( new IdentityTransformation() , population , network , fraction );
127  }
128 
129  public void putAttributeConverters( final Map<Class<?>, AttributeConverter<?>> converters ) {
130  this.converters.putAll( converters );
131  }
132 
133  public void putAttributeConverter( Class<?> key, AttributeConverter<?> converter ) {
134  this.converters.put( key, converter );
135  }
136 
140  @Override
141  public void write(final String filename) {
142  try {
143  this.handler.putAttributeConverters(converters);
144  this.openFile(filename);
145  this.handler.writeHeaderAndStartElement(this.writer);
146  this.handler.startPlans(this.population, this.writer);
147  this.handler.writeSeparator(this.writer);
148  this.writePersons();
149  this.handler.endPlans(this.writer);
150  log.info("Population written to: " + filename);
151  } catch (IOException e) {
152  throw new UncheckedIOException(e);
153  } finally {
154  this.close();
155  counter.printCounter();
156  counter.reset();
157  }
158  }
159 
164  public void write(OutputStream outputStream) {
165  try {
166  this.handler.putAttributeConverters(converters);
167  this.openOutputStream(outputStream);
168  this.handler.writeHeaderAndStartElement(this.writer);
169  this.handler.startPlans(this.population, this.writer);
170  this.handler.writeSeparator(this.writer);
171  this.writePersons();
172  this.handler.endPlans(this.writer);
173  } catch (IOException e) {
174  throw new UncheckedIOException(e);
175  } finally {
176  this.close();
177  counter.printCounter();
178  counter.reset();
179  }
180  }
181 
182 
183  private void writePersons() {
184  for (Person p : PopulationUtils.getSortedPersons(this.population).values()) {
185  writePerson(p);
186  }
187  }
188 
189  private void writePerson(final Person person) {
190  try {
191  if ((this.write_person_fraction < 1.0) && (MatsimRandom.getRandom().nextDouble() >= this.write_person_fraction)) {
192  return;
193  }
194  this.handler.writePerson(person, this.writer);
195  counter.incCounter();
196  } catch (IOException e) {
197  throw new UncheckedIOException(e);
198  }
199  }
200 
201  public void writeV0(final String filename) {
202  this.handler = new PopulationWriterHandlerImplV0( coordinateTransformation , this.network);
203  write(filename);
204  }
205 
206  public void writeV4(final String filename) {
207  this.handler = new PopulationWriterHandlerImplV4( coordinateTransformation , this.network );
208  write(filename);
209  }
210 
211  public void writeV5(final String filename) {
212  this.handler = new PopulationWriterHandlerImplV5(coordinateTransformation);
213  write(filename);
214  }
215 
216  public void writeV6(final String filename) {
217  if (FeatureFlags.useParallelIO()) {
218  this.handler = new ParallelPopulationWriterHandlerV6(coordinateTransformation);
219  write(filename);
220  } else {
221  this.handler = new PopulationWriterHandlerImplV6(coordinateTransformation);
222  write(filename);
223  }
224  }
225 
226  public void writeV6(final OutputStream stream) {
227  if (FeatureFlags.useParallelIO()) {
228  this.handler = new ParallelPopulationWriterHandlerV6(coordinateTransformation);
229  write(stream);
230  } else {
231  this.handler = new PopulationWriterHandlerImplV6(coordinateTransformation);
232  write(stream);
233  }
234  }
235 
236  public void setWriterHandler(final PopulationWriterHandler handler) {
237  this.handler = handler;
238  }
239 
240 }
PopulationWriter(final CoordinateTransformation coordinateTransformation, final Population population)
PopulationWriter(final CoordinateTransformation coordinateTransformation, final Population population, final Network network, final double fraction)
void setWriterHandler(final PopulationWriterHandler handler)
void writePerson(final Person person, final BufferedWriter out)
PopulationWriter(final CoordinateTransformation coordinateTransformation, final Population population, final Network network)
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
default void putAttributeConverters(Map< Class<?>, AttributeConverter<?>> converters)
final void openOutputStream(OutputStream outputStream)
PopulationWriter(final Population population, final Network network)
void startPlans(final Population plans, final BufferedWriter out)
static SortedMap< Id< Person >, Person > getSortedPersons(final Population population)
static boolean useParallelIO()
final CoordinateTransformation coordinateTransformation
final Map< Class<?>, AttributeConverter<?> > converters
PopulationWriter(final Population population, final Network network, final double fraction)
void putAttributeConverter(Class<?> key, AttributeConverter<?> converter)