Published
Edited
Aug 23, 2020
Importers
Insert cell
md`# geojson-rewind
all copyright goes to its appropriate owner


copy & pasted from: https://github.com/mapbox/geojson-rewind
`
Insert cell
function rewindRing(ring, dir) {
var area = 0;
for (var i = 0, len = ring.length, j = len - 1; i < len; j = i++) {
area += (ring[i][0] - ring[j][0]) * (ring[j][1] + ring[i][1]);
}
if (area >= 0 !== !!dir) ring.reverse();
}
Insert cell
function rewindRings(rings, outer) {
if (rings.length === 0) return;

rewindRing(rings[0], outer);
for (var i = 1; i < rings.length; i++) {
rewindRing(rings[i], !outer);
}
}
Insert cell
function rewind(gj, outer) {
var type = gj && gj.type, i;

if (type === 'FeatureCollection') {
for (i = 0; i < gj.features.length; i++) rewind(gj.features[i], outer);

} else if (type === 'GeometryCollection') {
for (i = 0; i < gj.geometries.length; i++) rewind(gj.geometries[i], outer);

} else if (type === 'Feature') {
rewind(gj.geometry, outer);

} else if (type === 'Polygon') {
rewindRings(gj.coordinates, outer);

} else if (type === 'MultiPolygon') {
for (i = 0; i < gj.coordinates.length; i++) rewindRings(gj.coordinates[i], outer);
}

return gj;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more