MATSIM
FacilitiesUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FacilitiesUtils.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 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.facilities;
22 
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
29 import org.matsim.api.core.v01.Id;
34 import org.matsim.core.config.Config;
35 import org.matsim.core.gbl.Gbl;
41 
47 public class FacilitiesUtils {
48  private static final Logger log = LogManager.getLogger( FacilitiesUtils.class ) ;
49 
50  private FacilitiesUtils() {} // container for static methods; do not instantiate
51 
53  return createActivityFacilities(null) ;
54  }
55 
56  public static ActivityFacilities createActivityFacilities(String name) {
57  return new ActivityFacilitiesImpl( name ) ;
58  }
59 
63  public static SortedMap<Id<ActivityFacility>, ActivityFacility> getSortedFacilities(final ActivityFacilities facilities) {
64  return new TreeMap<>(facilities.getFacilities());
65  }
66 
67  public static void setLinkID( final Facility facility , Id<Link> linkId ) {
68  if ( facility instanceof ActivityFacilityImpl ) {
69  ((ActivityFacilityImpl) facility).setLinkId(linkId);
70  } else {
71  throw new RuntimeException("cannot set linkID for this facility type; API needs to be cleaned up") ;
72  }
73  }
82  public static Id<Link> decideOnLinkId( final Facility facility, final Network network ) {
83  Link accessActLink = null ;
84 
85  Id<Link> accessActLinkId = null ;
86  try {
87  accessActLinkId = facility.getLinkId() ;
88  } catch ( Exception ee ) {
89  // there are implementations that throw an exception here although "null" is, in fact, an interpretable value. kai, oct'18
90  }
91  if ( accessActLinkId!=null ) {
92  return accessActLinkId ;
93  }
94  return decideOnLink( facility, network ).getId() ;
95  }
96 
101  @Deprecated
102  public static Link decideOnLink( final Facility facility, final Network network ) {
103  Link accessActLink = null ;
104 
105  Id<Link> accessActLinkId = null ;
106  try {
107  accessActLinkId = facility.getLinkId() ;
108  } catch ( Exception ee ) {
109  // there are implementations that throw an exception here although "null" is, in fact, an interpretable value. kai, oct'18
110  }
111 
112  if ( accessActLinkId!=null ) {
113  accessActLink = network.getLinks().get( facility.getLinkId() );
114  // i.e. if street address is in mode-specific subnetwork, I just use that, and do not search for another (possibly closer)
115  // other link.
116 
117  }
118 
119  if ( accessActLink==null ) {
120  // this is the case where the postal address link is NOT in the subnetwork, i.e. does NOT serve the desired mode,
121  // OR the facility does not have a street address link in the first place.
122 
123  if( facility.getCoord()==null ) {
124  throw new RuntimeException("link for facility cannot be determined when neither facility link id nor facility coordinate given") ;
125  }
126 
127  accessActLink = NetworkUtils.getNearestLink(network, facility.getCoord()) ;
128  if ( accessActLink == null ) {
129  log.warn("Facility without link for which no nearest link on the respective network could be found. " +
130  "About to abort. Writing out the first 10 links to understand which subnetwork was used to help debugging.");
131  int ii = 0 ;
132  for ( Link link : network.getLinks().values() ) {
133  if ( ii==10 ) {
134  break ;
135  }
136  ii++ ;
137  log.warn( link );
138  }
139  }
140  Gbl.assertNotNull(accessActLink);
141  }
142  return accessActLink;
143 
144  // I just found out that there are facilities that insist on links that may not be postal addresses since they cannot be reached by car.
145  // TransitStopFacility is an example. kai, jun'19
146 
147  }
148 
149  public static Facility toFacility( final Activity toWrap, ActivityFacilities activityFacilities ){
150  if ( activityFacilities!=null && toWrap.getFacilityId()!=null ){
151  ActivityFacility fac = activityFacilities.getFacilities().get( toWrap.getFacilityId() );
152  if( fac != null ){
153  return fac;
154  }
155  }
156  return new ActivityWrapperFacility( toWrap );
157  }
158 
163  public static Facility wrapActivity ( final Activity toWrap ) {
164  return new ActivityWrapperFacility( toWrap ) ;
165  }
166 
167  public static Facility wrapLink( final Link link ) {
168  return new LinkWrapperFacility( link ) ;
169  }
170  public static Facility wrapLinkAndCoord(final Link link, final Coord coord){
171  return new LinkWrapperFacilityWithSpecificCoord(link,coord);
172  }
173 
177  public static Coord decideOnCoord( final Facility facility, final Network network, final Config config ) {
178  return decideOnCoord( facility, network, config.global().getRelativePositionOfEntryExitOnLink() ) ;
179  }
183  public static Coord decideOnCoord( final Facility facility, final Network network, double relativePositionOfEntryExitOnLink ) {
184  if ( facility.getCoord() != null && ! ( facility instanceof LinkWrapperFacility)) {
185  return facility.getCoord() ;
186  }
187 
188  if ( facility.getLinkId()==null ) {
189  if ( facility instanceof Identifiable ) {
190  throw new RuntimeException( "facility with id=" + ((Identifiable) facility).getId() + " has neither coord nor linkId. This " +
191  "does not work ..." ) ;
192  } else {
193  throw new RuntimeException( "facility which does not implement Identifiable has neither coord nor linkId. This " +
194  "does not work ..." ) ;
195  }
196  }
197 
198  Gbl.assertNotNull( network ) ;
199  Link link = network.getLinks().get( facility.getLinkId() ) ;
200  Gbl.assertNotNull( link );
201  Coord fromCoord = link.getFromNode().getCoord() ;
202  Coord toCoord = link.getToNode().getCoord() ;
203  return new Coord( fromCoord.getX() + relativePositionOfEntryExitOnLink *( toCoord.getX() - fromCoord.getX()) , fromCoord.getY() + relativePositionOfEntryExitOnLink *( toCoord.getY() - fromCoord.getY() ) );
204 
205  }
206 
207  // Logic gotten from PopulationUtils, but I am actually a bit unsure about the value of those methods now that
208  // attributable is the only way to get attributes... td, aug'19
209  // yy I would agree. They are useful to manage the transition, but can be inlined afterwards. I would inline for all code we can reach, afterwards
210  // resurrect them but mark as deprecated. kai, nov'19
211 
212  public static <F extends Facility & Attributable> Object getFacilityAttribute(F facility, String key) {
213  return facility.getAttributes().getAttribute( key );
214  }
215 
216  public static <F extends Facility & Attributable> void putFacilityAttribute(F facility, String key, Object value ) {
217  facility.getAttributes().putAttribute( key, value ) ;
218  }
219 
220  public static <F extends Facility & Attributable> Object removeFacilityAttribute( F facility, String key ) {
221  return facility.getAttributes().removeAttribute( key );
222  }
223 }
Map< Id< ActivityFacility >, ? extends ActivityFacility > getFacilities()
static< F extends Facility &Attributable > Object removeFacilityAttribute(F facility, String key)
static Facility wrapLink(final Link link)
static< F extends Facility &Attributable > void putFacilityAttribute(F facility, String key, Object value)
static< F extends Facility &Attributable > Object getFacilityAttribute(F facility, String key)
static ActivityFacilities createActivityFacilities(String name)
static SortedMap< Id< ActivityFacility >, ActivityFacility > getSortedFacilities(final ActivityFacilities facilities)
static Facility wrapLinkAndCoord(final Link link, final Coord coord)
static Link decideOnLink(final Facility facility, final Network network)
static Link getNearestLink(Network network, final Coord coord)
static void assertNotNull(Object obj)
Definition: Gbl.java:212
static Facility wrapActivity(final Activity toWrap)
Map< Id< Link >, ? extends Link > getLinks()
Id< ActivityFacility > getFacilityId()
static Coord decideOnCoord(final Facility facility, final Network network, final Config config)
static void setLinkID(final Facility facility, Id< Link > linkId)
final GlobalConfigGroup global()
Definition: Config.java:395
static ActivityFacilities createActivityFacilities()
static Id< Link > decideOnLinkId(final Facility facility, final Network network)
static Facility toFacility(final Activity toWrap, ActivityFacilities activityFacilities)
static Coord decideOnCoord(final Facility facility, final Network network, double relativePositionOfEntryExitOnLink)