Search in sources :

Example 1 with StreetNote

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();
    }
}
Also used : Hints(org.geotools.util.factory.Hints) Query(org.geotools.data.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) TreeSet(java.util.TreeSet) StreetTraversalPermission(org.opentripplanner.routing.edgetype.StreetTraversalPermission) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) HashSet(java.util.HashSet) P2(org.opentripplanner.common.model.P2) CRSAuthorityFactory(org.opengis.referencing.crs.CRSAuthorityFactory) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(org.locationtech.jts.geom.Coordinate) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) IntersectionVertex(org.opentripplanner.routing.vertextype.IntersectionVertex) StreetNote(org.opentripplanner.model.StreetNote)

Example 2 with StreetNote

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;
}
Also used : NoteMatcher(org.opentripplanner.routing.services.notes.NoteMatcher) MatcherAndStreetNote(org.opentripplanner.routing.services.notes.MatcherAndStreetNote) MatcherAndStreetNote(org.opentripplanner.routing.services.notes.MatcherAndStreetNote) StreetNote(org.opentripplanner.model.StreetNote) T2(org.opentripplanner.common.model.T2)

Example 3 with StreetNote

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);
}
Also used : I18NString(org.opentripplanner.util.I18NString) TranslatedString(org.opentripplanner.util.TranslatedString) I18NString(org.opentripplanner.util.I18NString) LocalizedString(org.opentripplanner.util.LocalizedString) StreetNote(org.opentripplanner.model.StreetNote) LocalizedString(org.opentripplanner.util.LocalizedString) T2(org.opentripplanner.common.model.T2)

Example 4 with StreetNote

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;
}
Also used : StreetNote(org.opentripplanner.model.StreetNote) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet)

Example 5 with StreetNote

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();
}
Also used : TemporaryStreetLocation(org.opentripplanner.routing.location.TemporaryStreetLocation) DisposableEdgeCollection(org.opentripplanner.graph_builder.linking.DisposableEdgeCollection) LinearLocation(org.locationtech.jts.linearref.LinearLocation) State(org.opentripplanner.routing.core.State) NonLocalizedString(org.opentripplanner.util.NonLocalizedString) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) RoutingRequest(org.opentripplanner.routing.api.request.RoutingRequest) StreetNote(org.opentripplanner.model.StreetNote) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

StreetNote (org.opentripplanner.model.StreetNote)7 HashSet (java.util.HashSet)3 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)3 Edge (org.opentripplanner.routing.graph.Edge)3 NonLocalizedString (org.opentripplanner.util.NonLocalizedString)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)2 T2 (org.opentripplanner.common.model.T2)2 MatcherAndStreetNote (org.opentripplanner.routing.services.notes.MatcherAndStreetNote)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 Query (org.geotools.data.Query)1 Hints (org.geotools.util.factory.Hints)1 Test (org.junit.Test)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 LinearLocation (org.locationtech.jts.linearref.LinearLocation)1