11/* Copyright© 2000 - 2023 SuperMap Software Co.Ltd. All rights reserved.
22 * This program are made available under the terms of the Apache License, Version 2.0
33 * which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
4- import VectorSource from 'ol/source/Vector' ;
5- import { deserialize } from 'flatgeobuf/lib/mjs/geojson' ;
6- import { buildFeature } from 'flatgeobuf/lib/mjs/generic/feature' ;
7- import { parseGeometry } from 'flatgeobuf/lib/mjs/generic/geometry' ;
8- import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest' ;
9- import { all , bbox } from 'ol/loadingstrategy' ;
10- import { getIntersection } from '@supermap/iclient-common/util/MapCalculateUtil' ;
11- import GeoJSON from 'ol/format/GeoJSON' ;
12- import clonedeep from 'lodash.clonedeep' ;
13- import { v4 as uuidv4 } from 'uuid' ;
14- /**
15- * @class FGB
16- * @browsernamespace ol.source
17- * @category Visualization FGB
18- * @classdesc FGB 图层源,该图层源把 {@link FlatGeobuf} 格式解析为点线面要素。
19- * @version 11.1.0
20- * @param {Object } opt_options - 参数。
21- * @param {string } opt_options.url - FGB 服务地址,例如:http://localhost:8090/iserver/services/xxx/rest/data/featureResults/newResourceId.fgb。
22- * @param {ol.loadingstrategy } [opt_options.strategy='bbox'] - 指定加载策略,可选值为 ol.loadingstrategy.all,ol/loadingstrategy.bbox。all为全部加载, bbox为当前可见范围加载
23- * @param {Array } [opt_options.extent] - 加载范围, 参数规范为: [minX, minY, maxX, maxY], 传递此参数后, 图层将使用局部加载。
24- * @param {function } [opt_options.featureLoader] - 要素加载回调函数
25- * @param {boolean } [opt_options.overlaps] - 是否优化重叠要素的填充与描边操作
26- * @param {boolean } [opt_options.useSpatialIndex] - 是否启用要素空间索引
27- * @param {boolean } [opt_options.wrapX] - 是否平铺地图
28- * @extends {ol.source.Vector }
29- * @usage
30- */
31- export class FGB extends VectorSource {
32- constructor ( options ) {
33- const baseOptions = Object . assign ( { strategy : bbox } , options ) ;
34- delete baseOptions . url ;
35- delete baseOptions . extent ;
36- super ( baseOptions ) ;
37- this . options = options || { } ;
38- this . strategy = baseOptions . strategy ;
39- this . url = this . options . url ;
40- this . extent = this . options . extent ;
41- this . setLoader ( async function ( extent ) {
42- if ( this . extent && this . strategy === bbox ) {
43- const intersectExtent = getIntersection ( this . extent , extent ) ;
44- extent = intersectExtent && intersectExtent . length ? intersectExtent : this . extent ;
45- }
46- if ( ! this . extent && ( this . strategy === all || ! isFinite ( extent [ 0 ] ) ) ) {
47- extent = [ ] ;
48- }
49- let fgbStream ;
50- if ( ! Object . keys ( extent ) . length ) {
51- fgbStream = await this . _getStream ( this . url ) ;
52- }
53- this . _handleFeatures ( ( fgbStream && fgbStream . body ) || this . url , extent ) ;
54- } ) ;
55- }
56-
57- async _handleFeatures ( url , extent ) {
58- let rect = { } ;
59- if ( extent && extent . length ) {
60- rect = {
61- minX : extent [ 0 ] ,
62- minY : extent [ 1 ] ,
63- maxX : extent [ 2 ] ,
64- maxY : extent [ 3 ]
65- } ;
66- }
67- let headerMeta = { } ;
68- const fgb = deserialize ( url , rect , function ( metaInfo ) {
69- headerMeta = clonedeep ( metaInfo ) ;
70- console . log ( 'meta' , headerMeta ) ;
71- } ) ;
72- for await ( let feature of fgb ) {
73- feature = new GeoJSON ( ) . readFeature ( feature ) ;
74- let properties = feature . getProperties ( ) ;
75- let columns = [ ] ;
76- headerMeta . columns . forEach ( ( column ) => {
77- if ( properties [ column . name ] ) {
78- columns . push ( column ) ;
79- }
80- } ) ;
81- headerMeta . columns = columns ;
82- let res = buildFeature (
83- parseGeometry ( feature . getGeometry ( ) , headerMeta . geometryType ) ,
84- properties ,
85- headerMeta
86- ) ;
87- console . log ( 'res' , res ) ;
88- let xxx = uuidv4 ( { } , Array . from ( res ) ) ;
89- console . log ( 'xxx' , xxx ) ;
90- if ( this . options . featureLoader && typeof this . options . featureLoader === 'function' ) {
91- feature = this . options . featureLoader ( feature ) ;
92- }
93- this . addFeature ( feature ) ;
94- }
95- }
96-
97- async _getStream ( url ) {
98- return await FetchRequest . get ( url , { } , { withoutFormatSuffix : true } ) . then ( function ( response ) {
99- return response ;
100- } ) ;
101- }
102- }
4+ import VectorSource from 'ol/source/Vector' ;
5+ import { deserialize } from 'flatgeobuf/lib/mjs/geojson' ;
6+ import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest' ;
7+ import { all , bbox } from 'ol/loadingstrategy' ;
8+ import { getIntersection } from '@supermap/iclient-common/util/MapCalculateUtil' ;
9+ import GeoJSON from 'ol/format/GeoJSON' ;
10+ /**
11+ * @class FGB
12+ * @browsernamespace ol.source
13+ * @category Visualization FGB
14+ * @classdesc FGB 图层源,该图层源把 {@link FlatGeobuf} 格式解析为点线面要素。
15+ * @version 11.1.0
16+ * @param {Object } opt_options - 参数。
17+ * @param {string } opt_options.url - FGB 服务地址,例如:http://localhost:8090/iserver/services/xxx/rest/data/featureResults/newResourceId.fgb。
18+ * @param {ol.loadingstrategy } [opt_options.strategy='bbox'] - 指定加载策略,可选值为 ol.loadingstrategy.all,ol/loadingstrategy.bbox。all为全部加载, bbox为当前可见范围加载
19+ * @param {Array } [opt_options.extent] - 加载范围, 参数规范为: [minX, minY, maxX, maxY], 传递此参数后, 图层将使用局部加载。
20+ * @param {function } [opt_options.featureLoader] - 要素加载回调函数
21+ * @param {boolean } [opt_options.overlaps] - 是否优化重叠要素的填充与描边操作
22+ * @param {boolean } [opt_options.useSpatialIndex] - 是否启用要素空间索引
23+ * @param {boolean } [opt_options.wrapX] - 是否平铺地图
24+ * @extends {ol.source.Vector }
25+ * @usage
26+ */
27+ export class FGB extends VectorSource {
28+ constructor ( options ) {
29+ const baseOptions = Object . assign ( { strategy : bbox } , options ) ;
30+ delete baseOptions . url ;
31+ delete baseOptions . extent ;
32+ super ( baseOptions ) ;
33+ this . options = options || { } ;
34+ this . strategy = baseOptions . strategy ;
35+ this . url = this . options . url ;
36+ this . extent = this . options . extent ;
37+ this . setLoader ( async function ( extent ) {
38+ if ( this . extent && this . strategy === bbox ) {
39+ const intersectExtent = getIntersection ( this . extent , extent ) ;
40+ extent = ( intersectExtent && intersectExtent . length ) ? intersectExtent : this . extent ;
41+ }
42+ if ( ! this . extent && ( this . strategy === all || ! isFinite ( extent [ 0 ] ) ) ) {
43+ extent = [ ] ;
44+ }
45+ let fgbStream ;
46+ if ( ! Object . keys ( extent ) . length ) {
47+ fgbStream = await this . _getStream ( this . url ) ;
48+ }
49+ this . _handleFeatures ( ( fgbStream && fgbStream . body ) || this . url , extent ) ;
50+ } ) ;
51+ }
52+
53+ async _handleFeatures ( url , extent ) {
54+ let rect = { } ;
55+ if ( extent && extent . length ) {
56+ rect = {
57+ minX : extent [ 0 ] ,
58+ minY : extent [ 1 ] ,
59+ maxX : extent [ 2 ] ,
60+ maxY : extent [ 3 ]
61+ } ;
62+ }
63+ const fgb = deserialize ( url , rect ) ;
64+ for await ( let feature of fgb ) {
65+ feature = new GeoJSON ( ) . readFeature ( feature ) ;
66+ if ( this . options . featureLoader && typeof this . options . featureLoader === 'function' ) {
67+ feature = this . options . featureLoader ( feature ) ;
68+ }
69+ this . addFeature ( feature ) ;
70+ }
71+ }
72+
73+ async _getStream ( url ) {
74+ return await FetchRequest . get ( url , { } , { withoutFormatSuffix : true } ) . then ( function ( response ) {
75+ return response ;
76+ } ) ;
77+ }
78+ }
0 commit comments