# shpconverter
**Repository Path**: cellinlab/shpconverter
## Basic Information
- **Project Name**: shpconverter
- **Description**: shp2geojson geojson2shp support zip file
- **Primary Language**: JavaScript
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2019-09-27
- **Last Updated**: 2023-02-22
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# shpconverter
#### 介绍
ShapeFile和GeoJSON的相互转换
#### 结构说明
1. 整体结构
```javascript
// ShpConverter工具库
// Converter模块
// exportGeojson方法
// exportShp方法
// 其他方法
// Utils模块
// utilA 方法
// utilB 方法
// 其他方法
// 其他模块
// 其他方法
import ShpConverter from 'shpconverter'
ShpConverter.Converter.exportGeojson(files)
.then()
.catch()
ShpConverter.Utils.utilA()
```
#### 安装教程
1. 浏览器直接使用
```html
```
2. NPM安装
```powershell
npm i git+https://gitee.com/cellinlab/shpconverter.git
```
3. ESM 方式使用
``` javascript
// shpconverter.esm.js 位于项目dist目录下
// 需手动安装相关依赖
import shpConverter from '../utils/shpconverter.esm.js'
shpConverter.Utils.utilTest('test')
```
#### 使用说明
1. 基础用法
```javascript
// 导出GeoJSON
shpConverter.exportGeojson(input)
.then((result) => {
saveBlobToDownloadFolder(result.filename, result.content)
})
.catch((e) => {
console.log(e)
})
// 导出ShpFile
shpConverter.exportShp(input)
.then((result) => {
saveBlobToDownloadFolder(result.filename, result.content)
})
.catch((e) => {
console.log(e)
})
```
2. 参数及返回值说明
```javascript
// input 单个或多个文件 or 单个json 对象
// 单个支持*.zip、*.json、*.shp
// 多个支持 *.shp、*.shx、*.prj、*.dbf
// opts 参数
// 支持input为json对象时接受自定义输出文件名
shpConverter.Converter.exportGeojson(input,{exportFilename:'customFilename'})
shpConverter.Converter.exportShp(input,{exportFilename:'customFilename'})
```
##### Arguments
name | type | descriptioin
-|-|-
input|FileList|要进行转换的文件,单个*.zip、*.json、*.shp文件,或*.shp、*.shx、*.prj、*.dbf等多个文件
-|Object|单个json对象
opts|Object|备用参数
###### json对象结构示例
- 结构1
``` json
{
"type":"Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[
13.36617949899573,
52.44599246921813
],
//...
]
]
},
"properties":{
"name":"Tempelhof"
}
}
```
- 结构2
```json
{
"type":"Polygon",
"coordinates":[
[
[
13.36617949899573,
52.44599246921813
],
//...
]
]
}
```
- 结构3
```json
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"type":"Polygon",
"coordinates":[
[
[
13.36617949899573,
52.44599246921813
],
//...
]
]
},
"properties":{
"name":"Tempelhof"
}
}
]
}
```
#### Input
##### Result
###### 输出的Bolb转JSON对象示例
``` JavaScript
shpConverter.Converter.exportGeojson(files)
.then((result) => {
// 转JSON提取geometry
const fr = new FileReader()
fr.onload = function() {
const resultJSON = JSON.parse(this.result)
console.log(resultJSON.features)
}
fr.readAsText(result.content, 'UTF-8')
saveBlobToDownloadFolder(result.filename, result.content)
})
.catch((e) => {
alert(e)
})
```
#### 测试数据
/test/test_data