Skip to content

Commit

Permalink
[fix]mvt的geojson数据结构返回
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiao-supermap committed Jun 27, 2024
1 parent bc93185 commit d3263d8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/services/interaction/IPickingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ export interface ILayerPickService {
* 获取选中的要素
* @param id q
*/
getFeatureById(id: number, lngLat?: ILngLat): any;
getFeatureById(id: number, lngLat?: ILngLat, featureId): any;
}
12 changes: 5 additions & 7 deletions packages/core/src/services/interaction/PickingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,15 @@ export default class PickingService implements IPickingService {

const features = [];
const featuresIdMap: { [key: string]: boolean } = {};
const { featureId } = layer.getLayerConfig();
for (let i = 0; i < pickedColors.length / 4; i = i + 1) {
const color = pickedColors.slice(i * 4, i * 4 + 4);
const pickedFeatureIdx = decodePickingColor(color);
if (pickedFeatureIdx !== -1 && !featuresIdMap[pickedFeatureIdx]) {
const rawFeature =
layer.layerPickService.getFeatureById(pickedFeatureIdx);
features.push({
// @ts-ignore
...rawFeature,
pickedFeatureIdx,
});
let rawFeature =
layer.layerPickService.getFeatureById(pickedFeatureIdx, featureId);
rawFeature = rawFeature instanceof Array ? rawFeature : [rawFeature];
features.push(...rawFeature);
featuresIdMap[pickedFeatureIdx] = true;
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/layers/src/tile/service/TilePickService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export class TilePickService implements ITilePickService {
const [x, y, x1, y1]= box;
const minLngLat = map.unproject({x, y});
const maxLngLat = map.unproject({x:x1, y:y1})
console.log(minLngLat, maxLngLat)
const tiles = this.tileLayerService.getVisibleTileByBox(minLngLat, maxLngLat);
tiles.forEach(tile=>{
// TODO 多图层拾取
Expand Down Expand Up @@ -134,15 +133,15 @@ export class TilePickService implements ITilePickService {
}

/** 从瓦片中根据数据 */
public getFeatureById(pickedFeatureIdx: number) {
public getFeatureById(pickedFeatureIdx: number, featureId?: string) {
// 提取当前可见瓦片
const tiles = this.tileLayerService
.getTiles()
.filter((tile: ITile) => tile.visible);
// 提取当前可见瓦片中匹配 ID 的 feature 列表
const features: any[] = [];
tiles.forEach((tile: ITile) => {
features.push(...tile.getFeatureById(pickedFeatureIdx));
features.push(...tile.getFeatureById(pickedFeatureIdx, featureId));
});

// 将 feature 列表合并后返回
Expand Down
22 changes: 20 additions & 2 deletions packages/layers/src/tile/tileFactory/VectorTile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ILayer, ILayerAttributesOption } from '@antv/l7-core';
import { VectorSource } from '@antv/l7-source';
import { cloneDeep } from 'lodash';
import Tile from './Tile';
import { getTileLayer } from './util';

Expand Down Expand Up @@ -79,12 +80,29 @@ export default class VectorTile extends Tile {
* @param id
* @returns
*/
public getFeatureById(id: number) {
public getFeatureById(_id: number, featureId = 'id') {
const layer = this.getMainLayer();
if (!layer) {
return [];
}
const res = layer.getSource().data.dataArray.filter((d) => d._id === id);
const source = layer.getSource();
const res = cloneDeep(source.data.dataArray.filter((d) => d._id === _id));
if (res.length === 0) {
return res;
}
if (source.parser.type === 'geojson') {
const data = res.map((item) => {
const id = item[featureId];
const feature = source.originData.features.find(
(item) => item[featureId] === id || item.properties[featureId] === id,
);
const newFeature = cloneDeep(feature);
delete item.coordinates;
newFeature.properties = item;
return newFeature;
});
return data;
}
return res;
}
}

0 comments on commit d3263d8

Please sign in to comment.