Class GraphHopper

java.lang.Object
com.graphhopper.GraphHopper

public class GraphHopper extends Object
Easy to use access point to configure import and (offline) routing.
Author:
Peter Karich
  • Field Details

  • Constructor Details

    • GraphHopper

      public GraphHopper()
  • Method Details

    • setEncodedValuesString

      public GraphHopper setEncodedValuesString(String encodedValuesString)
    • getEncodedValuesString

      public String getEncodedValuesString()
    • getEncodingManager

      public EncodingManager getEncodingManager()
    • getOSMParsers

      public OSMParsers getOSMParsers()
    • getElevationProvider

      public ElevationProvider getElevationProvider()
    • setElevationProvider

      public GraphHopper setElevationProvider(ElevationProvider eleProvider)
    • setPathDetailsBuilderFactory

      public GraphHopper setPathDetailsBuilderFactory(PathDetailsBuilderFactory pathBuilderFactory)
    • getPathDetailsBuilderFactory

      public PathDetailsBuilderFactory getPathDetailsBuilderFactory()
    • setPreciseIndexResolution

      public GraphHopper setPreciseIndexResolution(int precision)
      Precise location resolution index means also more space (disc/RAM) could be consumed and probably slower query times, which would be e.g. not suitable for Android. The resolution specifies the tile width (in meter).
    • setMinNetworkSize

      public GraphHopper setMinNetworkSize(int minNetworkSize)
    • setUrbanDensityCalculation

      public GraphHopper setUrbanDensityCalculation(double residentialAreaRadius, double residentialAreaSensitivity, double cityAreaRadius, double cityAreaSensitivity, int threads)
      Configures the urban density classification. Each edge will be classified as 'rural','residential' or 'city', UrbanDensity
      Parameters:
      residentialAreaRadius - in meters. The higher this value the longer the calculation will take and the bigger the area for which the road density used to identify residential areas is calculated.
      residentialAreaSensitivity - Use this to find a trade-off between too many roads being classified as residential (too high values) and not enough roads being classified as residential (too small values)
      cityAreaRadius - in meters. The higher this value the longer the calculation will take and the bigger the area for which the road density used to identify city areas is calculated. Set this to zero to skip the city classification.
      cityAreaSensitivity - Use this to find a trade-off between too many roads being classified as city (too high values) and not enough roads being classified as city (too small values)
      threads - the number of threads used for the calculation. If this is zero the urban density calculation is skipped entirely
    • setStoreOnFlush

      public GraphHopper setStoreOnFlush(boolean storeOnFlush)
      Only valid option for in-memory graph and if you e.g. want to disable store on flush for unit tests. Specify storeOnFlush to true if you want that existing data will be loaded FROM disc and all in-memory data will be flushed TO disc after flush is called e.g. while OSM import.
      Parameters:
      storeOnFlush - true by default
    • setProfiles

      public GraphHopper setProfiles(Profile... profiles)
      Sets the routing profiles that shall be supported by this GraphHopper instance. The (and only the) given profiles can be used for routing without preparation and for CH/LM preparation.

      Here is an example how to setup two CH profiles and one LM profile (via the Java API)

       
         hopper.setProfiles(
           new Profile("my_car"),
           new Profile("your_bike")
         );
         hopper.getCHPreparationHandler().setCHProfiles(
           new CHProfile("my_car"),
           new CHProfile("your_bike")
         );
         hopper.getLMPreparationHandler().setLMProfiles(
           new LMProfile("your_bike")
         );
       
       

      See also https://github.com/graphhopper/graphhopper/pull/1922.

      See Also:
    • setProfiles

      public GraphHopper setProfiles(List<Profile> profiles)
    • getProfiles

      public List<Profile> getProfiles()
    • getProfile

      public Profile getProfile(String profileName)
      Returns the profile for the given profile name, or null if it does not exist
    • hasElevation

      public boolean hasElevation()
      Returns:
      true if storing and fetching elevation data is enabled. Default is false
    • setElevation

      public GraphHopper setElevation(boolean includeElevation)
      Enable storing and fetching elevation data. Default is false
    • getGraphHopperLocation

      public String getGraphHopperLocation()
    • setGraphHopperLocation

      public GraphHopper setGraphHopperLocation(String ghLocation)
      Sets the graphhopper folder.
    • getOSMFile

      public String getOSMFile()
    • setOSMFile

      public GraphHopper setOSMFile(String osmFile)
      This file can be an osm xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file (.pbf).
    • setMaxSpeedCalculator

      public GraphHopper setMaxSpeedCalculator(MaxSpeedCalculator maxSpeedCalculator)
    • getBaseGraph

      public BaseGraph getBaseGraph()
      The underlying graph used in algorithms.
      Throws:
      IllegalStateException - if graph is not instantiated.
    • setBaseGraph

      public void setBaseGraph(BaseGraph baseGraph)
    • getProperties

      public StorableProperties getProperties()
    • getCHGraphs

      public Map<String,RoutingCHGraph> getCHGraphs()
      Returns:
      a mapping between profile names and according CH preparations. The map will be empty before loading or import.
    • getLandmarks

      public Map<String,LandmarkStorage> getLandmarks()
      Returns:
      a mapping between profile names and according landmark preparations. The map will be empty before loading or import.
    • getLocationIndex

      public LocationIndex getLocationIndex()
      The location index created from the graph.
      Throws:
      IllegalStateException - if index is not initialized
    • setLocationIndex

      protected void setLocationIndex(LocationIndex locationIndex)
    • isAllowWrites

      public boolean isAllowWrites()
    • setAllowWrites

      public GraphHopper setAllowWrites(boolean allowWrites)
      Specifies if it is allowed for GraphHopper to write. E.g. for read only filesystems it is not possible to create a lock file and so we can avoid write locks.
    • getTranslationMap

      public TranslationMap getTranslationMap()
    • setImportRegistry

      public GraphHopper setImportRegistry(ImportRegistry importRegistry)
    • getImportRegistry

      public ImportRegistry getImportRegistry()
    • setCustomAreasDirectory

      public GraphHopper setCustomAreasDirectory(String customAreasDirectory)
    • getCustomAreasDirectory

      public String getCustomAreasDirectory()
    • setCountryRuleFactory

      public GraphHopper setCountryRuleFactory(CountryRuleFactory countryRuleFactory)
      Sets the factory used to create country rules. Use `null` to disable country rules
    • getCountryRuleFactory

      public CountryRuleFactory getCountryRuleFactory()
    • init

      public GraphHopper init(GraphHopperConfig ghConfig)
      Reads the configuration from a GraphHopperConfig object which can be manually filled, or more typically is read from `config.yml`.

      Important note: Calling this method overwrites the configuration done in some of the setter methods of this class, so generally it is advised to either use this method to configure GraphHopper or the different setter methods, but not both. Unfortunately, this still does not cover all cases and sometimes you have to use both, but then you should make sure there are no conflicts. If you need both it might also help to call the init before calling the setters, because this way the init method won't apply defaults to configuration options you already chose using the setters.

    • buildEncodingManager

      protected EncodingManager buildEncodingManager(Map<String,PMap> encodedValuesWithProps, Map<String,ImportUnit> activeImportUnits, Map<String,List<String>> restrictionVehicleTypesByProfile)
    • getEVSortIndex

      protected List<String> getEVSortIndex(Map<String,Profile> profilesByName)
    • buildOSMParsers

      protected OSMParsers buildOSMParsers(Map<String,PMap> encodedValuesWithProps, Map<String,ImportUnit> activeImportUnits, Map<String,List<String>> restrictionVehicleTypesByProfile, List<String> ignoredHighways, String dateRangeParserString)
    • parseEncodedValueString

      public static Map<String,PMap> parseEncodedValueString(String encodedValuesStr)
    • importOrLoad

      public GraphHopper importOrLoad()
      Imports provided data from disc and creates graph. Depending on the settings the resulting graph will be stored to disc so on a second call this method will only load the graph from disc which is usually a lot faster.
    • importAndClose

      public void importAndClose()
      Imports and processes data, storing it to disk when complete.
    • process

      protected void process(boolean closeEarly)
      Creates the graph from OSM data.
    • prepareImport

      protected void prepareImport()
    • postImportOSM

      protected void postImportOSM()
    • importOSM

      protected void importOSM()
    • createBaseGraphAndProperties

      protected void createBaseGraphAndProperties()
    • _getOSMFile

      protected File _getOSMFile()
      Currently we use this for a few tests where the dataReaderFile is loaded from the classpath
    • load

      public boolean load()
      Load from existing graph folder.
    • checkProfilesConsistency

      public void checkProfilesConsistency()
    • getCHPreparationHandler

      public final CHPreparationHandler getCHPreparationHandler()
    • getLMPreparationHandler

      public final LMPreparationHandler getLMPreparationHandler()
    • postProcessing

      protected void postProcessing(boolean closeEarly)
      Runs both after the import and when loading an existing Graph
      Parameters:
      closeEarly - release resources as early as possible
    • importPublicTransit

      protected void importPublicTransit()
    • createWeighting

      public final Weighting createWeighting(Profile profile, PMap hints)
    • createWeighting

      public final Weighting createWeighting(Profile profile, PMap hints, boolean disableTurnCosts)
    • createWeightingFactory

      protected WeightingFactory createWeightingFactory()
    • route

      public GHResponse route(GHRequest request)
    • doCreateRouter

      protected Router doCreateRouter(BaseGraph baseGraph, EncodingManager encodingManager, LocationIndex locationIndex, Map<String,Profile> profilesByName, PathDetailsBuilderFactory pathBuilderFactory, TranslationMap trMap, RouterConfig routerConfig, WeightingFactory weightingFactory, Map<String,RoutingCHGraph> chGraphs, Map<String,LandmarkStorage> landmarks)
    • createLocationIndex

      protected LocationIndex createLocationIndex(Directory dir)
    • initLocationIndex

      protected void initLocationIndex()
      Initializes the location index after the import is done.
    • loadOrPrepareCH

      protected void loadOrPrepareCH(boolean closeEarly)
    • prepareCH

      protected Map<String,PrepareContractionHierarchies.Result> prepareCH(boolean closeEarly, List<CHConfig> configsToPrepare)
    • loadOrPrepareLM

      protected void loadOrPrepareLM(boolean closeEarly)
      For landmarks it is required to always call this method: either it creates the landmark data or it loads it.
    • prepareLM

      protected List<PrepareLandmarks> prepareLM(boolean closeEarly, List<LMConfig> configsToPrepare)
    • cleanUp

      protected void cleanUp()
      Internal method to clean up the graph.
    • flush

      protected void flush()
    • close

      public void close()
      Releases all associated resources like memory or files. But it does not remove them. To remove the files created in graphhopperLocation you have to call clean().
    • clean

      public void clean()
      Removes the on-disc routing files. Call only after calling close or before importOrLoad or load
    • ensureNotLoaded

      protected void ensureNotLoaded()
    • ensureWriteAccess

      protected void ensureWriteAccess()
    • getFullyLoaded

      public boolean getFullyLoaded()
    • getRouterConfig

      public RouterConfig getRouterConfig()
    • getReaderConfig

      public OSMReaderConfig getReaderConfig()
    • resolveCustomAreas

      public static JsonFeatureCollection resolveCustomAreas(String customAreasDirectory)
    • resolveCustomModelFiles

      public static List<Profile> resolveCustomModelFiles(String customModelFolder, List<Profile> profiles, JsonFeatureCollection globalAreas)