Skip to content

Instantly share code, notes, and snippets.

@kenjiSpecial
Created July 26, 2019 04:10
Show Gist options
  • Save kenjiSpecial/fe2e008c8ab89c54506b015fd59ccd99 to your computer and use it in GitHub Desktop.
Save kenjiSpecial/fe2e008c8ab89c54506b015fd59ccd99 to your computer and use it in GitHub Desktop.
GeoJsonのデータをgl.linesで使えるjsonファイルに書き出し
import fs from 'fs';
import jsonfile from 'jsonfile';
import { IGeoJson } from './geo-json';
const eartDataUrl ='earth-line.json';
fs.readFile('./json/ne_110m_land.json', 'utf-8', (err, data) => {
const geoData = JSON.parse(data) as IGeoJson;
const pointData = [];
const indices = [];
const vertices = [];
let cnt = 0;
for (const feature of geoData.features) {
if (cnt > 0) {
// break;
}
for (const coordiantes of feature.geometry.coordinates) {
const pointArr = [];
for (const coordinate of coordiantes) {
const vec3 = lonLatToVec3(coordinate[0], coordinate[1]);
pointArr.push(vec3);
}
pointData.push(pointArr);
}
cnt = cnt + 1;
}
let startCnt = 0;
for (const pointArr of pointData) {
const pointSize = pointArr.length;
for (const point of pointArr) {
vertices.push(point[0], point[1], point[2]);
}
for (let ii = 0; ii < pointSize; ii = ii + 1) {
const index0 = startCnt + ii;
const index1 = startCnt + ((ii + 1) % pointSize);
indices.push(index0, index1);
}
startCnt = startCnt + pointArr.length;
}
const polygonData = { vertices: vertices, indices: indices };
jsonfile.writeFile(eartDataUrl, polygonData, () => {});
});
function lonLatToVec3(_lng: number, _lat: number) {
const lng = (_lng / 180) * Math.PI;
const lat = Math.PI / 2 - (_lat / 180) * Math.PI;
const x = Math.sin(lat) * Math.sin(lng);
const y = Math.cos(lat);
const z = Math.sin(lat) * Math.cos(lng);
return [parseFloat(x.toFixed(3)), parseFloat(y.toFixed(3)), parseFloat(z.toFixed(3))];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment