原文链接及内容

运行界面

只要不需要坐标变换,OpenLayers就可以很好地处理仅使用code(标识码)和units(单位)配置的投影。

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

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

// A minimal projection object is configured with only the SRS code and the map
// units. No client-side coordinate transforms are possible with such a
// projection object. Requesting tiles only needs the code together with a
// tile grid of Cartesian coordinates; it does not matter how those
// coordinates relate to latitude or longitude.
//
// 只使用 SRS 码和地图单位配置投影对象。使用这样的投影对象,客户端是不能进行坐标转换的。
// 请求的切片数据只需要将code(坐标系标识码)与笛卡尔坐标系的切片网格放在一起,这与这些坐标系与经度或纬度的关系无关。
//
// With no transforms available projection units must be assumed to represent
// true distances. In the case of local projections this may be a sufficiently
// close approximation for a meaningful (if not 100% accurate) ScaleLine control.
// 在没有(投影)变换的情况下,必须假设可用的投影单位表示真实距离。
// 在局部投影的情况下,对于(如果不是100%准确的)比例尺控制来说,这可能是一个足够接近的近似值。

const projection = new Projection({
code: 'EPSG:21781',
units: 'm',
});

const map = new Map({
controls: defaultControls().extend([new ScaleLine()]),
layers: layers,
target: 'map',
view: new View({
center: [660000, 190000],
projection: projection,
zoom: 9,
}),
});

界面布局文件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>WMS without Projection</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>