Skip to content

Commit 8a96fc3

Browse files
[fix]将webmapv3 filter 替换成get xxxx
1 parent 06be141 commit 8a96fc3

File tree

3 files changed

+665
-0
lines changed

3 files changed

+665
-0
lines changed

src/common/mapping/WebMapV3.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,30 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
336336
}
337337
return epsgCode;
338338
}
339+
_getFilterByCatalog(catalog, res = {}) {
340+
const { catalogType, children } = catalog;
341+
if(catalogType === 'group' && children) {
342+
children.forEach(child => {
343+
this._getFiltersByCatalog(child, res);
344+
})
345+
}
346+
if (catalogType === 'layer') {
347+
const { filter, layersContent = [] } = catalog;
348+
if (filter) {
349+
layersContent.forEach(layerId => {
350+
res[layerId] = filter;
351+
})
352+
}
353+
}
354+
}
355+
_getFiltersByCatalog(_mapResourceInfo = this._mapResourceInfo) {
356+
const { catalogs = [] } = _mapResourceInfo;
357+
const res = [];
358+
catalogs.forEach((item) => {
359+
this._getFilterByCatalog(item, res);
360+
})
361+
return res;
362+
}
339363

340364
/**
341365
* @private
@@ -363,6 +387,8 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
363387
description: relatedInfo.description
364388
};
365389
this._mapResourceInfo = JSON.parse(relatedInfo.projectInfo);
390+
const catalogFilters = this._getFiltersByCatalog();
391+
this._changeMapInfoFilter(catalogFilters);
366392
this._createMapRelatedInfo();
367393
this._addLayersToMap();
368394
})
@@ -371,6 +397,79 @@ export function createWebMapV3Extending(SuperClass, { MapManager, mapRepo, crsMa
371397
console.error(error);
372398
});
373399
}
400+
/**
401+
* @private
402+
* @function WebMapV3.prototype._changeMapInfoFilter
403+
* @description 更新地图图层的过滤器, 将filter的内容 ['==', 'Ctype', '']转换为['==', ['get', 'Ctype'], 'label']
404+
*/
405+
_changeMapInfoFilter(catalogFilters = {}) {
406+
const { layers =[]} = this._mapInfo;
407+
layers.forEach(layer => {
408+
if (layer.filter && catalogFilters[layer.id]) {
409+
const catalogFilter = catalogFilters[layer.id];
410+
const matchKeys = this._collectMatchKeys(catalogFilter);
411+
const filter = this._transformFilterByMatchKeys(layer.filter, matchKeys);
412+
layer.filter = filter;
413+
}
414+
})
415+
this._mapInfo ={
416+
...this._mapInfo,
417+
layers
418+
}
419+
}
420+
421+
_collectMatchKeys(filter) {
422+
const keys = [];
423+
const excludeKeys = ['$type', '$id', '$layer'];
424+
if (!Array.isArray(filter)) {return keys;}
425+
const traverse = (arr) => {
426+
for (const item of arr) {
427+
if (!Array.isArray(item)) {continue;}
428+
if (item.length >= 3 && this._isComparisonOperator(item[0])) {
429+
const prop = this._getPropertyKey(item[1]);
430+
if (prop && !excludeKeys.includes(prop)) {
431+
keys.push(prop);
432+
continue;
433+
}
434+
if (typeof item[1] === 'string' && !excludeKeys.includes(item[1])) {
435+
keys.push(item[1]);
436+
}
437+
} else {
438+
traverse(item);
439+
}
440+
}
441+
};
442+
traverse(filter);
443+
return [...new Set(keys)];
444+
}
445+
446+
_getPropertyKey(item) {
447+
if (Array.isArray(item) && item.length === 2 && item[0] === 'get' && typeof item[1] === 'string') {
448+
return item[1];
449+
}
450+
return null;
451+
}
452+
453+
_transformFilterByMatchKeys(filter, matchKeys) {
454+
if (!Array.isArray(filter)) {
455+
return filter;
456+
}
457+
if (filter.length >= 3 && typeof filter[1] === 'string' && this._isComparisonOperator(filter[0])) {
458+
if (matchKeys.includes(filter[1])) {
459+
return [filter[0], ['get', filter[1]], ...filter.slice(2)];
460+
}
461+
return filter;
462+
}
463+
if (filter.length >= 2 && typeof filter[1] !== 'string' && this._isComparisonOperator(filter[0])) {
464+
const operands = filter.slice(1).map(item => this._transformFilterByMatchKeys(item, matchKeys));
465+
return [filter[0], ...operands];
466+
}
467+
return filter.map(item => this._transformFilterByMatchKeys(item, matchKeys));
468+
}
469+
470+
_isComparisonOperator(op) {
471+
return ['==', '!=', '>', '<', '>=', '<=', 'in', '!in', 'all', 'any', 'none'].includes(op);
472+
}
374473

375474
/**
376475
* @private

test/mapboxgl/mapping/WebMapV3Spec.js

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

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

0 commit comments

Comments
 (0)