Skip to content

Commit 824112e

Browse files
committed
【feature】L7layer添加restdata优化
1 parent e401536 commit 824112e

File tree

3 files changed

+417
-31
lines changed

3 files changed

+417
-31
lines changed

src/mapboxgl/mapping/utils/L7LayerUtil.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,14 @@ function addCredentialToUrl(url, credential) {
190190
* @description 获取数据集表头信息
191191
* @param {string} datasetUrl 数据集地址
192192
* @param {Object} credential
193-
* @param {boolean} withCredentials
193+
* @param {Object} options
194194
*/
195-
function getRestDataFields(datasetUrl, credential, withCredentials) {
195+
function getRestDataFields(datasetUrl, credential, options) {
196196
const url = addCredentialToUrl(`${datasetUrl}/fields.json?returnAll=true`, credential);
197-
return FetchRequest.get(url, {
198-
withCredentials
199-
})
197+
return FetchRequest.get(url, null, options)
200198
.then((res) => res.json())
201199
.then((result) => {
202-
return result.data.map((item) => {
200+
return result.map((item) => {
203201
const { caption, name, isSystemField, type, maxLength, isZeroLengthAllowed } = item;
204202
return {
205203
name,
@@ -218,13 +216,11 @@ function getRestDataFields(datasetUrl, credential, withCredentials) {
218216
* @description 获取值域
219217
* @param {string} datasetUrl
220218
* @param {Object} credential
221-
* @param {boolean} withCredentials
219+
* @param {Object} options
222220
*/
223-
function getRestDataDomains(datasetUrl, credential, withCredentials) {
221+
function getRestDataDomains(datasetUrl, credential, options) {
224222
const url = addCredentialToUrl(`${datasetUrl}/domain.json`, credential);
225-
return FetchRequest.get(url, {
226-
withCredentials
227-
}).then((result) => {
223+
return FetchRequest.get(url, null, options).then((result) => {
228224
return result.json();
229225
});
230226
}
@@ -246,10 +242,10 @@ function sortRestdataField(fieldInfos) {
246242
return systemFields.concat(nonsystemFields);
247243
}
248244

249-
async function requestRestDataFieldsInfo(datasetUrl, credential, withCredentials) {
245+
async function requestRestDataFieldsInfo(datasetUrl, credential, options) {
250246
const [fields, domains] = await Promise.all([
251-
getRestDataFields(datasetUrl, credential, withCredentials),
252-
getRestDataDomains(datasetUrl, credential, withCredentials)
247+
getRestDataFields(datasetUrl, credential, options),
248+
getRestDataDomains(datasetUrl, credential, options)
253249
]);
254250
domains.forEach((domain) => {
255251
const { fieldName, type, rangeInfos, codeInfos } = domain;
@@ -274,8 +270,8 @@ async function requestRestDataFieldsInfo(datasetUrl, credential, withCredentials
274270
* @param credential
275271
* @param withCredentials
276272
*/
277-
async function getRestDataFieldInfo(datasetUrl, credential, withCredentials) {
278-
const fieldsInfos = await requestRestDataFieldsInfo(datasetUrl, credential, withCredentials);
273+
async function getRestDataFieldInfo(datasetUrl, credential, options) {
274+
const fieldsInfos = await requestRestDataFieldsInfo(datasetUrl, credential, options);
279275
return {
280276
fieldNames: fieldsInfos.map((el) => el.name),
281277
fieldTypes: fieldsInfos.map((el) => el.type)
@@ -392,12 +388,9 @@ async function getRestDataGeojsonByWebMap(data, options) {
392388
targetEpsgCode: '4326',
393389
queryParameter: { name: datasetName + '@' + dataSourceName }
394390
};
395-
const SQLConfig = {
396-
withCredentials: options.withCredentials
397-
};
398391
const datasetUrl = `${url.split('featureResults')[0]}datasources/${dataSourceName}/datasets/${datasetName}`;
399-
const { fieldNames, fieldTypes } = await getRestDataFieldInfo(datasetUrl, credential, options.withCredentials);
400-
const attrDataInfo = await FetchRequest.post(url, SQLParams, SQLConfig);
392+
const { fieldNames, fieldTypes } = await getRestDataFieldInfo(datasetUrl, credential, options);
393+
const attrDataInfo = await FetchRequest.post(url, JSON.stringify(SQLParams), options);
401394
const featuresRes = await attrDataInfo.json();
402395

403396
return {
@@ -409,9 +402,10 @@ async function getRestDataGeojsonByWebMap(data, options) {
409402
/**
410403
* 获取单条item
411404
* @param href
405+
* @param option
412406
*/
413407
function getStructDataItemJson(href, option) {
414-
return FetchRequest.get(href, option)
408+
return FetchRequest.get(href, null, option)
415409
.then((res) => res.json())
416410
.then((data) => {
417411
if (data.succeed === false) {
@@ -430,7 +424,7 @@ function getStructDataItemJson(href, option) {
430424
* @param options
431425
*/
432426
async function getStructDataItem(href, options) {
433-
const data = await getStructDataItemJson(href);
427+
const data = await getStructDataItemJson(href, options);
434428
const { features, links = [] } = data || {};
435429
const nextInfo = links.find((l) => l.rel === 'next');
436430
if (nextInfo) {
@@ -552,7 +546,11 @@ function transformGeometryCoordinates(features, fromProjection, toProjection = '
552546
*/
553547
async function getStructuredDataGeojsonByWebMap(data, options) {
554548
const allFeature = await getStructDataGeojson(data.dataId, options);
555-
const resultRes = await FetchRequest.get(`${options.server}web/datas/${data.dataId}/structureddata.json`);
549+
const resultRes = await FetchRequest.get(
550+
`${options.server}web/datas/${data.dataId}/structureddata.json`,
551+
null,
552+
options
553+
);
556554
const result = await resultRes.json();
557555
const projection = `EPSG:${result.epsgCode}`;
558556
if (projection !== 'EPSG:4326') {
@@ -1528,7 +1526,7 @@ function replaceSprite2X(url) {
15281526
*/
15291527
function getPixelRatioSpriteUrl(url) {
15301528
if (window.devicePixelRatio > 1 && !url.includes('/sprite@2x')) {
1531-
return replaceSprite2X(url);
1529+
return replaceSprite2X(url);
15321530
}
15331531
return url;
15341532
}
@@ -1593,11 +1591,7 @@ async function addTextures({ layers, sprite, spriteJson, scene, options }) {
15931591
const texture = (l.texture || {}).values;
15941592
const iconInfo = spriteJson[texture];
15951593
if (iconInfo) {
1596-
const image = await changeSpriteIconToImgData(
1597-
sprite,
1598-
{ ...iconInfo, id: texture },
1599-
options
1600-
);
1594+
const image = await changeSpriteIconToImgData(sprite, { ...iconInfo, id: texture }, options);
16011595
const style = l.style || {};
16021596
const color = style.textureColor,
16031597
rotate = style.textureRotate;
@@ -1956,7 +1950,12 @@ function getL7Layer(l) {
19561950
export async function addL7Layers({ map, webMapInfo, l7Layers, spriteDatas, options }) {
19571951
// 添加L7图层
19581952
const { layers, sources, sprite } = webMapInfo;
1959-
const formateL7Layers = await restoreL7Layers({ layers: l7Layers, sources, map, options });
1953+
const formateL7Layers = await restoreL7Layers({
1954+
layers: l7Layers,
1955+
sources,
1956+
map,
1957+
options: Object.assign({ withoutFormatSuffix: true }, options)
1958+
});
19601959
// 批量处理L7纹理
19611960
const scene = await getScene(map);
19621961
if (Object.keys(spriteDatas).length > 0) {
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { addL7Layers, isL7Layer } from '../../../../src/mapboxgl/mapping/utils/L7LayerUtil';
2+
import * as mockL7 from '../../../tool/mock_l7';
3+
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest';
4+
5+
describe('L7LayerUtil', () => {
6+
const mapstudioWebMap_L7LayersRes = JSON.parse(mapstudioWebMap_L7Layers);
7+
8+
const map = {
9+
getL7Scene: () => Promise.resolve(mockL7.Scene),
10+
addLayer: () => {},
11+
getZoom: () => 3
12+
};
13+
14+
const options = {
15+
withCredentials: true,
16+
server: 'http://localhost:8190/iportal/'
17+
};
18+
19+
const addOptions = {
20+
map,
21+
webMapInfo: { ...mapstudioWebMap_L7LayersRes },
22+
l7Layers: mapstudioWebMap_L7LayersRes.layers.filter((layer) => isL7Layer(layer)),
23+
spriteDatas: {},
24+
options
25+
};
26+
27+
beforeAll(() => {
28+
spyOn(FetchRequest, 'get').and.callFake((url) => {
29+
if (url.indexOf('map.json') > -1) {
30+
return Promise.resolve(new Response(mapstudioWebMap_L7Layers));
31+
}
32+
if (url.indexOf('617580084.json') > -1) {
33+
return Promise.resolve(new Response(msProjectINfo_L7Layers));
34+
}
35+
if (url.indexOf('/sprite') > -1) {
36+
return Promise.resolve(new Response(msSpriteInfo));
37+
}
38+
if (url.indexOf('/web/datas/1554834293/structureddata/ogc-features/collections/all/items.json') > -1) {
39+
return Promise.resolve(new Response(l7StructureData1052943054Items));
40+
}
41+
if (url.indexOf('/web/datas/1554834293/structureddata/ogc-features/collections/all/items.json') > -1) {
42+
return Promise.resolve(new Response(l7StructureData1052943054Items));
43+
}
44+
if (url.indexOf('/web/datas/1554834293/structureddata.json') > -1) {
45+
return Promise.resolve(new Response(l7StructureData1052943054));
46+
}
47+
if (url.indexOf('/web/datas/1767084124/structureddata/ogc-features/collections/all/items.json') > -1) {
48+
return Promise.resolve(new Response(l7StructureData1767084124Items));
49+
}
50+
if (url.indexOf('/web/datas/1767084124/structureddata.json') > -1) {
51+
return Promise.resolve(new Response(l7StructureData1767084124));
52+
}
53+
if (
54+
url.indexOf('iserver/services/data-Building/rest/data/datasources/newBuilding/datasets/New_LINE/fields.json') >
55+
-1
56+
) {
57+
return Promise.resolve(new Response(RESTDATA_FIELDS_RES));
58+
}
59+
if (
60+
url.indexOf('iserver/services/data-Building/rest/data/datasources/newBuilding/datasets/New_LINE/domain.json') >
61+
-1
62+
) {
63+
return Promise.resolve(new Response(RESTDATA_DOMAINS_RES));
64+
}
65+
console.log(url);
66+
return Promise.resolve();
67+
});
68+
spyOn(FetchRequest, 'post').and.callFake((url) => {
69+
if (url.indexOf('/iserver/services/data-Building/rest/data/featureResults.geojson') > -1) {
70+
return Promise.resolve(new Response(RESTDATA_FEATURES_RES));
71+
}
72+
console.log(url);
73+
return Promise.resolve();
74+
});
75+
});
76+
77+
it('add od layer', (done) => {
78+
const layers = [
79+
{
80+
source: 'ms_1554834293_1716384530289_76',
81+
'source-layer': '1554834293$msgeometry',
82+
minzoom: 0,
83+
maxzoom: 24,
84+
metadata: {
85+
MapStudio: {
86+
title: '国内航班数据_100'
87+
}
88+
},
89+
id: '国内航班数据_100',
90+
type: 'line-curve',
91+
paint: {
92+
'line-curve-color': '#ff6b34',
93+
'line-curve-opacity': 1,
94+
'line-curve-width': 1,
95+
'line-curve-pattern-opacity': 1,
96+
'line-curve-dasharray': [1, 0]
97+
},
98+
layout: {
99+
'line-curve-shape': 'greatcircle',
100+
'line-curve-segment': 30,
101+
visibility: 'visible'
102+
}
103+
},
104+
{
105+
source: 'ms_1554834293_1716384530289_76',
106+
'source-layer': '1554834293$msgeometry',
107+
minzoom: 0,
108+
maxzoom: 24,
109+
metadata: {
110+
MapStudio: {
111+
title: '国内航班数据_100'
112+
}
113+
},
114+
id: 'ms_composite_国内航班数据_100',
115+
type: 'line-curve',
116+
paint: {
117+
'line-curve-color': '#ff6b34',
118+
'line-curve-opacity': 1,
119+
'line-curve-width': 10,
120+
'line-curve-pattern': 'circle',
121+
'line-curve-pattern-color': 'rgba(89,89,89,1)',
122+
'line-curve-pattern-opacity': 1
123+
},
124+
layout: {
125+
'line-curve-shape': 'greatcircle',
126+
'line-curve-pattern-rotate': 0,
127+
'line-curve-pattern-interval': 20,
128+
'line-curve-pattern-blend': 'replace',
129+
'line-curve-segment': 30,
130+
'line-curve-animate-duration': 4,
131+
'line-curve-animate-interval': 0.4,
132+
'line-curve-animate-trailLength': 0.2,
133+
visibility: 'visible'
134+
}
135+
}
136+
];
137+
const sources = {
138+
ms_1554834293_1716384530289_76: {
139+
type: 'geojson',
140+
data: {
141+
type: 'supermap-structured-data',
142+
dataId: '1554834293'
143+
},
144+
od: true,
145+
originX: '起飞机场x',
146+
originY: '起飞机场y',
147+
destinationX: '降落机场x',
148+
destinationY: '降落机场y'
149+
}
150+
};
151+
const nextOptions = {
152+
...addOptions,
153+
webMapInfo: { ...mapstudioWebMap_L7LayersRes, layers, sources },
154+
l7Layers: layers
155+
};
156+
const spy = spyOn(nextOptions.map, 'addLayer').and.callThrough();
157+
addL7Layers(nextOptions).then(() => {
158+
expect(nextOptions.map.addLayer.calls.count()).toEqual(2);
159+
spy.calls.reset();
160+
done();
161+
});
162+
});
163+
164+
it('animate line layer', (done) => {
165+
const layers = [
166+
{
167+
layout: {
168+
visibility: 'visible',
169+
'line-extrusion-pattern-interval': 20,
170+
'line-extrusion-animate-duration': 6,
171+
'line-extrusion-pattern-blend': 'normal',
172+
'line-extrusion-animate-trailLength': 1.5,
173+
'line-extrusion-animate-interval': 0.6
174+
},
175+
metadata: {
176+
MapStudio: {
177+
title: 'New_LINE'
178+
}
179+
},
180+
paint: {
181+
'line-extrusion-base': 0,
182+
'line-extrusion-opacity': 1,
183+
'line-extrusion-width': 12,
184+
'line-extrusion-base-fixed': false,
185+
'line-extrusion-color': '#4CC8A3'
186+
},
187+
source: 'ms_New_LINE_ms_datasetId_1716864444834_6_1716864449917_10',
188+
'source-layer': 'New_LINE@newBuilding',
189+
id: 'ms_New_LINE_1716864449916_8',
190+
type: 'line-extrusion'
191+
}
192+
];
193+
const sources = {
194+
ms_New_LINE_ms_datasetId_1716864444834_6_1716864449917_10: {
195+
data: {
196+
datasetName: 'New_LINE',
197+
type: 'supermap-rest-data',
198+
url: 'http://localhost:8090/iserver/services/data-Building/rest/data/featureResults.geojson?returnContent=true&fromIndex=0&maxFeatures=2147483647',
199+
dataSourceName: 'newBuilding'
200+
},
201+
type: 'geojson'
202+
}
203+
};
204+
const nextOptions = {
205+
...addOptions,
206+
webMapInfo: { ...mapstudioWebMap_L7LayersRes, layers, sources },
207+
l7Layers: layers
208+
};
209+
const spy = spyOn(nextOptions.map, 'addLayer').and.callThrough();
210+
addL7Layers(nextOptions).then(() => {
211+
expect(nextOptions.map.addLayer.calls.count()).toEqual(1);
212+
spy.calls.reset();
213+
done();
214+
});
215+
});
216+
});

0 commit comments

Comments
 (0)