highmap gejson数据格式转换脚本

highmap数据源:

Highmaps - Map Collection

从上面的网址下载geojson格式的数据,然后使用下面的脚本进行转换。

#!/usr/bin/env node
const fs = require('fs');
const proj4 = require('proj4');

const argvArr = process.argv; //参数
const origialPath = argvArr[2]; // 传入的第一个参数,读取文件
const outPath = argvArr[3]; // 传入的第二个参数,保存文件

fs.readFile(origialPath,"utf-8", (err, data) => {
    if(err) {
        return console.error("read error: ", err);
    }
    const originalData = JSON.parse(data);
    const transform = originalData["hc-transform"].default;
    try{
        originalData.features.forEach(feature => {
            feature.geometry.coordinates.forEach(coord => {
                swap(coord, transform);
            })
            feature.properties = {
                name: feature.properties.name
            }
        })
        fs.writeFile(outPath, JSON.stringify(originalData), 'utf-8', e => {
            if(e) {
                return console.error('write error:', e);
            } else {
                return console.log('write success!')
            }
        })
    } catch(error) {
        console.error('no transform :', error);
    }
})

function swap(arr, transform){
    if(Array.isArray(arr[0])){
        arr.forEach(item => {
            swap(item, transform)
        })
    } else {
        const temp = pointToLonlat({x: arr[0], y: arr[1]}, transform);
        arr[0] = temp.lon;
        arr[1] = temp.lat;
    }
}


function pointToLonlat(point, transform) {
    var normalized = {
        x: (
            (
                point.x -
                (transform.jsonmarginX || 0)
            ) / (transform.jsonres || 1) -
            (transform.xpan || 0)
        ) / (transform.scale || 1) +
        (transform.xoffset || 0),
        y: -(
            (
                -point.y + (transform.jsonmarginY || 0)
            ) / (transform.jsonres || 1) -
            (transform.ypan || 0)
        ) / (transform.scale || 1) +
        (transform.yoffset || 0)
    },
        cosAngle = transform.cosAngle ||
        (transform.rotation && Math.cos(transform.rotation)),
        sinAngle = transform.sinAngle ||
        (transform.rotation && Math.sin(transform.rotation)),
        // Note: Inverted sinAngle to reverse rotation direction
        projected = proj4(transform.crs, 'WGS84', transform.rotation ? {
            x: normalized.x * cosAngle + normalized.y * -sinAngle,
            y: normalized.x * sinAngle + normalized.y * cosAngle
        } : normalized);
    return { lat: projected.y, lon: projected.x };
};

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乘风xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值