-
Notifications
You must be signed in to change notification settings - Fork 23
/
style_layer.ts
1191 lines (972 loc) · 36.6 KB
/
style_layer.ts
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
import type { StyleManager, Source } from "./style_manager";
import type { FlakeContext } from "./style_flake";
import type { FeatureState as ExprFeatureState,
Context as GlobalContext } from "./expression";
import { GeomType } from "./expression";
import type { Specification as PropSpec } from "./property";
import { Property } from "./property";
import type { TileLayer, Feature } from "./tile_layer";
import type { Vector4 } from "../GeoMath";
import type Primitive from "../Primitive";
import type { OJson } from "../util/json_type";
import { isObject as json_isObject } from "../util/json_type";
import { cfa_assert } from "../util/assertion";
/**
* ベクトル地図スタイルのレイヤーを表現する。
*
* このクラスのインスタンスは [[StyleManager]] インスタンスが保有する。
*
* @privateRemarks
*
* 各スタイルレイヤー型の実装では [[StyleLayer]], [[LayerFlake]],
* [[LayerFeature]] の具象クラスをセットで定義する。
*
* これらインスタンス間の動的関係は `vectile.asta` のシーケンス図
* "LayerFlake 初期評価" と "LayerFlake 再評価" を参照のこと。
*/
export abstract class StyleLayer {
/**
* このレイヤーの起源である [[StyleManager]] インスタンス
*/
public readonly style_manager: StyleManager;
/**
* スタイルに記述されたレイヤーの ID
*
* mapbox の
* [Layers](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/)
* の
* [id](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#id)
* プロパティと同じ文字列が設定される。
*/
public readonly id: string;
/**
* スタイルに記述されたレイヤーの型
*
* mapbox の
* [Layers](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/)
* の
* [type](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#id)
* プロパティと同じ文字列が設定される。
*/
public readonly type: string;
/**
* レイヤーが使用するソースのオブジェクト
*
* @internal
*/
public readonly __source_inst: Source;
/**
* レイヤーが使用するタイル内のレイヤー名
*
* @internal
*/
public readonly __source_layer: string;
/**
* レイヤーの `filter` プロパティ
*
* @internal
*/
public readonly __filter: Property;
/**
* レイヤーの `minzoom` プロパティ
*
* @internal
*/
public readonly __minzoom: Property;
/**
* レイヤーの `maxzoom` プロパティ
*
* @internal
*/
public readonly __maxzoom: Property;
/**
* `LayerFlake` インスタンス単位で変化するプロパティに関するデータ
*
* @internal
*/
public readonly __flake_property_data: FlakePropertyData;
/**
* フィーチャ単位で変化するプロパティに関するデータ
*
* @internal
*/
public readonly __feature_property_data: FeaturePropertyData;
/**
* インスタンスの初期化する。
*
* @internal
*/
protected constructor( owner: StyleManager,
json_layer: LayerJson,
prop_specs: Iterable<PropSpec> )
{
const source_id = json_layer['source'];
if ( typeof source_id !== 'string' ) {
throw new SyntaxError( "Vector tile layer requires a 'source' property." );
}
const source_inst = owner.__getSource( source_id );
if ( !source_inst ) {
throw new Error( `No source instance with name '${source_id}' found.` );
}
const source_layer = json_layer['source-layer'];
if ( typeof source_layer !== 'string' ) {
throw new SyntaxError( "Vector tile layer requires a 'source-layer' property." );
}
const json_layout = json_layer['layout'] || {};
if ( !json_isObject( json_layout ) ) {
throw new SyntaxError( "Layer's 'layout' property is invalid type." );
}
const json_paint = json_layer['paint'] || {};
if ( !json_isObject( json_paint ) ) {
throw new SyntaxError( "Layer's 'paint' property is invalid type." );
}
const visibility = json_layout['visibility'] || 'visible';
const filter_pspec: PropSpec = {
name: 'filter',
category: 'layout',
value_type: 'boolean',
default_value: true
};
const global_context: GlobalContext = {
zoom: 0, // この時点でズームは決定できない
image_names: owner.__image_manager.getImageNames(),
}
const filter = new Property( global_context, filter_pspec, json_layer['filter'] );
if ( filter.hasFState() ) {
// filter プロパティの評価値はフィーチャ状態の変化を想定しない
throw new Error( "Layer's 'filter' property cannot use the 'feature-state' operator." );
}
const minzoom_pspec: PropSpec = {
name: 'minzoom',
category: 'paint',
value_type: 'number',
default_value: 0
};
const maxzoom_pspec: PropSpec = {
name: 'maxzoom',
category: 'paint',
value_type: 'number',
default_value: 24
};
const minzoom = new Property( global_context, minzoom_pspec, json_layer['minzoom'] );
const maxzoom = new Property( global_context, maxzoom_pspec, json_layer['maxzoom'] );
const properties = new Map<string, Property>();
const evaluated_value_cache = new Map<Property, unknown>();
const flake_property_data: FlakePropertyData = {
cached_properties: [],
cached_paint_properties: [],
cached_layout_properties: [],
}
const feature_property_data: FeaturePropertyData = {
cached_properties: [],
cached_paint_properties: [],
cached_layout_properties: [],
}
for ( const prop_spec of prop_specs ) {
// layout または paint からプロパティ値を取得
const json_prop = (prop_spec.category === 'layout') ?
json_layout[prop_spec.name] : json_paint[prop_spec.name];
const property = new Property( global_context, prop_spec, json_prop );
if ( property.isLayoutType() && property.hasFState() ) {
// layout 内のプロパティの評価値はフィーチャ状態の変化を想定しない
throw new Error( "Layer's '${property.name}' property cannot use the 'feature-state' operator." );
}
// プロパティを登録
properties.set( prop_spec.name, property );
if ( property.hasFData() || property.hasFState() ) {
// データ式 (fdata=1 or fstate=1)
feature_property_data.cached_properties.push( property );
// fdata=1, fstate=0, zoom=0 のプロパティは除外
const is_excluded = property.hasFData() && !property.hasFState() && !property.hasZoom();
if ( !is_excluded ) {
if ( property.isLayoutType() ) {
feature_property_data.cached_layout_properties.push( property );
}
else {
feature_property_data.cached_paint_properties.push( property );
}
}
}
else {
// 非データ式 (fdata=0, fstate=0)
if ( property.hasZoom() ) {
// 非データ式で zoom=1
flake_property_data.cached_properties.push( property );
if ( property.isLayoutType() ) {
flake_property_data.cached_layout_properties.push( property );
}
else {
flake_property_data.cached_paint_properties.push( property );
}
}
else {
// 非データ式で zoom=0 (定数)
// 定数値のプロパティと値を登録
evaluated_value_cache.set( property, property.evaluate( global_context ) );
}
}
}
this.style_manager = owner;
this.id = json_layer['id'];
this.type = json_layer['type'];
this.__source_inst = source_inst;
this.__source_layer = source_layer;
this.__filter = filter;
this.__minzoom = minzoom;
this.__maxzoom = maxzoom;
this.__flake_property_data = flake_property_data;
this.__feature_property_data = feature_property_data;
this._evaluated_value_cache = evaluated_value_cache;
this._visibility = (visibility === 'visible');
this._properties = properties;
}
/**
* レイヤーの可視性
*/
get visibility(): boolean { return this._visibility; }
/**
* レイヤーの可視性
*/
set visibility( value: boolean )
{
this._visibility = value;
}
/**
* `id` が示すプロパティを取得する。
*
* [[StyleLayer]] のサブクラスの実装者が呼び出す。
*
* @internal
*/
public __getProperty( id: string ): Property
{
const item = this._properties.get( id );
if ( item !== undefined ) {
return item;
}
else {
throw new Error( `unexpected property '${id}'` );
}
}
/**
* すべてのプロパティを取得する。
*
* @internal
*/
public __getProperties(): IterableIterator<Property>
{
return this._properties.values();
}
/**
* 定数プロパティの値を取得する。
*
* 定数プロパティ `property` の評価済みの値を取得する。
*
* `property` が定数プロパティでなければ `undefined` を返す。
*
* @internal
*/
public __getConstantValue( property: Property ): unknown | undefined
{
return this._evaluated_value_cache.get( property );
}
/**
* 引数に対応する [[LayerFlake]] インスタンスを生成する。
*
* style フレームワークが呼び出す。
*
* @internal
*/
public abstract __createLayerFlake( tlie_layer: TileLayer,
flake_ctx: FlakeContext ): LayerFlake;
/**
* 対応できるフィーチャのジオメトリ型を確認する。
*
* 現在のプロパティ定数値で `geom_type` 型のフィーチャを描画できる
* かどうかを確認する。
*
* 定数プロパティによって結果を決めるので、途中で結果が変わること
* は想定していない。
*
* style フレームワークが呼び出す。
*
* @internal
*/
public abstract __isSupported( geom_type: GeomType ): boolean;
/**
* レイヤーの可視性
*
* 初期値は `visibility` プロパティの値になる。
*/
private _visibility: boolean;
/**
* プロパティ辞書
*
* レイヤーの全プロパティ (`filter` プロパティは除く) をプロパティ
* 名と対応付ける。
*/
private readonly _properties: Map<string, Property>;
/**
* 定数プロパティの値
*
* `StyleLayer` 単位で評価値が決定する式を持つプロパティの評価値
* キャッシュである。
*/
private readonly _evaluated_value_cache: Map<Property, unknown>;
}
/**
* Flake インスタンス内のベクトル地図の特定レイヤー部分を表現する。
*
* [[StyleLayer.__createLayerFlake]] メソッドによりサブクラスのインス
* タンスが生成される。
*
* このクラスは [[StyleLayer]] のサブクラスの実装者が使用する。
*
* @typeParam StyleLayerT - 生成元のインスタンスの [[StyleLayer]] サブクラス
*
* @internal
*/
export abstract class LayerFlake<StyleLayerT extends StyleLayer = StyleLayer> {
/**
* 親のスタイルレイヤー
*/
public readonly style_layer: StyleLayerT;
/**
* フィルター済みフィーチャ
*/
protected readonly _layer_features: Map<Feature, LayerFeature>;
/**
* 前回の評価時のズーム値
*
* style フレームワークが参照する。
*
* @internal
*/
public __last_zoom: number;
/**
* LayerFlake サブクラスから呼び出して初期化する。
*
* 実際には初期化はフレームワークが [[completeInitialization]] を
* 呼び出した後に完了する。
*/
protected constructor( style_layer: StyleLayerT,
tile_layer: TileLayer,
flake_ctx: FlakeContext )
{
this.style_layer = style_layer;
this._layer_features = new Map();
this.__last_zoom = flake_ctx.zoom;
this._tile_layer = tile_layer;
this._evaluated_value_cache = new Map();
// 初期の評価値を evaluated_value_cache にキャッシュする
for ( const property of style_layer.__flake_property_data.cached_properties ) {
const value = property.evaluate( flake_ctx );
this._evaluated_value_cache.set( property, value );
}
}
/**
* インスタンスを破棄
*
* このメソッドを呼び出した後は `this` にアクセスすることができない。
*
* @virtual
*/
public dispose(): void
{
for ( const layer_feature of this._layer_features.values() ) {
layer_feature.dispose();
}
}
/**
* 初期化時のフィーチャを生成
*
* `this._layer_features` に、初期評価のフィーチャを設定する。
*
* style フレームワークが呼び出す。
*
* @internal
*/
public __createInitialLayerFeatures( flake_ctx: FlakeContext ): void
{
const slayer = this.style_layer;
/**
* `tile_features` の要素毎に `LayerFeature` インスタンスを生成
*/
const create_features = ( tile_features: Iterable<Feature> ) =>
{
const filter = slayer.__filter;
if ( filter.hasFData() ) {
// フィーチャ毎にフィルタを評価
for ( const feature of tile_features ) {
if ( filter.evaluate( flake_ctx, feature ) as boolean ) {
const layer_feature = this.createLayerFeature( feature, flake_ctx );
this._layer_features.set( feature, layer_feature );
}
}
}
else {
// すべてのフィーチャが合格
if ( filter.evaluate( flake_ctx ) as boolean ) {
for ( const feature of tile_features ) {
const layer_feature = this.createLayerFeature( feature, flake_ctx );
this._layer_features.set( feature, layer_feature );
}
}
}
};
if ( slayer.__isSupported( GeomType.POINT ) ) {
create_features( this._tile_layer.point_features );
}
if ( slayer.__isSupported( GeomType.LINESTRING ) ) {
create_features( this._tile_layer.linestring_features );
}
if ( slayer.__isSupported( GeomType.POLYGON ) ) {
create_features( this._tile_layer.polygon_features );
}
}
/**
* [[LayerFeature]] の具象クラスのインスタンスを生成する。
*
* style フレームワークから呼び出される。
*
* @param feature - 基となるフィーチャ
* @param layer_flake - 生成されるインスタンスの所有者
* @param flake_ctx - コンテキスト
*/
public abstract createLayerFeature( feature: Feature,
flake_ctx: FlakeContext ): LayerFeature;
/**
* インスタンスの初期化を完了させる。
*
* style フレームワークから呼び出される。
*/
public abstract completeInitialization( flake_ctx: FlakeContext ): void;
/**
* フィーチャのフィルタと評価を行う。
*
* style フレームワークが呼び出す。
*
* @internal
*/
public __evaluateFeatures( flake_ctx: FlakeContext ): void
{
// 評価の開始を通知
const finishEvaluation = this.startEvaluation( flake_ctx );
const summary: EvaluationSummary = {
added_features: new Set(),
deleted_features: new Set(),
evaluated_properties: new Set(),
};
// LayerFlake 単位の評価
this.update_evaluated_value_cache( flake_ctx, summary );
// フィルターの評価
this.filterFeatures( flake_ctx, summary );
// LayerFeature 単位の評価
for ( const layer_feature of this._layer_features.values() ) {
layer_feature.__updateEvaluatedValueCache( flake_ctx, summary );
}
if ( this._layer_features.size === 0 ) {
// 評価値が変化したフィーチャそのものが存在しないので、評
// 価したプロパティも存在しないようにする
summary.evaluated_properties.clear();
}
// 評価の終了を通知
finishEvaluation( summary );
// フィルターで追加された LayerFeature インスタンスを実際
// に this._layer_features に追加
for ( const layer_feature of summary.added_features ) {
this._layer_features.set( layer_feature.feature, layer_feature );
}
// 削除された LayerFeature インスタンスの破棄
for ( const layer_feature of summary.deleted_features ) {
layer_feature.dispose();
}
// 前回ズーム値を更新
this.__last_zoom = flake_ctx.zoom;
}
/**
* 評価の開始をするときに呼び出される。
*
* 評価が終了したときに、戻り値の関数が呼び出される。
*
* @see [[EvaluationSummary]]
*/
public abstract startEvaluation( flake_ctx: FlakeContext ): EvaluationListener;
/**
* 描画するプリミティブ配列を取得する。
*
* style フレームワークが呼び出される。
*
* @see [[StyleFlake.getPrimitives]]
*/
public abstract getPrimitives( flake_ctx: FlakeContext ): Primitive[];
/**
* 自己の評価済みのプロパティ値を取得
*
* 自己のキャッシュが持っている `property` の評価値を返す。
*
* 自己にプロパティが存在しなければ `undefined` を返す。
*/
public getOwnEvaluatedValue( property: Property ): unknown
{
return this._evaluated_value_cache.get( property );
}
/**
* プロパティ `_evaluated_value_cache` を更新して、結果を `summary`
* に記録する。
*
* @see [[__evaluateFeatures]]
*/
private update_evaluated_value_cache( flake_ctx: FlakeContext,
summary: EvaluationSummary ): void
{
if ( flake_ctx.zoom === this.__last_zoom ) {
// プロパティの評価値は変化しない
return;
}
const prop_data = this.style_layer.__flake_property_data;
for ( const property of prop_data.cached_paint_properties ) {
const value = property.evaluate( flake_ctx );
this._evaluated_value_cache.set( property, value );
}
if ( Math.round( flake_ctx.zoom ) === Math.round( this.__last_zoom ) ) {
// layout プロパティの評価値は変化しない
return;
}
// さらにズーム丸め値が変化した
for ( const property of prop_data.cached_layout_properties ) {
const value = property.evaluate( flake_ctx );
this._evaluated_value_cache.set( property, value );
summary.evaluated_properties.add( property );
}
}
/**
* フィーチャのフィルター処理
*
* `this._layer_features` に表示されるフィーチャに対応したインスタ
* ンスが設定される。
*
* @see [[createInitailFeatures]]
*/
private filterFeatures( flake_ctx: FlakeContext,
summary: EvaluationSummary ): void
{
const slayer = this.style_layer;
const filter = slayer.__filter;
if ( !filter.hasZoom() ) {
// fstate=1 は想定していないので zoom=0 のとき、すべての合否は変わらない
return;
}
if ( Math.round( flake_ctx.zoom ) === Math.round( this.__last_zoom ) ) {
// filter のズームは layout のよう低頻度で評価する
return;
}
/**
* `tile_features` の要素毎に `LayerFeature` インスタンスの生
* 成と削除を行う。
*/
const filter_features = ( tile_features: Iterable<Feature> ) =>
{
const layer_feature_map = this._layer_features;
if ( filter.hasFData() ) {
// zoom=1, fdata=1
for ( const tile_feature of tile_features ) {
const layer_feature = layer_feature_map.get( tile_feature );
if ( filter.evaluate( flake_ctx, tile_feature ) as boolean ) {
if ( layer_feature === undefined ) {
// feature は不合格から合格に変わった
const new_feature = this.createLayerFeature( tile_feature, flake_ctx );
summary.added_features.add( new_feature );
}
}
else if ( layer_feature !== undefined ) {
// feature は合格から不合格に変わった
summary.deleted_features.add( layer_feature );
layer_feature_map.delete( tile_feature )
}
}
}
else {
// zoom=1, fdata=0
if ( filter.evaluate( flake_ctx ) as boolean ) {
// すべてのフィーチャが合格
for ( const tile_feature of tile_features ) {
if ( !layer_feature_map.has( tile_feature ) ) {
// feature は不合格から合格に変わった
const new_feature = this.createLayerFeature( tile_feature, flake_ctx );
summary.added_features.add( new_feature );
}
}
}
else {
// すべてのフィーチャが不合格
for ( const tile_feature of tile_features ) {
const layer_feature = layer_feature_map.get( tile_feature );
if ( layer_feature !== undefined ) {
// feature は合格から不合格に変わった
summary.deleted_features.add( layer_feature );
layer_feature_map.delete( tile_feature )
}
}
}
}
};
if ( slayer.__isSupported( GeomType.POINT ) ) {
filter_features( this._tile_layer.point_features );
}
if ( slayer.__isSupported( GeomType.LINESTRING ) ) {
filter_features( this._tile_layer.linestring_features );
}
if ( slayer.__isSupported( GeomType.POLYGON ) ) {
filter_features( this._tile_layer.polygon_features );
}
}
/**
* this に対応するタイルのレイヤー
*/
private readonly _tile_layer: TileLayer;
/**
* プロパティの評価値キャッシュ
*
* 登録されているプロパティは `LayerFlake` 単位で評価値が決定する
* 式を持つ。
*/
private readonly _evaluated_value_cache: Map<Property, unknown>;
}
/**
* フィルター済みのフィーチャを表現する。
*
* このクラスは [[StyleLayer]] のサブクラスの実装者が使用する。
*
* @typeParam LayerFlakeT - インスタンスを生成する [[LayerFlake]] クラス
* @typeParam FeatureT - インスタンスに対応する [[Feature]] クラス
*
* @internal
*/
export abstract class LayerFeature<LayerFlakeT extends LayerFlake = LayerFlake,
FeatureT extends Feature = Feature> {
/**
* `this` に対応する [[TileLayer]] インスタンス内の [[Feature]]
* インスタンス
*/
public readonly feature: FeatureT;
/**
* `this` を保有する [[LayerFlake]] インスタンス
*/
public readonly layer_flake: LayerFlakeT;
/**
* インスタンスを初期化する。
*
* `feature` と `layer_flake` パラメータは同名のプロパティの説明を
* 参照のこと。
*
* `flake_ctx` は `this` 生成時のコンテキストである。
*/
protected constructor( feature: FeatureT,
layer_flake: LayerFlakeT,
flake_ctx: FlakeContext )
{
// 注意: `layer_flake` は構築中の可能性があるため、ここでは
// `layer_flake.style_layer` の参照しか許されない
this.feature = feature;
this.layer_flake = layer_flake;
this._evaluated_value_cache = new Map();
const style_layer = layer_flake.style_layer;
const style_manager = style_layer.style_manager;
const prop_data = style_layer.__feature_property_data;
// 最初にすべてのデータ式のプロパティを評価
for ( const property of prop_data.cached_properties ) {
let expr_fstate: ExprFeatureState | undefined;
// property が feature-state 演算を含み、feature の
// FeatureState インスタンスが存在すれば expr_fstate に設
// 定する
if ( property.hasFState() ) {
expr_fstate = style_manager.__findExprFeatureState( feature.id );
}
const value = property.evaluate( flake_ctx, feature, expr_fstate );
this._evaluated_value_cache.set( property, value );
}
}
/**
* インスタンスを破棄
*
* デフォルト実装は何もしない。
*
* このメソッドを呼び出した後は `this` にアクセスすることができない。
*
* @virtual
*/
public dispose(): void {}
/**
* 評価済みのプロパティ値を取得
*
* プロパティ `property` の評価値を返す。
*
* プロパティが存在しなければ `undefined` を返す。
*/
public getEvaluatedValue( property: Property ): unknown
{
const own_value = this._evaluated_value_cache.get( property );
if ( own_value !== undefined ) {
return own_value;
}
const flake_value = this.layer_flake.getOwnEvaluatedValue( property );
if ( flake_value !== undefined ) {
return flake_value;
}
return this.layer_flake.style_layer.__getConstantValue( property );
}
/**
* 評価済みの色プロパティ値を取得
*
* 色プロパティ `property` の評価値を `Color` に変換して返す。
*
* 返される R, G, B 値にはそれぞれ A が乗算 (α前乗算) されている。
*
* `property` は存在しなければならない。
*
* @typeParam Color - 取得先の色の型
* @param property - 色として評価されるプロパティ
* @param dst - 取得先のインスタンス
*
* @return `dst`
*/
public getEvaluatedColor<Color extends Vector4>( property: Property,
dst: Color ): Color
{
const value = this.getEvaluatedValue( property );
cfa_assert( value !== undefined ); // 存在することが前提
// color の r, g, b は a が乗算された値になっている。
//
// 参照: mapbox-gl/src/style-spec/util/color.js
const color = value as { r: number, g: number, b: number, a: number }
dst[0] = color.r;
dst[1] = color.g;
dst[2] = color.b;
dst[3] = color.a;
return dst;
}
/**
* このフィーチャの [[_evaluated_value_cache]] の内容を更新
*
* 評価値が変化する可能性があるプロパティのみを評価して、評価値キャッ
* シュを更新する。
*
* style フレームワークから呼び出される。
*
* @internal
*/
public __updateEvaluatedValueCache( flake_ctx: FlakeContext,
summary: EvaluationSummary ): void
{
const layer_flake = this.layer_flake;
const style_layer = layer_flake.style_layer;
const style_manager = style_layer.style_manager;
const prop_data = style_layer.__feature_property_data;
const mods_zoom = (flake_ctx.zoom !== layer_flake.__last_zoom);
// paint プロパティ
for ( const property of prop_data.cached_paint_properties ) {
// fstate=1 or (fdata=1, zoom=1)
if ( property.hasFState() ) {
// fdata=?, fstate=1
const expr_fstate = style_manager.__findExprFeatureState( this.feature.id );
const value = property.evaluate( flake_ctx, this.feature, expr_fstate );
this._evaluated_value_cache.set( property, value );
}
else {
// fdata=1, fstate=0, zoom=1
if ( mods_zoom ) {
const value = property.evaluate( flake_ctx, this.feature );
this._evaluated_value_cache.set( property, value );
}
}
}
// layout プロパティ
const mods_izoom = (Math.round( flake_ctx.zoom ) !== Math.round( layer_flake.__last_zoom ));
if ( !mods_izoom ) {
// 整数 zoom が変化しないときは layout プロパティの評価値
// は変化しない
return;
}
for ( const property of prop_data.cached_layout_properties ) {
// fdata=1, fstate=0, zoom=1
const value = property.evaluate( flake_ctx, this.feature );
this._evaluated_value_cache.set( property, value );
// TODO: 重複設定
summary.evaluated_properties.add( property );
}
}
/**
* プロパティの評価値キャッシュ
*
* フィーチャ単位で違う値に評価される可能性があるプロパティの評価
* 値はここにキャッシュされる。
*/
private readonly _evaluated_value_cache: Map<Property, unknown>;
}
/**
* 非対応のレイヤー型に使用される仮のレイヤーである。
*/
export class UnsupportedLayer extends StyleLayer {
constructor( owner: StyleManager,
json_layer: LayerJson )
{
super( owner, json_layer, [] );
}
// from StyleLayer
override __createLayerFlake( tlie_layer: TileLayer,
flake_ctx: FlakeContext ): LayerFlake
{
return new UnsupportedFlake( this, tlie_layer, flake_ctx );
}
// from StyleLayer
override __isSupported()
{
return false;
}