基于node-gdal的矢量数据编辑
此前我参考了两篇node-gdal的软文,目前国内对node-gdal的案例并不多,在此我参考前人的软文以及结合了自己的实践经验记录下node-gdal的矢量数据(shp文件)入门操作。希望对使用node、gis技术栈的朋友有所帮助。
node-gdal于2020年9月底才支持了node12。如果安装不成功,请查看是不是node版本的问题。
教程环境 node:12.16.3 npm:6.14.8
一 node-gdal安装
Github地址:https://github.com/naturalatlas/node-gdal
使用nodejs安装:
npm install gdal –save / cnpm install gdal –save
一个读取shp文件信息的helloword 程序
const gdal = require("gdal");
// 查看所有支持的驱动程序
gdal.drivers.forEach(function (drive, i) {
console.log(drive.description);
});
//r只读 r+读取修改增加 w可写(创建)
const dataset = gdal.open("./shp/jiance.shp", "r+", "ESRI Shapefile");
const layer = dataset.layers.get(0);
// 获取图层的的数据长度
console.log("number of features: " + layer.features.count());
// 获取图层字段ID
console.log("fields: " + layer.fields.getNames());
dataset.layers.get(0).features.forEach(function (feature) {
console.log(feature.getGeometry().toJSON());
console.log(feature.fields.toJSON());
});
// 获取图层的四至
console.log("extent: " + JSON.stringify(layer.extent));
// 获取图层坐标系
console.log("srs: " + (layer.srs ? layer.srs.toWKT() : "null"));
往SHP文件增加数据
/* 新增数据 */
//构造一个feature
var feature = new gdal.Feature(layer);
//给字段赋值
feature.fields.set("类型", "不知道");
feature.fields.set("名称", "Grand Teton");
feature.fields.set("经度", -110.802414);
feature.fields.set("纬度", 43.741208);
feature.fields.set("高程", 35.248);
feature.setGeometry(new gdal.Point(43.741208, -110.802414, 35.248));
layer.features.add(feature);
// //将对shp图层的更改写入磁盘文件
layer.flush();
dataset.layers.get(0).features.forEach(function (feature) {
console.log(feature.getGeometry().toJSON());
console.log(feature.fields.toJSON());
});
根据逻辑 字段属性或者ID 删除shp矢量数据
/* 删除数据 */
layer.features.forEach((Feature) => {
console.log(Feature.fid);
console.log(Feature.fields);
// console.log(typeof Feature.fields.toJSON());
let filesJson = JSON.parse(Feature.fields.toJSON());
if (filesJson["名称"] === "Grand Teton") {
layer.features.remove(Feature.fid);
}
});
dataset.layers.get(0).features.forEach(function (feature) {
// console.log(feature);
console.log(feature.getGeometry().toJSON());
console.log(feature.fields.toJSON());
});
根据逻辑 字段属性或者ID 修改shp矢量数据
/* 修改数据 */
layer.features.forEach((Feature) => {
console.log(Feature.fid);
console.log(Feature.fields);
// console.log(typeof Feature.fields.toJSON());
let filesJson = JSON.parse(Feature.fields.toJSON());
if (filesJson["名称"] === "Grand Teton") {
// layer.features.remove(Feature.fid);
let updateFeature = new gdal.Feature(layer);
updateFeature.fields.set("类型", "不知道");
updateFeature.fields.set("名称", "Grand Teton");
updateFeature.fields.set("经度", -110.802414);
updateFeature.fields.set("纬度", 43.741208);
updateFeature.fields.set("高程", 65.248);
updateFeature.setGeometry(new gdal.Point(43.741208, -114.802414, 65.248));
layer.features.set(8, updateFeature); // 修改数据 指定ID修改
}
});
dataset.layers.get(0).features.forEach(function (feature) {
console.log(feature.fid);
console.log(feature.getGeometry().toJSON());
console.log(feature.fields.toJSON());
});
本站文章除注明转载外,均为原创文章。转载请注明:文章转载自:
葱爆GIS—刘博方GIS博客(
https://liubf.com )