From 29dcb0ea66c65bbf4fae0a9f10a6f5a485bbbf61 Mon Sep 17 00:00:00 2001 From: Cmen <1176967590@qq.com> Date: Thu, 10 Mar 2022 20:23:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=AF=BC=E5=85=A5geojson?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/actions/index.ts | 5 ++-- src/store/utils/getMapOptions.ts | 50 ++++++++++++++++++++++++++++++++ src/store/utils/index.ts | 1 + 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/store/utils/getMapOptions.ts diff --git a/src/store/actions/index.ts b/src/store/actions/index.ts index 24b581e..bdfd612 100644 --- a/src/store/actions/index.ts +++ b/src/store/actions/index.ts @@ -9,7 +9,7 @@ import { setItem, } from "@utils"; -import { getGeoJSON } from "../utils"; +import { getGeoJSON, getMapOptions } from "../utils"; import { OverlayTypes, EventTypes } from "@types"; import { IOverlay, IMapOptions } from "@map"; import { StoreAction } from "./StoreAction"; @@ -184,7 +184,8 @@ export class EditorAction { } private _openGeoJSON(geojson: GeoJSON.FeatureCollection) { - console.log(geojson); + const options = getMapOptions(geojson); + this._openProject(options); } private _openProject(options: IMapOptions) { diff --git a/src/store/utils/getMapOptions.ts b/src/store/utils/getMapOptions.ts new file mode 100644 index 0000000..445b711 --- /dev/null +++ b/src/store/utils/getMapOptions.ts @@ -0,0 +1,50 @@ +import { IMapOptions, IOverlay } from "@map"; +import { OverlayTypes } from "@types"; +import { getUID } from "@utils"; + +export function getMapOptions(geojson: GeoJSON.FeatureCollection) { + const { features } = geojson; + const mapOptions: IMapOptions = { + overlays: [], + }; + + const addPolygon = (feature: GeoJSON.Feature) => { + const { geometry, properties } = feature; + const { name, fineType, lngLat, radius } = properties as any; + const { coordinates } = geometry as GeoJSON.Polygon; + const [path] = coordinates; + const overlay: IOverlay = { + id: getUID(), + name, + type: OverlayTypes.Polygon, + }; + if (fineType === "rect") { + overlay.path = path; + overlay.type = OverlayTypes.Rectangle; + } else if (fineType === "circle") { + overlay.lngLat = lngLat; + overlay.radius = radius; + overlay.type = OverlayTypes.Circle; + } else { + overlay.type = OverlayTypes.Polygon; + overlay.path = path; + } + mapOptions.overlays.push(overlay); + }; + + const addPolyline = (feature: GeoJSON.Feature) => { + // + }; + + features.forEach((feature) => { + const { geometry } = feature; + const { type } = geometry; + if (type === "Polygon") { + addPolygon(feature); + } else { + addPolyline(feature); + } + }); + + return mapOptions; +} diff --git a/src/store/utils/index.ts b/src/store/utils/index.ts index 3793b30..8c15c10 100644 --- a/src/store/utils/index.ts +++ b/src/store/utils/index.ts @@ -1 +1,2 @@ export * from "./getGeoJSON"; +export * from "./getMapOptions";