use of org.opentripplanner.routing.edgetype.StreetTraversalPermission in project OpenTripPlanner by opentripplanner.
the class ShapefileStreetModule method buildGraph.
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
try {
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = _featureSourceFactory.getFeatureSource();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem worldCRS = factory.createCoordinateReferenceSystem("EPSG:4326");
Query query = new Query();
query.setCoordinateSystem(sourceCRS);
query.setCoordinateSystemReproject(worldCRS);
FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(query);
features = featureSource.getFeatures(query);
HashMap<String, HashMap<Coordinate, Integer>> intersectionNameToId = new HashMap<String, HashMap<Coordinate, Integer>>();
SimpleFeatureConverter<String> streetIdConverter = _schema.getIdConverter();
SimpleFeatureConverter<String> streetNameConverter = _schema.getNameConverter();
SimpleFeatureConverter<P2<StreetTraversalPermission>> permissionConverter = _schema.getPermissionConverter();
SimpleFeatureConverter<String> noteConverter = _schema.getNoteConverter();
HashMap<Coordinate, IntersectionVertex> intersectionsByLocation = new HashMap<Coordinate, IntersectionVertex>();
SimpleFeatureConverter<P2<Double>> safetyConverter = _schema.getBicycleSafetyConverter();
SimpleFeatureConverter<Boolean> slopeOverrideCoverter = _schema.getSlopeOverrideConverter();
SimpleFeatureConverter<Boolean> featureSelector = _schema.getFeatureSelector();
// Keep track of features that are duplicated so we don't have duplicate streets
Set<Object> seen = new HashSet<Object>();
List<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
FeatureIterator<SimpleFeature> it2 = features.features();
while (it2.hasNext()) {
SimpleFeature feature = it2.next();
if (featureSelector != null && !featureSelector.convert(feature)) {
continue;
}
featureList.add(feature);
}
it2.close();
it2 = null;
HashMap<Coordinate, TreeSet<String>> coordinateToStreetNames = getCoordinatesToStreetNames(featureList);
for (SimpleFeature feature : featureList) {
if (feature.getDefaultGeometry() == null) {
log.warn("feature has no geometry: " + feature.getIdentifier());
continue;
}
LineString geom = toLineString((Geometry) feature.getDefaultGeometry());
Object o = streetIdConverter.convert(feature);
String label = "" + o;
if (o != null && seen.contains(label)) {
continue;
}
seen.add(label);
String name = streetNameConverter.convert(feature);
Coordinate[] coordinates = geom.getCoordinates();
if (coordinates.length < 2) {
// not a real linestring
log.warn("Bad geometry for street with label " + label + " name " + name);
continue;
}
// this rounding is a total hack, to work around
// http://jira.codehaus.org/browse/GEOT-2811
Coordinate startCoordinate = new Coordinate(Math.round(coordinates[0].x * 1048576) / 1048576.0, Math.round(coordinates[0].y * 1048576) / 1048576.0);
Coordinate endCoordinate = new Coordinate(Math.round(coordinates[coordinates.length - 1].x * 1048576) / 1048576.0, Math.round(coordinates[coordinates.length - 1].y * 1048576) / 1048576.0);
String startIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, startCoordinate);
if (startIntersectionName == "null") {
log.warn("No intersection name for " + name);
}
String endIntersectionName = getIntersectionName(coordinateToStreetNames, intersectionNameToId, endCoordinate);
IntersectionVertex startIntersection = intersectionsByLocation.get(startCoordinate);
if (startIntersection == null) {
startIntersection = new IntersectionVertex(graph, startIntersectionName, startCoordinate.x, startCoordinate.y, new NonLocalizedString(startIntersectionName));
intersectionsByLocation.put(startCoordinate, startIntersection);
}
IntersectionVertex endIntersection = intersectionsByLocation.get(endCoordinate);
if (endIntersection == null) {
endIntersection = new IntersectionVertex(graph, endIntersectionName, endCoordinate.x, endCoordinate.y, new NonLocalizedString(endIntersectionName));
intersectionsByLocation.put(endCoordinate, endIntersection);
}
double length = 0;
for (int i = 0; i < coordinates.length - 1; ++i) {
length += JTS.orthodromicDistance(coordinates[i], coordinates[i + 1], worldCRS);
}
P2<StreetTraversalPermission> permissions = permissionConverter.convert(feature);
// TODO Set appropriate car speed from shapefile source.
StreetEdge street = edgeFactory.createEdge(startIntersection, endIntersection, geom, new NonLocalizedString(name), length, permissions.first, false);
LineString reversed = (LineString) geom.reverse();
StreetEdge backStreet = edgeFactory.createEdge(endIntersection, startIntersection, reversed, new NonLocalizedString(name), length, permissions.second, true);
backStreet.shareData(street);
if (noteConverter != null) {
String note = noteConverter.convert(feature);
if (note != null && note.length() > 0) {
Alert noteAlert = Alert.createSimpleAlerts(note);
graph.streetNotesService.addStaticNote(street, noteAlert, StreetNotesService.ALWAYS_MATCHER);
graph.streetNotesService.addStaticNote(backStreet, noteAlert, StreetNotesService.ALWAYS_MATCHER);
}
}
boolean slopeOverride = slopeOverrideCoverter.convert(feature);
street.setSlopeOverride(slopeOverride);
backStreet.setSlopeOverride(slopeOverride);
if (safetyConverter != null) {
P2<Double> safetyFactors = safetyConverter.convert(feature);
if (safetyFactors != null) {
street.setBicycleSafetyFactor(safetyFactors.first.floatValue());
backStreet.setBicycleSafetyFactor(safetyFactors.second.floatValue());
}
}
}
} catch (Exception ex) {
throw new IllegalStateException("error loading shapefile street data", ex);
} finally {
_featureSourceFactory.cleanup();
}
}
use of org.opentripplanner.routing.edgetype.StreetTraversalPermission in project OpenTripPlanner by opentripplanner.
the class OSMFilter method getPermissions.
/**
* Check OSM tags for various one-way and one-way-by-mode tags and return a pair of permissions
* for travel along and against the way.
*/
public static P2<StreetTraversalPermission> getPermissions(StreetTraversalPermission permissions, OSMWay way) {
StreetTraversalPermission permissionsFront = permissions;
StreetTraversalPermission permissionsBack = permissions;
// Check driving direction restrictions.
if (way.isOneWayForwardDriving() || way.isRoundabout()) {
permissionsBack = permissionsBack.remove(StreetTraversalPermission.BICYCLE_AND_CAR);
}
if (way.isOneWayReverseDriving()) {
permissionsFront = permissionsFront.remove(StreetTraversalPermission.BICYCLE_AND_CAR);
}
// Check bike direction restrictions.
if (way.isOneWayForwardBicycle()) {
permissionsBack = permissionsBack.remove(StreetTraversalPermission.BICYCLE);
}
if (way.isOneWayReverseBicycle()) {
permissionsFront = permissionsFront.remove(StreetTraversalPermission.BICYCLE);
}
// TODO(flamholz): figure out what this is for.
String oneWayBicycle = way.getTag("oneway:bicycle");
if (OSMWithTags.isFalse(oneWayBicycle) || way.isTagTrue("bicycle:backwards")) {
if (permissions.allows(StreetTraversalPermission.BICYCLE)) {
permissionsFront = permissionsFront.add(StreetTraversalPermission.BICYCLE);
permissionsBack = permissionsBack.add(StreetTraversalPermission.BICYCLE);
}
}
// TAG: bicycle:forward=use_sidepath
if (way.isForwardDirectionSidepath()) {
permissionsFront = permissionsFront.remove(StreetTraversalPermission.BICYCLE);
}
// TAG bicycle:backward=use_sidepath
if (way.isReverseDirectionSidepath()) {
permissionsBack = permissionsBack.remove(StreetTraversalPermission.BICYCLE);
}
if (way.isOpposableCycleway()) {
permissionsBack = permissionsBack.add(StreetTraversalPermission.BICYCLE);
}
return new P2<StreetTraversalPermission>(permissionsFront, permissionsBack);
}
use of org.opentripplanner.routing.edgetype.StreetTraversalPermission in project OpenTripPlanner by opentripplanner.
the class TurnRestrictionTest method edge.
/**
* Create an edge. If twoWay, create two edges (back and forth).
* @param back true if this is a reverse edge
*/
private StreetEdge edge(StreetVertex vA, StreetVertex vB, double length, boolean back) {
String labelA = vA.getLabel();
String labelB = vB.getLabel();
String name = String.format("%s_%s", labelA, labelB);
Coordinate[] coords = new Coordinate[2];
coords[0] = vA.getCoordinate();
coords[1] = vB.getCoordinate();
LineString geom = GeometryUtils.getGeometryFactory().createLineString(coords);
StreetTraversalPermission perm = StreetTraversalPermission.ALL;
return new StreetEdge(vA, vB, geom, name, length, perm, back);
}
use of org.opentripplanner.routing.edgetype.StreetTraversalPermission in project OpenTripPlanner by opentripplanner.
the class TurnCostTest method edge.
/**
* Create an edge. If twoWay, create two edges (back and forth).
*
* @param vA
* @param vB
* @param length
* @param back true if this is a reverse edge
*/
private StreetEdge edge(StreetVertex vA, StreetVertex vB, double length, boolean back) {
String labelA = vA.getLabel();
String labelB = vB.getLabel();
String name = String.format("%s_%s", labelA, labelB);
Coordinate[] coords = new Coordinate[2];
coords[0] = vA.getCoordinate();
coords[1] = vB.getCoordinate();
LineString geom = GeometryUtils.getGeometryFactory().createLineString(coords);
StreetTraversalPermission perm = StreetTraversalPermission.ALL;
StreetEdge pse = new StreetEdge(vA, vB, geom, name, length, perm, back);
pse.setCarSpeed(1.0f);
return pse;
}
use of org.opentripplanner.routing.edgetype.StreetTraversalPermission in project OpenTripPlanner by opentripplanner.
the class TestGraph method edge.
/**
* Create an edge. If twoWay, create two edges (back and forth).
*
* @param vA
* @param vB
* @param length
*/
private StreetEdge edge(StreetVertex vA, StreetVertex vB, double length) {
String labelA = vA.getLabel();
String labelB = vB.getLabel();
String name = String.format("%s_%s", labelA, labelB);
Coordinate[] coords = new Coordinate[2];
coords[0] = vA.getCoordinate();
coords[1] = vB.getCoordinate();
LineString geom = GeometryUtils.getGeometryFactory().createLineString(coords);
StreetTraversalPermission perm = StreetTraversalPermission.ALL;
return new StreetEdge(vA, vB, geom, name, length, perm, false);
}
Aggregations