-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathosmappainter.pas
3378 lines (2822 loc) · 107 KB
/
osmappainter.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
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 rendering routines
MapPainter:
MapPainter
*)
unit OsMapPainter;
{$ifdef FPC}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils,
{$ifdef FPC}
fgl,
{$else}
System.Generics.Collections,
{$endif}
OsMapTypes, OsMapStyles, OsMapGeometry,
OsMapParameters, OsMapObjTypes, OsMapObjects, OsMapStyleConfig,
OsMapObjFeatures, OsMapLabels, OsMapTransform, OsMapProjection;
type
TRenderSteps = (
rsInitialize, // Setup internal state of renderer for executing next steps with current projection and parameters
rsDumpStatistics, // Prints details for debugging, if debug flag (performance, data) is set in renderer parameter
rsPreprocessData, // Convert geographical coordinates of object points to screen coordinates,
rsPrerender, // Implementation specific preparison
rsDrawGroundTiles, // Draw unknown/sea/land tiles and tiles with "coastlines"
rsDrawOSMTileGrids, // If special style exists, renders grid corresponding to OSM tiles
rsDrawAreas,
rsDrawWays,
rsDrawWayDecorations,
rsDrawWayContourLabels,
rsPrepareAreaLabels,
rsDrawAreaBorderLabels,
rsDrawAreaBorderSymbols,
rsPrepareNodeLabels,
rsDrawLabels,
rsPostrender // Implementation specific final step
);
{ Structure used for internal statistic collection }
TDataStatistic = record
InfoType: Integer; // Type
ObjectCount: Int64; // Sum of nodeCount, wayCount, areaCont
NodeCount: Int64; // Number of Node objects
WayCount: Int64; // Number of Way objects
AreaCount: Int64; // Number of Area objects
CoordCount: Int64; // Number of coordinates
LabelCount: Int64; // Number of labels
IconCount: Int64; // Number of icons
end;
{ Data structure for holding temporary data about ways }
TWayData = record
Ref: TObjectFileRef;
pBuffer: ^TFeatureValueBuffer; // Features of the line segment
Layer: Byte; // Layer this way is in
LineStyle: TLineStyle; // Line style
WayPriority: Integer; // Priority of way (from style sheet)
TransStart: Integer; // Start of coordinates in transformation buffer
TransEnd: Integer; // End of coordinates in transformation buffer
LineWidth: TReal; // Line width in pixels
IsStartClosed: Boolean; // The end of the way is closed, it does not lead to another way or area
IsEndClosed: Boolean; // The end of the way is closed, it does not lead to another way or area
end;
TWayDataCompare = function(const A, B: TWayData): Integer;
{ TWayDataList }
TWayDataList = object
FCount: Integer;
function GetCapacity: Integer;
procedure SetCapacity(AValue: Integer);
public
Items: array of TWayData;
procedure Clear();
function Append(const AItem: TWayData): Integer;
procedure Sort(ASorter: TWayDataCompare);
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read FCount;
end;
{ We then draw lines in order of layer (Smaller layers first)
Within a layer, we draw lines in order of line style priority (first overlays, lower priority value first)
Within a style priority, we draw transparent lines over solid lines
Within a style priority we draw lines in order of style sheet way priority
(more important ways on top of less important ways, higher priority value first) }
function CompareWayData(const AItem1, AItem2: TWayData): Integer;
type
{ Data structure for holding temporary data about way paths (a way may consist of
multiple paths/lines rendered) }
TWayPathData = record
Ref: TObjectFileRef;
pBuffer: ^TFeatureValueBuffer; // Features of the line segment
pDrawOptions: PMapItemDrawOptions;
TransStart: Integer; // Start of coordinates in transformation buffer
TransEnd: Integer; // End of coordinates in transformation buffer
end;
{ TWayPathDataList }
TWayPathDataList = object
FCount: Integer;
function GetCapacity: Integer;
procedure SetCapacity(AValue: Integer);
public
Items: array of TWayPathData;
procedure Clear();
function Append(const AItem: TWayPathData): Integer;
//procedure Sort(ASorter: TAreaSorter);
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read FCount;
end;
TPolyData = record
TransStart: Integer; // Start of coordinates in transformation buffer
TransEnd: Integer; // End of coordinates in transformation buffer
end;
{ Data structure for holding temporary data about areas }
TAreaData = record
Ref: TObjectFileRef;
TypeInfo: TTypeInfo; //
pBuffer: ^TFeatureValueBuffer; // Features of the line segment
pDrawOptions: PMapItemDrawOptions; // cached draw options
FillStyle: TFillStyle; // Fill style
BorderStyle: TBorderStyle; // Border style
BoundingBox: TGeoBox; // Bounding box of the area (in geo coordinates)
//VisualCenter: TVertex2D; // Visual center point for label, in screen coordinates
IsOuter: Boolean; // flag if this area is outer ring of some relation
TransStart: Integer; // Start of coordinates in transformation buffer
TransEnd: Integer; // End of coordinates in transformation buffer
Clippings: array of TPolyData; // Clipping polygons to be used during drawing of this area
end;
PAreaData = ^TAreaData;
TAreaDataCompare = function(const A, B: TAreaData): Integer;
{ TAreaDataList }
TAreaDataList = object
private
FCount: Integer;
FItems: array of PAreaData;
function GetCapacity: Integer;
procedure SetCapacity(AValue: Integer);
function GetPItem(AIndex: Integer): PAreaData;
procedure QSort(L, R: Integer);
public
procedure Clear();
function Append(const AItem: TAreaData): Integer;
procedure Sort(ASorter: TAreaDataCompare);
property Capacity: Integer read GetCapacity write SetCapacity;
property Count: Integer read FCount;
property PItems[AIndex: Integer]: PAreaData read GetPItem;
end;
{ TContourLabelHelper }
{ Helper class for drawing contours. Allows the MapPainter base class
to inject itself at certain points in the contour label rendering code of
the actual backend. }
TContourLabelHelper = object
public
ContourLabelOffset: TReal;
ContourLabelSpace: TReal;
PathLength: TReal;
TextWidth: TReal;
CurrentOffset: TReal;
function Init(APathLength: TReal = 0.0; ATextWidth: TReal = 0.0): Boolean;
function ContinueDrawing(): Boolean;
function GetCurrentOffset(): TReal;
procedure AdvancePartial(AWidth: TReal);
procedure AdvanceText();
procedure AdvanceSpace();
end;
{ render step method }
TStepMethod = procedure(const AProjection: TProjection;
const AMapParameter: TMapParameter;
const AMapData: TMapData) of object;
{ TMapPainter }
{ Abstract base class of all renders (though you can always write
your own renderer without inheriting from this class) It
implements the general rendering algorithm. Concrete renders are
implemented by implementing the abstract methods defined by this class
and used as callbacks to the concrete renderer. }
TMapPainter = class(TObject)
private
//FStepMethods: array [TRenderSteps] of TStepMethod;
FErrorTolerancePixel: TReal;
FAreaDataList: TAreaDataList;
FWayDataList: TWayDataList;
FWayPathDataList: TWayPathDataList;
FTextStyles: TTextStyleList; // Temporary storage for StyleConfig return value
FLineStyles: TLineStyleList; // Temporary storage for StyleConfig return value
FBorderStyles: TBorderStyleList; // Temporary storage for StyleConfig return value
{ Fallback styles in case they are missing for the style sheet }
FLandFill: TFillStyle;
FSeaFill: TFillStyle;
FCoastlineSegmentAttributes: TFeatureValueBuffer;
{ Precalculations }
FStandardFontHeight: TReal; // Default font height in pixels
FAreaMinDimension: TReal; // Minimal width or height in pixels for visible area
FOnLog: TGetStrProc;
private
{ Debugging }
procedure DumpDataStatistics(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
{ Private draw algorithm implementation routines. }
{ Get label text for feature of specified type }
function GetLabelText(AFeatureType: TFeatureType;
const AParameter: TMapParameter;
const ABuffer: TFeatureValueBuffer): string;
procedure PrepareNode(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const ANode: TMapNode);
procedure PrepareNodes(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
function CalculatePaths(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const ARef: TObjectFileRef;
const ABuffer: TFeatureValueBuffer;
const AWay: TMapWay): Boolean;
procedure PrepareWays(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure PrepareArea(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AArea: TMapArea);
procedure PrepareAreaLabel(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AAreaData: TAreaData);
procedure PrepareAreas(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure RegisterPointWayLabel(const AProjection: TProjection;
const AParameter: TMapParameter;
const AShieldStyle: TPathShieldStyle;
const AText: string;
const ANodes: TGeoPointArray);
{ Request layout of a point label
AProjection - Projection instance to use
AParameter - General map drawing parameter that might influence the result
ABuffer - The FeatureValueBuffer of the object that owns the label
AIconStyle - An optional icon style to use
ATextStyles - A list of text styles to use (the object could have more than
label styles attached)
X, Y - position to place the label at (currently always the center of the area or the coordinate of the node)
AObjectWidth - The (rough) width of the object
AObjectHeight - The (rough) height of the object }
function LayoutPointLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const ABuffer: TFeatureValueBuffer;
const AIconStyle: TIconStyle;
const ATextStyles: TTextStyleList;
X, Y: TReal;
AObjectWidth: TReal = 0;
AObjectHeight: TReal = 0): Boolean;
function DrawWayDecoration(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TWayPathData): Boolean;
function CalculateWayShieldLabels(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapWay): Boolean;
function DrawWayContourLabel(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TWayPathData): Boolean;
function DrawAreaBorderLabel(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AAreaData: TAreaData): Boolean;
function DrawAreaBorderSymbol(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AAreaData: TAreaData): Boolean;
procedure DrawOSMTileGrid(const AProjection: TProjection;
const AParameter: TMapParameter;
const AMagnification: TMagnification;
const AOsmTileLine: TLineStyle);
{ This are the official render step methods. One method for each render step. }
{ Base method that must get called to initial the renderer for a render action.
The derived method of the concrete renderer implementation can have
Returns False if there was either an error or of the rendering was already interrupted, else True }
procedure InitializeRender(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DumpStatistics(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure PreprocessData(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure Prerender(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawGroundTiles(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawOSMTileGrids(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawAreas(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawWays(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawWayDecorations(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawWayContourLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure PrepareAreaLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawAreaBorderLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure DrawAreaBorderSymbols(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure PrepareNodeLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
procedure Postrender(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData);
protected
FDebugLabel: TTextStyle;
FIsBusy: Boolean;
{ current render step }
FCurStep: TRenderSteps;
FCurItemIndex: Integer;
FCurItemDesc: string;
FStyleConfig: TStyleConfig; // Reference to the style configuration to be used
{ Scratch variables for path optimization algorithm }
FTransBuffer: TTransBuffer; // Static (avoid reallocation) buffer of transformed coordinates
{ Attribute readers }
FNameReader: TFeatureValueReader;
FNameAltReader: TFeatureValueReader;
FRefReader: TFeatureValueReader;
FLayerReader: TFeatureValueReader;
FWidthReader: TFeatureValueReader;
FAddressReader: TFeatureValueReader;
FLanesReader: TFeatureValueReader;
FAccessReader: TFeatureValueReader;
{ Presets, precalculations and similar }
FEmptyDash: array of TReal; // Empty dash array
FTunnelDash: array of TReal; // Dash array for drawing tunnel border
FAreaMarkStyle: TFillStyle; // Marker fill style for internal debugging
FContourLabelOffset: TReal; // Same value as in MapParameter but converted to pixel
FContourLabelSpace: TReal; // Same value as in MapParameter but converted to pixel
FShieldGridSizeHoriz: TReal; // Width of a cell for shield label placement
FShieldGridSizeVert: TReal; // Height of a cell for shield label placement
protected
{ Useful global helper functions. }
function IsVisibleArea(const AProjection: TProjection;
const ABoundingBox: TGeoBox;
APixelOffset: TReal): Boolean;
{ Return True if given ABoundingBox for way path is intersects with
projection visible area. APixelOffset is half width of Way line, in pixels }
function IsVisibleWay(const AProjection: TProjection;
const ABoundingBox: TGeoBox;
APixelOffset: TReal): Boolean;
procedure Transform(const AProjection: TProjection;
const AParameter: TMapParameter;
const ACoord: TGeoPoint;
var X, Y: TReal);
{ translate meters to pixels, result not less than AMinPixel }
function GetProjectedWidth(const AProjection: TProjection;
AMinPixel, AWidth: TReal): TReal; overload;
{ translate meters to pixels }
function GetProjectedWidth(const AProjection: TProjection;
AWidth: TReal): TReal; overload;
// GetWayData -> FWayDataList
// GetAreaData -> FAreaDataList
{ === Low level drawing routines that have to be implemented by
the concrete drawing engine. }
{ Some optional callbacks between individual processing steps. }
procedure AfterPreprocessing(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData); virtual;
procedure BeforeDrawing(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData); virtual; abstract;
procedure AfterDrawing(const AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData); virtual;
{ Return true, if the icon in the IconStyle is available and can be drawn.
If this method returns false, possibly a fallback (using a Symbol)
will be chosen.
Icon style dimensions and iconId may be setup for later usage. }
function HasIcon(AStyleConfig: TStyleConfig;
AProjection: TProjection;
AParameter: TMapParameter;
AStyle: TIconStyle): Boolean; virtual; abstract;
{ Returns the height of the font in pixel in relation to the given AFontSize in mm }
function GetFontHeight(const AProjection: TProjection;
const AParameter: TMapParameter;
AFontSize: TReal): TReal; virtual; abstract;
{ (Optionally) fills the area with the given default color
for ground. In 2D backends this just fills the given area,
3D backends might draw a sphere or an infinite plane. }
procedure DrawGround(const AProjection: TProjection;
const AParameter: TMapParameter;
const AStyle: TFillStyle); virtual; abstract;
{ Register regular label with given text at the given pixel coordinate
in a style defined by the given LabelStyle. }
procedure RegisterRegularLabel(const AProjection: TProjection;
const AParameter: TMapParameter;
const ALabels: TLabelDataList;
const APosition: TVertex2D;
AObjectWidth: TReal); virtual; abstract;
{ Register contour label }
procedure RegisterContourLabel(const AProjection: TProjection;
const AParameter: TMapParameter;
const ALabel: TPathLabelData;
const ALabelPath: TLabelPath); virtual; abstract;
procedure DrawLabels(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TMapData); virtual; abstract;
{ Draw the Icon as defined by the IconStyle at the given pixel coordinate (icon center). }
procedure DrawIcon(AStyle: TIconStyle;
ACenterX, ACenterY: TReal;
AWidth, Aheight: TReal); virtual; abstract;
{ Draw the Symbol as defined by the SymbolStyle at the given pixel coordinate (symbol center). }
procedure DrawSymbol(AProjection: TProjection;
AParameter: TMapParameter;
ASymbol: TMapSymbol;
X, Y: TReal); virtual; abstract;
{ Draw simple line with the given style,the given color, the given width
and the given untransformed nodes. }
procedure DrawPath(const AProjection: TProjection;
const AParameter: TMapParameter;
const AColor: TMapColor;
AWidth: TReal;
const ADash: array of TReal;
AStartCap: TLineCapStyle;
AEndCap: TLineCapStyle;
ATransStart, ATransEnd: Integer); virtual; abstract;
{ Draw the given text as a contour of the given path in a style defined
by the given LabelStyle. }
procedure DrawContourSymbol(const AProjection: TProjection;
const AParameter: TMapParameter;
const ASymbol: TMapSymbol;
ASpace: TReal;
ATransStart, ATransEnd: Integer); virtual; abstract;
{ Draw the given area using the given FillStyle
for the area outline. }
procedure DrawArea(const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TAreaData); virtual; abstract;
{ Compute suggested label width for given parameters.
It may be used by backend for layout labels with wrapping words. }
function GetProposedLabelWidth(const AParameter: TMapParameter;
AAverageCharWidth: TReal;
AObjectWidth: TReal;
AStringLength: Integer): TReal; virtual;
procedure DrawWay(AStyleConfig: TStyleConfig;
const AProjection: TProjection;
const AParameter: TMapParameter;
const AData: TWayData); virtual;
public
constructor Create(const AStyleConfig: TStyleConfig);
destructor Destroy; override;
{ Get current renderer state description, for debugging }
function GetStateStr(): string;
procedure CheckDebug(AMapData: TMapData);
{ }
function Draw(AProjection: TProjection;
AParameter: TMapParameter;
AData: TMapData;
AStartStep: TRenderSteps;
AEndStep: TRenderSteps): Boolean;
function DrawMap(AProjection: TProjection;
AParameter: TMapParameter;
AData: TMapData): Boolean; virtual;
property IsBusy: Boolean read FIsBusy;
property CurrentStep: TRenderSteps read FCurStep;
property OnLog: TGetStrProc read FOnLog write FOnLog;
// debug
property AreaDataList: TAreaDataList read FAreaDataList;
end;
{$ifdef FPC}
TMapPainterList = specialize TFPGList<TMapPainter>;
{$else}
TMapPainterList = TList<TMapPainter>;
{$endif}
{ Batch renderer helps to render map based on multiple databases
- map data and corresponding MapPainter }
{ TMapPainterBatch }
TMapPainterBatch = class
protected
FData: TMapDataList;
FPainters: TMapPainterList;
{ Render bach of multiple databases, step by step (\see RenderSteps).
All painters should have initialised its (backend specific) state. }
function BatchPaintInternal(const AProjection: TProjection;
const AParameter: TMapParameter): Boolean;
public
constructor Create;
destructor Destroy; override;
procedure AddData(var AData: TMapData; APainter: TMapPainter);
end;
{ TMapPainterNoOp }
TMapPainterNoOp = class(TMapPainter)
protected
function HasIcon(AStyleConfig: TStyleConfig;
AProjection: TProjection;
AParameter: TMapParameter;
AStyle: TIconStyle): Boolean; override;
function GetFontHeight(const AProjection: TProjection;
const AParameter: TMapParameter; AFontSize: TReal): TReal; override;
procedure DrawGround(const AProjection: TProjection;
const AParameter: TMapParameter; const AStyle: TFillStyle); override;
procedure RegisterRegularLabel(const AProjection: TProjection;
const AParameter: TMapParameter; const ALabels: TLabelDataList;
const APosition: TVertex2D; AObjectWidth: TReal); override;
procedure RegisterContourLabel(const AProjection: TProjection;
const AParameter: TMapParameter; const ALabel: TPathLabelData;
const ALabelPath: TLabelPath); override;
procedure DrawLabels(const AProjection: TProjection;
const AParameter: TMapParameter; const AData: TMapData); override;
procedure DrawIcon(AStyle: TIconStyle; ACenterX, ACenterY: TReal;
AWidth, Aheight: TReal); override;
procedure DrawSymbol(AProjection: TProjection;
AParameter: TMapParameter;
ASymbol: TMapSymbol;
X, Y: TReal); override;
procedure DrawPath(const AProjection: TProjection;
const AParameter: TMapParameter; const AColor: TMapColor; AWidth: TReal;
const ADash: array of TReal; AStartCap: TLineCapStyle;
AEndCap: TLineCapStyle; ATransStart, ATransEnd: Integer); override;
procedure DrawContourSymbol(const AProjection: TProjection;
const AParameter: TMapParameter; const ASymbol: TMapSymbol;
ASpace: TReal; ATransStart, ATransEnd: Integer); override;
procedure DrawArea(const AProjection: TProjection;
const AParameter: TMapParameter; const AData: TAreaData); override;
public
constructor Create(const AStyleConfig: TStyleConfig);
function DrawMap(AProjection: TProjection; AParameter: TMapParameter; AData: TMapData): Boolean; override;
end;
function CompareAreas(const A, B: TMapArea): Integer;
implementation
uses Math, OsMapUtils;
const
RenderStepsNames: array[TRenderSteps] of string = (
'Initialize',
'DumpStatistics',
'PreprocessData',
'Prerender',
'DrawGroundTiles',
'DrawOSMTileGrids',
'DrawAreas',
'DrawWays',
'DrawWayDecorations',
'DrawWayContourLabels',
'PrepareAreaLabels',
'DrawAreaBorderLabels',
'DrawAreaBorderSymbols',
'PrepareNodeLabels',
'DrawLabels',
'Postrender'
);
function CompareWayData(const AItem1, AItem2: TWayData): Integer;
begin
Result := 0;
if AItem1.Layer > AItem2.Layer then
Result := 1
else if AItem1.Layer < AItem2.Layer then
Result := -1;
if Result <> 0 then Exit;
if AItem1.LineStyle.ZIndex > AItem2.LineStyle.ZIndex then
Result := 1
else if AItem1.LineStyle.ZIndex < AItem2.LineStyle.ZIndex then
Result := -1;
if Result <> 0 then Exit;
if AItem1.LineStyle.Priority > AItem2.LineStyle.Priority then
Result := 1
else if AItem1.LineStyle.Priority < AItem2.LineStyle.Priority then
Result := -1;
if Result <> 0 then Exit;
if AItem1.WayPriority > AItem2.WayPriority then
Result := 1
else if AItem1.WayPriority < AItem2.WayPriority then
Result := -1;
end;
function CompareAreaData(const A, B: TAreaData): Integer;
var
n1, n2: Integer;
begin
{if A.TypeInfo.ZOrder > B.TypeInfo.ZOrder then
Exit(1)
else if A.TypeInfo.ZOrder < B.TypeInfo.ZOrder then
Exit(-1); }
if A.pDrawOptions^.ZOrder > B.pDrawOptions^.ZOrder then
Exit(1)
else if A.pDrawOptions^.ZOrder < B.pDrawOptions^.ZOrder then
Exit(-1);
n1 := StrToIntDef(A.pBuffer^.GetFeatureValue(ftLayer), 1);
n2 := StrToIntDef(B.pBuffer^.GetFeatureValue(ftLayer), 1);
if n1 > n2 then
Exit(1)
else if (n1 < n2) then
Exit(-1);
if Assigned(A.FillStyle) and Assigned(B.FillStyle) then
begin
if A.FillStyle.FillColor.IsSolid() and (not B.FillStyle.FillColor.IsSolid()) then
Exit(1)
else if (not A.FillStyle.FillColor.IsSolid()) and B.FillStyle.FillColor.IsSolid() then
Exit(-1);
end
else if Assigned(A.FillStyle) then
Exit(1)
else if Assigned(B.FillStyle) then
Exit(-1);
if (A.BoundingBox.MinCoord.Lon > B.BoundingBox.MinCoord.Lon)
and (A.BoundingBox.MaxCoord.Lon < B.BoundingBox.MaxCoord.Lon)
then
Exit(1)
else
if (A.BoundingBox.MinCoord.Lat > B.BoundingBox.MinCoord.Lat)
and (A.BoundingBox.MaxCoord.Lat < B.BoundingBox.MaxCoord.Lat)
then
Exit(1)
else
Exit(0);
{if (A.BoundingBox.MinCoord.Lon = B.BoundingBox.MinCoord.Lon) then
begin
if (A.BoundingBox.MaxCoord.Lon = B.BoundingBox.MaxCoord.Lon) then
begin
if (A.BoundingBox.MinCoord.Lat = B.BoundingBox.MinCoord.Lat) then
begin
if (A.BoundingBox.MaxCoord.Lat = B.BoundingBox.MaxCoord.Lat) then
begin
(**
* Condition for the case when one area exists in two relations
* - in one as outer ring (type of relation is used) and in second relation as inner ring.
* In such case, we want to draw area with outer type after that one of inner type
*)
if ((not A.IsOuter) and B.isOuter) then
Result := 1
else
Result := 0;
end
else
Result := Trunc(A.BoundingBox.MaxCoord.Lat - B.BoundingBox.MaxCoord.Lat);
end
else
Result := Trunc(A.BoundingBox.MinCoord.Lat - B.BoundingBox.MinCoord.Lat);
end
else
Result := Trunc(A.BoundingBox.MaxCoord.Lon - B.BoundingBox.MaxCoord.Lon);
end
else
Result := Trunc(A.BoundingBox.MinCoord.Lon - B.BoundingBox.MinCoord.Lon); }
end;
function CompareAreas(const A, B: TMapArea): Integer;
var
n1, n2: Integer;
begin
if A.TypeInfo.ZOrder > B.TypeInfo.ZOrder then
Exit(1)
else if A.TypeInfo.ZOrder < B.TypeInfo.ZOrder then
Exit(-1);
n1 := StrToIntDef(A.Rings[0].FeatureValueBuffer.GetFeatureValue(ftLayer), 1);
n2 := StrToIntDef(B.Rings[0].FeatureValueBuffer.GetFeatureValue(ftLayer), 1);
if n1 > n2 then
Exit(1)
else if (n1 < n2) then
Exit(-1);
if (A.Rings[0].BBox.MinCoord.Lon > B.Rings[0].BBox.MinCoord.Lon)
and (A.Rings[0].BBox.MaxCoord.Lon < B.Rings[0].BBox.MaxCoord.Lon)
then
Exit(1)
else
if (A.Rings[0].BBox.MinCoord.Lat > B.Rings[0].BBox.MinCoord.Lat)
and (A.Rings[0].BBox.MaxCoord.Lat < B.Rings[0].BBox.MaxCoord.Lat)
then
Exit(1);
if A.FileOffset > B.FileOffset then
Result := 1
else if A.FileOffset < B.FileOffset then
Result := -1
else
Result := 0;
end;
function FMod(a, b: TReal): TReal;
begin
Result := a - b * Int(a / b);
end;
procedure GetGridPoints(const ANodes: TGeoPointArray;
AGridSizeHoriz, AGridSizeVert: TReal;
var AIntersections: TGeoPointArray);
var
i, n, cellXStart, cellYStart, cellXEnd, cellYEnd: Integer;
lower, upper: TReal;
xIndex, yIndex: Integer;
intersection: TGeoPoint;
xCoord, yCoord: TReal;
begin
Assert(Length(ANodes) >= 2);
for i := 0 to Length(ANodes)-2 do
begin
cellXStart := Trunc((ANodes[i].Lon + 180.0) / AGridSizeHoriz);
cellYStart := Trunc((ANodes[i].Lat + 90.0) / AGridSizeVert);
cellXEnd := Trunc((ANodes[i+1].Lon + 180.0) / AGridSizeHoriz);
cellYEnd := Trunc((ANodes[i+1].Lat + 90.0) / AGridSizeVert);
if (cellXStart <> cellXEnd) then
begin
lower := min(cellYStart, cellYEnd) * AGridSizeVert - 90.0;
upper := (max(cellYStart, cellYEnd)+1) * AGridSizeVert - 90.0;
for xIndex := cellXStart+1 to cellXEnd do
begin
xCoord := xIndex * AGridSizeHoriz - 180.0;
if (GetLineIntersection(ANodes[i],
ANodes[i+1],
GeoCoord(lower, xCoord),
GeoCoord(upper, xCoord),
intersection))
then
begin
n := Length(AIntersections);
SetLength(AIntersections, n+1);
AIntersections[n].Assign(intersection);
end;
end;
end;
if (cellYStart <> cellYEnd) then
begin
lower := min(cellXStart, cellXEnd) * AGridSizeHoriz - 180.0;
upper := (max(cellXStart, cellXEnd)+1) * AGridSizeHoriz - 180.0;
for yIndex := cellYStart+1 to cellYEnd do
begin
yCoord := yIndex * AGridSizeVert - 90.0;
if (GetLineIntersection(ANodes[i],
ANodes[i+1],
GeoCoord(yCoord, lower),
GeoCoord(yCoord, upper),
intersection))
then
begin
n := Length(AIntersections);
SetLength(AIntersections, n+1);
AIntersections[n].Assign(intersection);
end;
end;
end;
end;
end;
{ TContourLabelHelper }
function TContourLabelHelper.Init(APathLength: TReal; ATextWidth: TReal
): Boolean;
var
a: TReal;
begin
Result := False;
PathLength := APathLength;
TextWidth := ATextWidth;
a := PathLength - TextWidth - (2 * ContourLabelOffset);
if a <= 0.0 then
Exit;
CurrentOffset := FMod(a, TextWidth + ContourLabelSpace) / 2 + ContourLabelOffset;
Result := True;
end;
function TContourLabelHelper.ContinueDrawing(): Boolean;
begin
Result := (CurrentOffset < PathLength);
end;
function TContourLabelHelper.GetCurrentOffset(): TReal;
begin
Result := CurrentOffset;
end;
procedure TContourLabelHelper.AdvancePartial(AWidth: TReal);
begin
CurrentOffset := CurrentOffset + AWidth;
end;
procedure TContourLabelHelper.AdvanceText();
begin
CurrentOffset := CurrentOffset + TextWidth;
end;
procedure TContourLabelHelper.AdvanceSpace();
begin
CurrentOffset := CurrentOffset + ContourLabelSpace;
end;
{ TMapPainter }
procedure TMapPainter.DumpDataStatistics(const AProjection: TProjection;
const AParameter: TMapParameter; const AData: TMapData);
begin
//Assert(False, 'not implemented');
{ TODO : implement }
end;
function TMapPainter.GetLabelText(AFeatureType: TFeatureType;
const AParameter: TMapParameter; const ABuffer: TFeatureValueBuffer): string;
begin
Result := ABuffer.GetFeatureValue(AFeatureType);
end;
procedure TMapPainter.PrepareNode(const AStyleConfig: TStyleConfig;
const AProjection: TProjection; const AParameter: TMapParameter;
const ANode: TMapNode);
var
IconStyle: TIconStyle;
x, y: TReal;
begin
IconStyle := AStyleConfig.GetNodeIconStyle(ANode.FeatureValueBuffer, AProjection);
FTextStyles.Clear();
AStyleConfig.GetNodeTextStyles(ANode.FeatureValueBuffer, AProjection, FTextStyles);
Transform(AProjection, AParameter, ANode.Coord, x, y);
LayoutPointLabels(AProjection,