Browse Source

走通工程导入导出逻辑

master
Cmen 3 years ago
parent
commit
f17fb64a93
  1. 64
      src/store/actions/index.ts
  2. 114
      src/store/utils/getGeoJSON.ts
  3. 1
      src/utils/index.ts
  4. 23
      src/utils/upload.ts

64
src/store/actions/index.ts

@ -1,7 +1,7 @@
import { IMapEditor } from "@map"; import { IMapEditor } from "@map";
import { Action } from "redux"; import { Action } from "redux";
import { Modal } from "antd"; import { Modal } from "antd";
import { registerHotkey, downloadJSON } from "@utils"; import { registerHotkey, downloadJSON, openUpload } from "@utils";
import { getGeoJSON } from "../utils"; import { getGeoJSON } from "../utils";
import { OverlayTypes, EventTypes } from "@types"; import { OverlayTypes, EventTypes } from "@types";
@ -49,16 +49,7 @@ export class EditorAction {
} }
setInterval(() => { setInterval(() => {
const { mapOptions } = this; this.saveTemp();
sessionStorage.setItem(
"fine-geojson",
JSON.stringify({
...mapOptions,
center: this.mapEditor?.getCenter(),
zoom: this.mapEditor?.getZoom(),
})
);
console.log("saved!");
}, 10000); }, 10000);
} }
// ---------- public methods ------------- // ---------- public methods -------------
@ -92,11 +83,6 @@ export class EditorAction {
}); });
} }
saveGeoJSON() {
const geojson = getGeoJSON(this.mapOptions);
downloadJSON(geojson, "fine.geojson");
}
selectOverlay(id: string) { selectOverlay(id: string) {
const { selectedIds } = this.store.getState(); const { selectedIds } = this.store.getState();
if (selectedIds.indexOf(id) >= 0) { if (selectedIds.indexOf(id) >= 0) {
@ -108,12 +94,24 @@ export class EditorAction {
} }
} }
saveGeoJSON() {
const geojson = getGeoJSON(this.mapOptions);
downloadJSON(geojson, "fine.geojson");
}
saveProject() { saveProject() {
// const mapOptions = this._getMapOptions();
downloadJSON(mapOptions, "fine.fjd");
} }
saveTemp() { saveTemp() {
// sessionStorage.setItem(
"fine-geojson",
JSON.stringify(this._getMapOptions())
);
console.log("saved!");
} }
clearOverlays() { clearOverlays() {
Modal.confirm({ Modal.confirm({
title: "确定移除所有覆盖物吗?", title: "确定移除所有覆盖物吗?",
@ -124,16 +122,30 @@ export class EditorAction {
}); });
} }
openGeoJSON() { openGeoJSON() {
// openUpload({
accept: ".geojson",
onReady: (content) => {
this._openGeoJSON(JSON.parse(content));
},
});
} }
openProject() { openProject() {
// openUpload({
accept: ".fjd",
onReady: (content) => {
this._openProject(JSON.parse(content));
},
});
} }
showCommand() { showCommand() {
// //
} }
private _openGeoJSON(geojson: GeoJSON.FeatureCollection) {
console.log(geojson);
}
private _openProject(options: IMapOptions) { private _openProject(options: IMapOptions) {
this.mapEditor?.importProject(options); this.mapEditor?.importProject(options);
this.dispatch( this.dispatch(
@ -144,8 +156,20 @@ export class EditorAction {
); );
} }
private _getMapOptions(): IMapOptions {
const { mapOptions } = this;
return {
...mapOptions,
center: this.mapEditor?.getCenter(),
zoom: this.mapEditor?.getZoom(),
};
}
private _bindActions() { private _bindActions() {
this.saveGeoJSON = this.saveGeoJSON.bind(this); this.saveGeoJSON = this.saveGeoJSON.bind(this);
this.saveProject = this.saveProject.bind(this);
this.openGeoJSON = this.openGeoJSON.bind(this);
this.openProject = this.openProject.bind(this);
this.createRectangle = this.createRectangle.bind(this); this.createRectangle = this.createRectangle.bind(this);
this.createPolygon = this.createPolygon.bind(this); this.createPolygon = this.createPolygon.bind(this);
this.createPolyline = this.createPolyline.bind(this); this.createPolyline = this.createPolyline.bind(this);

114
src/store/utils/getGeoJSON.ts

@ -6,70 +6,78 @@ const EarthRadius = 6378137;
export function getGeoJSON(state: IMapOptions): GeoJSON.FeatureCollection { export function getGeoJSON(state: IMapOptions): GeoJSON.FeatureCollection {
// //
const features: GeoJSON.Feature[] = []; const features: GeoJSON.Feature[] = [];
const { rectangles, polygons, polylines, circles } = state; const { overlays } = state;
rectangles.forEach((rect) => { overlays
const path = rect.path!; .filter((overlay) => overlay.type === OverlayTypes.Rectangle)
.forEach((rect) => {
const path = rect.path!;
features.push({ features.push({
type: "Feature", type: "Feature",
geometry: { geometry: {
type: "Polygon", type: "Polygon",
coordinates: [path], coordinates: [path],
}, },
properties: { properties: {
fineType: OverlayTypes.Rectangle, fineType: OverlayTypes.Rectangle,
}, },
});
}); });
});
polygons.forEach((polygon) => { overlays
const path = polygon.path!; .filter((overlay) => overlay.type === OverlayTypes.Polygon)
features.push({ .forEach((polygon) => {
type: "Feature", const path = polygon.path!;
geometry: { features.push({
type: "Polygon", type: "Feature",
coordinates: [path], geometry: {
}, type: "Polygon",
properties: { coordinates: [path],
fineType: OverlayTypes.Polygon, },
}, properties: {
fineType: OverlayTypes.Polygon,
},
});
}); });
});
polylines.forEach((polyline) => { overlays
const path = polyline.path!; .filter((overlay) => overlay.type === OverlayTypes.Polyline)
features.push({ .forEach((polyline) => {
type: "Feature", const path = polyline.path!;
geometry: { features.push({
type: "LineString", type: "Feature",
coordinates: path, geometry: {
}, type: "LineString",
properties: { coordinates: path,
fineType: OverlayTypes.Polyline, },
}, properties: {
fineType: OverlayTypes.Polyline,
},
});
}); });
});
circles.forEach((circle) => { overlays
const center = circle.lngLat!; .filter((overlay) => overlay.type === OverlayTypes.Circle)
const radius = circle.radius!; .forEach((circle) => {
const center = circle.lngLat!;
const radius = circle.radius!;
const path = getCirclePath(center, radius); const path = getCirclePath(center, radius);
features.push({ features.push({
type: "Feature", type: "Feature",
geometry: { geometry: {
type: "Polygon", type: "Polygon",
coordinates: [path], coordinates: [path],
}, },
properties: { properties: {
fineType: OverlayTypes.Circle, fineType: OverlayTypes.Circle,
center, center,
radius, radius,
}, },
});
}); });
});
return { return {
type: "FeatureCollection", type: "FeatureCollection",

1
src/utils/index.ts

@ -3,6 +3,7 @@ import { OverlayTypes } from "@types";
export * from "./hotkeys"; export * from "./hotkeys";
export * from "./download"; export * from "./download";
export * from "./upload";
export function getUID() { export function getUID() {
return uid(12); return uid(12);

23
src/utils/upload.ts

@ -0,0 +1,23 @@
const fileInput: HTMLInputElement = document.createElement("input");
fileInput.type = "file";
type UploadProps = {
accept: string;
onReady: (content: string) => void;
};
export function openUpload(props: UploadProps) {
fileInput.accept = props.accept;
fileInput.onchange = () => {
const { files } = fileInput;
if (files == null) return;
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target?.result?.toString()!;
props.onReady(content);
};
reader.readAsText(files[0]);
};
fileInput.files = null;
fileInput.click();
}
Loading…
Cancel
Save