@@ -19,8 +19,9 @@ import { deserialize } from 'flatgeobuf/lib/mjs/geojson';
19
19
* @param {Object } options - 参数。
20
20
* @param {function } [options.pointToLayer] - 定义点要素如何绘制在地图上。
21
21
* @param {function } [options.style] - 定义点、线、面要素样式。参数为{@link L.Path-option}。
22
- * @param {string } [options.strategy='bbox'] - 指定加载策略,可选值为 all,bbox。 all为全量加载, bbox为按需加载 。
22
+ * @param {string } [options.strategy='bbox'] - all为全量加载,要素会以流的方式渲染到地图。 bbox为当前可见范围加载,当地图范围改变时会重新加载要素,此时可以通过idField 参数来标识已被加载过的要素,被标识的要素无需再次加载。idField 参数无效时会清空要素,重新加载 。
23
23
* @param {Array } [options.extent] - 加载范围, 参数规范为: [minX, minY, maxX, maxY], 传递此参数后, 图层将使用局部加载。
24
+ * @param {boolean } [options.idField='SmID'] - 是否指定要素字段作为唯一id,当 strategy 为 bbox 时生效。
24
25
* @param {function } [options.featureLoader] - 要素自定义方法。
25
26
* @param {function } [options.onEachFeature] - 要素创建时调用
26
27
* @usage
@@ -49,6 +50,10 @@ export var FGBLayer = L.LayerGroup.extend({
49
50
this . previousLayer = null ;
50
51
this . loadedExtentsRtree_ = new RBush ( ) ;
51
52
this . extent = this . options . extent ;
53
+ this . _cacheIds = [ ] ;
54
+ this . _validatedId = false ;
55
+ this . _checked = false ;
56
+ this . idField = this . options . idField || 'SmID' ;
52
57
this . _updateFeaturesFn = this . _updateFeatures . bind ( this ) ;
53
58
L . Util . setOptions ( this , options ) ;
54
59
} ,
@@ -57,7 +62,7 @@ export var FGBLayer = L.LayerGroup.extend({
57
62
let extent = [ ] ;
58
63
if ( this . strategy === 'bbox' ) {
59
64
const bounds = map . getBounds ( ) ;
60
- extent = [ bounds . getWest ( ) , bounds . getSouth ( ) , bounds . getEast ( ) , bounds . getNorth ( ) ] ;
65
+ extent = [ bounds . getWest ( ) , bounds . getSouth ( ) , bounds . getEast ( ) , bounds . getNorth ( ) ] ;
61
66
map . on ( 'moveend' , this . _updateFeaturesFn ) ;
62
67
}
63
68
@@ -72,6 +77,7 @@ export var FGBLayer = L.LayerGroup.extend({
72
77
this . loadedExtentsRtree_ = null ;
73
78
if ( this . strategy === 'bbox' ) {
74
79
map . off ( 'moveend' , this . _updateFeaturesFn ) ;
80
+ this . _cacheIds = [ ] ;
75
81
}
76
82
} ,
77
83
_updateFeatures : async function ( e ) {
@@ -82,9 +88,7 @@ export var FGBLayer = L.LayerGroup.extend({
82
88
return this . _containsExtent ( object . extent , extentToLoad ) ;
83
89
} ) ;
84
90
if ( ! alreadyLoaded ) {
85
-
86
91
this . _handleFeatures ( extentToLoad ) ;
87
-
88
92
}
89
93
} ,
90
94
_handleFeatures : async function ( extent ) {
@@ -101,19 +105,33 @@ export var FGBLayer = L.LayerGroup.extend({
101
105
rect . value = { extent : extent . slice ( ) } ;
102
106
this . loadedExtentsRtree_ . insert ( rect ) ;
103
107
}
104
-
108
+
105
109
const fgb = deserialize ( ( fgbStream && fgbStream . body ) || this . url , rect ) ;
106
-
107
- let curLayer = L . geoJSON ( null , this . options ) ;
108
- curLayer . addTo ( this ) ;
110
+ if ( ! this . _validatedId ) {
111
+ this . curLayer = L . geoJSON ( null , this . options ) ;
112
+ this . previousLayer && this . removeLayer ( this . previousLayer ) ;
113
+ this . previousLayer = this . curLayer ;
114
+ this . curLayer . addTo ( this ) ;
115
+ }
109
116
for await ( let feature of fgb ) {
117
+ if ( this . strategy === 'bbox' ) {
118
+ let id = feature . properties [ this . idField ] ;
119
+ if ( id && ! this . _validatedId ) {
120
+ this . _validatedId = true ;
121
+ this . _checked = true ;
122
+ }
123
+ if ( id && this . _checked ) {
124
+ if ( this . _cacheIds . includes ( id ) ) {
125
+ continue ;
126
+ }
127
+ this . _cacheIds . push ( id ) ;
128
+ }
129
+ }
110
130
if ( this . options . featureLoader && typeof this . options . featureLoader === 'function' ) {
111
131
feature = this . options . featureLoader ( feature ) ;
112
132
}
113
- curLayer . addData ( feature ) ;
133
+ this . curLayer . addData ( feature ) ;
114
134
}
115
- this . previousLayer && this . removeLayer ( this . previousLayer ) ;
116
- this . previousLayer = curLayer ;
117
135
} ,
118
136
async _getStream ( url ) {
119
137
return await FetchRequest . get ( url , { } , { withoutFormatSuffix : true } ) . then ( function ( response ) {
0 commit comments