# mapshaper-demo **Repository Path**: cellinlab/mapshaper-demo ## Basic Information - **Project Name**: mapshaper-demo - **Description**: mapshaper-demo - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2019-11-29 - **Last Updated**: 2022-09-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mapshaper-demo #### 介绍 mapshaper-demo 该项目旨在为mapshaper在node中使用提供示例,并对一些常用shp转换接口进行封装方便使用。在前端中使用可以参考[shpconverter](https://gitee.com/SatImage2014/shpconverter)。 #### 安装教程 1. 在node项目中安装mapshaper ``` powershell npm install mapshaper ``` 2. 复制shpConverter模块到项目中 封装的shpConverter模块文件位于 [mapshaper-demo/utils](https://gitee.com/cellinlab/mapshaper-demo/blob/master/utils/shpConverter.js) 路径下 3. 安装shpConverter模块涉及的依赖模块(当前只涉及zip解压) ``` powershell npm install unzipper ``` #### 使用说明 这里只对参数和返回值进行简单介绍,具体实现细节可查看[mapshaper-demo/utils](https://gitee.com/cellinlab/mapshaper-demo/blob/master/utils/shpConverter.js) 1. geojson2shp ##### 接口说明 ``` javascript /** * 将geojson格式的json文件转换为shp系列文件 * @param {Buffer} inputFile 要转换的json文件的Buffer形式 * @param {String} outputName 输出文件名称,不含后缀,可选,默认'export' * @returns {Promise} * reject error Object * resolve [Object] 转换结果对象,属性为文件名,属性值为文件[Buffer],可通过遍历对象属性及属性值获取或写出 */ function geojson2shp(inputFile, outputName) {} ``` ##### 示例 ``` javascript const inputJson = fs.readFileSync('./testdata/two_states.json') await ShpConverter.geojson2shp(inputJson, 'fromJson') .then(result => { console.log('geojson2shpTest success') // 遍历结果对象,把结果写入目录 for (const filename in result) { fs.writeFileSync(`${outputPath}${filename}`, result[filename]) } }) .catch(err => { console.log('geojson2shpTest error') console.log(err) }) ``` 2. shp2geojson shp系列文件形式 ##### 接口说明 ``` javascript /** * 将shp系列文件转换为geojson格式json文件 * @param {Object} inputFiles 含shp系列文件Buffer的Map对象 * @param {String} outputName 输出文件名称,不含后缀,可选,默认'export' */ function shp2geojson(inputFiles, outputName) ``` ##### 示例 ``` javascript const shpFile = fs.readFileSync('./testdata/two_states.shp') const shxFile = fs.readFileSync('./testdata/two_states.shx') const dbfFile = fs.readFileSync('./testdata/two_states.dbf') const prjFile = fs.readFileSync('./testdata/two_states.prj') await ShpConverter.shp2geojson( {shpFile, shxFile, prjFile, dbfFile}, 'fromShps' ) .then(result => { console.log('shp2geojsonTest success') // 遍历结果对象,把结果写入目录 for (const filename in result) { fs.writeFileSync(`${outputPath}${filename}`, result[filename]) } }) .catch(err => { console.log('shp2geojsonTest error') console.log(err) }) ``` 3. shp2geojson shp系列文件zip压缩包形式 ##### 接口说明 ``` javascript /** * 将shp系列文件转换为geojson格式json文件 * @param {Object} inputFiles 含zip文件Buffer的Map对象 * @param {String} outputName 输出文件名称,不含后缀,可选,默认'export' */ function shp2geojson(inputFiles, outputName) ``` ##### 示例 ``` javascript const zipFile = fs.readFileSync('./testdata/testdata.zip') await ShpConverter.shp2geojson({zipFile}, 'fromZip') .then(result => { console.log('shpzip2geojsonTest success') // 遍历结果对象,把结果写入目录 for (const filename in result) { fs.writeFileSync(`${outputPath}${filename}`, result[filename]) } }) .catch(err => { console.log('shpzip2geojsonTest error') console.log(err) }) ``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 更多 1. 如遇到bug或有新的建议可以新建 Issue进行说明 2. 也可以直接和我联系qq 1746806993