MATSIM
SelectedPlans2ESRIShape.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Plans2ESRIShape.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 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.utils.gis.matsim2esri.plans;
22 
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 import org.geotools.api.feature.simple.SimpleFeature;
29 import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
30 import org.geotools.feature.simple.SimpleFeatureBuilder;
31 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
32 import org.locationtech.jts.geom.Coordinate;
33 import org.locationtech.jts.geom.GeometryFactory;
34 import org.locationtech.jts.geom.LineString;
35 import org.locationtech.jts.geom.Point;
36 import org.matsim.api.core.v01.Coord;
37 import org.matsim.api.core.v01.Id;
57 
67  private final CoordinateReferenceSystem crs;
68  private final Population population;
69  private double outputSample = 1;
70  private double actBlurFactor = 0;
71  private double legBlurFactor = 0;
72  private final String outputDir;
73  private boolean writeActs = true;
74  private boolean writeLegs = true;
75  private ArrayList<Plan> outputSamplePlans;
76  private SimpleFeatureBuilder actBuilder;
77  private SimpleFeatureBuilder legBuilder;
78  private final GeometryFactory geofac;
79  private final Network network;
80 
81  public SelectedPlans2ESRIShape(final Population population, final Network network, final CoordinateReferenceSystem crs, final String outputDir) {
82  this.population = population;
83  this.network = network;
84  this.crs = crs;
85  this.outputDir = outputDir;
86  this.geofac = new GeometryFactory();
88  }
89 
90  public void setOutputSample(final double sample) {
91  this.outputSample = sample;
92  }
93 
94  public void setWriteActs(final boolean writeActs) {
95  this.writeActs = writeActs;
96  }
97 
98  public void setWriteLegs(final boolean writeLegs) {
99  this.writeLegs = writeLegs;
100  }
101 
102  public void setActBlurFactor(final double actBlurFactor) {
103  this.actBlurFactor = actBlurFactor;
104  }
105 
106  public void setLegBlurFactor(final double legBlurFactor) {
107  this.legBlurFactor = legBlurFactor;
108  }
109 
110  public void write() {
111  try {
113  if (this.writeActs) {
114  writeActs();
115  }
116  if (this.writeLegs) {
117  writeLegs();
118  }
119  } catch (IOException e) {
120  throw new RuntimeException(e);
121  }
122  }
123 
124  private void drawOutputSample() {
125  this.outputSamplePlans = new ArrayList<Plan>();
126  for (Person pers : PopulationUtils.getSortedPersons(this.population).values()) {
127  if (MatsimRandom.getRandom().nextDouble() <= this.outputSample) {
128  this.outputSamplePlans.add(pers.getSelectedPlan());
129  }
130  }
131  }
132 
133  private void writeActs() throws IOException {
134  String outputFile = this.outputDir + "/acts.shp";
135  ArrayList<SimpleFeature> fts = new ArrayList<SimpleFeature>();
136  for (Plan plan : this.outputSamplePlans) {
137  String id = plan.getPerson().getId().toString();
138  for (PlanElement pe : plan.getPlanElements()) {
139  if (pe instanceof Activity) {
140  Activity act = (Activity) pe;
141  fts.add(getActFeature(id, act));
142  }
143  }
144  }
145 
146  GeoFileWriter.writeGeometries(fts, outputFile);
147  }
148 
149  private void writeLegs() throws IOException {
150  String outputFile = this.outputDir + "/legs.shp";
151  ArrayList<SimpleFeature> fts = new ArrayList<SimpleFeature>();
152  for (Plan plan : this.outputSamplePlans) {
153  String id = plan.getPerson().getId().toString();
154  for (PlanElement pe : plan.getPlanElements()) {
155  if (pe instanceof Leg) {
156  Leg leg = (Leg) pe;
157  if (leg.getRoute() instanceof NetworkRoute) {
159  fts.add(getLegFeature(leg, id));
160  }
161  } else if (leg.getRoute().getDistance() > 0) {
162  fts.add(getLegFeature(leg, id));
163  }
164  }
165  }
166  }
167  GeoFileWriter.writeGeometries(fts, outputFile);
168  }
169 
170  private SimpleFeature getActFeature(final String id, final Activity act) {
171  String type = act.getType();
172  String linkId = act.getLinkId().toString();
173  Double startTime = act.getStartTime().seconds();
174  Double endTime = act.getEndTime().seconds();
175  double rx = MatsimRandom.getRandom().nextDouble() * this.actBlurFactor;
176  double ry = MatsimRandom.getRandom().nextDouble() * this.actBlurFactor;
177  Coord cc = this.network.getLinks().get(act.getLinkId()).getCoord();
178  Coord c = new Coord(cc.getX() + rx, cc.getY() + ry);
179 
180  try {
181  return this.actBuilder.buildFeature(null, new Object [] {MGC.coord2Point(c), id, type, linkId, startTime, endTime});
182  } catch (IllegalArgumentException e) {
183  e.printStackTrace();
184  }
185 
186  return null;
187  }
188 
189  private SimpleFeature getLegFeature(final Leg leg, final String id) {
190  if (!(leg.getRoute() instanceof NetworkRoute)) {
191  return null;
192  }
193  String mode = leg.getMode();
194  Double depTime = leg.getDepartureTime().seconds();
195  Double travTime = leg.getTravelTime().seconds();
197 
198  List<Id<Link>> linkIds = ((NetworkRoute) leg.getRoute()).getLinkIds();
199  Coordinate [] coords = new Coordinate[linkIds.size() + 1];
200  for (int i = 0; i < linkIds.size(); i++) {
201  Link link = this.network.getLinks().get(linkIds.get(i));
202  Coord c = link.getFromNode().getCoord();
203  double rx = MatsimRandom.getRandom().nextDouble() * this.legBlurFactor;
204  double ry = MatsimRandom.getRandom().nextDouble() * this.legBlurFactor;
205  Coordinate cc = new Coordinate(c.getX()+rx,c.getY()+ry);
206  coords[i] = cc;
207  }
208 
209  Link link = this.network.getLinks().get(linkIds.get(linkIds.size() - 1));
210  Coord c = link.getToNode().getCoord();
211  double rx = MatsimRandom.getRandom().nextDouble() * this.legBlurFactor;
212  double ry = MatsimRandom.getRandom().nextDouble() * this.legBlurFactor;
213  Coordinate cc = new Coordinate(c.getX()+rx,c.getY()+ry);
214  coords[linkIds.size()] = cc;
215 
216  LineString ls = this.geofac.createLineString(coords);
217 
218  try {
219  return this.legBuilder.buildFeature(null, new Object [] {ls,id,mode,depTime,travTime,dist});
220  } catch (IllegalArgumentException e) {
221  e.printStackTrace();
222  }
223 
224  return null;
225  }
226 
227 
228  private void initFeatureType() {
229  SimpleFeatureTypeBuilder actBuilder = new SimpleFeatureTypeBuilder();
230  actBuilder.setName("activity");
231  actBuilder.setCRS(this.crs);
232  actBuilder.add("the_geom", Point.class);
233  actBuilder.add("PERS_ID", String.class);
234  actBuilder.add("TYPE", String.class);
235  actBuilder.add("LINK_ID", String.class);
236  actBuilder.add("START_TIME", Double.class);
237  actBuilder.add("END_TIME", Double.class);
238 
239  SimpleFeatureTypeBuilder legBuilder = new SimpleFeatureTypeBuilder();
240  legBuilder.setName("leg");
241  legBuilder.setCRS(this.crs);
242  legBuilder.add("the_geom", LineString.class);
243  legBuilder.add("PERS_ID", String.class);
244  legBuilder.add("MODE", String.class);
245  legBuilder.add("DEP_TIME", Double.class);
246  legBuilder.add("TRAV_TIME", Double.class);
247  legBuilder.add("DIST", Double.class);
248 
249  this.actBuilder = new SimpleFeatureBuilder(actBuilder.buildFeatureType());
250  this.legBuilder = new SimpleFeatureBuilder(legBuilder.buildFeatureType());
251  }
252 
253  public static void main(final String [] args) {
254  // FIXME hard-coded file names; does this class really need a main-method?
255  final String populationFilename = "./examples/equil/plans100.xml";
256  final String networkFilename = "./examples/equil/network.xml";
257  // final String populationFilename = "./test/scenarios/berlin/plans_hwh_1pct.xml.gz";
258  // final String networkFilename = "./test/scenarios/berlin/network.xml.gz";
259 
260  final String outputDir = "./plans/";
261  new File(outputDir).mkdir();
262 
264  new MatsimNetworkReader(scenario.getNetwork()).readFile(networkFilename);
265  new PopulationReader(scenario).readFile(populationFilename);
266 
267  CoordinateReferenceSystem crs = MGC.getCRS("DHDN_GK4");
269  sp.setOutputSample(0.05);
270  sp.setActBlurFactor(100);
271  sp.setLegBlurFactor(100);
272  sp.setWriteActs(true);
273  sp.setWriteLegs(true);
274 
275  sp.write();
276  }
277 
278 }
279 
static Point coord2Point(final Coord coord)
Definition: MGC.java:131
SelectedPlans2ESRIShape(final Population population, final Network network, final CoordinateReferenceSystem crs, final String outputDir)
static void writeGeometries(final Collection< SimpleFeature > features, final String filename)
SimpleFeature getActFeature(final String id, final Activity act)
static SortedMap< Id< Person >, Person > getSortedPersons(final Population population)
Map< Id< Link >, ? extends Link > getLinks()
static double calcDistanceExcludingStartEndLink(final NetworkRoute route, final Network network)
final void readFile(final String filename)
static Scenario createScenario(final Config config)
static Config createConfig(final String context)
static CoordinateReferenceSystem getCRS(final String wktOrAuthorityCodeOrShorthandName)
Definition: MGC.java:169