Skip to content

Commit 19860c4

Browse files
committed
更新文件导入
1 parent f9246b6 commit 19860c4

File tree

4 files changed

+129
-75
lines changed

4 files changed

+129
-75
lines changed

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

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,23 @@
2020
</template>
2121

2222
<script setup lang="ts">
23-
import { TIdentityGeoJSONFeature, Tools } from '../../../../packages/maplugin-core';
23+
import { TIdentityGeoJSONFeature } from '../../../../packages/maplugin-core';
24+
import FileProcesses from '../../services/file-processes';
2425
2526
const props = defineProps<{
2627
onUpload(features: Array<TIdentityGeoJSONFeature>): void,
2728
onDownload(): GeoJSON.FeatureCollection
2829
}>();
2930
30-
const file_process: Array<{
31-
extension: string,
32-
description: string,
33-
contentType: string
34-
decode?(file: File, onFinish: (features: Array<TIdentityGeoJSONFeature>) => void): void,
35-
encode?(geojson: GeoJSON.FeatureCollection): string | Blob
36-
}> = [
37-
{
38-
extension: ".geojson",
39-
description: "geojson",
40-
contentType: "application/json",
41-
decode(file, onFinish) {
42-
const reader = new FileReader();
43-
44-
reader.onload = e => {
45-
try {
46-
const geojson = JSON.parse(e.target?.result as string) as GeoJSON.FeatureCollection;
47-
geojson.features.forEach(f => {
48-
f.properties ??= {};
49-
if (!f.properties['id']) {
50-
f.properties['id'] = Tools.uuid();
51-
}
52-
});
53-
onFinish(geojson.features as any);
54-
} catch (e) {
55-
alert("geojson文件格式不正确");
56-
}
57-
};
58-
59-
reader.readAsText(file);
60-
},
61-
62-
encode(geojson) {
63-
return JSON.stringify(geojson, null, 4);
64-
}
65-
}
66-
]
67-
6831
function handleUpload() {
6932
const input = document.createElement('input');
7033
input.type = 'file';
71-
input.accept = file_process.filter(x => x.encode).map(x => x.extension).join("|");
72-
input.onchange = function (e: any) {
34+
input.accept = FileProcesses.filter(x => x.decode).map(x => x.extension).join(", ");
35+
input.onchange = async function (e: any) {
7336
const file: File = e.target.files[0];
7437
const extension = file.name.split('.').pop()!;
75-
file_process.find(x => x.extension === `.${extension}`)?.decode?.(file, features => props.onUpload(features));
38+
const features = await FileProcesses.find(x => x.extension === `.${extension}`)!.decode!(file);
39+
props.onUpload(features);
7640
input.remove();
7741
}
7842
input.click();
@@ -83,7 +47,7 @@ async function handleDownload() {
8347
8448
if ((window as any)['showSaveFilePicker']) {
8549
const opts = {
86-
types: file_process.filter(x => x.encode).map(x => ({
50+
types: FileProcesses.filter(x => x.encode).map(x => ({
8751
description: x.description,
8852
accept: {
8953
[x.contentType]: [x.extension]
@@ -97,8 +61,8 @@ async function handleDownload() {
9761
writable = await handle.createWritable(); // 创建可写入的文件对象
9862
9963
const extension = handle.name.split('.').pop()!;
100-
let encode = file_process.find(x => x.extension === `.${extension}`)?.encode;
101-
if (!encode) encode = file_process.find(x => x.extension === '.geojson')!.encode!;
64+
let encode = FileProcesses.find(x => x.extension === `.${extension}`)?.encode;
65+
if (!encode) encode = FileProcesses.find(x => x.extension === '.geojson')!.encode!;
10266
10367
writable.write(encode(geojson)).then(() => {
10468
writable?.close();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { TIdentityGeoJSONFeature, Tools } from "../../../packages/maplugin-maplibre";
2+
import shp from "shpjs";
3+
4+
function appendId(fc: GeoJSON.FeatureCollection) {
5+
fc.features.forEach(f => {
6+
f.properties ??= {};
7+
if (!f.properties['id']) {
8+
f.properties['id'] = Tools.uuid();
9+
}
10+
});
11+
return fc;
12+
}
13+
14+
const FileProcesses: Array<{
15+
extension: string,
16+
description: string,
17+
contentType: string
18+
decode?(file: File): Promise<Array<TIdentityGeoJSONFeature>>,
19+
encode?(geojson: GeoJSON.FeatureCollection): string | Blob
20+
}> = [{
21+
extension: ".geojson",
22+
description: "geojson",
23+
contentType: "application/json",
24+
async decode(file) {
25+
const str = await file.text();
26+
const geojson = JSON.parse(str) as GeoJSON.FeatureCollection;
27+
return appendId(geojson).features as any;
28+
},
29+
30+
encode(geojson) {
31+
return JSON.stringify(geojson, null, 4);
32+
}
33+
}, {
34+
extension: ".zip",
35+
description: "shp file (zip)",
36+
contentType: "application/x-zip-compressed",
37+
async decode(file) {
38+
const buffer = await file.arrayBuffer();
39+
const geojson = await shp(buffer) as GeoJSON.FeatureCollection;
40+
return appendId(geojson).features as any;
41+
}
42+
}]
43+
44+
export default FileProcesses;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"docs:build": "vitepress build docs",
1313
"docs:preview": "vitepress preview docs",
1414
"dev-ge": "vite --config ./geojson-editor/vite.config.ts",
15-
"build-ge":"vite build --config ./geojson-editor/vite.config.ts"
15+
"build-ge": "vite build --config ./geojson-editor/vite.config.ts"
1616
},
1717
"keywords": [],
1818
"author": "",
@@ -28,9 +28,11 @@
2828
"@types/mapbox__mapbox-gl-draw": "^1.4.8",
2929
"@types/node": "^22.8.6",
3030
"@types/proj4": "^2.5.5",
31+
"@types/shpjs": "^3.4.7",
3132
"@vitejs/plugin-vue": "^5.1.4",
3233
"bumpp": "^9.7.1",
3334
"monaco-editor": "^0.52.0",
35+
"shpjs": "^6.1.0",
3436
"typescript": "^5.6.3",
3537
"vite": "^5.4.8",
3638
"vite-plugin-dts": "^4.2.4",

0 commit comments

Comments
 (0)