org.opentripplanner.graph_builder.module.shapefile.CaseBasedTraversalPermissionConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
package org.opentripplanner.graph_builder.module.shapefile;
import java.util.HashMap;
import java.util.Map;
import org.opengis.feature.simple.SimpleFeature;
import org.opentripplanner.common.model.P2;
import org.opentripplanner.graph_builder.services.shapefile.SimpleFeatureConverter;
import org.opentripplanner.routing.edgetype.StreetTraversalPermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Calculates street traversal permissions based upon a fixed set of cases.
*
* For example, given a shapefile that includes a DIRECTION column with data as follows:
*
* | DIRECTION | NAME |
* | ONE_WAY_F | Side St |
* | TWO_WAY | Main St |
* | ONE_WAY_B | Foo St. |
*
* You could use a CaseBasedTraversalPermissionConverter to implement the following rules:
*
* By default, all streets should be traversable by pedestrians and bicycles in both directions.
*
* If a street's DIRECTION attribute is ONE_WAY_F, it should be traversable by cars and bikes in
* only the forward direction and traversable by pedestrians in both directions.
*
* If a street's DIRECTION attribute is ONE_WAY_B, it should be traversable by cars and bikes in
* only the backward direction and traversable by pedestrians in both directions.
*
* If a street's DIRECTION attribute is TWO_WAY, it should be traversable by everyone in both
* directions.
*
*
* These rules could be implemented by configuring the converter bean as follows:
*
* {@code
*
*
*
*
*
*
* }
*
* @see org.opentripplanner.routing.edgetype.StreetTraversalPermission
*
*/
public class CaseBasedTraversalPermissionConverter implements
SimpleFeatureConverter> {
private static Logger log = LoggerFactory.getLogger(CaseBasedBicycleSafetyFeatureConverter.class);
private String attributeName;
private P2 defaultPermission = P2.createPair(
StreetTraversalPermission.ALL, StreetTraversalPermission.ALL);
private Map> _permissions = new HashMap>();
public CaseBasedTraversalPermissionConverter() {
}
public CaseBasedTraversalPermissionConverter(String attributeName) {
this.attributeName = attributeName;
}
public CaseBasedTraversalPermissionConverter(String attributeName,
StreetTraversalPermission defaultPermission) {
this.attributeName = attributeName;
this.defaultPermission = P2.createPair(defaultPermission, defaultPermission);
}
/**
* The name of the feature attribute to use when calculating the traversal permissions.
*/
public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}
/**
* The default permission to use when no matching case is found for a street.
*/
public void setDefaultPermission(StreetTraversalPermission permission) {
defaultPermission = P2.createPair(permission, permission);
}
/**
* The mapping from attribute values to permissions to use when determining a street's traversal
* permission.
*/
public void setPermissions(Map permissions) {
for (Map.Entry entry : permissions.entrySet()) {
String attributeValue = entry.getKey();
String perms = entry.getValue();
String[] tokens = perms.split(",");
if (tokens.length != 2)
throw new IllegalArgumentException("invalid street traversal permissions: " + perms);
StreetTraversalPermission forward = StreetTraversalPermission.valueOf(tokens[0]);
StreetTraversalPermission reverse = StreetTraversalPermission.valueOf(tokens[1]);
addPermission(attributeValue, forward, reverse);
}
}
public void addPermission(String attributeValue, StreetTraversalPermission forward,
StreetTraversalPermission reverse) {
_permissions.put(attributeValue, P2.createPair(forward, reverse));
}
@Override
public P2 convert(SimpleFeature feature) {
if (attributeName == null) {
return defaultPermission;
}
Object key = feature.getAttribute(attributeName);
if (key == null) {
return defaultPermission;
}
P2 permission = _permissions.get(key.toString());
if (permission == null) {
log.info("unexpected permission " + key.toString());
return defaultPermission;
}
return permission;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy