MATSIM
XY2Links.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * XY2Links.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.core.population.algorithms;
22 
23 import org.matsim.api.core.v01.Scenario;
30 import org.matsim.core.gbl.Gbl;
34 
35 import java.util.List;
36 import java.util.Objects;
37 
44 public final class XY2Links extends AbstractPersonAlgorithm implements PlanAlgorithm {
45 
46  private final Network network;
48 
54  public XY2Links(final Network network, final ActivityFacilities activityFacilities ) {
55  super();
56  this.network = network;
57  this.activityFacilities = activityFacilities;
58  }
59 
60  public XY2Links(final Scenario scenario) {
61  this(scenario.getNetwork(), scenario.getActivityFacilities());
62  }
63 
65  @Override
66  public void run(final Person person) {
67  for (Plan plan : person.getPlans()) {
68  processPlan(plan);
69  }
70  }
71 
73  @Override
74  public void run(final Plan plan) {
75  processPlan(plan);
76  }
77 
78  private void processPlan(final Plan plan) {
79  Objects.requireNonNull( this.activityFacilities ) ;
80  List<PlanElement> planElements = plan.getPlanElements();
81  for (PlanElement planElement : planElements) {
82  if (planElement instanceof Activity) {
83  Activity act = (Activity) planElement;
84 
85  if ( this.activityFacilities != null ) {
86  // since the facilities in Scenario are now permanently enabled, this can only happen when called through the
87  // more specific constructor. kai, feb'16
88 
89  //following is not necessary. Kai, Amit July'18
90 // if (act.getFacilityId() != null) {
91 // ActivityFacility facility = facilities.getFacilities().get(act.getFacilityId());
92 // if (facility != null) {
93 // act.setLinkId(facility.getLinkId());
94 // // yy facility.getLinkId may be null, in particular since linkId is not even part of the facilities DTD. kai, feb'16
95 // // right, just had such a situation in CarSharingIT test. Amit Jul'18
96 //
97 // if (act.getLinkId() == null && act.getCoord()==null){
98 // // for FacilitiesSource.onePerActivityLocationInPlansFile, one can opt to keep coords in facility only. Amit Jan'18
99 // act.setCoord(facility.getCoord());
100 // }
101 // }
102 // }
103  }
104 
105  if ( act.getLinkId() != null ) {
106  // there may be activities in a plan that have a link and others that have a coordinate.
107  // Those that have a link do not need a new link. In addition, they may not even have a
108  // coordinate. kai/dominik, nov'11
109  continue ;
110  }
111 
112  if ( act.getCoord() == null ) {
114  final ActivityFacility activityFacility = this.activityFacilities.getFacilities().get( act.getFacilityId() );
115  Gbl.assertNotNull( activityFacility );
116  act.setCoord( activityFacility.getCoord() ) ;
117  }
118 
119  // If the linkId is still null get nearest link from the network
120 // Link link = this.network.getNearestLinkExactly(act.getCoord());
121  Link link = NetworkUtils.getNearestLink(this.network, act.getCoord());
122  // getNearestLinkExactly not necessarily better than getNearestLink. E.g.
123  // n--n-----------------------------n
124  // A home location slightly to the right of the middle node will take:
125  // * the left link with getNearestLink
126  // * the right link with getNearestLinkExactly
127  // kai/dominik, jan'13
128  /* ownPrepareForSimExample in matsim tutorials gives an example how to use
129  * getNearestLinkExactly . tt feb'2016
130  */
131 
132  if (null == link) {
133  throw new RuntimeException("For person id="+plan.getPerson().getId()+": getNearestLink returned Null! act="+act);
134  }
135  act.setLinkId(link.getId());
136  }
137  }
138  }
139 }
Map< Id< ActivityFacility >, ? extends ActivityFacility > getFacilities()
static Link getNearestLink(Network network, final Coord coord)
List< PlanElement > getPlanElements()
static void assertNotNull(Object obj)
Definition: Gbl.java:212
Id< ActivityFacility > getFacilityId()
ActivityFacilities getActivityFacilities()
abstract List<? extends T > getPlans()