-
Notifications
You must be signed in to change notification settings - Fork 12
/
osmaprouting.pas
1692 lines (1406 loc) · 49.5 KB
/
osmaprouting.pas
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
(*
OsMap components for offline rendering and routing functionalities
based on OpenStreetMap data
Copyright (C) 2019 Sergey Bodrov
This source is ported from libosmscout library
Copyright (C) 2009 Tim Teulings
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
(*
OsMap routing
Intersection
JunctionRef -> TIntersection
routing/DBFileOffset
DBId -> TMapDBId
DBFileOffset -> TMapDBFileOffset
routing/route
Description -> TRouteItemDescription
Node -> TRouteItem
RouteDescription -> TRouteDescription
routing/RouteNode
ObjectVariantData
ObjectData -> TRouteNodeObjectData
Exclude -> TRouteNodeExclude
Path -> TRouteNodePath
RouteNode -> TRouteNode
routing/RouteData
RouteData
RouteEntry
routing/RoutingProfile
RoutingProfile -> TAbstractRoutingProfile
AbstractRoutingProfile -> TRoutingProfile
ShortestPathRoutingProfile -> TRoutingProfile
FastestPathRoutingProfile
*)
unit OsMapRouting;
{$ifdef FPC}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils,
{$ifdef FPC}
fgl,
{$endif}
OsMapTypes, OsMapGeometry, OsMapUtils, OsMapObjTypes, OsMapObjects,
OsMapObjFeatures, OsMapFiles;
const
NODE_START_DESC = 'NodeStart'; // start node (StartDescription)
NODE_TARGET_DESC = 'NodeTarget'; // target node (TargetDescription)
WAY_NAME_DESC = 'WayName'; // name of the way (NameDescription)
WAY_NAME_CHANGED_DESC = 'WayChangedName'; // a change of way name (NameChangedDescription)
CROSSING_WAYS_DESC = 'CrossingWays'; // list of way name crossing a node (CrossingWaysDescription)
DIRECTION_DESC = 'Direction'; // a turn (TurnDescription)
TURN_DESC = 'Turn'; // an explicit turn (TurnDescription)
ROUNDABOUT_ENTER_DESC = 'RountaboutEnter'; // entering a roundabout (RoundaboutEnterDescription)
ROUNDABOUT_LEAVE_DESC = 'RountaboutLeave'; // leaving a roundabout (RoundaboutLeaveDescription)
MOTORWAY_ENTER_DESC = 'MotorwayEnter'; // entering a motorway (MotorwayEnterDescription)
MOTORWAY_CHANGE_DESC = 'MotorwayChange'; // changing a motorway (MotorwayChangeDescription)
MOTORWAY_LEAVE_DESC = 'MotorwayLeave'; // leaving a motorway (MotorwayLeaveDescription)
MOTORWAY_JUNCTION_DESC = 'MotorwayJunction'; // motorway junction (MotorwayJunctionDescription)
CROSSING_DESTINATION_DESC = 'CrossingDestination'; // destination to choose at a junction
WAY_MAXSPEED_DESC = 'MaxSpeed'; // the maximum speed for the given way
WAY_TYPE_NAME_DESC = 'TypeName'; // type name of the way (TypeNameDescription)
POI_AT_ROUTE_DESC = 'POIAtRoute'; //
type
TMapDatabaseId = LongWord;
{ Helper structure to implement a reference to a routing node in a given
database (identified by a unique index). }
{ TMapDBId }
TMapDBId = object
DatabaseId: TMapDatabaseId;
Id: TId;
procedure Init(ADatabaseId: TMapDatabaseId = 0; AId: TId = 0);
function IsValid(): Boolean;
function IsEqual(const AValue: TMapDBId): Boolean;
function AsStr(): string;
end;
TMapDBIdArray = array of TMapDBId;
TMapDatabaseIdMap = specialize TFPGMap<TMapDatabaseId, string>; // DatabaseId : string
{ TMapDBFileOffset }
TMapDBFileOffset = object
DatabaseId: TMapDatabaseId;
Offset: TFileOffset;
procedure Init(ADatabaseId: TMapDatabaseId; AOffset: TFileOffset);
function AsStr(): string;
function IsEqual(const AValue: TMapDBFileOffset): Boolean;
end;
TMapDBFileOffsetArray = array of TMapDBFileOffset;
{ A Intersection is a node, where multiple routeable ways or areas
meet. }
{ TIntersection }
TIntersection = class(TMapObject)
public
// The id/file offset of the node where the ways meet
NodeId: TId;
// The objects that meet at the given node
Objects: TObjectFileRefArray;
procedure Init();
function ReadData(AScanner: TFileScanner): Boolean;
procedure Read(const ATypeConfig: TTypeConfig; AScanner: TFileScanner); override;
end;
TIntersectionArray = array of TIntersection;
TIntersectionMapById = specialize TFPGMap<TId, TIntersection>; // Id : JunctionRef
{ Description of a route, enhanced with information that are required to
give a human textual (or narrative) drive instructions;
A route consists of nodes. A Node can be the crossing point of a number of
ways and is a route decision point (where the driver possibly has the change ways)
that requires some potential action by the driver.
For each node you can pass a number of descriptions. For the way from the current node
to the next node also a number of descriptions can get retrieved.
Descriptions are typed and must derive from class Description.. }
TRouteItemType = (
riStart, // Start of the route
riTarget, // Target of the route
riName, // Something has a name. A name consists of a name and a optional alphanumeric reference (LIke B1 or A40).
riNameChanged, // Contain original and target names
riCrossingWays, // List the names of all ways, that are crossing the current node.
riDirection, // Describes the turn and the curve while getting from the previous node to the next node via the current node.
riTurn, // Signals an explicit turn
riRoundaboutEnter, // Signals entering a roundabout
riRoundaboutLeave, // Signals leaving a roundabout
riMotorwayEnter, // Signals entering a motorway
riMotorwayChange, // Signals changing a motorway
riMotorwayLeave, // Signals leaving a motorway
riMotorwayJunction, // A motorway junction
riDestination, // Destination of the route
riMaxSpeed, // Max speed
riTypeName, // Something has a type name. This is the name of the type of the way used.
riPOIAtRoute // POI at route
);
{ Base class of all descriptions. }
{ TRouteItemDescription }
TRouteItemDescription = class
public
ItemType: TRouteItemType;
Values: array of string;
function GetDebugString(): string; virtual;
procedure CombineNameRef(out AValue: string; const AName, ARef: string);
procedure ExtractNameRef(const AValue: string; out AName, ARef: string);
function GetValue(AIndex: Integer): string;
function AngleToDirectionStr(AAngle: TReal): string;
// riStart, riTarget, riName
// riMotorwayEnter, riMotorwayLeave, riMotorwayJunction
// riDestination, riTypeName
function GetDescription(): string;
procedure SetDescription(const AValue: string; AItemType: TRouteItemType);
// riNameChanged
procedure SetNameChanged(const AOrigin, ATarget: string);
// riCrossingWays
procedure SetCrossingWays(AExitCount: Integer; const AOrigin, ATarget: string);
procedure AddCrossingWay(const ADescription: string);
function GetExitCount(): Integer;
// riDirection
{ Describes the turn and the curve while getting from the previous node to the next node via the current node.
The turn is the angle between the incoming way (previous node and current node)
and the outgoing way (current node and next node) at the given node.
The curve is a heuristic measurement that not only take the next node of the target way into
account (which could only the start of a slight curve) but tries to determine the last node
of the curve and this gives a better description of the curve the vehicle needs to take. }
procedure SetDirection(ATurnAngle, ACurveAngle: TReal);
function GetTurnAngle(): TReal;
function GetCurveAngle(): TReal;
function GetTurn(): string;
function GetCurve(): string;
// riRoundaboutLeave
procedure SetRoundaboutLeave(AExitCount: Integer);
// riMotorwayChange
procedure SetMotorwayChange(const AOrigin, ATarget: string);
// riMaxSpeed
procedure SetMaxSpeed(AValue: Integer);
// riPOIAtRoute
procedure SetPOIAtRoute(ADatabaseID: TMapDatabaseId; AObject: TObjectFileRef;
AName: string; ADistance: TDistance);
end;
{ TRouteItemDescriptionList }
TRouteItemDescriptionList = class(TList)
protected
FStringHash: TSimpleStringHash;
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure AddItem(const AName: string; AItem: TRouteItemDescription);
function GetItem(AIndex: Integer): TRouteItemDescription;
function GetItemByName(const AName: string): TRouteItemDescription;
end;
TObjectFileRefArray = array of TObjectFileRef;
TRouteItem = class
private
FDatabaseId: TMapDatabaseId;
FCurrentNodeIndex: Integer;
FObjects: TObjectFileRefArray;
FPathObject: TObjectFileRef;
FTargetNodeIndex: Integer;
FDistance: TDistance; // distance from route start (meters)
FTime: TDateTime; // time from route start
FLocation: TGeoPoint;
//FDescriptionMap: std::unordered_map<std::string,DescriptionRef> ;
FDescriptions: TRouteItemDescriptionList;
public
constructor Create(ADatabaseId: TMapDatabaseId;
ACurrentNodeIndex: Integer;
const AObjects: TObjectFileRefArray;
const APathObject: TObjectFileRef;
ATargetNodeIndex: Integer);
{ There exists a object/path from the current node to the next node in the route. }
function HasPathObject(): Boolean;
function GetDBFileOffset(): TMapDBFileOffset;
function HasDescription(const AName: string): Boolean;
function GetDescription(const AName: string): TRouteItemDescription;
procedure AddDescription(const AName: string; ADescription: TRouteItemDescription);
property CurrentNodeIndex: Integer read FCurrentNodeIndex;
{ Return the objects that intersect at the current node index. }
property Objects: TObjectFileRefArray read FObjects;
{ Return a list of descriptions attached to the current node }
property Descriptions: TRouteItemDescriptionList read FDescriptions;
property DatabaseId: TMapDatabaseId read FDatabaseId;
{ Return the path object that connects the current node to the next node. }
property PathObject: TObjectFileRef read FPathObject;
{ The the index of the target node on the path that is the next node on the route. }
property TargetNodeIndex: Integer read FTargetNodeIndex;
{ Distance from the start of the route in meters. }
property Distance: TDistance read FDistance write FDistance;
{ Time from the start of the route }
property Time: TDateTime read FTime write FTime;
{ Location (latitude,longitude) of the node }
property Location: TGeoPoint read FLocation write FLocation;
end;
{ TRouteItemList }
TRouteItemList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
public
function GetItem(AIndex: Integer): TRouteItem;
function AddItem(ADatabaseId: TMapDatabaseId;
ACurrentNodeIndex: Integer;
const AObjects: TObjectFileRefArray;
const APathObject: TObjectFileRef;
ATargetNodeIndex: Integer): Integer;
end;
{ TRouteDescription }
TRouteDescription = class
private
FNodes: TRouteItemList;
protected
// assigned
FDatabaseMapping: TMapDatabaseIdMap; // DatabaseId : string
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure Clear();
function IsEmpty: Boolean;
procedure AddNode(ADatabaseId: TMapDatabaseId;
ACurrentNodeIndex: Integer;
const AObjects: TObjectFileRefArray;
const APathObject: TObjectFileRef;
ATargetNodeIndex: Integer);
property Nodes: TRouteItemList read FNodes;
property DatabaseMapping: TMapDatabaseIdMap read FDatabaseMapping write FDatabaseMapping;
end;
{ For every unique combination of object attributes that are routing
relevant we store an ObjectvariantData entry. }
{ TObjectVariantData }
TObjectVariantData = object
TypeInfo: TTypeInfo; // The type of the object
MaxSpeed: Byte; // Maximum speed allowed on the way
Grade: Byte; // Quality of road/track 1 (good)...5 (bad)
{ Read data from the given FileScanner }
procedure Read(ATypeConfig: TTypeConfig; AScanner: TFileScanner);
{ Write the data to the given FileWriter. }
procedure Write(AWriter: TFileWriter);
end;
TObjectVariantDataArray = array of TObjectVariantData;
{ Information for an object referenced by a path. }
TRouteNodeObjectData = record
Obj: TObjectFileRef; // Reference to the object
ObjVariantIndex: Integer; // Index into the lookup table, holding object specific routing data
end;
{ Exclude regarding use of paths. You cannot use the path with the index "targetPath" if you come
from the source object. }
TRouteNodeExclude = record
Source: TObjectFileRef; // The source object
TargetIndex: Integer; // The index of the target path
end;
{ A single path that starts at the given route node. A path contains a number of information
that are relevant for the router. }
{ TRouteNodePath }
TRouteNodePath = object
Distance: TDistance; // Distance from the current route node to the target route node
Id: TId; // id of the targeting route node
ObjectIndex: Integer; // The index of the way to use from this route node to the target route node
Flags: TVehicleTypes; // Certain flags
// AVehicle in Flags
function IsRestricted(AVehicle: TVehicleType): Boolean;
end;
{ TRouteNode }
PRouteNode = ^TRouteNode;
TRouteNode = object
private
FFileOffset: TFileOffset;
FPoint: TGeoPoint;
FSerial: Byte;
public
Objects: array of TRouteNodeObjectData;
Paths: array of TRouteNodePath;
Excludes: array of TRouteNodeExclude;
procedure Init(); overload;
procedure Init(const AFileOffset: TFileOffset; const APoint: TGeoPoint); overload;
function AddObject(const AObj: TObjectFileRef; AObjVariantIndex: Integer): Integer;
function GetId(): TId; // Point.GetId()
//function GetCoord(): TGeoPoint; // Point.GetCoord()
function IsRelevant(): Boolean; // Point.IsRelevant()
procedure Read(ATypeConfig: TTypeConfig; AScanner: TFileScanner);
procedure Write(AWriter: TFileWriter);
// FileOffset of the route node
property FileOffset: TFileOffset read FFileOffset;
// Coordinate and id of the route node
property Point: TGeoPoint read FPoint;
property Serial: Byte read FSerial;
end;
TRouteNodeArray = array of TRouteNode;
//TRouteNodeMapById = specialize TFPGMap<TId, TRouteNode>; // Id : RouteNode
{ TRouteEntry }
TRouteEntry = object
DatabaseId: TMapDatabaseId;
CurNodeId: TId;
CurNodeIndex: Integer;
Objects: TObjectFileRefArray;
PathObject: TObjectFileRef;
TargetNodeIndex: Integer;
procedure Init(ADatabaseId: TMapDatabaseId;
ACurNodeId: TId;
ACurNodeIndex: Integer;
const APathObject: TObjectFileRef;
ATargetNodeIndex: Integer);
function GetDBFileOffset(): TMapDBFileOffset;
end;
TRouteEntryArray = array of TRouteEntry;
{ TRouteData }
TRouteData = object
Entries: TRouteEntryArray;
procedure Clear();
procedure AddEntry(ADatabaseId: TMapDatabaseId;
ACurNodeId: TId;
ACurNodeIndex: Integer;
const APathObject: TObjectFileRef;
ATargetNodeIndex: Integer);
procedure Append(ARoutePart: TRouteData);
procedure PopEntry();
function IsEmpty(): Boolean;
end;
{ Abstract interface for a routing profile. A routing profile decides about the costs
of taking a certain way. It thus may hold information about how fast ways can be used,
maximum speed of the traveling device etc... }
TAbstractRoutingProfile = class
public
function GetVehicle(): TVehicleType; virtual; abstract;
function GetCostLimitDistance(): TDistance; virtual; abstract;
function GetCostLimitFactor(): TReal; virtual; abstract;
function CanUse(const ACurrentNode: TRouteNode;
const AObjectVariantDataArr: TObjectVariantDataArray;
APathIndex: Integer): Boolean; virtual; abstract;
function CanUse(AArea: TMapArea): Boolean; virtual; abstract; overload;
function CanUse(AWay: TMapWay): Boolean; virtual; abstract; overload;
function CanUseForward(AWay: TMapWay): Boolean; virtual; abstract;
function CanUseBackward(AWay: TMapWay): Boolean; virtual; abstract;
function GetCosts(const ACurrentNode: TRouteNode;
const AObjectVariantDataArr: TObjectVariantDataArray;
APathIndex: Integer): TReal; virtual; abstract; overload;
function GetCosts(AArea: TMapArea; const ADistance: TDistance): TReal; virtual; abstract; overload;
function GetCosts(AWay: TMapWay; const ADistance: TDistance): TReal; virtual; abstract; overload;
function GetCosts(const ADistance: TDistance): TReal; virtual; abstract; overload;
function GetTime(AArea: TMapArea; const ADistance: TDistance): TDateTime; virtual; abstract;
function GetTime(AWay: TMapWay; const ADistance: TDistance): TDateTime; virtual; abstract; overload;
end;
TSpeedMap = specialize TFPGMap<string, TReal>;
{ Common base class for our concrete profile instantiations. Offers a number of profile
type independent interface implementations and helper methods. }
{ TRoutingProfile }
TRoutingProfile = class(TAbstractRoutingProfile)
protected
FTypeConfig: TTypeConfig;
//FAccessReader: TFeatureValueReader;
//FMaxSpeedReader: TFeatureValueReader;
FVehicle: TVehicleType;
FVehicleRouteNodeMask: TVehicleMask;
FCostLimitDistance: TDistance;
FCostLimitFactor: TReal;
FSpeeds: array of TReal;
FMinSpeed: TReal;
FMaxSpeed: TReal;
FVehicleMaxSpeed: TReal;
procedure SetVehicle(const AValue: TVehicleType);
public
constructor Create(ATypeConfig: TTypeConfig);
function GetVehicle(): TVehicleType; override;
function GetCostLimitDistance(): TDistance; override;
function GetCostLimitFactor(): TReal; override;
procedure ParametrizeForFoot(ATypeConfig: TTypeConfig; AMaxSpeed: TReal);
procedure ParametrizeForBicycle(ATypeConfig: TTypeConfig; AMaxSpeed: TReal);
function ParametrizeForCar(ATypeConfig: TTypeConfig;
const ASpeedMap: TSpeedMap;
AMaxSpeed: TReal): Boolean;
procedure AddType(const AType: TTypeInfo; ASpeed: TReal);
function CanUse(const ACurrentNode: TRouteNode;
const AObjectVariantDataArr: TObjectVariantDataArray;
APathIndex: Integer): Boolean; override;
function CanUse(AArea: TMapArea): Boolean; override; overload;
function CanUse(AWay: TMapWay): Boolean; override; overload;
function CanUseForward(AWay: TMapWay): Boolean; override;
function CanUseBackward(AWay: TMapWay): Boolean; override;
function GetTime(AArea: TMapArea; const ADistance: TDistance): TDateTime; override;
function GetTime(AWay: TMapWay; const ADistance: TDistance): TDateTime; override; overload;
{ for shortest path by default }
function GetCosts(const ACurrentNode: TRouteNode;
const AObjectVariantDataArr: TObjectVariantDataArray;
APathIndex: Integer): TReal; override;
function GetCosts(AArea: TMapArea; const ADistance: TDistance): TReal; override; overload;
function GetCosts(AWay: TMapWay; const ADistance: TDistance): TReal; override; overload;
function GetCosts(const ADistance: TDistance): TReal; override; overload;
property Vehicle: TVehicleType read GetVehicle write SetVehicle;
property VehicleMaxSpeed: TReal read FVehicleMaxSpeed write FVehicleMaxSpeed;
{ static distance value added to the maximum cost }
property CostLimitDistance: TDistance read GetCostLimitDistance write FCostLimitDistance;
{ The router tries to minimize the actual costs of the route. There is a lower limit
defined by GetCosts(double distance). Applying the given factor to the minimal cost
results in a upper limit for the costs.
Increasing the factor results in the router trying harder to find a route by looking for
bigger and even bigger detours, decreasing the factor result in the router either finding a rather direct
route or none. Setting the factor below 1.0 should result in the router not finding any route at all.
If there is a router the current router will find it and the router will look for the optimal route first.
So, if there is a route the limit could be set to std::limits<double>::max(). If there is no route though
the limit will stop the router to search for all possible detours, walking the whole graph in the end.
Since this might take for ever the limit should be reasonable high.
The actual maximum cost limit is calculated based on a constant limit distance (default 10.0 Km)
and a cost factor applied to the minimum costs 8default 5.0).
So the resulting maxium cost are profile.GetCosts(profile.GetCostLimitDistance())+
profile.GetCosts(distance)*profile.GetCostLimitFactor(). }
property CostLimitFactor: TReal read GetCostLimitFactor write FCostLimitFactor;
end;
{ Profile that defines costs base of the time the traveling device needs
for a certain way resulting in the fastest path chosen (cost=distance/speedForWayType). }
{ TFastestPathRoutingProfile }
TFastestPathRoutingProfile = class(TRoutingProfile)
public
function GetCosts(const ACurrentNode: TRouteNode;
const AObjectVariantDataArr: TObjectVariantDataArray;
APathIndex: Integer): TReal; override;
function GetCosts(AArea: TMapArea; const ADistance: TDistance): TReal; override; overload;
function GetCosts(AWay: TMapWay; const ADistance: TDistance): TReal; override; overload;
function GetCosts(const ADistance: TDistance): TReal; override; overload;
end;
function MapDBId(ADatabaseId: TMapDatabaseId; AId: TId): TMapDBId;
function MapDBFileOffset(ADatabaseId: TMapDatabaseId; AOffset: TFileOffset): TMapDBFileOffset;
function MapDBIdArrayAppend(var AArray: TMapDBIdArray; const AValue: TMapDBId): Integer;
function MapDBFileOffsetArrayAppend(var AArray: TMapDBFileOffsetArray; const AValue: TMapDBFileOffset): Integer;
implementation
uses Math;
function MapDBId(ADatabaseId: TMapDatabaseId; AId: TId): TMapDBId;
begin
Result.DatabaseId := ADatabaseId;
Result.Id := AId;
end;
function MapDBFileOffset(ADatabaseId: TMapDatabaseId; AOffset: TFileOffset): TMapDBFileOffset;
begin
Result.DatabaseId := ADatabaseId;
Result.Offset := AOffset;
end;
function MapDBIdArrayAppend(var AArray: TMapDBIdArray; const AValue: TMapDBId): Integer;
begin
Result := Length(AArray);
SetLength(AArray, Result + 1);
AArray[Result] := AValue;
end;
function MapDBFileOffsetArrayAppend(var AArray: TMapDBFileOffsetArray; const AValue: TMapDBFileOffset): Integer;
begin
Result := Length(AArray);
SetLength(AArray, Result + 1);
AArray[Result] := AValue;
end;
{ TMapDBId }
procedure TMapDBId.Init(ADatabaseId: TMapDatabaseId; AId: TId);
begin
DatabaseId := ADatabaseId;
Id := AId;
end;
function TMapDBId.IsValid(): Boolean;
begin
Result := (Id <> 0);
end;
function TMapDBId.IsEqual(const AValue: TMapDBId): Boolean;
begin
Result := (AValue.Id = Id) and (AValue.DatabaseId = DatabaseId);
end;
function TMapDBId.AsStr(): string;
begin
Result := Format('%d:%d', [DatabaseId, Id]);
end;
{ TMapDBFileOffset }
procedure TMapDBFileOffset.Init(ADatabaseId: TMapDatabaseId;
AOffset: TFileOffset);
begin
DatabaseId := ADatabaseId;
Offset := AOffset;
end;
function TMapDBFileOffset.AsStr(): string;
begin
Result := IntToHex(Offset, 2) + ':' + IntToHex(DatabaseId, 2);
end;
function TMapDBFileOffset.IsEqual(const AValue: TMapDBFileOffset): Boolean;
begin
Result := (AValue.Offset = Offset) and (AValue.DatabaseId = DatabaseId);
end;
{ TRouteItemDescription }
function TRouteItemDescription.GetDebugString(): string;
var
i: Integer;
begin
case ItemType of
riStart:
Result := 'Start: ' + GetValue(0);
riTarget:
Result := 'Target: ' + GetValue(0);
riName:
Result := 'Name: ' + GetValue(0);
riNameChanged:
Result := 'Name Change: ' + GetValue(0) + ' -> ' + GetValue(1);
riCrossingWays:
begin
Result := 'Crossing: from ' + GetValue(0) + ' to ' + GetValue(1)
+ ' exits (' + GetValue(2) + '): ';
for i := 3 to Length(Values)-1 do
begin
if i > 3 then
Result := Result + ', ';
Result := Result + GetValue(i);
end;
end;
riDirection:
begin
Result := 'Direction: '
+ GetValue(2) + ' (' + GetValue(0) + ' deg), curve '
+ GetValue(3) + ' (' + GetValue(1) + ' deg), ';
end;
riTurn:
Result := 'Turn';
riRoundaboutEnter:
Result := 'Enter roundabout';
riRoundaboutLeave:
Result := 'Leave roundabout';
riMotorwayEnter:
Result := 'Enter motorway: ' + GetValue(0);
riMotorwayChange:
Result := 'Change motorway';
riMotorwayLeave:
Result := 'Leave motorway';
riMotorwayJunction:
Result := 'Motorway junction';
riDestination:
Result := 'Destination: ' + GetValue(0);
riMaxSpeed:
Result := 'Max. speed: ' + GetValue(0);
riTypeName:
Result := 'Type name: ' + GetValue(0);
riPOIAtRoute:
Result := 'POI: ' + GetValue(0) + ' (' + GetValue(1) + ' m)';
else
Result := '';
for i := Low(Values) to High(Values) do
begin
if Result <> '' then
Result := Result + ', ';
Result := Result + Values[i];
end;
end;
end;
procedure TRouteItemDescription.CombineNameRef(out AValue: string; const AName,
ARef: string);
begin
AValue := AName;
if ARef <> '' then
AValue := AValue + '|' + ARef;
end;
procedure TRouteItemDescription.ExtractNameRef(const AValue: string; out AName,
ARef: string);
var
n: Integer;
begin
n := Pos('|', AValue);
if n = 0 then
begin
AName := AValue;
ARef := '';
end
else
begin
AName := Copy(AValue, 1, n-1);
ARef := Copy(AValue, n+1, MaxInt);
end;
end;
function TRouteItemDescription.GetValue(AIndex: Integer): string;
begin
if Length(Values) >= (AIndex + 1) then
Result := Values[AIndex]
else
Result := '';
end;
function TRouteItemDescription.AngleToDirectionStr(AAngle: TReal): string;
begin
if (AAngle >= -10.0) and (AAngle <= 10.0) then
Result := '^'
else
if (AAngle >= -45.0) then
Result := '<'
else
if (AAngle >= -120.0) then
Result := '<<'
else
if (AAngle >= -180.0) then
Result := '<<<'
else
if (AAngle <= 45.0) then
Result := '>'
else
if (AAngle <= 120.0) then
Result := '>>'
else
if (AAngle <= 180.0) then
Result := '>>>'
else
Result := '???'
end;
function TRouteItemDescription.GetDescription(): string;
begin
if Length(Values) > 0 then
Result := Values[0]
else
Result := '';
end;
procedure TRouteItemDescription.SetDescription(const AValue: string;
AItemType: TRouteItemType);
begin
ItemType := AItemType;
if Length(Values) = 0 then
SetLength(Values, 1);
Values[0] := AValue;
end;
procedure TRouteItemDescription.SetNameChanged(const AOrigin, ATarget: string);
begin
ItemType := riNameChanged;
if Length(Values) < 2 then
SetLength(Values, 2);
Values[0] := AOrigin;
Values[1] := ATarget;
end;
procedure TRouteItemDescription.SetCrossingWays(AExitCount: Integer;
const AOrigin, ATarget: string);
begin
ItemType := riCrossingWays;
if Length(Values) < 3 then
SetLength(Values, 3);
Values[0] := IntToStr(AExitCount);
Values[1] := AOrigin;
Values[2] := ATarget;
end;
procedure TRouteItemDescription.AddCrossingWay(const ADescription: string);
var
n: Integer;
begin
Assert(ItemType = riCrossingWays);
if Length(Values) < 3 then
SetLength(Values, 3);
n := Length(Values);
SetLength(Values, n+1);
Values[n] := ADescription;
end;
function TRouteItemDescription.GetExitCount(): Integer;
begin
Assert(ItemType in [riCrossingWays, riRoundaboutLeave]);
if Length(Values) > 0 then
Result := StrTointDef(Values[0], 0)
else
Result := 0;
end;
procedure TRouteItemDescription.SetDirection(ATurnAngle, ACurveAngle: TReal);
begin
ItemType := riDirection;
if Length(Values) < 4 then
SetLength(Values, 4);
Values[0] := FloatToStr(ATurnAngle);
Values[1] := FloatToStr(ACurveAngle);
Values[2] := AngleToDirectionStr(ATurnAngle);
Values[3] := AngleToDirectionStr(ACurveAngle);
end;
function TRouteItemDescription.GetTurnAngle(): TReal;
begin
Result := StrToFloatDef(GetValue(0), 0);
end;
function TRouteItemDescription.GetCurveAngle(): TReal;
begin
Result := StrToFloatDef(GetValue(1), 0);
end;
function TRouteItemDescription.GetTurn(): string;
begin
Result := GetValue(2);
end;
function TRouteItemDescription.GetCurve(): string;
begin
Result := GetValue(3);
end;
procedure TRouteItemDescription.SetRoundaboutLeave(AExitCount: Integer);
begin
ItemType := riRoundaboutLeave;
if Length(Values) < 1 then
SetLength(Values, 1);
Values[0] := IntToStr(AExitCount);
end;
procedure TRouteItemDescription.SetMotorwayChange(const AOrigin,
ATarget: string);
begin
ItemType := riMotorwayChange;
if Length(Values) < 2 then
SetLength(Values, 2);
Values[0] := AOrigin;
Values[1] := ATarget;
end;
procedure TRouteItemDescription.SetMaxSpeed(AValue: Integer);
begin
ItemType := riMaxSpeed;
if Length(Values) < 1 then
SetLength(Values, 1);
Values[0] := IntToStr(AValue);
end;
procedure TRouteItemDescription.SetPOIAtRoute(ADatabaseID: TMapDatabaseId;
AObject: TObjectFileRef; AName: string; ADistance: TDistance);
begin
ItemType := riPOIAtRoute;
if Length(Values) < 4 then
SetLength(Values, 4);
Values[0] := IntToStr(ADatabaseID);
Values[1] := AObject.GetName();
Values[2] := AName;
Values[3] := FloatToStr(ADistance);
end;
{ TRouteItemDescriptionList }
procedure TRouteItemDescriptionList.Notify(Ptr: Pointer;
Action: TListNotification);
begin
inherited Notify(Ptr, Action);
if Action = lnDeleted then
TRouteItemDescription(Ptr).Free();
end;
procedure TRouteItemDescriptionList.AfterConstruction;
begin
inherited AfterConstruction;
FStringHash.Init(16);
end;
procedure TRouteItemDescriptionList.BeforeDestruction;
begin
FStringHash.Clear();
inherited BeforeDestruction;
end;
procedure TRouteItemDescriptionList.AddItem(const AName: string;
AItem: TRouteItemDescription);
var
n: Integer;
begin
Assert(not FStringHash.FindValue(AName, n));
n := Add(AItem);
FStringHash.Add(AName, n);
end;
function TRouteItemDescriptionList.GetItem(AIndex: Integer): TRouteItemDescription;
begin
Result := TRouteItemDescription(Get(AIndex));
end;
function TRouteItemDescriptionList.GetItemByName(const AName: string): TRouteItemDescription;
var
n: Integer;
begin
n := FStringHash.ValueOf(AName);
if n <> -1 then
Result := GetItem(n)
else
Result := nil;
end;
{ TRouteItem }
constructor TRouteItem.Create(ADatabaseId: TMapDatabaseId;
ACurrentNodeIndex: Integer; const AObjects: TObjectFileRefArray;
const APathObject: TObjectFileRef; ATargetNodeIndex: Integer);
begin
inherited Create;
FDatabaseId := ADatabaseId;
FCurrentNodeIndex := ACurrentNodeIndex;
FObjects := AObjects;
FPathObject := APathObject;
FTargetNodeIndex := ATargetNodeIndex;
end;
function TRouteItem.HasPathObject(): Boolean;
begin
Result := FPathObject.IsValid();
end;
function TRouteItem.GetDBFileOffset(): TMapDBFileOffset;
begin
Result.Init(FDatabaseId, FPathObject.Offset);
end;
function TRouteItem.HasDescription(const AName: string): Boolean;
begin
Result := (Descriptions.GetItemByName(AName) <> nil);
end;
function TRouteItem.GetDescription(const AName: string): TRouteItemDescription;
begin
Result := Descriptions.GetItemByName(AName);
end;
procedure TRouteItem.AddDescription(const AName: string;
ADescription: TRouteItemDescription);
begin
Descriptions.AddItem(AName, ADescription);
end;
{ TRouteItemList }