-
Notifications
You must be signed in to change notification settings - Fork 625
/
geojson.ts
68 lines (68 loc) · 1.83 KB
/
geojson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// @ts-ignore
import rewind from '@mapbox/geojson-rewind';
import {
Feature,
FeatureCollection,
Geometries,
Geometry,
Properties,
} from '@turf/helpers';
import { getCoords } from '@turf/invariant';
import * as turfMeta from '@turf/meta';
import { IFeatureKey, IParseDataItem, IParserData } from '../interface';
interface IGeoJSON {
features: object[];
}
interface IParserCFG {
idField?: string;
[key: string]: any;
}
export default function geoJSON(
data: FeatureCollection<Geometries, Properties>,
cfg?: IParserCFG,
): IParserData {
rewind(data, true); // 设置地理多边形方向 If clockwise is true, the outer ring is clockwise, otherwise it is counterclockwise.
const resultData: IParseDataItem[] = [];
const featureKeys: IFeatureKey = {};
data.features = data.features.filter((item: Feature) => {
const geometry: Geometry | null = item.geometry as Geometry;
return (
item != null &&
geometry &&
geometry.type &&
geometry.coordinates &&
geometry.coordinates.length > 0
);
});
// 数据为空时处理
const i = 0;
// multi polygon 拆分
turfMeta.flattenEach(
data,
(currentFeature: Feature<Geometries, Properties>, featureIndex: number) => {
const coord = getCoords(currentFeature);
const id = featureIndex;
if (currentFeature.geometry.type === 'Polygon') {
coord.forEach((coor) => {
const dataItem = {
...currentFeature.properties,
coordinates: [coor],
_id: id,
};
resultData.push(dataItem);
});
} else {
const dataItem: IParseDataItem = {
...currentFeature.properties,
coordinates: coord,
_id: id,
};
resultData.push(dataItem);
}
},
);
return {
dataArray: resultData,
featureKeys,
};
}