MATSIM
PersonWriter.java
Go to the documentation of this file.
1 package org.matsim.analysis;
2 
3 import jakarta.inject.Inject;
4 import org.apache.commons.csv.CSVFormat;
5 import org.apache.commons.csv.CSVPrinter;
6 import org.apache.logging.log4j.LogManager;
7 import org.apache.logging.log4j.Logger;
11 import org.matsim.core.config.Config;
14 import org.matsim.core.utils.io.IOUtils;
15 
16 import java.io.BufferedWriter;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 
22 public class PersonWriter {
23 
24  @Inject
25  Config config;
26 
27  @Inject
28  Scenario scenario;
29 
30  @Inject
31  OutputDirectoryHierarchy outputDirectoryHierarchy;
32 
33  private static final Logger log = LogManager.getLogger(PersonWriter.class);
34 
35  void writeCsv() {
36  log.info("Writing all Persons to " + Controler.DefaultFiles.personscsv);
37 
38  List<String> attributes = prepareAttributes();
39  String[] header = prepareHeader(attributes);
40 
41  try {
42  BufferedWriter bufferedWriter = IOUtils.getBufferedWriter(outputDirectoryHierarchy.getOutputFilename(Controler.DefaultFiles.personscsv));
43  CSVPrinter csvPrinter = new CSVPrinter(bufferedWriter, CSVFormat.Builder.create()
44  .setDelimiter(config.global().getDefaultDelimiter().charAt(0))
45  .setHeader(header).build());
46  for (Person p : scenario.getPopulation().getPersons().values()) {
47  writePerson(p, attributes, csvPrinter);
48  }
49 
50  csvPrinter.close();
51  } catch (IOException e) {
52  e.printStackTrace();
53  }
54  log.info("...done");
55  }
56 
57  private void writePerson(Person p, List<String> attributes, CSVPrinter csvPrinter) throws IOException {
58  if (p.getSelectedPlan() == null) {
59  log.error("Found person without a selected plan: " + p.getId().toString() + " will not be added to output_persons.csv");
60  return;
61  }
62  List<String> line = new ArrayList<>();
63  line.add(p.getId().toString());
64  line.add(p.getSelectedPlan().getScore() == null ? "null" : p.getSelectedPlan().getScore().toString());
65  String x = "";
66  String y = "";
67  String actType = "";
68  if (!p.getSelectedPlan().getPlanElements().isEmpty()) {
69  Activity firstAct = (Activity) p.getSelectedPlan().getPlanElements().get(0);
70  if (firstAct.getCoord() != null) {
71  x = Double.toString(firstAct.getCoord().getX());
72  y = Double.toString(firstAct.getCoord().getY());
73  }
74  actType = firstAct.getType();
75  }
76  line.add(x);
77  line.add(y);
78  line.add(actType);
79  for (String attribute : attributes) {
80  Object value = p.getAttributes().getAttribute(attribute);
81  String result = value != null ? String.valueOf(value) : "";
82  line.add(result);
83  }
84  csvPrinter.printRecord(line);
85  }
86 
87  private String[] prepareHeader(List<String> attributes) {
88  List<String> header = new ArrayList<>();
89  header.add("person");
90  header.add("executed_score");
91  header.add("first_act_x");
92  header.add("first_act_y");
93  header.add("first_act_type");
94  header.addAll(attributes);
95  return header.toArray(String[]::new);
96  }
97 
98  private List<String> prepareAttributes() {
99  List<String> attributes = scenario.getPopulation().getPersons().values().parallelStream()
100  .flatMap(p -> p.getAttributes().getAsMap().keySet().stream()).distinct()
101  .collect(Collectors.toList());
102  attributes.remove("vehicles");
103  return attributes;
104  }
105 }
void writePerson(Person p, List< String > attributes, CSVPrinter csvPrinter)
static BufferedWriter getBufferedWriter(URL url, Charset charset, boolean append)
Definition: IOUtils.java:390
String [] prepareHeader(List< String > attributes)