接上文:IDEA中配置运行node.js
一、安装所需的npm包
1.读写shp文件的js库gdal
安装命令
npm install gdal -g --save
node-gdal库地址:https://github.com/naturalatlas/node-gdal 文档地址:http://naturalatlas.github.io/node-gdal/classes/gdal.html
2.修改shp文件数据坐标系的js库gcoord
安装命令
npm install gcoord -g
gcoord库地址:https://github.com/hujiulong/gcoord
二、转换代码
两个包安装完成后就可以开始写代码啦,完整的转换代码如下:
const gcoord = require('gcoord');
const gdal = require("gdal");
var dataset = gdal.open("C:\\Users\\syc\\Desktop\\my_road_data\\sz_main_road_test\\mainRoad.shp", "r+", "ESRI Shapefile");
var layer = dataset.layers.get(0);
console.log("number of features: " + layer.features.count());
var i = 0;
var sum = 0;
layer.features.forEach((feature) => {
var geojson = JSON.parse(feature.getGeometry().toJSON());
gcoord.transform(geojson, gcoord.GCJ02, gcoord.WGS84);
feature.setGeometry(gdal.Geometry.fromGeoJson(geojson));
layer.features.set(feature.fid, feature);
i++;
sum++;
if (i === 1000) {
console.log("已完成数据转换" + sum + "条...............");
i = 0;
}
})
console.log("<-----------------转换完成,共转换完成"+ sum + "条数据------------------------>");
|