use of org.opentripplanner.model.StreetNote in project OpenTripPlanner by opentripplanner.
the class ShapefileStreetModule method buildGraph.
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra, DataImportIssueStore issueStore) {
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) {
StreetNote noteAlert = new StreetNote(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.model.StreetNote in project OpenTripPlanner by opentripplanner.
the class WFSNotePollingGraphUpdater method buildMatcherAndStreetNote.
/**
* Create a MatcherAndStreetNote, interning it if the note and matcher pair is already created.
* Note: we use the default Object.equals() for matchers, as they are mostly already singleton
* instances.
*/
private MatcherAndStreetNote buildMatcherAndStreetNote(NoteMatcher noteMatcher, StreetNote note) {
T2<NoteMatcher, StreetNote> key = new T2<>(noteMatcher, note);
MatcherAndStreetNote interned = uniqueMatchers.get(key);
if (interned != null) {
return interned;
}
MatcherAndStreetNote ret = new MatcherAndStreetNote(noteMatcher, note);
uniqueMatchers.put(key, ret);
return ret;
}
use of org.opentripplanner.model.StreetNote in project OpenTripPlanner by opentripplanner.
the class NoteProperties method generateNote.
public T2<StreetNote, NoteMatcher> generateNote(OSMWithTags way) {
I18NString text;
// TODO: this could probably be made without patternMatch for {} since all notes (at least currently) have {note} as notePattern
if (patternMatcher.matcher(notePattern).matches()) {
// This gets language -> translation of notePattern and all tags (which can have translations name:en for example)
Map<String, String> noteText = TemplateLibrary.generateI18N(notePattern, way);
text = TranslatedString.getI18NString(noteText);
} else {
text = new LocalizedString(notePattern, way);
}
StreetNote note = new StreetNote(text);
return new T2<>(note, noteMatcher);
}
use of org.opentripplanner.model.StreetNote in project OpenTripPlanner by opentripplanner.
the class StreetNotesService method getNotes.
/**
* Return the set of notes applicable for this state / backedge pair.
*
* @param state
* @return The set of notes or null if empty.
*/
public Set<StreetNote> getNotes(State state) {
Edge edge = state.getBackEdge();
Set<MatcherAndStreetNote> maas = new HashSet<MatcherAndStreetNote>();
for (StreetNotesSource source : sources) {
Set<MatcherAndStreetNote> maas2 = source.getNotes(edge);
if (maas2 != null)
maas.addAll(maas2);
}
if (maas == null || maas.isEmpty()) {
return null;
}
Set<StreetNote> notes = new HashSet<StreetNote>(maas.size());
for (MatcherAndStreetNote maa : maas) {
if (maa.getMatcher().matches(state)) {
notes.add(maa.getNote());
}
}
if (notes.isEmpty()) {
return null;
}
return notes;
}
use of org.opentripplanner.model.StreetNote in project OpenTripPlanner by opentripplanner.
the class TestHalfEdges method testStreetSplittingAlerts.
/**
* Test that alerts on split streets are preserved, i.e. if there are alerts on the street that is split the same alerts should be present on the
* new street.
*/
@Test
public void testStreetSplittingAlerts() {
DisposableEdgeCollection tempEdges = new DisposableEdgeCollection(graph);
HashSet<Edge> turns = new HashSet<Edge>();
turns.add(left);
turns.add(leftBack);
StreetNote alert = new StreetNote("This is the alert");
Set<StreetNote> alerts = new HashSet<>();
alerts.add(alert);
graph.streetNotesService.addStaticNote(left, alert, StreetNotesService.ALWAYS_MATCHER);
graph.streetNotesService.addStaticNote(leftBack, alert, StreetNotesService.ALWAYS_MATCHER);
TemporaryStreetLocation start = StreetVertexIndex.createTemporaryStreetLocationForTest("start", new NonLocalizedString("start"), filter(turns, StreetEdge.class), new LinearLocation(0, 0.4).getCoordinate(left.getGeometry()), false, tempEdges);
// The alert should be preserved
// traverse the FreeEdge from the StreetLocation to the new IntersectionVertex
RoutingRequest req = new RoutingRequest();
State traversedOne = new State(start, req);
State currentState;
for (Edge e : start.getOutgoing()) {
currentState = e.traverse(traversedOne);
if (currentState != null) {
traversedOne = currentState;
break;
}
}
assertEquals(alerts, graph.streetNotesService.getNotes(traversedOne));
assertNotSame(left, traversedOne.getBackEdge().getFromVertex());
assertNotSame(leftBack, traversedOne.getBackEdge().getFromVertex());
// now, make sure wheelchair alerts are preserved
StreetNote wheelchairAlert = new StreetNote("This is the wheelchair alert");
Set<StreetNote> wheelchairAlerts = new HashSet<>();
wheelchairAlerts.add(wheelchairAlert);
graph.streetNotesService.removeStaticNotes(left);
graph.streetNotesService.removeStaticNotes(leftBack);
graph.streetNotesService.addStaticNote(left, wheelchairAlert, StreetNotesService.WHEELCHAIR_MATCHER);
graph.streetNotesService.addStaticNote(leftBack, wheelchairAlert, StreetNotesService.WHEELCHAIR_MATCHER);
req.setWheelchairAccessible(true);
start = StreetVertexIndex.createTemporaryStreetLocationForTest("start", new NonLocalizedString("start"), filter(turns, StreetEdge.class), new LinearLocation(0, 0.4).getCoordinate(left.getGeometry()), false, tempEdges);
traversedOne = new State(start, req);
for (Edge e : start.getOutgoing()) {
currentState = e.traverse(traversedOne);
if (currentState != null) {
traversedOne = currentState;
break;
}
}
assertEquals(wheelchairAlerts, graph.streetNotesService.getNotes(traversedOne));
assertNotSame(left, traversedOne.getBackEdge().getFromVertex());
assertNotSame(leftBack, traversedOne.getBackEdge().getFromVertex());
tempEdges.disposeEdges();
}
Aggregations