Skip to content

Commit 1ffdf59

Browse files
committed
增加shape文件导入导出
1 parent 19860c4 commit 1ffdf59

File tree

4 files changed

+125
-5
lines changed

4 files changed

+125
-5
lines changed

geojson-editor/src/components/features/IO.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ async function handleDownload() {
6464
let encode = FileProcesses.find(x => x.extension === `.${extension}`)?.encode;
6565
if (!encode) encode = FileProcesses.find(x => x.extension === '.geojson')!.encode!;
6666
67-
writable.write(encode(geojson)).then(() => {
67+
const encodeData = await encode(geojson);
68+
writable.write(encodeData).then(() => {
6869
writable?.close();
6970
});
7071

geojson-editor/src/services/file-processes.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TIdentityGeoJSONFeature, Tools } from "../../../packages/maplugin-maplibre";
2-
import shp from "shpjs";
2+
import shpReader from "shpjs";
3+
import shpWriter from '@mapbox/shp-write';
34

45
function appendId(fc: GeoJSON.FeatureCollection) {
56
fc.features.forEach(f => {
@@ -16,7 +17,7 @@ const FileProcesses: Array<{
1617
description: string,
1718
contentType: string
1819
decode?(file: File): Promise<Array<TIdentityGeoJSONFeature>>,
19-
encode?(geojson: GeoJSON.FeatureCollection): string | Blob
20+
encode?(geojson: GeoJSON.FeatureCollection): Promise<string | Blob>
2021
}> = [{
2122
extension: ".geojson",
2223
description: "geojson",
@@ -27,7 +28,7 @@ const FileProcesses: Array<{
2728
return appendId(geojson).features as any;
2829
},
2930

30-
encode(geojson) {
31+
async encode(geojson) {
3132
return JSON.stringify(geojson, null, 4);
3233
}
3334
}, {
@@ -36,8 +37,14 @@ const FileProcesses: Array<{
3637
contentType: "application/x-zip-compressed",
3738
async decode(file) {
3839
const buffer = await file.arrayBuffer();
39-
const geojson = await shp(buffer) as GeoJSON.FeatureCollection;
40+
const geojson = await shpReader(buffer) as GeoJSON.FeatureCollection;
4041
return appendId(geojson).features as any;
42+
},
43+
async encode(geojson) {
44+
return (await shpWriter.zip(geojson, {
45+
'outputType': 'blob',
46+
'compression': 'DEFLATE'
47+
})) as Blob;
4148
}
4249
}]
4350

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"proj4": "^2.12.1"
2525
},
2626
"devDependencies": {
27+
"@mapbox/shp-write": "^0.4.3",
2728
"@types/geojson": "^7946.0.14",
2829
"@types/mapbox__mapbox-gl-draw": "^1.4.8",
2930
"@types/node": "^22.8.6",

0 commit comments

Comments
 (0)