@@ -19,8 +19,9 @@ import { deserialize } from 'flatgeobuf/lib/mjs/geojson';
1919 * @param {Object } options - 参数。
2020 * @param {function } [options.pointToLayer] - 定义点要素如何绘制在地图上。
2121 * @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 参数无效时会清空要素,重新加载 。
2323 * @param {Array } [options.extent] - 加载范围, 参数规范为: [minX, minY, maxX, maxY], 传递此参数后, 图层将使用局部加载。
24+ * @param {boolean } [options.idField='SmID'] - 是否指定要素字段作为唯一id,当 strategy 为 bbox 时生效。
2425 * @param {function } [options.featureLoader] - 要素自定义方法。
2526 * @param {function } [options.onEachFeature] - 要素创建时调用
2627 * @usage
@@ -49,6 +50,10 @@ export var FGBLayer = L.LayerGroup.extend({
4950 this . previousLayer = null ;
5051 this . loadedExtentsRtree_ = new RBush ( ) ;
5152 this . extent = this . options . extent ;
53+ this . _cacheIds = [ ] ;
54+ this . _validatedId = false ;
55+ this . _checked = false ;
56+ this . idField = this . options . idField || 'SmID' ;
5257 this . _updateFeaturesFn = this . _updateFeatures . bind ( this ) ;
5358 L . Util . setOptions ( this , options ) ;
5459 } ,
@@ -57,7 +62,7 @@ export var FGBLayer = L.LayerGroup.extend({
5762 let extent = [ ] ;
5863 if ( this . strategy === 'bbox' ) {
5964 const bounds = map . getBounds ( ) ;
60- extent = [ bounds . getWest ( ) , bounds . getSouth ( ) , bounds . getEast ( ) , bounds . getNorth ( ) ] ;
65+ extent = [ bounds . getWest ( ) , bounds . getSouth ( ) , bounds . getEast ( ) , bounds . getNorth ( ) ] ;
6166 map . on ( 'moveend' , this . _updateFeaturesFn ) ;
6267 }
6368
@@ -72,6 +77,7 @@ export var FGBLayer = L.LayerGroup.extend({
7277 this . loadedExtentsRtree_ = null ;
7378 if ( this . strategy === 'bbox' ) {
7479 map . off ( 'moveend' , this . _updateFeaturesFn ) ;
80+ this . _cacheIds = [ ] ;
7581 }
7682 } ,
7783 _updateFeatures : async function ( e ) {
@@ -82,9 +88,7 @@ export var FGBLayer = L.LayerGroup.extend({
8288 return this . _containsExtent ( object . extent , extentToLoad ) ;
8389 } ) ;
8490 if ( ! alreadyLoaded ) {
85-
8691 this . _handleFeatures ( extentToLoad ) ;
87-
8892 }
8993 } ,
9094 _handleFeatures : async function ( extent ) {
@@ -101,19 +105,33 @@ export var FGBLayer = L.LayerGroup.extend({
101105 rect . value = { extent : extent . slice ( ) } ;
102106 this . loadedExtentsRtree_ . insert ( rect ) ;
103107 }
104-
108+
105109 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+ }
109116 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+ }
110130 if ( this . options . featureLoader && typeof this . options . featureLoader === 'function' ) {
111131 feature = this . options . featureLoader ( feature ) ;
112132 }
113- curLayer . addData ( feature ) ;
133+ this . curLayer . addData ( feature ) ;
114134 }
115- this . previousLayer && this . removeLayer ( this . previousLayer ) ;
116- this . previousLayer = curLayer ;
117135 } ,
118136 async _getStream ( url ) {
119137 return await FetchRequest . get ( url , { } , { withoutFormatSuffix : true } ) . then ( function ( response ) {
0 commit comments