Skip to content

Instantly share code, notes, and snippets.

@Hirosaji
Last active July 26, 2021 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hirosaji/d1b2506189820f7631fc67312e006853 to your computer and use it in GitHub Desktop.
Save Hirosaji/d1b2506189820f7631fc67312e006853 to your computer and use it in GitHub Desktop.
format Arctic sea ice Polygon data
const fs = require('fs');
const geojson = JSON.parse(fs.readFileSync('./input.geojson', 'utf8'));
const splitIndexes = [];
geojson.features.forEach(a => {
const _data = [];
a.geometry.coordinates.forEach(b => {
const __data = [];
b.forEach((c, i) => {
if (i !== 0 && Math.abs(c[0] - b[i - 1][0]) > 180) __data.push(i);
})
_data.push(__data);
})
splitIndexes.push(_data);
})
geojson.features.forEach((a, l) => {
const coordinates = [];
a.geometry.coordinates.forEach((b, m) => {
const target = splitIndexes[l][m];
if (target.length > 0) {
const _coordinates_odd = [];
const _coordinates_even = [];
target.forEach((splitIndex, n) => {
const sliceStart = (n === 0) ? 0 : target[n - 1];
const sliceTarget = b.slice(sliceStart, splitIndex);
sliceTarget.push(sliceTarget[sliceTarget.length - 1]);
if (n % 2 !== 0) _coordinates_odd.push(sliceTarget);
else _coordinates_even.push(sliceTarget);
if (n === target.length - 1) {
const sliceTarget2 = b.slice(splitIndex, b.length);
sliceTarget2.push(sliceTarget2[sliceTarget2.length - 1]);
if (n % 2 !== 0) _coordinates_even.push(sliceTarget2);
else _coordinates_odd.push(sliceTarget2);
}
})
const _coordinates = [];
[_coordinates_odd, _coordinates_even.reverse()].forEach((coors, j) => {
coors.forEach((coo, i) => {
const thisFirst = coo[0];
const thisLast = coo[coo.length-1];
if (thisFirst[0] > 0) {
_coordinates.push([[180, 90]]);
_coordinates.push([[180, thisFirst[1]]]);
}
else {
_coordinates.push([[-180, 90]]);
_coordinates.push([[-180, thisFirst[1]]]);
}
_coordinates.push(coo);
if (thisLast[0] > 0) {
_coordinates.push([[180, thisLast[1]]]);
_coordinates.push([[180, 90]]);
}
else {
_coordinates.push([[-180, thisLast[1]]]);
_coordinates.push([[-180, 90]]);
}
})
})
_coordinates.push([_coordinates[0][0]]);
coordinates.push(_coordinates.flat());
}
else coordinates.push(b);
})
a.geometry.coordinates = coordinates;
a.properties = {};
})
fs.writeFileSync('output.geojson', JSON.stringify(geojson));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment