原文链接及内容

运行界面

通过集成proj4js库,OpenLayers可以在任意投影之间转换坐标。

main.js代码如下:

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
import ImageLayer from 'ol/layer/Image.js';
import ImageWMS from 'ol/source/ImageWMS.js';
import Map from 'ol/Map.js';
import Projection from 'ol/proj/Projection.js';
import View from 'ol/View.js';
import proj4 from 'proj4';
import {ScaleLine, defaults as defaultControls} from 'ol/control.js';
import {fromLonLat} from 'ol/proj.js';
import {register} from 'ol/proj/proj4.js';

// 众所周知,Proj4js支持:
//
// EPSG:21781 is known to Proj4js because its definition is registered by
// calling proj4.defs(). Now when we create an ol/proj/Projection instance with
// the 'EPSG:21781' code, OpenLayers will pick up the transform functions from
// Proj4js. To get the registered ol/proj/Projection instance with other
// parameters like units and axis orientation applied from Proj4js, use
// `ol/proj#get('EPSG:21781')`.
// 通过调用proj4.defs()函数可以注册EPSG:21781投影的信息,因此proj4js能够识别该投影。
// 然后利用该投影的投影码(code,空间参考系统的标识符代码)来创建投影实例`ol/proj/Projection`
// (详见:https://openlayers.org/en/latest/apidoc/module-ol_proj_Projection-Projection.html ),
// 并且OpenLayers可以从Proj4js中获取转换函数。通过使用`ol/proj#get('EPSG:21781')`方法,
// 可以获取已注册投影的实例及其参数,像单位、坐标轴方向等。
//
// Note that we are setting the projection's extent here, which is used to
// determine the view resolution for zoom level 0. Recommended values for a
// projection's validity extent can be found at https://epsg.io/.
// 注意,我们在这里设置了投影的范围,它用于确定缩放级别0的视图分辨率。投影有效性范围的推荐值可以在https://epsg.io/上找到。

proj4.defs(
'EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 ' +
'+x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=660.077,13.551,369.344,2.484,1.783,2.939,5.66 +units=m +no_defs'
);
register(proj4);

const projection = new Projection({
code: 'EPSG:21781',
extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
});

const extent = [420000, 30000, 900000, 350000];
const layers = [
new ImageLayer({
extent: extent,
source: new ImageWMS({
url: 'https://wms.geo.admin.ch/',
crossOrigin: 'anonymous',
attributions:
'© <a href="https://shop.swisstopo.admin.ch/en/products/maps/national/lk1000"' +
'target="_blank">Pixelmap 1:1000000 / geo.admin.ch</a>',
params: {
'LAYERS': 'ch.swisstopo.pixelkarte-farbe-pk1000.noscale',
'FORMAT': 'image/jpeg',
},
serverType: 'mapserver',
}),
}),
new ImageLayer({
extent: extent,
source: new ImageWMS({
url: 'https://wms.geo.admin.ch/',
crossOrigin: 'anonymous',
attributions:
'© <a href="https://www.hydrodaten.admin.ch/en/notes-on-the-flood-alert-maps.html"' +
'target="_blank">Flood Alert / geo.admin.ch</a>',
params: {'LAYERS': 'ch.bafu.hydroweb-warnkarte_national'},
serverType: 'mapserver',
}),
}),
];

const map = new Map({
controls: defaultControls().extend([new ScaleLine()]),
layers: layers,
target: 'map',
view: new View({
projection: projection,
center: fromLonLat([8.23, 46.86], projection),
extent: extent,
zoom: 2,
}),
});

界面布局文件index.html代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Single Image WMS with Proj4js</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js"></script>
<script type="module" src="main.js"></script>
</body>
</html>