mirror of https://gitee.com/antv-l7/antv-l7
feat: source 支持 json 下新增 geometry 字段 (#1312)
* feat: source 支持 json 下 geometry 解析 * refactor: 优化循环逻辑
This commit is contained in:
parent
f2a35106cb
commit
1b1fa6238e
|
@ -8,6 +8,7 @@ export interface IParserCfg {
|
|||
x1?: string;
|
||||
y1?: string;
|
||||
coordinates?: string;
|
||||
geometry?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
type CallBack = (...args: any[]) => any;
|
||||
|
|
|
@ -8,8 +8,9 @@ import {
|
|||
Properties,
|
||||
} from '@turf/helpers';
|
||||
import { getCoords } from '@turf/invariant';
|
||||
import * as turfMeta from '@turf/meta';
|
||||
import { flattenEach } from '@turf/meta';
|
||||
import { IFeatureKey, IParseDataItem, IParserData } from '../interface';
|
||||
|
||||
interface IParserCFG {
|
||||
idField?: string;
|
||||
featureId?: string;
|
||||
|
@ -58,12 +59,14 @@ export default function geoJSON(
|
|||
): IParserData {
|
||||
const resultData: IParseDataItem[] = [];
|
||||
const featureKeys: IFeatureKey = {};
|
||||
|
||||
if (!data.features) {
|
||||
data.features = [];
|
||||
return {
|
||||
dataArray: [],
|
||||
};
|
||||
}
|
||||
|
||||
data.features = data.features.filter((item: Feature) => {
|
||||
const geometry: Geometry | null = item.geometry as Geometry;
|
||||
return (
|
||||
|
@ -74,15 +77,18 @@ export default function geoJSON(
|
|||
geometry.coordinates.length > 0
|
||||
);
|
||||
});
|
||||
|
||||
rewind(data, true); // 设置地理多边形方向 If clockwise is true, the outer ring is clockwise, otherwise it is counterclockwise.
|
||||
|
||||
if (data.features.length === 0) {
|
||||
return {
|
||||
dataArray: [],
|
||||
featureKeys,
|
||||
};
|
||||
}
|
||||
// multi polygon 拆分
|
||||
turfMeta.flattenEach(
|
||||
|
||||
// multi feature 情况拆分
|
||||
flattenEach(
|
||||
data,
|
||||
(currentFeature: Feature<Geometries, Properties>, featureIndex: number) => {
|
||||
let featureId = getFeatureID(currentFeature, cfg?.featureId);
|
||||
|
@ -100,6 +106,7 @@ export default function geoJSON(
|
|||
resultData.push(dataItem);
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
dataArray: resultData,
|
||||
featureKeys,
|
||||
|
|
|
@ -1,31 +1,53 @@
|
|||
// @ts-ignore
|
||||
import rewind from '@mapbox/geojson-rewind';
|
||||
import {
|
||||
IJsonData,
|
||||
IJsonItem,
|
||||
IParseDataItem,
|
||||
IParserCfg,
|
||||
IParserData,
|
||||
} from '@antv/l7-core';
|
||||
// @ts-ignore
|
||||
import rewind from '@mapbox/geojson-rewind';
|
||||
import { flattenEach } from '@turf/meta';
|
||||
import { getCoords } from '@turf/invariant';
|
||||
import { Feature, Geometries, Properties } from '@turf/helpers';
|
||||
|
||||
export default function json(data: IJsonData, cfg: IParserCfg): IParserData {
|
||||
const { x, y, x1, y1, coordinates } = cfg;
|
||||
const { x, y, x1, y1, coordinates, geometry } = cfg;
|
||||
const resultData: IParseDataItem[] = [];
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
return {
|
||||
dataArray: [],
|
||||
};
|
||||
}
|
||||
data.forEach((col: IJsonItem, featureIndex: number) => {
|
||||
let coords = [];
|
||||
if (x && y) {
|
||||
coords = [parseFloat(col[x]), parseFloat(col[y])];
|
||||
} // 点数据
|
||||
if (x && y && x1 && y1) {
|
||||
const from = [parseFloat(col[x]), parseFloat(col[y])];
|
||||
const to = [parseFloat(col[x1]), parseFloat(col[y1])];
|
||||
coords = [from, to];
|
||||
|
||||
for (let featureIndex = 0; featureIndex < data.length; featureIndex++) {
|
||||
const col = data[featureIndex];
|
||||
|
||||
// GeoJson geometry 数据
|
||||
if (geometry) {
|
||||
const _geometry = { ...col[geometry] };
|
||||
const rewindGeometry = rewind(_geometry, true);
|
||||
// multi feature 情况拆分
|
||||
flattenEach(
|
||||
rewindGeometry,
|
||||
(currentFeature: Feature<Geometries, Properties>) => {
|
||||
const coord = getCoords(currentFeature);
|
||||
const dataItem = {
|
||||
...col,
|
||||
_id: featureIndex,
|
||||
coordinates: coord,
|
||||
};
|
||||
|
||||
resultData.push(dataItem);
|
||||
},
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let coords = [];
|
||||
|
||||
// GeoJson coordinates 数据
|
||||
// 仅支持 Point LineString Polygon 三种 coordinates
|
||||
if (coordinates) {
|
||||
let type = 'Polygon';
|
||||
if (!Array.isArray(coordinates[0])) {
|
||||
|
@ -38,16 +60,26 @@ export default function json(data: IJsonData, cfg: IParserCfg): IParserData {
|
|||
type,
|
||||
coordinates: [...col[coordinates]],
|
||||
};
|
||||
rewind(geometry, true);
|
||||
coords = geometry.coordinates;
|
||||
const rewindGeometry = rewind(geometry, true);
|
||||
coords = rewindGeometry.coordinates;
|
||||
} else if (x && y && x1 && y1) {
|
||||
// 起终点数据
|
||||
const from = [parseFloat(col[x]), parseFloat(col[y])];
|
||||
const to = [parseFloat(col[x1]), parseFloat(col[y1])];
|
||||
coords = [from, to];
|
||||
} else if (x && y) {
|
||||
// 点数据
|
||||
coords = [parseFloat(col[x]), parseFloat(col[y])];
|
||||
}
|
||||
|
||||
const dataItem = {
|
||||
...col,
|
||||
_id: featureIndex,
|
||||
coordinates: coords,
|
||||
};
|
||||
resultData.push(dataItem);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
dataArray: resultData,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue