Search in sources :

Example 11 with BBox

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

the class SpatialRuleLookupArrayTest method testSpatialLookup.

@Test
public void testSpatialLookup() {
    List<SpatialRule> spatialRules = new ArrayList<>();
    SpatialRule germany = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "DEU";
        }
    }.addBorder(new Polygon(new double[] { 1, 1, 2, 2 }, new double[] { 1, 2, 2, 1 }));
    spatialRules.add(germany);
    SpatialRule austria = new DefaultSpatialRule() {

        @Override
        public String getId() {
            return "AUT";
        }
    }.addBorder(new Polygon(new double[] { 5, 5, 6, 6 }, new double[] { 5, 6, 6, 5 }));
    spatialRules.add(austria);
    // create lookup with bbox just for DEU (for space reduction)
    SpatialRuleLookupArray lookup = new SpatialRuleLookupArray(spatialRules, 1, false, new BBox(1, 2, 1, 2));
    SpatialRule rule = lookup.lookupRule(1.5, 1.5);
    assertEquals(germany, rule);
    assertEquals("DEU", rule.getId());
    int id = lookup.getSpatialId(rule);
    assertTrue(id > 0);
    assertEquals(rule, lookup.getSpatialRule(id));
}
Also used : BBox(com.graphhopper.util.shapes.BBox) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with BBox

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

the class SpatialRuleLookupArrayTest method testSmallScenario.

@Test
public void testSmallScenario() {
    List<SpatialRule> spatialRules = new ArrayList<>();
    spatialRules.add(getSpatialRule(new Polygon(new double[] { 1, 1, 2, 2 }, new double[] { 1, 2, 2, 1 }), "1"));
    spatialRules.add(getSpatialRule(new Polygon(new double[] { 1, 1, 3.6, 3.6 }, new double[] { 3, 4, 4, 3 }), "2"));
    SpatialRuleLookup spatialRuleLookup = new SpatialRuleLookupArray(spatialRules, 1, false, new BBox(1, 4, 1, 4));
    Assert.assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE));
    assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, spatialRuleLookup.lookupRule(1.2, 3.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE));
    assertEquals(AccessValue.ACCESSIBLE, spatialRuleLookup.lookupRule(2.2, 1.7).getAccessValue(null, TransportationMode.MOTOR_VEHICLE, AccessValue.ACCESSIBLE));
}
Also used : BBox(com.graphhopper.util.shapes.BBox) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with BBox

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

the class SimpleRouteSerializer method toJSON.

@Override
public Map<String, Object> toJSON(GHResponse rsp, boolean calcPoints, boolean pointsEncoded, boolean includeElevation, boolean enableInstructions) {
    Map<String, Object> json = new HashMap<String, Object>();
    if (rsp.hasErrors()) {
        json.put("message", getMessage(rsp.getErrors().get(0)));
        List<Map<String, Object>> errorHintList = new ArrayList<>();
        for (Throwable t : rsp.getErrors()) {
            Map<String, Object> map = new HashMap<>();
            map.put("message", getMessage(t));
            map.put("details", t.getClass().getName());
            if (t instanceof GHException) {
                map.putAll(((GHException) t).getDetails());
            }
            errorHintList.add(map);
        }
        json.put("hints", errorHintList);
    } else {
        Map<String, Object> jsonInfo = new HashMap<String, Object>();
        json.put("info", jsonInfo);
        json.put("hints", rsp.getHints().toMap());
        // If you replace GraphHopper with your own brand name, this is fine.
        // Still it would be highly appreciated if you mention us in your about page!
        jsonInfo.put("copyrights", Arrays.asList("GraphHopper", "OpenStreetMap contributors"));
        List<Map<String, Object>> jsonPathList = new ArrayList<Map<String, Object>>();
        for (PathWrapper ar : rsp.getAll()) {
            Map<String, Object> jsonPath = new HashMap<String, Object>();
            jsonPath.put("distance", Helper.round(ar.getDistance(), 3));
            jsonPath.put("weight", Helper.round6(ar.getRouteWeight()));
            jsonPath.put("time", ar.getTime());
            jsonPath.put("transfers", ar.getNumChanges());
            if (!ar.getDescription().isEmpty())
                jsonPath.put("description", ar.getDescription());
            if (calcPoints) {
                jsonPath.put("points_encoded", pointsEncoded);
                PointList points = ar.getPoints();
                if (points.getSize() >= 2) {
                    BBox maxBounds2D = new BBox(maxBounds.minLon, maxBounds.maxLon, maxBounds.minLat, maxBounds.maxLat);
                    jsonPath.put("bbox", ar.calcRouteBBox(maxBounds2D).toGeoJson());
                }
                jsonPath.put("points", createPoints(points, pointsEncoded, includeElevation));
                if (enableInstructions) {
                    InstructionList instructions = ar.getInstructions();
                    jsonPath.put("instructions", instructions.createJson());
                }
                jsonPath.put("legs", ar.getLegs());
                jsonPath.put("details", ar.getPathDetails());
                jsonPath.put("ascend", ar.getAscend());
                jsonPath.put("descend", ar.getDescend());
            }
            jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
            if (ar.getFare() != null) {
                jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(ar.getFare()));
            }
            jsonPathList.add(jsonPath);
        }
        json.put("paths", jsonPathList);
    }
    return json;
}
Also used : PointList(com.graphhopper.util.PointList) InstructionList(com.graphhopper.util.InstructionList) PathWrapper(com.graphhopper.PathWrapper) BBox(com.graphhopper.util.shapes.BBox) GHException(com.graphhopper.util.exceptions.GHException)

Example 14 with BBox

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

the class GraphHopperGeocodingIT method testExtent.

@Test
public void testExtent() {
    GHGeocodingResponse response = geocoding.geocode(new GHGeocodingRequest("new york", "en", 7));
    BBox extent = response.getHits().get(0).getExtendBBox();
    assertTrue(extent.isValid());
    assertTrue(extent.minLon < -79);
    assertTrue(extent.maxLon > -72);
    assertTrue(extent.minLat < 40.5);
    assertTrue(extent.maxLat > 45);
}
Also used : BBox(com.graphhopper.util.shapes.BBox) GHGeocodingRequest(com.graphhopper.api.model.GHGeocodingRequest) GHGeocodingResponse(com.graphhopper.api.model.GHGeocodingResponse) Test(org.junit.Test)

Example 15 with BBox

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

the class PathWrapper method calcRouteBBox.

/**
 * Calculates the bounding box of this route response
 */
public BBox calcRouteBBox(BBox _fallback) {
    check("calcRouteBBox");
    BBox bounds = BBox.createInverse(_fallback.hasElevation());
    int len = pointList.getSize();
    if (len == 0)
        return _fallback;
    for (int i = 0; i < len; i++) {
        double lat = pointList.getLatitude(i);
        double lon = pointList.getLongitude(i);
        if (bounds.hasElevation()) {
            double ele = pointList.getEle(i);
            bounds.update(lat, lon, ele);
        } else {
            bounds.update(lat, lon);
        }
    }
    return bounds;
}
Also used : BBox(com.graphhopper.util.shapes.BBox)

Aggregations

BBox (com.graphhopper.util.shapes.BBox)38 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)11 GHPoint (com.graphhopper.util.shapes.GHPoint)7 GHJsonFactory (com.graphhopper.json.GHJsonFactory)4 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)4 JsonFeature (com.graphhopper.json.geo.JsonFeature)3 Graph (com.graphhopper.storage.Graph)3 IntArrayList (com.carrotsearch.hppc.IntArrayList)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 EdgeFilter (com.graphhopper.routing.util.EdgeFilter)2 SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)2 GraphBuilder (com.graphhopper.storage.GraphBuilder)2 PMap (com.graphhopper.util.PMap)2 PointList (com.graphhopper.util.PointList)2 Circle (com.graphhopper.util.shapes.Circle)2 Map (java.util.Map)2 IntIntHashMap (com.carrotsearch.hppc.IntIntHashMap)1 IntLongHashMap (com.carrotsearch.hppc.IntLongHashMap)1 GTFSFeed (com.conveyal.gtfs.GTFSFeed)1