Search in sources :

Example 1 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class CompressedArray method get.

public GHPoint get(long index) {
    int segmentNo = (int) (index / entriesPerSegment);
    int entry = (int) (index % entriesPerSegment);
    try {
        if (segmentNo >= segments.size()) {
            return null;
        }
        byte[] bytes = segments.get(segmentNo);
        VLongStorage store = new VLongStorage(decompress(bytes));
        long len = store.getLength();
        for (int i = 0; store.getPosition() < len; i++) {
            long latlon = store.readVLong();
            if (i == entry) {
                GHPoint point = new GHPoint();
                algo.decode(latlon, point);
                return point;
            }
        }
        return null;
    } catch (ArrayIndexOutOfBoundsException ex) {
        throw new RuntimeException("index " + index + "=> segNo:" + segmentNo + ", entry=" + entry + ", segments:" + segments.size(), ex);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : VLongStorage(com.graphhopper.storage.VLongStorage) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) DataFormatException(java.util.zip.DataFormatException)

Example 2 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class ViaRoutingTemplate method lookup.

@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
    if (points.size() < 2)
        throw new IllegalArgumentException("At least 2 points have to be specified, but was:" + points.size());
    EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
    queryResults = new ArrayList<>(points.size());
    for (int placeIndex = 0; placeIndex < points.size(); placeIndex++) {
        GHPoint point = points.get(placeIndex);
        QueryResult res;
        if (ghRequest.hasPointHints()) {
            res = locationIndex.findClosest(point.lat, point.lon, new NameSimilarityEdgeFilter(edgeFilter, ghRequest.getPointHints().get(placeIndex)));
            if (!res.isValid()) {
                res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
            }
        } else {
            res = locationIndex.findClosest(point.lat, point.lon, edgeFilter);
        }
        if (!res.isValid())
            ghResponse.addError(new PointNotFoundException("Cannot find point " + placeIndex + ": " + point, placeIndex));
        queryResults.add(res);
    }
    return queryResults;
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) PointNotFoundException(com.graphhopper.util.exceptions.PointNotFoundException) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 3 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class TestAlgoCollector method queryIndex.

void queryIndex(Graph g, LocationIndex idx, double lat, double lon, double expectedDist) {
    QueryResult res = idx.findClosest(lat, lon, EdgeFilter.ALL_EDGES);
    if (!res.isValid()) {
        errors.add("node not found for " + lat + "," + lon);
        return;
    }
    GHPoint found = res.getSnappedPoint();
    double dist = distCalc.calcDist(lat, lon, found.lat, found.lon);
    if (Math.abs(dist - expectedDist) > .1) {
        errors.add("queried lat,lon=" + (float) lat + "," + (float) lon + " (found: " + (float) found.lat + "," + (float) found.lon + ")" + "\n   expected distance:" + expectedDist + ", but was:" + dist);
    }
}
Also used : QueryResult(com.graphhopper.storage.index.QueryResult) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 4 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class SpatialRuleLookupArray method addRule.

@Override
public void addRule(SpatialRule rule) {
    if (rule == null)
        throw new IllegalArgumentException("rule cannot be null");
    if (rule.equals(SpatialRule.EMPTY))
        throw new IllegalArgumentException("rule cannot be EMPTY");
    addSingleRule(rule);
    int ruleContainerIndex = addRuleContainer(new SpatialRuleContainer().addRule(rule));
    for (Polygon polygon : rule.getBorders()) {
        for (int i = getXIndexForLon(polygon.getMinLon()); i < getXIndexForLon(polygon.getMaxLon()) + 1; i++) {
            for (int j = getYIndexForLat(polygon.getMinLat()); j < getYIndexForLat(polygon.getMaxLat()) + 1; j++) {
                if (i >= lookupArray.length || j >= lookupArray[0].length) {
                    continue;
                }
                GHPoint center = getCoordinatesForIndex(i, j);
                // TODO: Consider creating a new method in Polygon that does the 5 checks - p.partOfTile?
                if (polygon.contains(center) || polygon.contains(center.getLat() - checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() - checkDiff, center.getLon() + checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() + checkDiff)) {
                    if (lookupArray[i][j] == EMPTY_RULE_INDEX) {
                        lookupArray[i][j] = (byte) ruleContainerIndex;
                    } else {
                        // Merge Rules
                        SpatialRuleContainer curContainer = getContainerFor2DIndex(i, j);
                        SpatialRuleContainer newContainer = curContainer.copy().addRule(rule);
                        int newRuleContainerIndex = addRuleContainer(newContainer);
                        lookupArray[i][j] = (byte) newRuleContainerIndex;
                    }
                }
            }
        }
    }
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 5 with GHPoint

use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.

the class Location2IDQuadtree method fillQuadtree.

private GHBitSet fillQuadtree(int size) {
    int locs = graph.getNodes();
    if (locs <= 0) {
        throw new IllegalStateException("check your graph - it is empty!");
    }
    GHBitSet filledIndices = new GHBitSetImpl(size);
    GHPoint coord = new GHPoint();
    for (int nodeId = 0; nodeId < locs; nodeId++) {
        double lat = nodeAccess.getLatitude(nodeId);
        double lon = nodeAccess.getLongitude(nodeId);
        int key = (int) keyAlgo.encode(lat, lon);
        long bytePos = (long) key * 4;
        if (filledIndices.contains(key)) {
            int oldNodeId = index.getInt(bytePos);
            keyAlgo.decode(key, coord);
            // decide which one is closer to 'key'
            double distNew = distCalc.calcNormalizedDist(coord.lat, coord.lon, lat, lon);
            double oldLat = nodeAccess.getLatitude(oldNodeId);
            double oldLon = nodeAccess.getLongitude(oldNodeId);
            double distOld = distCalc.calcNormalizedDist(coord.lat, coord.lon, oldLat, oldLon);
            // new point is closer to quad tree point (key) so overwrite old
            if (distNew < distOld) {
                index.setInt(bytePos, nodeId);
            }
        } else {
            index.setInt(bytePos, nodeId);
            filledIndices.add(key);
        }
    }
    return filledIndices;
}
Also used : GHBitSetImpl(com.graphhopper.coll.GHBitSetImpl) GHBitSet(com.graphhopper.coll.GHBitSet) GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint)

Aggregations

GHPoint (com.graphhopper.util.shapes.GHPoint)206 Test (org.junit.jupiter.api.Test)62 GHRequest (com.graphhopper.GHRequest)57 GHResponse (com.graphhopper.GHResponse)50 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)50 Test (org.junit.Test)45 LMProfile (com.graphhopper.config.LMProfile)27 Profile (com.graphhopper.config.Profile)27 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)27 CHProfile (com.graphhopper.config.CHProfile)26 Snap (com.graphhopper.storage.index.Snap)17 QueryResult (com.graphhopper.storage.index.QueryResult)15 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)13 ResponsePath (com.graphhopper.ResponsePath)11 ArrayList (java.util.ArrayList)11 EnumSource (org.junit.jupiter.params.provider.EnumSource)11 PathWrapper (com.graphhopper.PathWrapper)8 PointNotFoundException (com.graphhopper.util.exceptions.PointNotFoundException)8 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)8 IntArrayList (com.carrotsearch.hppc.IntArrayList)6