antv-l7/node_modules/@mapbox/geojson-rewind/index.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-11-21 12:04:58 +08:00
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;
}