Skip to content

Commit 4fb1f17

Browse files
[fix]将webmapv3 filter 替换成get xxxx
1 parent 50ac25f commit 4fb1f17

File tree

4 files changed

+749
-84
lines changed

4 files changed

+749
-84
lines changed

src/common/mapping/WebMapV3.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,30 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
384384
}
385385
}
386386
}
387+
_getFilterByCatalog(catalog, res = {}) {
388+
const { catalogType, children } = catalog;
389+
if(catalogType === 'group' && children) {
390+
children.forEach(child => {
391+
this._getFiltersByCatalog(child, res);
392+
})
393+
}
394+
if (catalogType === 'layer') {
395+
const { filter, layersContent = [] } = catalog;
396+
if (filter) {
397+
layersContent.forEach(layerId => {
398+
res[layerId] = filter;
399+
})
400+
}
401+
}
402+
}
403+
_getFiltersByCatalog(_mapResourceInfo = this._mapResourceInfo) {
404+
const { catalogs = [] } = _mapResourceInfo;
405+
const res = [];
406+
catalogs.forEach((item) => {
407+
this._getFilterByCatalog(item, res);
408+
})
409+
return res;
410+
}
387411
_getPopupInfos(_mapResourceInfo = this._mapResourceInfo) {
388412
const { catalogs = [] } = _mapResourceInfo;
389413
const res = [];
@@ -419,6 +443,8 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
419443
description: relatedInfo.description
420444
};
421445
this._mapResourceInfo = JSON.parse(relatedInfo.projectInfo);
446+
const catalogFilters = this._getFiltersByCatalog();
447+
this._changeMapInfoFilter(catalogFilters);
422448
this._createMapRelatedInfo();
423449
this._addLayersToMap();
424450
})
@@ -427,6 +453,79 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
427453
console.error(error);
428454
});
429455
}
456+
/**
457+
* @private
458+
* @function WebMapV3.prototype._changeMapInfoFilter
459+
* @description 更新地图图层的过滤器, 将filter的内容 ['==', 'Ctype', '']转换为['==', ['get', 'Ctype'], 'label']
460+
*/
461+
_changeMapInfoFilter(catalogFilters = {}) {
462+
const { layers =[]} = this._mapInfo;
463+
layers.forEach(layer => {
464+
if (layer.filter && catalogFilters[layer.id]) {
465+
const catalogFilter = catalogFilters[layer.id];
466+
const matchKeys = this._collectMatchKeys(catalogFilter);
467+
const filter = this._transformFilterByMatchKeys(layer.filter, matchKeys);
468+
layer.filter = filter;
469+
}
470+
})
471+
this._mapInfo ={
472+
...this._mapInfo,
473+
layers
474+
}
475+
}
476+
477+
_collectMatchKeys(filter) {
478+
const keys = [];
479+
const excludeKeys = ['$type', '$id', '$layer'];
480+
if (!Array.isArray(filter)) {return keys;}
481+
const traverse = (arr) => {
482+
for (const item of arr) {
483+
if (!Array.isArray(item)) {continue;}
484+
if (item.length >= 3 && this._isComparisonOperator(item[0])) {
485+
const prop = this._getPropertyKey(item[1]);
486+
if (prop && !excludeKeys.includes(prop)) {
487+
keys.push(prop);
488+
continue;
489+
}
490+
if (typeof item[1] === 'string' && !excludeKeys.includes(item[1])) {
491+
keys.push(item[1]);
492+
}
493+
} else {
494+
traverse(item);
495+
}
496+
}
497+
};
498+
traverse(filter);
499+
return [...new Set(keys)];
500+
}
501+
502+
_getPropertyKey(item) {
503+
if (Array.isArray(item) && item.length === 2 && item[0] === 'get' && typeof item[1] === 'string') {
504+
return item[1];
505+
}
506+
return null;
507+
}
508+
509+
_transformFilterByMatchKeys(filter, matchKeys) {
510+
if (!Array.isArray(filter)) {
511+
return filter;
512+
}
513+
if (filter.length >= 3 && typeof filter[1] === 'string' && this._isComparisonOperator(filter[0])) {
514+
if (matchKeys.includes(filter[1])) {
515+
return [filter[0], ['get', filter[1]], ...filter.slice(2)];
516+
}
517+
return filter;
518+
}
519+
if (filter.length >= 2 && typeof filter[1] !== 'string' && this._isComparisonOperator(filter[0])) {
520+
const operands = filter.slice(1).map(item => this._transformFilterByMatchKeys(item, matchKeys));
521+
return [filter[0], ...operands];
522+
}
523+
return filter.map(item => this._transformFilterByMatchKeys(item, matchKeys));
524+
}
525+
526+
_isComparisonOperator(op) {
527+
return ['==', '!=', '>', '<', '>=', '<=', 'in', '!in', 'all', 'any', 'none'].includes(op);
528+
}
430529

431530
/**
432531
* @private

test/mapboxgl/mapping/WebMapV3Spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ describe('mapboxgl-webmap3.0', () => {
162162
});
163163
});
164164

165+
it('filters mapinfo', (done) => {
166+
spyOn(FetchRequest, 'get').and.callFake((url) => {
167+
if (url.indexOf('web/config/portal.json') > -1) {
168+
return Promise.resolve(new Response(JSON.stringify(iportal_serviceProxy)));
169+
}
170+
if (url.indexOf('map.json') > -1) {
171+
return Promise.resolve(new Response(mapstudioWebMap_filters));
172+
}
173+
if (url.indexOf('617580084.json') > -1) {
174+
return Promise.resolve(new Response(msProjectINfo_filters));
175+
}
176+
if (url.indexOf('/sprite') > -1) {
177+
return Promise.resolve(new Response(msSpriteInfo));
178+
}
179+
return Promise.resolve();
180+
});
181+
mapstudioWebmap = new WebMap(id, {
182+
server: server
183+
});
184+
185+
mapstudioWebmap.on('mapcreatesucceeded', ({ map }) => {
186+
expect(map).not.toBeUndefined();
187+
expect(mapstudioWebmap.map).toEqual(map);
188+
const style = map.getStyle();
189+
const webMapV3 = mapstudioWebmap._getWebMapInstance();
190+
const mapInfo = JSON.parse(mapstudioWebMap_symbol);
191+
expect(style.layers.length).toBe(mapInfo.layers.length);
192+
expect(webMapV3._mapInfo.layers[0].filter).toBe([]);
193+
expect(webMapV3._mapInfo.layers[1].filter).toBe([]);
194+
expect(webMapV3._mapInfo.layers[2].filter).toBe([]);
195+
expect(webMapV3._mapInfo.layers[3].filter).toBe([]);
196+
expect(webMapV3._mapInfo.layers[4].filter).toBe([]);
197+
done();
198+
});
199+
});
200+
165201
it('mapId is JSON', (done) => {
166202
spyOn(FetchRequest, 'get').and.callFake((url) => {
167203
if (url.indexOf('/sprite') > -1) {

0 commit comments

Comments
 (0)