GraphHopper 简介

GraphHopper 是一个快速且高效使用内存的 Java 路由引擎,基于 Apache License 2.0 发布。默认情况下使用 OpenStreetMap 和 GTFS 数据,但它可以导入其他数据源。

准备地图数据

  • Geofabrik 下载页面 点我
  • china-latest.osm.pbf 地址 点我

准备 GraphHopper

  • GraphHopper Github 点我
  • graphhopper-web-0.13.0.jar 下载地址 点我

GraphHopper 配置文件

文件名称命名为 config-example.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
graphhopper:

# OpenStreetMap input file
# datareader.file: some.pbf

##### Vehicles #####


# More options: foot,bike,bike2,mtb,racingbike,motorcycle (comma separated)
# bike2 takes elevation data into account (like up-hill is slower than down-hill) and requires enabling graph.elevation.provider below.
# 导航方式(谨慎选择,加载一个都需要好长时间)
graph.flag_encoders: car,foot,bike
graph.bytes_for_flags: 64

# Enable turn restrictions for car or motorcycle.
# graph.flag_encoders: car|turn_costs=true

# Add additional information to every edge. Used for path details.
# If road_environment is added and elevation is enabled then also a tunnel and bridge interpolation is done, see #798.
# More options are: surface,max_width,max_height,max_weight,max_axle_load,max_length,toll,track_type
graph.encoded_values: road_class,road_class_link,road_environment,max_speed,road_access

##### Elevation #####


# To populate your graph with elevation data use SRTM, default is noop (no elevation)
# graph.elevation.provider: srtm


# default location for cache is /tmp/srtm
# graph.elevation.cache_dir: ./srtmprovider/


# If you have a slow disk or plenty of RAM change the default MMAP to:
# graph.elevation.dataaccess: RAM_STORE



#### Speed, hybrid and flexible mode ####


# By default the speed mode with the 'fastest' weighting is used. Internally a graph preparation via
# contraction hierarchies (CH) is done to speed routing up. This requires more RAM/disc space for holding the
# graph but less for every request. You can also setup multiple weightings, by providing a comma separated list.
# To enable finite u-turn costs use something like fastest|u_turn_costs=30, where 30 are the u-turn costs in seconds
# (given as integer). Note that since the u-turn costs are given in seconds the weighting you use should also
# calculate the weight in seconds. The u-turn costs will only be applied for edge_based, see below.
prepare.ch.weightings: fastest

# To enable turn-costs in speed mode (contraction hierarchies) edge-based graph traversal and a more elaborate
# pre-processing is required. Using this option you can either turn off the edge-based pre-processing (choose 'off'),
# use edge-based pre-processing for all encoders/vehicles with turn_costs=true (choose 'edge_or_node') or use node-based
# pre-processing for all encoders/vehicles and additional edge-based pre-processing for all encoders/vehicles with
# turn_costs=true (choose 'edge_and_node').
prepare.ch.edge_based: off


# Disable the speed mode. Should be used only with routing.max_visited_nodes or when the hybrid mode is enabled instead
# prepare.ch.weightings: no


# To make CH preparation faster for multiple flagEncoders you can increase the default threads if you have enough RAM.
# Change this setting only if you know what you are doing and if the default worked for you.
# prepare.ch.threads: 1


# The hybrid mode can be enabled with
# prepare.lm.weightings: fastest

# To tune the performance vs. memory usage for the hybrid mode use
# prepare.lm.landmarks: 16

# Make landmark preparation parallel if you have enough RAM. Change this only if you know what you are doing and if the default worked for you.
# prepare.lm.threads: 1


# avoid being stuck in a (oneway) subnetwork, see https://discuss.graphhopper.com/t/93
prepare.min_network_size: 200
prepare.min_one_way_network_size: 200



##### Routing #####


# You can define the maximum visited nodes when routing. This may result in not found connections if there is no
# connection between two points within the given visited nodes. The default is Integer.MAX_VALUE. Useful for flexibility mode
# routing.max_visited_nodes: 1000000


# If enabled, allows a user to run flexibility requests even if speed mode is enabled. Every request then has to include a hint ch.disable=true.
# Attention, non-CH route calculations take way more time and resources, compared to CH routing.
# A possible attacker might exploit this to slow down your service. Only enable it if you need it and with routing.maxVisitedNodes
# routing.ch.disabling_allowed: true


# If enabled, allows a user to run flexible mode requests even if the hybrid mode is enabled. Every such request then has to include a hint routing.lm.disable=true.
# routing.lm.disabling_allowed: true

# Control how many active landmarks are picked per default, this can improve query performance
# routing.lm.active_landmarks: 4


# You can limit the max distance between two consecutive waypoints of flexible routing requests to be less or equal
# the given distance in meter. Default is set to 1000km.
routing.non_ch.max_waypoint_distance: 1000000


##### Storage #####


# configure the memory access, use RAM_STORE for well equipped servers (default and recommended)
graph.dataaccess: RAM_STORE


# will write way names in the preferred language (language code as defined in ISO 639-1 or ISO 639-2):
# datareader.preferred_language: en


# Sort the graph after import to make requests roughly ~10% faster. Note that this requires significantly more RAM on import.
# graph.do_sort: true



##### Spatial Rules #####
# Spatial Rules require some configuration and only work with the DataFlagEncoder.


# Spatial Rules require you to provide Polygons in which the rules are enforced
# The line below contains the default location for these rules
# spatial_rules.location: core/files/spatialrules/countries.geo.json

# You can define the maximum BBox for which spatial rules are loaded.
# You might want to do this if you are only importing a small area and don't need rules for other countries.
# Having less rules, might result in a smaller graph. The line below contains the world-wide bounding box, uncomment and adapt to your need.
# spatial_rules.max_bbox: -180,180,-90,90


# Uncomment the following to point /maps to the source directory in the filesystem instead of
# the Java resource path. Helpful for development of the web client.
# Assumes that the web module is the working directory.
#
# assets:
# overrides:
# /maps: web/src/main/resources/assets/

# Dropwizard server configuration
server:
applicationConnectors:
- type: http
port: 8989
# for security reasons bind to localhost
bindHost: localhost
requestLog:
appenders: []
adminConnectors:
- type: http
port: 8990
bindHost: localhost
# See https://www.dropwizard.io/1.3.8/docs/manual/configuration.html#logging
logging:
appenders:
- type: file
timeZone: UTC+8
currentLogFilename: logs/graphhopper.log
logFormat: "%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
archive: true
archivedLogFilenamePattern: ./logs/graphhopper-%d.log.gz
archivedFileCount: 30
neverBlock: true
- type: console
timeZone: UTC+8
logFormat: "%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"

启动 graphhopper

Xmx -Xms 参数按需设置即可

  • -Xms 为 jvm 启动时分配的内存,比如-Xms200m,表示分配 200M;
  • -Xmx 为 jvm 运行过程中分配的最大内存,比如 -Xms500m,表示 jvm 进程最多只能够占用 500M 内存。
1
java -Xmx4g -Xms4g -Dgraphhopper.datareader.file=china-latest.osm.pbf -jar graphhopper-web-0.13.0.jar server config-example.yml

访问 graphhopper

1
http://localhost:8989

The END!