1
+ /*
2
+ * Licensed to GraphHopper GmbH under one or more contributor
3
+ * license agreements. See the NOTICE file distributed with this work for
4
+ * additional information regarding copyright ownership.
5
+ *
6
+ * GraphHopper GmbH licenses this file to you under the Apache License,
7
+ * Version 2.0 (the "License"); you may not use this file except in
8
+ * compliance with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ package com .graphhopper .tools ;
20
+
21
+ import com .graphhopper .GHRequest ;
22
+ import com .graphhopper .GHResponse ;
23
+ import com .graphhopper .GraphHopper ;
24
+ import com .graphhopper .GraphHopperConfig ;
25
+ import com .graphhopper .config .CHProfile ;
26
+ import com .graphhopper .config .Profile ;
27
+ import com .graphhopper .routing .ch .CHParameters ;
28
+ import com .graphhopper .routing .util .countryrules .CountryRuleFactory ;
29
+ import com .graphhopper .util .MiniPerfTest ;
30
+ import com .graphhopper .util .PMap ;
31
+ import com .graphhopper .util .exceptions .ConnectionNotFoundException ;
32
+ import com .graphhopper .util .exceptions .PointNotFoundException ;
33
+ import com .graphhopper .util .shapes .BBox ;
34
+ import com .graphhopper .util .shapes .GHPoint ;
35
+
36
+ import java .util .Arrays ;
37
+ import java .util .Collections ;
38
+ import java .util .Random ;
39
+ import java .util .concurrent .atomic .AtomicInteger ;
40
+
41
+ public class CHImportTest {
42
+ public static void main (String [] args ) {
43
+ System .out .println ("running for args: " + Arrays .toString (args ));
44
+ PMap map = PMap .read (args );
45
+ String vehicle = map .getString ("vehicle" , "car" );
46
+ GraphHopperConfig config = new GraphHopperConfig (map );
47
+ config .putObject ("datareader.file" , map .getString ("pbf" , "map-matching/files/leipzig_germany.osm.pbf" ));
48
+ config .putObject ("graph.location" , map .getString ("gh" , "ch-import-test-gh" ));
49
+ config .setProfiles (Arrays .asList (
50
+ new Profile (vehicle ).setVehicle (vehicle ).setWeighting ("fastest" )
51
+ ));
52
+ config .setCHProfiles (Collections .singletonList (new CHProfile (vehicle )));
53
+ config .putObject (CHParameters .PERIODIC_UPDATES , map .getInt ("periodic" , 0 ));
54
+ config .putObject (CHParameters .LAST_LAZY_NODES_UPDATES , map .getInt ("lazy" , 100 ));
55
+ config .putObject (CHParameters .NEIGHBOR_UPDATES , map .getInt ("neighbor" , 100 ));
56
+ config .putObject (CHParameters .NEIGHBOR_UPDATES_MAX , map .getInt ("neighbor_max" , 2 ));
57
+ config .putObject (CHParameters .CONTRACTED_NODES , map .getInt ("contracted" , 100 ));
58
+ config .putObject (CHParameters .LOG_MESSAGES , map .getInt ("logs" , 20 ));
59
+ config .putObject (CHParameters .EDGE_DIFFERENCE_WEIGHT , map .getDouble ("edge_diff" , 10 ));
60
+ config .putObject (CHParameters .ORIGINAL_EDGE_COUNT_WEIGHT , map .getDouble ("orig_edge" , 1 ));
61
+ config .putObject (CHParameters .MAX_POLL_FACTOR_HEURISTIC_NODE , map .getDouble ("mpf_heur" , 5 ));
62
+ config .putObject (CHParameters .MAX_POLL_FACTOR_CONTRACTION_NODE , map .getDouble ("mpf_contr" , 200 ));
63
+ GraphHopper hopper = new GraphHopper ();
64
+ hopper .init (config );
65
+ if (map .getBool ("use_country_rules" , false ))
66
+ // note that using this requires a new import of the base graph!
67
+ hopper .setCountryRuleFactory (new CountryRuleFactory ());
68
+ hopper .importOrLoad ();
69
+ runQueries (hopper , vehicle );
70
+ }
71
+
72
+ private static void runQueries (GraphHopper hopper , String profile ) {
73
+ // Bavaria, but trying to avoid regions that are not covered
74
+ BBox bounds = new BBox (10.508422 , 12.326602 , 47.713457 , 49.940615 );
75
+ int numQueries = 10_000 ;
76
+ long seed = 123 ;
77
+ Random rnd = new Random (seed );
78
+ AtomicInteger notFoundCount = new AtomicInteger ();
79
+ MiniPerfTest test = new MiniPerfTest ().setIterations (numQueries ).start ((warmup , run ) -> {
80
+ GHPoint from = getRandomPoint (rnd , bounds );
81
+ GHPoint to = getRandomPoint (rnd , bounds );
82
+ GHRequest req = new GHRequest (from , to ).setProfile (profile );
83
+ GHResponse rsp = hopper .route (req );
84
+ if (rsp .hasErrors ()) {
85
+ if (rsp .getErrors ().stream ().anyMatch (t -> !(t instanceof PointNotFoundException || t instanceof ConnectionNotFoundException )))
86
+ throw new IllegalStateException ("Unexpected error: " + rsp .getErrors ().toString ());
87
+ notFoundCount .incrementAndGet ();
88
+ return 0 ;
89
+ } else {
90
+ return (int ) rsp .getBest ().getRouteWeight ();
91
+ }
92
+ });
93
+ System .out .println ("Total queries: " + numQueries + ", Failed queries: " + notFoundCount .get ());
94
+ System .out .println (test .getReport ());
95
+ }
96
+
97
+ private static GHPoint getRandomPoint (Random rnd , BBox bounds ) {
98
+ double lat = bounds .minLat + rnd .nextDouble () * (bounds .maxLat - bounds .minLat );
99
+ double lon = bounds .minLon + rnd .nextDouble () * (bounds .maxLon - bounds .minLon );
100
+ return new GHPoint (lat , lon );
101
+ }
102
+ }
0 commit comments