-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (118 loc) · 3.74 KB
/
index.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
*
* @param {*} geojson
* @param {*} coordinateCount per geojons coordinate count
* @returns
*/
export function seg(geojson, coordinateCount = 5000) {
if (isArray(geojson)) {
return arraySeg(geojson.data || geojson.RECORDS || geojson, coordinateCount);
}
if (!geojson || !geojson.features || geojson.features.length === 0) {
return [];
}
const geojsons = [];
let totalCount = 0;
let features = [];
for (let i = 0, len = geojson.features.length; i < len; i++) {
const count = measureCoordianteCount(geojson.features[i]);
if (count >= coordinateCount) {
geojsons.push(getGeoJSON([geojson.features[i]], count));
// if (count > coordinateCount) {
// console.warn(geojson.features[i], `is big ,coordinate count(${count}) >${coordinateCount}`);
// }
continue;
}
if (count + totalCount <= coordinateCount) {
features.push(geojson.features[i]);
totalCount += count;
} else {
geojsons.push(getGeoJSON(features, totalCount));
features = [geojson.features[i]];
totalCount = count;
}
}
if (features.length) {
geojsons.push(getGeoJSON(features, totalCount));
}
return geojsons;
}
function isArray(geojson) {
return Array.isArray(geojson) || Array.isArray(geojson.data) || Array.isArray(geojson.RECORDS);
}
function arraySeg(data, coordinateCount) {
const result = [], errorData = [];
let features = [], totalCount = 0;
for (let i = 0, len = data.length; i < len; i++) {
const d = data[i];
const { lnglat, lnglats, coordinates, fanwei, zuobiao, xy, xys, location } = (d || {});
const ll = lnglats || lnglat || coordinates || fanwei || zuobiao || xy || xys || location;
if (!ll || ll.indexOf(',') === -1 || !(typeof ll === 'string') || ll.indexOf('encode:') > -1) {
errorData.push(d);
continue;
}
let count = 0;
if (ll.replaceAll) {
const strLen = ll.length;
const str = ll.replaceAll(',', '');
count = strLen - str.length;
} else {
count = ll.split(',').length - 1;
}
if (ll.indexOf(';') === -1) {
count /= 2;
}
// d._coordinateCount = count;
if (count >= coordinateCount) {
result.push([d]);
continue;
}
if (totalCount + count <= coordinateCount) {
features.push(d);
totalCount += count;
} else {
result.push(features);
features = [d];
totalCount = count;
}
}
if (features.length) {
result.push(features);
}
if (errorData.length) {
result.push(errorData);
}
return result;
}
// const APPLICATION_JSON = { type: 'application/json' };
// const MB_SIZE = 1024 * 1024;
export function measureCoordianteCount(feature) {
if (feature.geometry && feature.geometry.coordinates && Array.isArray(feature.geometry.coordinates)) {
const { coordinates } = feature.geometry;
if (Array.isArray(coordinates[0])) {
return forEachRing(coordinates);
}
// point
return 1;
}
return 0;
}
function forEachRing(coordinates) {
// multipoint,linestring
if (!Array.isArray(coordinates[0][0])) {
return coordinates.length;
}
// MultiLineString,Polyogn,MultiPolygon
let count = 0;
for (let i = 0, len = coordinates.length; i < len; i++) {
count += forEachRing(coordinates[i]);
}
return count;
}
function getGeoJSON(features, coordinateCount) {
return {
type: 'FeatureCollection',
features,
coordinateCount
};
}