Search in sources :

Example 1 with Way

use of com.conveyal.osmlib.Way in project traffic-engine by opentraffic.

the class OSMDataStore method getStreetSegments.

/**
 * Chop up given OSM into segments at tripengine's tripline clusters.
 *
 * @param osm	an osm object
 * @return	a list of street segments
 */
private List<StreetSegment> getStreetSegments(OSM osm) {
    GeometryFactory gf = new GeometryFactory();
    Set<Long> intersectionNodes = findIntersections(osm);
    List<StreetSegment> newSegments = new ArrayList<>();
    for (Entry<Long, Way> entry : osm.ways.entrySet()) {
        Long wayId = entry.getKey();
        Way way = entry.getValue();
        // only find segments for traffic edges
        if (!StreetSegment.isTrafficEdge(way))
            continue;
        LineString wayLineString;
        try {
            wayLineString = OSMUtils.getLineStringForWay(way, osm);
        } catch (RuntimeException ex) {
            continue;
        }
        double segmentDist = 0;
        Long lastNodeId = null;
        Point lastPoint = null;
        List<Coordinate> segmentCords = new ArrayList<>();
        for (int i = 0; i < way.nodes.length; i++) {
            Long nodeId = way.nodes[i];
            if (lastNodeId == null)
                lastNodeId = nodeId;
            // get the linear reference of this node along the way
            Point point = wayLineString.getPointN(i);
            if (lastPoint != null)
                segmentDist += getDistance(lastPoint.getX(), lastPoint.getY(), point.getX(), point.getY());
            lastPoint = point;
            segmentCords.add(point.getCoordinate());
            // check to see if segment completes the line or is an intersection pair
            if (segmentCords.size() > 1 && (intersectionNodes.contains(nodeId) || i == (way.nodes.length - 1))) {
                // make segment
                Coordinate[] segmentCoordArray = new Coordinate[segmentCords.size()];
                LineString segmentGeometry = gf.createLineString(segmentCords.toArray(segmentCoordArray));
                StreetSegment streetSegment = new StreetSegment(this.streetSegments.getNextId(), way, wayId, lastNodeId, nodeId, segmentGeometry, segmentDist);
                newSegments.add(streetSegment);
                // create reverse
                if (!streetSegment.oneway) {
                    LineString reverseSegmentGeometry = (LineString) segmentGeometry.reverse();
                    newSegments.add(new StreetSegment(this.streetSegments.getNextId(), way, wayId, nodeId, lastNodeId, reverseSegmentGeometry, segmentDist));
                }
                // reset for next segment
                segmentCords = new ArrayList<>();
                segmentCords.add(point.getCoordinate());
                segmentDist = 0;
                lastNodeId = nodeId;
            }
        }
    }
    return newSegments;
}
Also used : StreetSegment(io.opentraffic.engine.geom.StreetSegment) Way(com.conveyal.osmlib.Way)

Example 2 with Way

use of com.conveyal.osmlib.Way in project traffic-engine by opentraffic.

the class OSMDataStore method findIntersections.

/**
 * Returns the id of every node encountered in an OSM dataset more than once.
 * @param osm
 * @return
 */
private Set<Long> findIntersections(OSM osm) {
    Set<Long> intersectionNodes = new HashSet<>();
    // link nodes to the ways that visit them
    Map<Long, Integer> nodeToWay = new HashMap<>();
    for (Entry<Long, Way> wayEntry : osm.ways.entrySet()) {
        Way way = wayEntry.getValue();
        // only find intersections with other traffic edges
        if (!StreetSegment.isTrafficEdge(way))
            continue;
        for (Long node : way.nodes) {
            Integer count = nodeToWay.get(node);
            if (count == null) {
                nodeToWay.put(node, 1);
            } else {
                // the non-first time you've seen a node, add it to the
                // intersections set
                // note after the first time the add will be redundant, but
                // a duplicate add will have no effect
                intersectionNodes.add(node);
            }
        }
    }
    return intersectionNodes;
}
Also used : Way(com.conveyal.osmlib.Way)

Aggregations

Way (com.conveyal.osmlib.Way)2 StreetSegment (io.opentraffic.engine.geom.StreetSegment)1