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";