Package com.vividsolutions.jts.linearref

Examples of com.vividsolutions.jts.linearref.LinearLocation


    private LinearLocation getSegmentFraction(double[] distances, double distance) {
        int index = Arrays.binarySearch(distances, distance);
        if (index < 0)
            index = -(index + 1);
        if (index == 0)
            return new LinearLocation(0, 0.0);
        if (index == distances.length)
            return new LinearLocation(distances.length, 0.0);

        double prevDistance = distances[index - 1];
        if (prevDistance == distances[index]) {
            return new LinearLocation(index - 1, 1.0);
        }
        double indexPart = (distance - distances[index - 1])
                / (distances[index] - prevDistance);
        return new LinearLocation(index - 1, indexPart);
    }
View Full Code Here


        }

        LinearIterator it = new LinearIterator(routeGeometry, routeIndex);
        if (it.hasNext()) {
            it.next();
            LinearLocation routeSuccessor = it.getLocation();

            // now we want to see where this new point is in terms of the edge's geometry
            Coordinate newRouteCoord = routeSuccessor.getCoordinate(routeGeometry);
            LinearLocation newEdgeIndex = indexedEdge.project(newRouteCoord);

            Coordinate edgeCoord = newEdgeIndex.getCoordinate(edgeGeometry);
            if (newEdgeIndex.compareTo(edgeIndex) <= 0) {
                // we must make forward progress along the edge... or go to the next edge
                /* this should not require the try/catch, but there is a bug in JTS */
                try {
                    LinearLocation projected2 = indexedEdge.indexOfAfter(edgeCoord, edgeIndex);
                    //another bug in JTS
                    if (Double.isNaN(projected2.getSegmentFraction())) {
                        // we are probably moving backwards
                        return Collections.emptyList();
                    } else {
                        newEdgeIndex = projected2;
                        if (newEdgeIndex.equals(edgeIndex)) {
                            return Collections.emptyList();
                        }
                    }
                    edgeCoord = newEdgeIndex.getCoordinate(edgeGeometry);
                } catch (AssertionFailedException e) {
                    // we are not making progress, so just return an empty list
                    return Collections.emptyList();
                }
            }

            if (newEdgeIndex.getSegmentIndex() == edgeGeometry.getNumPoints() - 1) {

                // we might choose to continue from the end of the edge and a point mid-way
                // along this route segment

                // find nearest point that makes progress along the route
                Vertex toVertex = edge.getToVertex();
                Coordinate endCoord = toVertex.getCoordinate();
                LocationIndexedLine indexedRoute = new LocationIndexedLine(routeGeometry);

                // FIXME: it would be better to do this project/indexOfAfter in one step
                // as the two-step version could snap to a bad place and be unable to escape.

                LinearLocation routeProjectedEndIndex = indexedRoute.project(endCoord);
                Coordinate routeProjectedEndCoord = routeProjectedEndIndex
                        .getCoordinate(routeGeometry);

                if (routeProjectedEndIndex.compareTo(routeIndex) <= 0) {
                    try {
                        routeProjectedEndIndex = indexedRoute.indexOfAfter(routeProjectedEndCoord,
                                routeIndex);
                        if (Double.isNaN(routeProjectedEndIndex.getSegmentFraction())) {
                            // can't go forward
                            routeProjectedEndIndex = routeIndex; // this is bad, but not terrible
                                                                 // since we are advancing along the edge
                        }
                    } catch (AssertionFailedException e) {
                        routeProjectedEndIndex = routeIndex;
                    }
                    routeProjectedEndCoord = routeProjectedEndIndex.getCoordinate(routeGeometry);
                }

                double positionError = distance(routeProjectedEndCoord, endCoord);
                double travelAlongRoute = distanceAlongGeometry(routeGeometry, routeIndex,
                        routeProjectedEndIndex);
                double travelAlongEdge = distanceAlongGeometry(edgeGeometry, edgeIndex,
                        newEdgeIndex);
                double travelError = Math.abs(travelAlongEdge - travelAlongRoute);

                double error = positionError + travelError;
               
                if (error > MAX_ERROR) {
                    // we're not going to bother with states which are
                    // totally wrong
                    return nextStates;
                }
               
                for (Edge e : getOutgoingMatchableEdges(toVertex)) {
                    double cost = error + NEW_SEGMENT_PENALTY;
                    if (!carsCanTraverse(e)) {
                        cost += NO_TRAVERSE_PENALTY;
                    }
                    MatchState nextState = new MidblockMatchState(this, routeGeometry, e,
                            routeProjectedEndIndex, new LinearLocation(), cost, travelAlongRoute);
                    nextStates.add(nextState);
                }

            } else {

                double travelAlongEdge = distanceAlongGeometry(edgeGeometry, edgeIndex,
                        newEdgeIndex);
                double travelAlongRoute = distanceAlongGeometry(routeGeometry, routeIndex,
                        routeSuccessor);
                double travelError = Math.abs(travelAlongRoute - travelAlongEdge);

                double positionError = distance(edgeCoord, newRouteCoord);

                double error = travelError + positionError;

                MatchState nextState = new MidblockMatchState(this, routeGeometry, edge,
                        routeSuccessor, newEdgeIndex, error, travelAlongRoute);
                nextStates.add(nextState);

                // it's also possible that, although we have not yet reached the end of this edge,
                // we are going to turn, because the route turns earlier than the edge. In that
                // case, we jump to the corner, and our error is the distance from the route point
                // and the corner

                Vertex toVertex = edge.getToVertex();
                double travelAlongOldEdge = distanceAlongGeometry(edgeGeometry, edgeIndex, null);

                for (Edge e : getOutgoingMatchableEdges(toVertex)) {
                    Geometry newEdgeGeometry = e.getGeometry();
                    LocationIndexedLine newIndexedEdge = new LocationIndexedLine(newEdgeGeometry);
                    newEdgeIndex = newIndexedEdge.project(newRouteCoord);
                    Coordinate newEdgeCoord = newEdgeIndex.getCoordinate(newEdgeGeometry);
                    positionError = distance(newEdgeCoord, newRouteCoord);
                    travelAlongEdge = travelAlongOldEdge + distanceAlongGeometry(newEdgeGeometry, new LinearLocation(), newEdgeIndex);
                    travelError = Math.abs(travelAlongRoute - travelAlongEdge);

                    error = travelError + positionError;

                    if (error > MAX_ERROR) {
                        // we're not going to bother with states which are
                        // totally wrong
                        return nextStates;
                    }

                    double cost = error + NEW_SEGMENT_PENALTY;
                    if (!carsCanTraverse(e)) {
                        cost += NO_TRAVERSE_PENALTY;
                    }

                    nextState = new MidblockMatchState(this, routeGeometry, e, routeSuccessor,
                            new LinearLocation(), cost, travelAlongRoute);
                    nextStates.add(nextState);
                }

            }
            return nextStates;

        } else {
            Coordinate routeCoord = routeIndex.getCoordinate(routeGeometry);
            LinearLocation projected = indexedEdge.project(routeCoord);
            double locationError = distance(projected.getCoordinate(edgeGeometry), routeCoord);

            MatchState end = new EndMatchState(this, locationError, 0);
            return Arrays.asList(end);
        }

View Full Code Here

            return currentLine.getCoordinateN(vertexIndex + 1);
        return null;
    }

    public LinearLocation getLocation() {
        return new LinearLocation(componentIndex, vertexIndex, segmentFraction);
    }
View Full Code Here

        //the version in LinearLocation is broken
       
        int lastComponentIndex = linear.getNumGeometries() - 1;
        LineString lastLine = (LineString) linear.getGeometryN(lastComponentIndex);
        int lastSegmentIndex = lastLine.getNumPoints() - 1;
        return new LinearLocation(lastComponentIndex, lastSegmentIndex, 0.0);
    }
View Full Code Here

            return LinearIterator.this.hasNext();
        }

        @Override
        public LinearLocation next() {
            LinearLocation result = getLocation();
            LinearIterator.this.next();
            return result;
        }
View Full Code Here

        if (endIndex == null) {
            endIndex = LinearLocation.getEndLocation(geometry);
        }
        double total = 0;
        LinearIterator it = new LinearIterator(geometry, startIndex);
        LinearLocation index = startIndex;
        Coordinate previousCoordinate = startIndex.getCoordinate(geometry);

        it.next();
        index = it.getLocation();
        while (index.compareTo(endIndex) < 0) {
            Coordinate thisCoordinate = index.getCoordinate(geometry);
            double distance = distanceLibrary.fastDistance(previousCoordinate, thisCoordinate);
            total += distance;
            previousCoordinate = thisCoordinate;
            if (!it.hasNext())
                break;
View Full Code Here

        routeGeometry = DouglasPeuckerSimplifier.simplify(routeGeometry, 0.00001);

        // initial state: start midway along a block.
        LocationIndexedLine indexedLine = new LocationIndexedLine(routeGeometry);

        LinearLocation startIndex = indexedLine.getStartIndex();

        Coordinate routeStartCoordinate = startIndex.getCoordinate(routeGeometry);
        Envelope envelope = new Envelope(routeStartCoordinate);
        double distanceThreshold = DISTANCE_THRESHOLD;
        envelope.expandBy(distanceThreshold);

        BinHeap<MatchState> states = new BinHeap<MatchState>();
        List<Edge> nearbyEdges = index.query(envelope);
        while (nearbyEdges.isEmpty()) {
            envelope.expandBy(distanceThreshold);
            distanceThreshold *= 2;
            nearbyEdges = index.query(envelope);
        }

        // compute initial states
        for (Edge initialEdge : nearbyEdges) {
            Geometry edgeGeometry = initialEdge.getGeometry();
           
            LocationIndexedLine indexedEdge = new LocationIndexedLine(edgeGeometry);
            LinearLocation initialLocation = indexedEdge.project(routeStartCoordinate);
           
            double error = MatchState.distance(initialLocation.getCoordinate(edgeGeometry), routeStartCoordinate);
            MidblockMatchState state = new MidblockMatchState(null, routeGeometry, initialEdge, startIndex, initialLocation, error, 0.01);
            states.insert(state, 0); //make sure all initial states are visited by inserting them at 0
        }

        // search for best-matching path
View Full Code Here

                        LineString ls = ((StreetVertex)v).getOutgoing().iterator().next().getGeometry();
                        int numPoints = ls.getNumPoints();
                        LocationIndexedLine lil = new LocationIndexedLine(ls);
                        int seg = random.nextInt(numPoints);
                        double frac = random.nextDouble();
                        LinearLocation ll = new LinearLocation(seg, frac);
                        c = lil.extractPoint(ll);
                    } else {
                        c = v.getCoordinate();
                    }
                    // perturb
View Full Code Here

        Expression lineExpression = parameters.get(1);
        Geometry line = lineExpression.evaluate(object, Geometry.class);
   
        LocationIndexedLine index = new LocationIndexedLine(line);
   
        LinearLocation location = index.project(point.getCoordinate());
   
        Coordinate snap = index.extractPoint(location);
   
        Point pt = point.getFactory().createPoint(snap);
   
View Full Code Here

            // distance plus a little bit
            double minDist = MAX_SEARCH_DISTANCE + 1.0e-6;
            Coordinate minDistPoint = null;

            for (LocationIndexedLine line : lines) {
                LinearLocation here = line.project(pt);
                Coordinate point = line.extractPoint(here);
                double dist = point.distance(pt);
                if (dist < minDist) {
                    minDist = dist;
                    minDistPoint = point;
View Full Code Here

TOP

Related Classes of com.vividsolutions.jts.linearref.LinearLocation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.