nodejs 把CSV转换成trip geojson

const path = require('path');
const fs = require('fs');
const http = require('http')


// 构造geojson对象,features为空列表

var myCoordinates = [];
var myGeoJson = {
    "type": "FeatureCollection",
    "name": "poi",
    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
    "features": [
        { 
            "type": "Feature", 
            "properties": { "JLX": "4101", "MLP": "1277号", "ZXJD": "113.30826 ", "ZXWD": "23.15027 ", "LASTUPDATEDTIME": "2021-04-06 11:25:40", "timestamp": "1617679540" }, 
            "geometry": { 
                "type": "LineString", 
                "coordinates": myCoordinates
            } 
        }
    ]
};

//console.log('test');

// readFile方法是将要读取的文件内容完整读入缓存区,再从该缓存区中读取文件内容

fs.readFile('./data/5542.csv', 'utf-8',function (err, data) {
    var table = new Array();
    if (err) {
        console.log(err.stack);
        return;
    };

    ConvertToTable(data, function (table) {
        //console.log(table.length);
        //console.log(table[0]);
        //console.log(table[1][2]);
        for (var i=1;i<table.length;i++){
            myCoordinates.push([parseFloat(table[i][4]),parseFloat(table[i][3]),0,parseInt(table[i][2]) ]);
        };
        //console.log(myGeoJson.features[0].geometry);
        //console.log(myCoordinates[0]);
        var myData =JSON.stringify(myGeoJson) ;

        fs.writeFile('./data/5542.geojson', myData, function (error) {
            if (error) {
              console.log('写入失败')
            } else {
                //console.log(myData);
              console.log('写入成功了')
            }
        });

    })
});
console.log("程序执行完毕");

function ConvertToTable(data, callBack) {
    data = data.toString();
    var table = new Array();
    var rows = new Array();
    rows = data.split("\r\n");
    for (var i = 0; i < rows.length; i++) {
        table.push(rows[i].split(","));
    }
    callBack(table);
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值