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;
|
x1?: string;
|
||||||
y1?: string;
|
y1?: string;
|
||||||
coordinates?: string;
|
coordinates?: string;
|
||||||
|
geometry?: string;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
type CallBack = (...args: any[]) => any;
|
type CallBack = (...args: any[]) => any;
|
||||||
|
|
|
@ -8,8 +8,9 @@ import {
|
||||||
Properties,
|
Properties,
|
||||||
} from '@turf/helpers';
|
} from '@turf/helpers';
|
||||||
import { getCoords } from '@turf/invariant';
|
import { getCoords } from '@turf/invariant';
|
||||||
import * as turfMeta from '@turf/meta';
|
import { flattenEach } from '@turf/meta';
|
||||||
import { IFeatureKey, IParseDataItem, IParserData } from '../interface';
|
import { IFeatureKey, IParseDataItem, IParserData } from '../interface';
|
||||||
|
|
||||||
interface IParserCFG {
|
interface IParserCFG {
|
||||||
idField?: string;
|
idField?: string;
|
||||||
featureId?: string;
|
featureId?: string;
|
||||||
|
@ -58,12 +59,14 @@ export default function geoJSON(
|
||||||
): IParserData {
|
): IParserData {
|
||||||
const resultData: IParseDataItem[] = [];
|
const resultData: IParseDataItem[] = [];
|
||||||
const featureKeys: IFeatureKey = {};
|
const featureKeys: IFeatureKey = {};
|
||||||
|
|
||||||
if (!data.features) {
|
if (!data.features) {
|
||||||
data.features = [];
|
data.features = [];
|
||||||
return {
|
return {
|
||||||
dataArray: [],
|
dataArray: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
data.features = data.features.filter((item: Feature) => {
|
data.features = data.features.filter((item: Feature) => {
|
||||||
const geometry: Geometry | null = item.geometry as Geometry;
|
const geometry: Geometry | null = item.geometry as Geometry;
|
||||||
return (
|
return (
|
||||||
|
@ -74,15 +77,18 @@ export default function geoJSON(
|
||||||
geometry.coordinates.length > 0
|
geometry.coordinates.length > 0
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
rewind(data, true); // 设置地理多边形方向 If clockwise is true, the outer ring is clockwise, otherwise it is counterclockwise.
|
rewind(data, true); // 设置地理多边形方向 If clockwise is true, the outer ring is clockwise, otherwise it is counterclockwise.
|
||||||
|
|
||||||
if (data.features.length === 0) {
|
if (data.features.length === 0) {
|
||||||
return {
|
return {
|
||||||
dataArray: [],
|
dataArray: [],
|
||||||
featureKeys,
|
featureKeys,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// multi polygon 拆分
|
|
||||||
turfMeta.flattenEach(
|
// multi feature 情况拆分
|
||||||
|
flattenEach(
|
||||||
data,
|
data,
|
||||||
(currentFeature: Feature<Geometries, Properties>, featureIndex: number) => {
|
(currentFeature: Feature<Geometries, Properties>, featureIndex: number) => {
|
||||||
let featureId = getFeatureID(currentFeature, cfg?.featureId);
|
let featureId = getFeatureID(currentFeature, cfg?.featureId);
|
||||||
|
@ -100,6 +106,7 @@ export default function geoJSON(
|
||||||
resultData.push(dataItem);
|
resultData.push(dataItem);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dataArray: resultData,
|
dataArray: resultData,
|
||||||
featureKeys,
|
featureKeys,
|
||||||
|
|
|
@ -1,31 +1,53 @@
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
import rewind from '@mapbox/geojson-rewind';
|
||||||
import {
|
import {
|
||||||
IJsonData,
|
IJsonData,
|
||||||
IJsonItem,
|
|
||||||
IParseDataItem,
|
IParseDataItem,
|
||||||
IParserCfg,
|
IParserCfg,
|
||||||
IParserData,
|
IParserData,
|
||||||
} from '@antv/l7-core';
|
} from '@antv/l7-core';
|
||||||
// @ts-ignore
|
import { flattenEach } from '@turf/meta';
|
||||||
import rewind from '@mapbox/geojson-rewind';
|
import { getCoords } from '@turf/invariant';
|
||||||
|
import { Feature, Geometries, Properties } from '@turf/helpers';
|
||||||
|
|
||||||
export default function json(data: IJsonData, cfg: IParserCfg): IParserData {
|
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[] = [];
|
const resultData: IParseDataItem[] = [];
|
||||||
|
|
||||||
if (!Array.isArray(data)) {
|
if (!Array.isArray(data)) {
|
||||||
return {
|
return {
|
||||||
dataArray: [],
|
dataArray: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
data.forEach((col: IJsonItem, featureIndex: number) => {
|
|
||||||
let coords = [];
|
for (let featureIndex = 0; featureIndex < data.length; featureIndex++) {
|
||||||
if (x && y) {
|
const col = data[featureIndex];
|
||||||
coords = [parseFloat(col[x]), parseFloat(col[y])];
|
|
||||||
} // 点数据
|
// GeoJson geometry 数据
|
||||||
if (x && y && x1 && y1) {
|
if (geometry) {
|
||||||
const from = [parseFloat(col[x]), parseFloat(col[y])];
|
const _geometry = { ...col[geometry] };
|
||||||
const to = [parseFloat(col[x1]), parseFloat(col[y1])];
|
const rewindGeometry = rewind(_geometry, true);
|
||||||
coords = [from, to];
|
// 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) {
|
if (coordinates) {
|
||||||
let type = 'Polygon';
|
let type = 'Polygon';
|
||||||
if (!Array.isArray(coordinates[0])) {
|
if (!Array.isArray(coordinates[0])) {
|
||||||
|
@ -38,16 +60,26 @@ export default function json(data: IJsonData, cfg: IParserCfg): IParserData {
|
||||||
type,
|
type,
|
||||||
coordinates: [...col[coordinates]],
|
coordinates: [...col[coordinates]],
|
||||||
};
|
};
|
||||||
rewind(geometry, true);
|
const rewindGeometry = rewind(geometry, true);
|
||||||
coords = geometry.coordinates;
|
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 = {
|
const dataItem = {
|
||||||
...col,
|
...col,
|
||||||
_id: featureIndex,
|
_id: featureIndex,
|
||||||
coordinates: coords,
|
coordinates: coords,
|
||||||
};
|
};
|
||||||
resultData.push(dataItem);
|
resultData.push(dataItem);
|
||||||
});
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
dataArray: resultData,
|
dataArray: resultData,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue