-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
GraphHopper.java
1266 lines (1078 loc) · 50 KB
/
GraphHopper.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper;
import com.graphhopper.json.geo.JsonFeature;
import com.graphhopper.reader.DataReader;
import com.graphhopper.reader.dem.*;
import com.graphhopper.routing.*;
import com.graphhopper.routing.ch.CHAlgoFactoryDecorator;
import com.graphhopper.routing.ch.PrepareContractionHierarchies;
import com.graphhopper.routing.lm.LMAlgoFactoryDecorator;
import com.graphhopper.routing.profiles.DefaultEncodedValueFactory;
import com.graphhopper.routing.profiles.EncodedValueFactory;
import com.graphhopper.routing.profiles.EnumEncodedValue;
import com.graphhopper.routing.profiles.RoadEnvironment;
import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks;
import com.graphhopper.routing.template.AlternativeRoutingTemplate;
import com.graphhopper.routing.template.RoundTripRoutingTemplate;
import com.graphhopper.routing.template.RoutingTemplate;
import com.graphhopper.routing.template.ViaRoutingTemplate;
import com.graphhopper.routing.util.*;
import com.graphhopper.routing.util.parsers.DefaultTagParserFactory;
import com.graphhopper.routing.util.parsers.TagParserFactory;
import com.graphhopper.routing.weighting.*;
import com.graphhopper.storage.*;
import com.graphhopper.storage.change.ChangeGraphHelper;
import com.graphhopper.storage.change.ChangeGraphResponse;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.LocationIndexTree;
import com.graphhopper.storage.index.QueryResult;
import com.graphhopper.util.*;
import com.graphhopper.util.Parameters.CH;
import com.graphhopper.util.Parameters.Landmark;
import com.graphhopper.util.Parameters.Routing;
import com.graphhopper.util.details.PathDetailsBuilderFactory;
import com.graphhopper.util.exceptions.PointDistanceExceededException;
import com.graphhopper.util.exceptions.PointOutOfBoundsException;
import com.graphhopper.util.shapes.BBox;
import com.graphhopper.util.shapes.GHPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static com.graphhopper.routing.ch.CHAlgoFactoryDecorator.EdgeBasedCHMode.EDGE_OR_NODE;
import static com.graphhopper.routing.ch.CHAlgoFactoryDecorator.EdgeBasedCHMode.OFF;
import static com.graphhopper.util.Helper.*;
import static com.graphhopper.util.Parameters.Algorithms.*;
/**
* Easy to use access point to configure import and (offline) routing.
*
* @author Peter Karich
* @see GraphHopperAPI
*/
public class GraphHopper implements GraphHopperAPI {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final String fileLockName = "gh.lock";
private final Set<RoutingAlgorithmFactoryDecorator> algoDecorators = new LinkedHashSet<>();
// utils
private final TranslationMap trMap = new TranslationMap().doImport();
boolean removeZipped = true;
// for graph:
private GraphHopperStorage ghStorage;
private EncodingManager encodingManager;
private int defaultSegmentSize = -1;
private String ghLocation = "";
private DAType dataAccessType = DAType.RAM_STORE;
private boolean sortGraph = false;
private boolean elevation = false;
private LockFactory lockFactory = new NativeFSLockFactory();
private boolean allowWrites = true;
private boolean fullyLoaded = false;
private boolean smoothElevation = false;
// for routing
private int maxRoundTripRetries = 3;
private boolean simplifyResponse = true;
private TraversalMode traversalMode = TraversalMode.NODE_BASED;
private int maxVisitedNodes = Integer.MAX_VALUE;
private int nonChMaxWaypointDistance = Integer.MAX_VALUE;
// for index
private LocationIndex locationIndex;
private int preciseIndexResolution = 300;
private int maxRegionSearch = 4;
// for prepare
private int minNetworkSize = 200;
private int minOneWayNetworkSize = 0;
// for LM prepare
private final LMAlgoFactoryDecorator lmFactoryDecorator = new LMAlgoFactoryDecorator();
// for CH prepare
private final CHAlgoFactoryDecorator chFactoryDecorator = new CHAlgoFactoryDecorator();
// for data reader
private String dataReaderFile;
private double dataReaderWayPointMaxDistance = 1;
private int dataReaderWorkerThreads = 2;
private boolean calcPoints = true;
private ElevationProvider eleProvider = ElevationProvider.NOOP;
private FlagEncoderFactory flagEncoderFactory = new DefaultFlagEncoderFactory();
private EncodedValueFactory encodedValueFactory = new DefaultEncodedValueFactory();
private TagParserFactory tagParserFactory = new DefaultTagParserFactory();
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private PathDetailsBuilderFactory pathBuilderFactory = new PathDetailsBuilderFactory();
public GraphHopper() {
chFactoryDecorator.setEnabled(true);
lmFactoryDecorator.setEnabled(false);
// order is important to use CH as base algo and set the approximation in the followed lm factory decorator
algoDecorators.add(chFactoryDecorator);
algoDecorators.add(lmFactoryDecorator);
}
/**
* For testing only
*/
protected GraphHopper loadGraph(GraphHopperStorage g) {
this.ghStorage = g;
fullyLoaded = true;
initLocationIndex();
return this;
}
/**
* @return the first flag encoder of the encoding manager
*/
FlagEncoder getDefaultVehicle() {
if (encodingManager == null)
throw new IllegalStateException("No encoding manager specified or loaded");
return encodingManager.fetchEdgeEncoders().get(0);
}
public EncodingManager getEncodingManager() {
return encodingManager;
}
/**
* Specify which vehicles can be read by this GraphHopper instance. An encoding manager defines
* how data from every vehicle is written (und read) into edges of the graph.
*/
public GraphHopper setEncodingManager(EncodingManager em) {
ensureNotLoaded();
this.encodingManager = em;
if (em.needsTurnCostsSupport())
traversalMode = TraversalMode.EDGE_BASED;
return this;
}
public ElevationProvider getElevationProvider() {
return eleProvider;
}
public GraphHopper setElevationProvider(ElevationProvider eleProvider) {
if (eleProvider == null || eleProvider == ElevationProvider.NOOP)
setElevation(false);
else
setElevation(true);
this.eleProvider = eleProvider;
return this;
}
/**
* Threads for data reading.
*/
protected int getWorkerThreads() {
return dataReaderWorkerThreads;
}
/**
* Return maximum distance (in meter) to reduce points via douglas peucker while OSM import.
*/
protected double getWayPointMaxDistance() {
return dataReaderWayPointMaxDistance;
}
/**
* This parameter specifies how to reduce points via douglas peucker while OSM import. Higher
* value means more details, unit is meter. Default is 1. Disable via 0.
*/
public GraphHopper setWayPointMaxDistance(double wayPointMaxDistance) {
this.dataReaderWayPointMaxDistance = wayPointMaxDistance;
return this;
}
public TraversalMode getTraversalMode() {
return traversalMode;
}
/**
* Sets the default traversal mode used for the algorithms and preparation.
*/
public GraphHopper setTraversalMode(TraversalMode traversalMode) {
this.traversalMode = traversalMode;
return this;
}
public GraphHopper setPathDetailsBuilderFactory(PathDetailsBuilderFactory pathBuilderFactory) {
this.pathBuilderFactory = pathBuilderFactory;
return this;
}
public PathDetailsBuilderFactory getPathDetailsBuilderFactory() {
return pathBuilderFactory;
}
/**
* Configures the underlying storage and response to be used on a well equipped server. Result
* also optimized for usage in the web module i.e. try reduce network IO.
*/
public GraphHopper forServer() {
setSimplifyResponse(true);
return setInMemory();
}
/**
* Configures the underlying storage to be used on a Desktop computer or within another Java
* application with enough RAM but no network latency.
*/
public GraphHopper forDesktop() {
setSimplifyResponse(false);
return setInMemory();
}
/**
* Configures the underlying storage to be used on a less powerful machine like Android or
* Raspberry Pi with only few MB of RAM.
*/
public GraphHopper forMobile() {
setSimplifyResponse(false);
return setMemoryMapped();
}
/**
* 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).
*/
public GraphHopper setPreciseIndexResolution(int precision) {
ensureNotLoaded();
preciseIndexResolution = precision;
return this;
}
public GraphHopper setMinNetworkSize(int minNetworkSize, int minOneWayNetworkSize) {
this.minNetworkSize = minNetworkSize;
this.minOneWayNetworkSize = minOneWayNetworkSize;
return this;
}
/**
* This method call results in an in-memory graph.
*/
public GraphHopper setInMemory() {
ensureNotLoaded();
dataAccessType = DAType.RAM_STORE;
return this;
}
/**
* 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.
*
* @param storeOnFlush true by default
*/
public GraphHopper setStoreOnFlush(boolean storeOnFlush) {
ensureNotLoaded();
if (storeOnFlush)
dataAccessType = DAType.RAM_STORE;
else
dataAccessType = DAType.RAM;
return this;
}
/**
* Enable memory mapped configuration if not enough memory is available on the target platform.
*/
public GraphHopper setMemoryMapped() {
ensureNotLoaded();
dataAccessType = DAType.MMAP;
return this;
}
/**
* Not yet stable enough to offer it for everyone
*/
private GraphHopper setUnsafeMemory() {
ensureNotLoaded();
dataAccessType = DAType.UNSAFE_STORE;
return this;
}
/**
* This method enabled or disables the speed mode (Contraction Hierarchies)
*
* @deprecated use {@link #setCHEnabled(boolean)} instead
*/
public GraphHopper setCHEnable(boolean enable) {
return setCHEnabled(enable);
}
public final boolean isCHEnabled() {
return chFactoryDecorator.isEnabled();
}
/**
* Enables or disables contraction hierarchies (CH). This speed-up mode is enabled by default.
*/
public GraphHopper setCHEnabled(boolean enable) {
ensureNotLoaded();
chFactoryDecorator.setEnabled(enable);
return this;
}
public int getMaxVisitedNodes() {
return maxVisitedNodes;
}
/**
* This methods stops the algorithm from searching further if the resulting path would go over
* the specified node count, important if none-CH routing is used.
*/
public void setMaxVisitedNodes(int maxVisitedNodes) {
this.maxVisitedNodes = maxVisitedNodes;
}
/**
* @return true if storing and fetching elevation data is enabled. Default is false
*/
public boolean hasElevation() {
return elevation;
}
/**
* Enable storing and fetching elevation data. Default is false
*/
public GraphHopper setElevation(boolean includeElevation) {
this.elevation = includeElevation;
return this;
}
/**
* This methods enables gps point calculation. If disabled only distance will be calculated.
*/
public GraphHopper setEnableCalcPoints(boolean b) {
calcPoints = b;
return this;
}
/**
* This method specifies if the returned path should be simplified or not, via douglas-peucker
* or similar algorithm.
*/
private GraphHopper setSimplifyResponse(boolean doSimplify) {
this.simplifyResponse = doSimplify;
return this;
}
public String getGraphHopperLocation() {
return ghLocation;
}
/**
* Sets the graphhopper folder.
*/
public GraphHopper setGraphHopperLocation(String ghLocation) {
ensureNotLoaded();
if (ghLocation == null)
throw new IllegalArgumentException("graphhopper location cannot be null");
this.ghLocation = ghLocation;
return this;
}
public String getDataReaderFile() {
return dataReaderFile;
}
/**
* This file can be any file type supported by the DataReader. E.g. for the OSMReader it is the
* OSM xml (.osm), a compressed xml (.osm.zip or .osm.gz) or a protobuf file (.pbf)
*/
public GraphHopper setDataReaderFile(String dataReaderFileStr) {
ensureNotLoaded();
if (isEmpty(dataReaderFileStr))
throw new IllegalArgumentException("Data reader file cannot be empty.");
dataReaderFile = dataReaderFileStr;
return this;
}
/**
* The underlying graph used in algorithms.
*
* @throws IllegalStateException if graph is not instantiated.
*/
public GraphHopperStorage getGraphHopperStorage() {
if (ghStorage == null)
throw new IllegalStateException("GraphHopper storage not initialized");
return ghStorage;
}
public void setGraphHopperStorage(GraphHopperStorage ghStorage) {
this.ghStorage = ghStorage;
fullyLoaded = true;
}
/**
* The location index created from the graph.
*
* @throws IllegalStateException if index is not initialized
*/
public LocationIndex getLocationIndex() {
if (locationIndex == null)
throw new IllegalStateException("Location index not initialized");
return locationIndex;
}
protected void setLocationIndex(LocationIndex locationIndex) {
this.locationIndex = locationIndex;
}
/**
* Sorts the graph which requires more RAM while import. See #12
*/
public GraphHopper setSortGraph(boolean sortGraph) {
ensureNotLoaded();
this.sortGraph = sortGraph;
return this;
}
public boolean isAllowWrites() {
return 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.
*/
public GraphHopper setAllowWrites(boolean allowWrites) {
this.allowWrites = allowWrites;
return this;
}
public TranslationMap getTranslationMap() {
return trMap;
}
public GraphHopper setFlagEncoderFactory(FlagEncoderFactory factory) {
this.flagEncoderFactory = factory;
return this;
}
public EncodedValueFactory getEncodedValueFactory() {
return this.encodedValueFactory;
}
public GraphHopper setEncodedValueFactory(EncodedValueFactory factory) {
this.encodedValueFactory = factory;
return this;
}
public TagParserFactory getTagParserFactory() {
return this.tagParserFactory;
}
public GraphHopper setTagParserFactory(TagParserFactory factory) {
this.tagParserFactory = factory;
return this;
}
/**
* Reads the configuration from a CmdArgs object which can be manually filled, or via
* CmdArgs.read(String[] args)
*/
public GraphHopper init(CmdArgs args) {
args.merge(CmdArgs.readFromSystemProperties());
if (args.has("osmreader.osm"))
throw new IllegalArgumentException("Instead osmreader.osm use datareader.file, for other changes see core/files/changelog.txt");
String tmpOsmFile = args.get("datareader.file", "");
if (!isEmpty(tmpOsmFile))
dataReaderFile = tmpOsmFile;
String graphHopperFolder = args.get("graph.location", "");
if (isEmpty(graphHopperFolder) && isEmpty(ghLocation)) {
if (isEmpty(dataReaderFile))
throw new IllegalArgumentException("If no graph.location is provided you need to specify an OSM file.");
graphHopperFolder = pruneFileEnd(dataReaderFile) + "-gh";
}
// graph
setGraphHopperLocation(graphHopperFolder);
defaultSegmentSize = args.getInt("graph.dataaccess.segment_size", defaultSegmentSize);
String graphDATypeStr = args.get("graph.dataaccess", "RAM_STORE");
dataAccessType = DAType.fromString(graphDATypeStr);
sortGraph = args.getBool("graph.do_sort", sortGraph);
removeZipped = args.getBool("graph.remove_zipped", removeZipped);
int bytesForFlags = args.getInt("graph.bytes_for_flags", 4);
EncodingManager.Builder emBuilder = new EncodingManager.Builder(bytesForFlags);
String flagEncodersStr = args.get("graph.flag_encoders", "");
String encodedValueStr = args.get("graph.encoded_values", "");
if (!flagEncodersStr.isEmpty() || !encodedValueStr.isEmpty()) {
if (!encodedValueStr.isEmpty())
emBuilder.addAll(tagParserFactory, encodedValueStr);
if (!flagEncodersStr.isEmpty())
emBuilder.addAll(flagEncoderFactory, flagEncodersStr);
emBuilder.setEnableInstructions(args.getBool("datareader.instructions", true));
emBuilder.setPreferredLanguage(args.get("datareader.preferred_language", ""));
// overwrite EncodingManager object from configuration file
setEncodingManager(emBuilder.build());
}
if (args.get("graph.locktype", "native").equals("simple"))
lockFactory = new SimpleFSLockFactory();
else
lockFactory = new NativeFSLockFactory();
// elevation
String eleProviderStr = toLowerCase(args.get("graph.elevation.provider", "noop"));
this.smoothElevation = args.getBool("graph.elevation.smoothing", false);
// keep fallback until 0.8
boolean eleCalcMean = args.has("graph.elevation.calcmean")
? args.getBool("graph.elevation.calcmean", false)
: args.getBool("graph.elevation.calc_mean", false);
String cacheDirStr = args.get("graph.elevation.cache_dir", "");
if (cacheDirStr.isEmpty())
cacheDirStr = args.get("graph.elevation.cachedir", "");
String baseURL = args.get("graph.elevation.base_url", "");
if (baseURL.isEmpty())
args.get("graph.elevation.baseurl", "");
boolean removeTempElevationFiles = args.getBool("graph.elevation.cgiar.clear", true);
removeTempElevationFiles = args.getBool("graph.elevation.clear", removeTempElevationFiles);
DAType elevationDAType = DAType.fromString(args.get("graph.elevation.dataaccess", "MMAP"));
ElevationProvider tmpProvider = ElevationProvider.NOOP;
if (eleProviderStr.equalsIgnoreCase("srtm")) {
tmpProvider = new SRTMProvider(cacheDirStr);
} else if (eleProviderStr.equalsIgnoreCase("cgiar")) {
tmpProvider = new CGIARProvider(cacheDirStr);
} else if (eleProviderStr.equalsIgnoreCase("gmted")) {
tmpProvider = new GMTEDProvider(cacheDirStr);
} else if (eleProviderStr.equalsIgnoreCase("srtmgl1")) {
tmpProvider = new SRTMGL1Provider(cacheDirStr);
} else if (eleProviderStr.equalsIgnoreCase("multi")) {
tmpProvider = new MultiSourceElevationProvider(cacheDirStr);
}
tmpProvider.setAutoRemoveTemporaryFiles(removeTempElevationFiles);
tmpProvider.setCalcMean(eleCalcMean);
if (!baseURL.isEmpty())
tmpProvider.setBaseURL(baseURL);
tmpProvider.setDAType(elevationDAType);
setElevationProvider(tmpProvider);
// optimizable prepare
minNetworkSize = args.getInt("prepare.min_network_size", minNetworkSize);
minOneWayNetworkSize = args.getInt("prepare.min_one_way_network_size", minOneWayNetworkSize);
// prepare CH, LM, ...
for (RoutingAlgorithmFactoryDecorator decorator : algoDecorators) {
decorator.init(args);
}
// osm import
dataReaderWayPointMaxDistance = args.getDouble(Routing.INIT_WAY_POINT_MAX_DISTANCE, dataReaderWayPointMaxDistance);
dataReaderWorkerThreads = args.getInt("datareader.worker_threads", dataReaderWorkerThreads);
// index
preciseIndexResolution = args.getInt("index.high_resolution", preciseIndexResolution);
maxRegionSearch = args.getInt("index.max_region_search", maxRegionSearch);
// routing
maxVisitedNodes = args.getInt(Routing.INIT_MAX_VISITED_NODES, Integer.MAX_VALUE);
maxRoundTripRetries = args.getInt(RoundTrip.INIT_MAX_RETRIES, maxRoundTripRetries);
nonChMaxWaypointDistance = args.getInt(Parameters.NON_CH.MAX_NON_CH_POINT_DISTANCE, Integer.MAX_VALUE);
return this;
}
private void printInfo() {
logger.info("version " + Constants.VERSION + "|" + Constants.BUILD_DATE + " (" + Constants.getVersions() + ")");
if (ghStorage != null)
logger.info("graph " + ghStorage.toString() + ", details:" + ghStorage.toDetailsString());
}
/**
* 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.
*/
public GraphHopper importOrLoad() {
if (!load(ghLocation)) {
printInfo();
process(ghLocation);
} else {
printInfo();
}
return this;
}
/**
* Creates the graph from OSM data.
*/
private GraphHopper process(String graphHopperLocation) {
setGraphHopperLocation(graphHopperLocation);
GHLock lock = null;
try {
if (ghStorage.getDirectory().getDefaultType().isStoring()) {
lockFactory.setLockDir(new File(graphHopperLocation));
lock = lockFactory.create(fileLockName, true);
if (!lock.tryLock())
throw new RuntimeException("To avoid multiple writers we need to obtain a write lock but it failed. In " + graphHopperLocation, lock.getObtainFailedReason());
}
try {
DataReader reader = importData();
DateFormat f = createFormatter();
ghStorage.getProperties().put("datareader.import.date", f.format(new Date()));
if (reader.getDataDate() != null)
ghStorage.getProperties().put("datareader.data.date", f.format(reader.getDataDate()));
} catch (IOException ex) {
throw new RuntimeException("Cannot read file " + getDataReaderFile(), ex);
}
cleanUp();
postProcessing();
flush();
} finally {
if (lock != null)
lock.release();
}
return this;
}
protected DataReader importData() throws IOException {
ensureWriteAccess();
if (ghStorage == null)
throw new IllegalStateException("Load graph before importing OSM data");
if (dataReaderFile == null)
throw new IllegalStateException("Couldn't load from existing folder: " + ghLocation
+ " but also cannot use file for DataReader as it wasn't specified!");
DataReader reader = createReader(ghStorage);
logger.info("using " + ghStorage.toString() + ", memory:" + getMemInfo());
reader.readGraph();
return reader;
}
protected DataReader createReader(GraphHopperStorage ghStorage) {
throw new UnsupportedOperationException("Cannot create DataReader. Solutions: avoid import via calling load directly, "
+ "provide a DataReader or use e.g. GraphHopperOSM or a different subclass");
}
protected DataReader initDataReader(DataReader reader) {
if (dataReaderFile == null)
throw new IllegalArgumentException("No file for DataReader specified");
logger.info("start creating graph from " + dataReaderFile);
return reader.setFile(new File(dataReaderFile)).
setElevationProvider(eleProvider).
setWorkerThreads(dataReaderWorkerThreads).
setWayPointMaxDistance(dataReaderWayPointMaxDistance).
setSmoothElevation(this.smoothElevation);
}
/**
* Opens existing graph folder.
*
* @param graphHopperFolder is the folder containing graphhopper files. Can be a compressed file
* too ala folder-content.ghz.
*/
@Override
public boolean load(String graphHopperFolder) {
if (isEmpty(graphHopperFolder))
throw new IllegalStateException("GraphHopperLocation is not specified. Call setGraphHopperLocation or init before");
if (fullyLoaded)
throw new IllegalStateException("graph is already successfully loaded");
File tmpFileOrFolder = new File(graphHopperFolder);
if (!tmpFileOrFolder.isDirectory() && tmpFileOrFolder.exists()) {
throw new IllegalArgumentException("GraphHopperLocation cannot be an existing file. Has to be either non-existing or a folder.");
} else {
File compressed = new File(graphHopperFolder + ".ghz");
if (compressed.exists() && !compressed.isDirectory()) {
try {
new Unzipper().unzip(compressed.getAbsolutePath(), graphHopperFolder, removeZipped);
} catch (IOException ex) {
throw new RuntimeException("Couldn't extract file " + compressed.getAbsolutePath()
+ " to " + graphHopperFolder, ex);
}
}
}
setGraphHopperLocation(graphHopperFolder);
if (encodingManager == null)
setEncodingManager(EncodingManager.create(encodedValueFactory, flagEncoderFactory, ghLocation));
if (!allowWrites && dataAccessType.isMMap())
dataAccessType = DAType.MMAP_RO;
GHDirectory dir = new GHDirectory(ghLocation, dataAccessType);
GraphExtension ext = encodingManager.needsTurnCostsSupport()
? new TurnCostExtension() : new GraphExtension.NoOpExtension();
if (lmFactoryDecorator.isEnabled())
initLMAlgoFactoryDecorator();
if (chFactoryDecorator.isEnabled()) {
initCHAlgoFactoryDecorator();
ghStorage = new GraphHopperStorage(chFactoryDecorator.getNodeBasedWeightings(), chFactoryDecorator.getEdgeBasedWeightings(),
dir, encodingManager, hasElevation(), ext);
} else {
ghStorage = new GraphHopperStorage(dir, encodingManager, hasElevation(), ext);
}
ghStorage.setSegmentSize(defaultSegmentSize);
if (!new File(graphHopperFolder).exists())
return false;
GHLock lock = null;
try {
// create locks only if writes are allowed, if they are not allowed a lock cannot be created
// (e.g. on a read only filesystem locks would fail)
if (ghStorage.getDirectory().getDefaultType().isStoring() && isAllowWrites()) {
lockFactory.setLockDir(new File(ghLocation));
lock = lockFactory.create(fileLockName, false);
if (!lock.tryLock())
throw new RuntimeException("To avoid reading partial data we need to obtain the read lock but it failed. In " + ghLocation, lock.getObtainFailedReason());
}
if (!ghStorage.loadExisting())
return false;
postProcessing();
fullyLoaded = true;
return true;
} finally {
if (lock != null)
lock.release();
}
}
public RoutingAlgorithmFactory getAlgorithmFactory(HintsMap map) {
RoutingAlgorithmFactory routingAlgorithmFactory = new RoutingAlgorithmFactorySimple();
for (RoutingAlgorithmFactoryDecorator decorator : algoDecorators) {
if (decorator.isEnabled())
routingAlgorithmFactory = decorator.getDecoratedAlgorithmFactory(routingAlgorithmFactory, map);
}
return routingAlgorithmFactory;
}
public GraphHopper addAlgorithmFactoryDecorator(RoutingAlgorithmFactoryDecorator algoFactoryDecorator) {
if (!algoDecorators.add(algoFactoryDecorator))
throw new IllegalArgumentException("Decorator was already added " + algoFactoryDecorator.getClass());
return this;
}
public final CHAlgoFactoryDecorator getCHFactoryDecorator() {
return chFactoryDecorator;
}
private void initCHAlgoFactoryDecorator() {
if (!chFactoryDecorator.hasWeightings()) {
for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
for (String chWeightingStr : chFactoryDecorator.getWeightingsAsStrings()) {
// ghStorage is null at this point
CHAlgoFactoryDecorator.EdgeBasedCHMode edgeBasedCHMode = chFactoryDecorator.getEdgeBasedCHMode();
if (!(edgeBasedCHMode == EDGE_OR_NODE && encoder.supports(TurnWeighting.class))) {
chFactoryDecorator.addNodeBasedWeighting(createWeighting(new HintsMap(chWeightingStr), encoder, null));
}
if (edgeBasedCHMode != OFF && encoder.supports(TurnWeighting.class)) {
chFactoryDecorator.addEdgeBasedWeighting(createWeighting(new HintsMap(chWeightingStr), encoder, null));
}
}
}
}
}
public final LMAlgoFactoryDecorator getLMFactoryDecorator() {
return lmFactoryDecorator;
}
private void initLMAlgoFactoryDecorator() {
if (lmFactoryDecorator.hasWeightings())
return;
for (FlagEncoder encoder : encodingManager.fetchEdgeEncoders()) {
for (String lmWeightingStr : lmFactoryDecorator.getWeightingsAsStrings()) {
Weighting weighting = createWeighting(new HintsMap(lmWeightingStr), encoder, null);
lmFactoryDecorator.addWeighting(weighting);
}
}
}
/**
* Does the preparation and creates the location index
*/
public void postProcessing() {
// Later: move this into the GraphStorage.optimize method
// Or: Doing it after preparation to optimize shortcuts too. But not possible yet #12
if (sortGraph) {
if (ghStorage.isCHPossible() && isCHPrepared())
throw new IllegalArgumentException("Sorting a prepared CHGraph is not possible yet. See #12");
GraphHopperStorage newGraph = GHUtility.newStorage(ghStorage);
GHUtility.sortDFS(ghStorage, newGraph);
logger.info("graph sorted (" + getMemInfo() + ")");
ghStorage = newGraph;
}
if (hasElevation()) {
interpolateBridgesAndOrTunnels();
}
initLocationIndex();
if (chFactoryDecorator.isEnabled())
chFactoryDecorator.createPreparations(ghStorage);
if (!isCHPrepared())
prepareCH();
if (lmFactoryDecorator.isEnabled())
lmFactoryDecorator.createPreparations(ghStorage, locationIndex);
loadOrPrepareLM();
}
private void interpolateBridgesAndOrTunnels() {
if (ghStorage.getEncodingManager().hasEncodedValue(RoadEnvironment.KEY)) {
EnumEncodedValue<RoadEnvironment> roadEnvEnc = ghStorage.getEncodingManager().getEnumEncodedValue(RoadEnvironment.KEY, RoadEnvironment.class);
StopWatch sw = new StopWatch().start();
new EdgeElevationInterpolator(ghStorage, roadEnvEnc, RoadEnvironment.TUNNEL).execute();
float tunnel = sw.stop().getSeconds();
sw = new StopWatch().start();
new EdgeElevationInterpolator(ghStorage, roadEnvEnc, RoadEnvironment.BRIDGE).execute();
logger.info("Bridge interpolation " + (int) sw.stop().getSeconds() + "s, " + "tunnel interpolation " + (int) tunnel + "s");
}
}
/**
* Based on the hintsMap and the specified encoder a Weighting instance can be
* created. Note that all URL parameters are available in the hintsMap as String if
* you use the web module.
*
* @param hintsMap all parameters influencing the weighting. E.g. parameters coming via
* GHRequest.getHints or directly via "&api.xy=" from the URL of the web UI
* @param encoder the required vehicle
* @param graph The Graph enables the Weighting for NodeAccess and more
* @return the weighting to be used for route calculation
* @see HintsMap
*/
public Weighting createWeighting(HintsMap hintsMap, FlagEncoder encoder, Graph graph) {
String weightingStr = toLowerCase(hintsMap.getWeighting());
Weighting weighting = null;
if (encoder.supports(GenericWeighting.class)) {
weighting = new GenericWeighting((DataFlagEncoder) encoder, hintsMap);
} else if ("shortest".equalsIgnoreCase(weightingStr)) {
weighting = new ShortestWeighting(encoder);
} else if ("fastest".equalsIgnoreCase(weightingStr) || weightingStr.isEmpty()) {
if (encoder.supports(PriorityWeighting.class))
weighting = new PriorityWeighting(encoder, hintsMap);
else
weighting = new FastestWeighting(encoder, hintsMap);
} else if ("curvature".equalsIgnoreCase(weightingStr)) {
if (encoder.supports(CurvatureWeighting.class))
weighting = new CurvatureWeighting(encoder, hintsMap);
} else if ("short_fastest".equalsIgnoreCase(weightingStr)) {
weighting = new ShortFastestWeighting(encoder, hintsMap);
}
if (weighting == null)
throw new IllegalArgumentException("weighting " + weightingStr + " not supported");
if (hintsMap.has(Routing.BLOCK_AREA)) {
String blockAreaStr = hintsMap.get(Parameters.Routing.BLOCK_AREA, "");
GraphEdgeIdFinder.BlockArea blockArea = new GraphEdgeIdFinder(graph, locationIndex).
parseBlockArea(blockAreaStr, DefaultEdgeFilter.allEdges(encoder), hintsMap.getDouble("block_area.edge_id_max_area", 1000 * 1000));
return new BlockAreaWeighting(weighting, blockArea);
}
return weighting;
}
/**
* Potentially wraps the specified weighting into a TurnWeighting instance.
*/
public Weighting createTurnWeighting(Graph graph, Weighting weighting, TraversalMode tMode) {
FlagEncoder encoder = weighting.getFlagEncoder();
if (encoder.supports(TurnWeighting.class) && !tMode.equals(TraversalMode.NODE_BASED))
return new TurnWeighting(weighting, (TurnCostExtension) graph.getExtension());
return weighting;
}
@Override
public GHResponse route(GHRequest request) {
GHResponse response = new GHResponse();
calcPaths(request, response);
return response;
}
/**
* This method calculates the alternative path list using the low level Path objects.
*/
public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
if (ghStorage == null || !fullyLoaded)
throw new IllegalStateException("Do a successful call to load or importOrLoad before routing");
if (ghStorage.isClosed())
throw new IllegalStateException("You need to create a new GraphHopper instance as it is already closed");
// default handling
String vehicle = request.getVehicle();
if (vehicle.isEmpty()) {
vehicle = getDefaultVehicle().toString();
request.setVehicle(vehicle);
}
Lock readLock = readWriteLock.readLock();
readLock.lock();
try {
if (!encodingManager.hasEncoder(vehicle))
throw new IllegalArgumentException("Vehicle not supported: " + vehicle + ". Supported are: " + encodingManager.toString());
HintsMap hints = request.getHints();
String tModeStr = hints.get("traversal_mode", traversalMode.toString());
TraversalMode tMode = TraversalMode.fromString(tModeStr);
if (hints.has(Routing.EDGE_BASED))
tMode = hints.getBool(Routing.EDGE_BASED, false) ? TraversalMode.EDGE_BASED : TraversalMode.NODE_BASED;
FlagEncoder encoder = encodingManager.getEncoder(vehicle);
boolean disableCH = hints.getBool(CH.DISABLE, false);
if (!chFactoryDecorator.isDisablingAllowed() && disableCH)
throw new IllegalArgumentException("Disabling CH not allowed on the server-side");
boolean disableLM = hints.getBool(Landmark.DISABLE, false);
if (!lmFactoryDecorator.isDisablingAllowed() && disableLM)
throw new IllegalArgumentException("Disabling LM not allowed on the server-side");
String algoStr = request.getAlgorithm();
if (algoStr.isEmpty())
algoStr = chFactoryDecorator.isEnabled() && !disableCH ? DIJKSTRA_BI : ASTAR_BI;
List<GHPoint> points = request.getPoints();
// TODO Maybe we should think about a isRequestValid method that checks all that stuff that we could do to fail fast
// For example see #734
checkIfPointsAreInBounds(points);
RoutingTemplate routingTemplate;
if (ROUND_TRIP.equalsIgnoreCase(algoStr))
routingTemplate = new RoundTripRoutingTemplate(request, ghRsp, locationIndex, encodingManager, maxRoundTripRetries);
else if (ALT_ROUTE.equalsIgnoreCase(algoStr))
routingTemplate = new AlternativeRoutingTemplate(request, ghRsp, locationIndex, encodingManager);
else
routingTemplate = new ViaRoutingTemplate(request, ghRsp, locationIndex, encodingManager);