月球大数据地理空间分析展示平台-【前端】-月球2期前端
Surpriseplus
2023-09-04 1355c04087927dfed32827d23609e2b04a8cabea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { getToken } from "@/utils/auth";
//配置文件地址
import config from "./config";
import { Map, View } from "ol"; //地图,视图
import OSM from "ol/source/OSM"; //可以理解为数据源,就是一张图片
import TileLayer from "ol/layer/Tile"; //可以理解为图层
import { fromLonLat, transform } from "ol/proj"; //将坐标从经度/纬度转换为不同的投影。
import XYZ from "ol/source/XYZ";
import WMTS from "ol/source/WMTS.js";
import WMTSTileGrid from "ol/tilegrid/WMTS.js";
import { get as getProjection, Projection } from "ol/proj.js";
import { register } from "ol/proj/proj4";
 
import { getTopLeft, getWidth } from "ol/extent.js";
const olMap = {
  map: null,
  Layer: null,
  projectionObj: {
    code: "ESRI:103881",
    extent: [-180, -90, 180, 90],
  },
  initMap() {
    //google地图
    // var googleMapLayer = new TileLayer({
    //   source: new XYZ({
    //     url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}",
    //   }),
    // });
    // proj4.defs(
    //   "ESRI:103880",
    //   "+proj=laea +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +R=1737400 +units=m +no_defs +type=crs"
    // );
    // register(proj4);
    const projection = new Projection({
      // code: this.projectionObj.code,
      code: "ESRI:103881",
      extent: [-180, -90, 180, 90],
      //   extent: this.projectionObj.extent,
    });
    this.map = new Map({
      target: "mapView",
      layers: [], //AMapLayer, baiduMapLayer
      view: new View({
        projection: projection,
        center: [180, 90],
        zoom: 4,
      }),
    });
  },
  addTreeData(treeNode, obj) {
    // this.delLayerAll();
    this.projectionObj = obj;
    console.log(this.projectionObj);
    //判断是否为代理
    if (treeNode.proxy) {
      this.addProxyAddress(treeNode); //有代理
    } else {
      this.addUrlAddress(treeNode); //无代理
    }
  },
  //代理地址
  addProxyAddress(res) {
    //判断数据类型
    switch (res.data) {
      case 1: //数字正射影像图
        this.setDataType(res);
        break;
      case 2: //场景地形数据
        this.setTerrainData(res);
        break;
      case 3: //数字高程模型(晕渲图)
        this.setDataType(res);
        break;
      case 4: //单波段栅格数据
        this.setDataType(res);
        break;
      case 5: //多光谱栅格数据
        this.setDataType(res);
        break;
      case 6: //高光谱栅格数据
        this.setDataType(res);
        break;
      case 7: //矢量图层
        this.setVectorData(res);
        break;
      case 8: //三维模型
        this.setModelData(res);
        break;
    }
  },
  //普通地址
  addUrlAddress(res) {
    switch (res.category) {
      case 0: //其他
        break;
      case 1: //GisServer
        this.addProxyAddress(res);
        break;
      case 2: //GeoServer
        this.addGeoServerAddress(res);
        break;
      case 3: //数简
        this.addProxyAddress(res);
        break;
    }
  },
  setDataType(res) {
    switch (res.type) {
      case 0: //URL
        break;
      case 1: //TMS
        // this.setAddTmsLayer(res);
        break;
      case 2: //WMTS
        this.addWmts(res);
        break;
      case 3: //WMS
        // this.setAddWmsLayer(res);
        break;
    }
  },
  setTerrainData(res) {
    switch (res.type) {
      case 0: //URL
        // this.setAddTearrinLayer(res);
        break;
      case 1: //TMS
        // this.setAddTearrinLayer(res);
        break;
    }
  },
  setVectorData(res) {
    switch (res.type) {
      case 0: //URL
        break;
      case 3: //WMS
        // this.setAddWmsLayer(res);
        break;
      case 4: //WFS
        break;
    }
  },
  //获取服务地址
  getLayrUrl(res) {
    var url;
    if (res.proxy) {
      const token = getToken();
      url = config.proxy + res.proxy.replaceAll("{token}", token);
    } else {
      url = res.url;
    }
    return url;
  },
  deleteLayer() {
    if (this.Layer) {
      this.map.removeLayer(this.Layer);
      this.Layer = null;
    }
  },
  addWmts(res) {
    this.deleteLayer();
    var url = this.getLayrUrl(res);
    const projection = new Projection({
      code: this.projectionObj.code,
      // extent: this.projectionObj.extent,
      // code: "ESRI:103879",
      extent: [-180, -90, 180, 90],
    });
    // var projection = getProjection("ESRI:103880");
    var projectionExtent = projection.getExtent();
 
    var size = getWidth(projectionExtent) / 256;
    var resolutions = new Array(18);
    var matrixIds = new Array(18);
    for (var z = 1; z < 19; ++z) {
      // generate resolutions and matrixIds arrays for this WMTS
      resolutions[z] = size / Math.pow(2, z);
      matrixIds[z] = z;
    }
    this.Layer = new TileLayer({
      opacity: 0.7,
      source: new WMTS({
        url: url,
        layer: "img", //注意每个图层这里不同
        matrixSet: "GoogleMapsCompatible",
        format: "image/png",
        style: "default",
        projection: projection,
        tileGrid: new WMTSTileGrid({
          origin: getTopLeft(projectionExtent), //原点(左上角)
          resolutions: resolutions, //分辨率数组
          matrixIds: matrixIds, //矩阵标识列表,与地图级数保持一致
        }),
        wrapX: true,
      }),
    });
    if (this.map) {
      this.map.addLayer(this.Layer);
      var that = this;
      window.olMap = this.map;
      window.layer = this.Layer
      setTimeout(() => {
        that.getLayersExtent();
      }, 1000)
      // this.Layer.getSource().on('addfeature', function () {
      //   this.map.getView().fit(this.Layer.getSource().getExtent());
      // });
 
 
    }
  },
  getLayersExtent() {
    // 缩放至图层范围
 
    this.map.getView().fit(this.Layer.renderer_.renderedExtent_);
 
 
 
  }
 
};
export default olMap;