Skip to content

Commit 04c4a65

Browse files
committed
add webmap3.0 geojson图层支持数据切换
1 parent 3979cfe commit 04c4a65

3 files changed

Lines changed: 140 additions & 4 deletions

File tree

‎src/mapboxgl/mapping/webmap/v3/WebMap.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,63 @@ export class WebMap extends mapboxgl.Evented {
219219
this.map.addLayer(copyLayer);
220220
}
221221

222+
updateOverlayLayer(layerInfo, features, mergeByField) {
223+
if (layerInfo.renderSource.type === 'geojson') {
224+
const sourceId = layerInfo.renderSource.id;
225+
features = this._mergeFeatures(sourceId, features, mergeByField);
226+
const featureCollection = {
227+
type: 'FeatureCollection',
228+
features
229+
};
230+
this.map.getSource(sourceId).setData(featureCollection);
231+
}
232+
}
233+
234+
_mergeFeatures(sourceId, features, mergeByField) {
235+
if (!(features instanceof Array)) {
236+
return features;
237+
}
238+
features = features.map((feature, index) => {
239+
if (!Object.prototype.hasOwnProperty.call(feature.properties, 'index')) {
240+
feature.properties.index = index;
241+
}
242+
return feature;
243+
});
244+
if (!features.length || !mergeByField && features[0].geometry) {
245+
return features;
246+
}
247+
const source = this.map.getSource(sourceId);
248+
if ((!source || !source._data.features) && features[0].geometry) {
249+
return features;
250+
}
251+
const prevFeatures = source && source._data && source._data.features;
252+
const nextFeatures = [];
253+
if (!mergeByField && prevFeatures) {
254+
return prevFeatures;
255+
}
256+
features.forEach((feature) => {
257+
const prevFeature = prevFeatures.find((item) => {
258+
if (isNaN(+item.properties[mergeByField]) && isNaN(+feature.properties[mergeByField])) {
259+
return (
260+
JSON.stringify(item.properties[mergeByField] || '') ===
261+
JSON.stringify(feature.properties[mergeByField] || '')
262+
);
263+
} else {
264+
return +item.properties[mergeByField] === +feature.properties[mergeByField];
265+
}
266+
});
267+
if (prevFeature) {
268+
nextFeatures.push({
269+
...prevFeature,
270+
...feature
271+
});
272+
} else if (feature.geometry) {
273+
nextFeatures.push(feature);
274+
}
275+
});
276+
return nextFeatures;
277+
}
278+
222279
/**
223280
* @private
224281
* @function WebMap.prototype._createMap

‎test/mapboxgl/mapping/WebMapV3Spec.js‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,4 +780,78 @@ describe('mapboxgl-webmap3.0', () => {
780780
});
781781
});
782782
});
783+
784+
it('updateOverlayLayer', (done) => {
785+
const mapInfo = JSON.parse(mapstudioWebMap_L7Layers);
786+
spyOn(L7, 'PointLayer').and.callFake(mockL7.PointLayer);
787+
spyOn(L7, 'LineLayer').and.callFake(mockL7.PointLayer);
788+
spyOn(L7, 'PolygonLayer').and.callFake(mockL7.PointLayer);
789+
spyOn(L7, 'HeatmapLayer').and.callFake(mockL7.PointLayer);
790+
spyOn(L7, 'Scene').and.callFake(mockL7.Scene);
791+
spyOn(L7, 'Mapbox').and.callFake(mockL7.Mapbox);
792+
mapboxgl.Map.prototype.getCRS = function () {
793+
return { epsgCode: mapInfo.crs.name, getExtent: () => {} };
794+
};
795+
spyOn(FetchRequest, 'get').and.callFake((url) => {
796+
if (url.indexOf('map.json') > -1) {
797+
return Promise.resolve(new Response(mapstudioWebMap_L7Layers));
798+
}
799+
if (url.indexOf('617580084.json') > -1) {
800+
return Promise.resolve(new Response(msProjectINfo_L7Layers));
801+
}
802+
if (url.indexOf('/sprite') > -1) {
803+
return Promise.resolve(new Response(msSpriteInfo));
804+
}
805+
if (url.indexOf('/web/datas/1052943054/structureddata/ogc-features/collections/all/items.json') > -1) {
806+
return Promise.resolve(new Response(l7StructureData1052943054Items));
807+
}
808+
if (url.indexOf('/web/datas/1052943054/structureddata.json') > -1) {
809+
return Promise.resolve(new Response(l7StructureData1052943054));
810+
}
811+
if (url.indexOf('/web/datas/1767084124/structureddata/ogc-features/collections/all/items.json') > -1) {
812+
return Promise.resolve(new Response(l7StructureData1767084124Items));
813+
}
814+
if (url.indexOf('/web/datas/1767084124/structureddata.json') > -1) {
815+
return Promise.resolve(new Response(l7StructureData1767084124));
816+
}
817+
return Promise.resolve();
818+
});
819+
mapboxgl.CRS = function () {};
820+
mapboxgl.CRS.set = function () {};
821+
const mapOptions = {
822+
transformRequest: function(url) { return { url }; }
823+
}
824+
spyOn(mapOptions, 'transformRequest').and.callThrough();
825+
mapstudioWebmap = new WebMap(id, {
826+
server: server
827+
}, mapOptions);
828+
mapstudioWebmap.on('addlayerssucceeded', ({ map }) => {
829+
const webmapInstance = mapstudioWebmap._getWebMapInstance();
830+
const layerInfo = {
831+
id: '北京市轨道交通线路减',
832+
renderSource: {
833+
id: 'ms_1052943054_1715672103742_8',
834+
type: 'geojson'
835+
}
836+
}
837+
const features = [{
838+
id: '1',
839+
type: 'Feature',
840+
properties: {
841+
终点y: '39.948990864179734',
842+
SmID: 1,
843+
标准名称: '地铁二号线',
844+
起点x: '116.38050072430798',
845+
smpid: 1,
846+
起点y: '39.94888011518407',
847+
testID: 1,
848+
终点x: '116.39553809862188'
849+
}
850+
}]
851+
webmapInstance.updateOverlayLayer(layerInfo, features, '标准名称');
852+
const newFeatures = map.getSource('ms_1052943054_1715672103742_8').getData().features;
853+
expect(newFeatures.length).toBe(1);
854+
done();
855+
});
856+
});
783857
});

‎test/tool/mock_l7.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Layer {
5252
this.stacks = {};
5353
this.rawConfig = options;
5454
this.layerSource = {
55-
originData: [],
55+
originData: {type: 'FeatureCollection', features: []},
5656
data: {
5757
dataArray: []
5858
}
@@ -63,7 +63,7 @@ class Layer {
6363
this.layerSource = {
6464
...options,
6565
parser,
66-
originData: data.features ? data.features : data,
66+
originData: data.features ? data : {type: 'FeatureCollection', features: data},
6767
data: {
6868
dataArray: parser.type === "geojson" ? data : []
6969
}
@@ -126,7 +126,7 @@ class Layer {
126126
return this;
127127
}
128128
getSource() {
129-
return {
129+
const sourceInfo = {
130130
on: (type, cb) => {
131131
this.stacks[type] = [cb];
132132
},
@@ -136,8 +136,13 @@ class Layer {
136136
});
137137
}
138138
};
139+
sourceInfo._data = this.layerSource.originData;
140+
sourceInfo.getData = () => this.layerSource.originData;
141+
sourceInfo.setData = this.setData;
142+
return sourceInfo;
139143
}
140-
setData() {
144+
setData(data) {
145+
this.layerSource.originData = data;
141146
this.getSource().emit("update");
142147
}
143148

0 commit comments

Comments
 (0)