|
@@ -0,0 +1,144 @@
|
|
|
+import { drawTool } from "./drawTool";
|
|
|
+// 引入OpenLayers的主模块
|
|
|
+import Map from 'ol/Map';
|
|
|
+import View from 'ol/View';
|
|
|
+import Projection from 'ol/proj/Projection';
|
|
|
+import { getWidth, getTopLeft } from 'ol/extent';
|
|
|
+import TileLayer from 'ol/layer/Tile';
|
|
|
+import WMTS from 'ol/source/WMTS';
|
|
|
+import WMTSTileGrid from 'ol/tilegrid/WMTS';
|
|
|
+import WMTSCapabilities from 'ol/format/WMTSCapabilities';
|
|
|
+import proj4 from 'proj4';
|
|
|
+import { register } from 'ol/proj/proj4';
|
|
|
+import { defaults } from 'ol/control';
|
|
|
+import axios from 'axios';
|
|
|
+import { useHistory } from '@/hooks/useHistory';
|
|
|
+import olPlot from 'ol-plot';
|
|
|
+
|
|
|
+proj4.defs('EPSG:4490', '+proj=longlat +ellps=GRS80 +no_defs +type=crs');
|
|
|
+proj4.defs('EPSG:4525', '+proj=tmerc +lat_0=0 +lon_0=111 +k=1 +x_0=37500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs');
|
|
|
+register(proj4);
|
|
|
+const commonUrl = import.meta.env.VITE_APP_BASE_API + '/api/oneShare/proxyHandler/mm/';
|
|
|
+const projection = new Projection({
|
|
|
+ code: 'EPSG:4490',
|
|
|
+ units: 'degrees'
|
|
|
+});
|
|
|
+const projectionExtent = [-180, -90, 180, 90];
|
|
|
+const size = getWidth(projectionExtent) / 256;
|
|
|
+const resolutions = [];
|
|
|
+for (let z = 2; z < 22; ++z) {
|
|
|
+ resolutions[z] = size / Math.pow(2, z);
|
|
|
+}
|
|
|
+
|
|
|
+export class olMap {
|
|
|
+ private map;
|
|
|
+ private drawOptions = {
|
|
|
+ strokeColor: options.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: 2,
|
|
|
+ fillColor: options.color,
|
|
|
+ fillOpacity: options.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ // private { currentState, commit, undo, history, future } = useHistory();
|
|
|
+ private overlays = [];
|
|
|
+ private overlaysData = [];
|
|
|
+ private graphicsType = '';
|
|
|
+ private plot;
|
|
|
+
|
|
|
+ constructor(options) {
|
|
|
+ this.map = new Map({
|
|
|
+ controls: defaults({
|
|
|
+ zoom: false,
|
|
|
+ rotate: false
|
|
|
+ }),
|
|
|
+ layers: [],
|
|
|
+ target: options.dom,
|
|
|
+ view: new View({
|
|
|
+ center: options.center && options.center.length === 2 ? [options.center[0], options.center[1]] : [110.90153121597234, 21.98323671981171],
|
|
|
+ zoom: options.zoom ? options.zoom : 9.6,
|
|
|
+ projection: projection,
|
|
|
+ maxZoom: options.maxZoom ? options.maxZoom : 18,
|
|
|
+ minZoom: options.minZoom ? options.minZoom : 1
|
|
|
+ })
|
|
|
+ });
|
|
|
+ // 初始化比例尺
|
|
|
+ if (options.showScale) {
|
|
|
+ // map.addControl(new AMap.Scale());
|
|
|
+ }
|
|
|
+ if (options.drawTool.use) {
|
|
|
+ this.initMouseTool(options.drawTool);
|
|
|
+ }
|
|
|
+ if (typeof options.onLoadCompleted === 'function') {
|
|
|
+ options.onLoadCompleted(this.map);
|
|
|
+ }
|
|
|
+ this.formatXml(options.id);
|
|
|
+ }
|
|
|
+
|
|
|
+ formatXml(code, minZoom, maxZoom, zIndex, visible) {
|
|
|
+ const xml = new WMTSCapabilities();
|
|
|
+ this.getCapabilities(code).then((lists) => {
|
|
|
+ const mapData = xml.read(lists.data);
|
|
|
+ const layerParam = {
|
|
|
+ layerName: mapData.Contents.Layer[0].Title,
|
|
|
+ styleName: mapData.Contents.Layer[0].Style[0].Identifier,
|
|
|
+ tilematrixset: mapData.Contents.TileMatrixSet[0].Identifier,
|
|
|
+ format: mapData.Contents.Layer[0].Format[0]
|
|
|
+ };
|
|
|
+ this.createWmsLayer(code, layerParam, minZoom, maxZoom, zIndex, visible);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 请求接口获取地图信息
|
|
|
+ getCapabilities(code) {
|
|
|
+ return axios.get(commonUrl + code);
|
|
|
+ }
|
|
|
+ // 请求地图图片加载图层
|
|
|
+ createWmsLayer(code, layerParam, minZoom = 0, maxZoom, zIndex = -1, visible = true) {
|
|
|
+ const source = new WMTS({
|
|
|
+ url: commonUrl + code,
|
|
|
+ crossOrigin: 'Anonymous',
|
|
|
+ layer: layerParam.layerName,
|
|
|
+ style: layerParam.styleName,
|
|
|
+ matrixSet: layerParam.tilematrixset,
|
|
|
+ format: layerParam.format,
|
|
|
+ wrapX: true,
|
|
|
+ tileGrid: new WMTSTileGrid({
|
|
|
+ origin: getTopLeft(projectionExtent),
|
|
|
+ resolutions: resolutions,
|
|
|
+ matrixIds: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21']
|
|
|
+ })
|
|
|
+ });
|
|
|
+ const layer = new TileLayer({
|
|
|
+ source: source,
|
|
|
+ zIndex: zIndex,
|
|
|
+ minZoom: minZoom,
|
|
|
+ maxZoom,
|
|
|
+ visible: visible
|
|
|
+ });
|
|
|
+ layer.set('layerName', code);
|
|
|
+
|
|
|
+ this.map.addLayer(layer);
|
|
|
+ };
|
|
|
+ // 初始化绘画工具
|
|
|
+ initMouseTool(options) {
|
|
|
+ this.drawOptions = {
|
|
|
+ strokeColor: options.color,
|
|
|
+ strokeOpacity: 1,
|
|
|
+ strokeWeight: 2,
|
|
|
+ fillColor: options.color,
|
|
|
+ fillOpacity: options.drawType === '1' ? 0 : 0.5,
|
|
|
+ strokeStyle: 'solid'
|
|
|
+ };
|
|
|
+ this.plot = new olPlot(this.map, {
|
|
|
+ zoomToExtent: true
|
|
|
+ });
|
|
|
+ this.plot.plotDraw.on('drawEnd', this.onDrawEnd);
|
|
|
+ }
|
|
|
+ // 绘制结束事件
|
|
|
+ onDrawEnd(event) {
|
|
|
+ const feature = event.feature;
|
|
|
+ const aa = feature.getGeometry();
|
|
|
+ // 开始编辑
|
|
|
+ this.plot.plotEdit.activate(feature);
|
|
|
+ }
|
|
|
+}
|