function invert(d) {
const shared = {};
let p = {
type: "Polygon",
coordinates: d3.merge(d.coordinates.map(polygon => {
return polygon.map(ring => {
return ring.map(point => {
return [point[0] / n * 360 - 180, 90 - point[1] / m * 180];
}).reverse();
});
}))
};
p.coordinates.forEach(ring => {
ring.forEach(p => {
if (p[0] === -180) shared[p[1]] |= 1;
else if (p[0] === 180) shared[p[1]] |= 2;
});
});
p.coordinates.forEach(ring => {
ring.forEach(p => {
if ((p[0] === -180 || p[0] === 180) && shared[p[1]] !== 3) {
p[0] = p[0] === -180 ? -179.9995 : 179.9995;
}
});
});
p = d3.geoStitch(p);
return p.coordinates.length
? {type: "Polygon", coordinates: p.coordinates}
: {type: "Sphere"};
}