-
Notifications
You must be signed in to change notification settings - Fork 625
/
geojson.js
38 lines (37 loc) · 1.16 KB
/
geojson.js
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
import * as turfMeta from '@turf/meta';
import { getCoords } from '@turf/invariant';
import { djb2hash } from '../../util/bkdr-hash';
import rewind from '@mapbox/geojson-rewind';
export default function geoJSON(data, cfg) {
// 矢量瓦片图层不做 rewind
rewind(data, true);
const resultData = [];
const featureKeys = {};
data.features = data.features.filter(item => {
return item != null && item.geometry && item.geometry.type && item.geometry.coordinates && item.geometry.coordinates.length > 0;
});
// 数据为空时处理
let i = 0;
turfMeta.flattenEach(data, (currentFeature, featureIndex) => { // 多个polygon 拆成一个
const coord = getCoords(currentFeature);
let id = featureIndex + 1;
if (cfg.idField && currentFeature.properties[cfg.idField]) {
const value = currentFeature.properties[cfg.idField];
id = djb2hash(value) % 1000019;
featureKeys[id] = {
index: i++,
idField: value
};
}
const dataItem = {
...currentFeature.properties,
coordinates: coord,
_id: id
};
resultData.push(dataItem);
});
return {
dataArray: resultData,
featureKeys
};
}