mirror of https://gitee.com/antv-l7/antv-l7
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
|
var geojsonArea = require('@mapbox/geojson-area');
|
||
|
|
||
|
module.exports = rewind;
|
||
|
|
||
|
function rewind(gj, outer) {
|
||
|
switch ((gj && gj.type) || null) {
|
||
|
case 'FeatureCollection':
|
||
|
gj.features = gj.features.map(curryOuter(rewind, outer));
|
||
|
return gj;
|
||
|
case 'GeometryCollection':
|
||
|
gj.geometries = gj.geometries.map(curryOuter(rewind, outer));
|
||
|
return gj;
|
||
|
case 'Feature':
|
||
|
gj.geometry = rewind(gj.geometry, outer);
|
||
|
return gj;
|
||
|
case 'Polygon':
|
||
|
case 'MultiPolygon':
|
||
|
return correct(gj, outer);
|
||
|
default:
|
||
|
return gj;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function curryOuter(a, b) {
|
||
|
return function(_) { return a(_, b); };
|
||
|
}
|
||
|
|
||
|
function correct(_, outer) {
|
||
|
if (_.type === 'Polygon') {
|
||
|
_.coordinates = correctRings(_.coordinates, outer);
|
||
|
} else if (_.type === 'MultiPolygon') {
|
||
|
_.coordinates = _.coordinates.map(curryOuter(correctRings, outer));
|
||
|
}
|
||
|
return _;
|
||
|
}
|
||
|
|
||
|
function correctRings(_, outer) {
|
||
|
outer = !!outer;
|
||
|
_[0] = wind(_[0], outer);
|
||
|
for (var i = 1; i < _.length; i++) {
|
||
|
_[i] = wind(_[i], !outer);
|
||
|
}
|
||
|
return _;
|
||
|
}
|
||
|
|
||
|
function wind(_, dir) {
|
||
|
return cw(_) === dir ? _ : _.reverse();
|
||
|
}
|
||
|
|
||
|
function cw(_) {
|
||
|
return geojsonArea.ring(_) >= 0;
|
||
|
}
|