1、shapefile数据说明
ESRI Shapefile(shp),或简称shapefile,是美国环境系统研究所公司(ESRI)开发的一种空间数据开放格式。[1]该文件格式已经成为了地理信息软件界的一个开放标准,这表明ESRI公司在全球的地理信息系统市场的重要性。Shapefile也是一种重要的交换格式,它能够在ESRI与其他公司的产品之间进行数据互操作。
Shapefile文件用于描述几何体对象:点,折线与多边形。例如,Shapefile文件可以存储井、河流、湖泊等空间对象的几何位置。除了几何位置,shp文件也可以存储这些空间对象的属性,例如一条河流的名字,一个城市的温度等等。
2、解析库
npm install shapefile
2.1 接口
1、shapefile.read(shp[, dbf[, options]])
直接读取shp文件,并直接返回geojson格式数据
2、shapefile.open(shp[, dbf[, options]])
返回source信息,需要逐行读取
3、options包括
1、encoding:windows-1252
2、highWaterMark
:defaults to 65536
3、实现代码
1、采用read模式
const shp2json=(shpPath,dbfShp,callback)=>{shapefile.read(shpPath,dbfShp).then(result=>{callback(result);})
}
2、采用 open模式
const shp2json=(shpPath,dbfShp,callback)=>{let features = [];shapefile.open(shpPath,dbfShp).then(async source => {let result = await source.read();while (!result.done){features.push(result.value);result = await source.read();}callback({type: "FeatureCollection",features: features,});})
}
3、调用代码
shp2json("shp/plot/plot.shp","shp/plot/plot.dbf",(result)=>{console.log(result);
});