Search in sources :

Example 16 with BBox

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

the class SpatialRuleLookupHelper method buildAndInjectSpatialRuleIntoGH.

public static void buildAndInjectSpatialRuleIntoGH(GraphHopper graphHopper, CmdArgs args) {
    String spatialRuleLocation = args.get("spatial_rules.location", "");
    if (!spatialRuleLocation.isEmpty()) {
        try {
            final BBox maxBounds = BBox.parseBBoxString(args.get("spatial_rules.max_bbox", "-180, 180, -90, 90"));
            final InputStreamReader reader = new InputStreamReader(new FileInputStream(spatialRuleLocation), UTF_CS);
            final SpatialRuleLookup index = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, maxBounds);
            logger.info("Set spatial rule lookup with " + index.size() + " rules");
            final FlagEncoderFactory oldFEF = graphHopper.getFlagEncoderFactory();
            graphHopper.setFlagEncoderFactory(new FlagEncoderFactory() {

                @Override
                public FlagEncoder createFlagEncoder(String name, PMap configuration) {
                    if (name.equals(GENERIC)) {
                        return new DataFlagEncoder(configuration).setSpatialRuleLookup(index);
                    }
                    return oldFEF.createFlagEncoder(name, configuration);
                }
            });
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) PMap(com.graphhopper.util.PMap) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) BBox(com.graphhopper.util.shapes.BBox) GHJsonFactory(com.graphhopper.json.GHJsonFactory) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) CountriesSpatialRuleFactory(com.graphhopper.routing.util.spatialrules.CountriesSpatialRuleFactory) FlagEncoderFactory(com.graphhopper.routing.util.FlagEncoderFactory)

Example 17 with BBox

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

the class SpatialRuleLookupBuilderTest method testNoIntersection.

@Test
public void testNoIntersection() throws FileNotFoundException {
    final FileReader reader = new FileReader(COUNTRIES_FILE);
    SpatialRuleLookup spatialRuleLookup = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, new BBox(-180, -179, -90, -89));
    assertEquals(SpatialRuleLookup.EMPTY, spatialRuleLookup);
}
Also used : GHJsonFactory(com.graphhopper.json.GHJsonFactory) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) Test(org.junit.Test)

Example 18 with BBox

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

the class SpatialRuleLookupBuilderTest method testBounds.

@Test
public void testBounds() throws FileNotFoundException {
    final FileReader reader = new FileReader(COUNTRIES_FILE);
    SpatialRuleLookup spatialRuleLookup = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, new BBox(-180, 180, -90, 90));
    BBox almostWorldWide = new BBox(-179, 179, -89, 89);
    // Might fail if a polygon is defined outside the above coordinates
    assertTrue("BBox seems to be not contracted", almostWorldWide.contains(spatialRuleLookup.getBounds()));
}
Also used : GHJsonFactory(com.graphhopper.json.GHJsonFactory) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) Test(org.junit.Test)

Example 19 with BBox

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

the class SpatialRuleLookupBuilderTest method testIntersection.

@Test
public void testIntersection() throws FileNotFoundException {
    /*
         We are creating a BBox smaller than Germany. We have the German Spatial rule activated by default.
         So the BBox should not contain a Point lying somewhere close in Germany.
        */
    final FileReader reader = new FileReader(COUNTRIES_FILE);
    SpatialRuleLookup spatialRuleLookup = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, new BBox(9, 10, 51, 52));
    assertFalse("BBox seems to be incorrectly contracted", spatialRuleLookup.getBounds().contains(49.9, 8.9));
}
Also used : GHJsonFactory(com.graphhopper.json.GHJsonFactory) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) Test(org.junit.Test)

Example 20 with BBox

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

the class Measurement method printLocationIndexQuery.

private void printLocationIndexQuery(Graph g, final LocationIndex idx, int count) {
    count *= 2;
    final BBox bbox = g.getBounds();
    final double latDelta = bbox.maxLat - bbox.minLat;
    final double lonDelta = bbox.maxLon - bbox.minLon;
    final Random rand = new Random(seed);
    MiniPerfTest miniPerf = new MiniPerfTest() {

        @Override
        public int doCalc(boolean warmup, int run) {
            double lat = rand.nextDouble() * latDelta + bbox.minLat;
            double lon = rand.nextDouble() * lonDelta + bbox.minLon;
            int val = idx.findClosest(lat, lon, EdgeFilter.ALL_EDGES).getClosestNode();
            return val;
        }
    }.setIterations(count).start();
    print("location_index", miniPerf);
}
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