自定义平铺 WMS
自定义平铺 WMS示例。 WMS 瓦片 瓦片图层 投影 mapserver
使用 addCoordinateTransforms() 可以向已配置的投影添加自定义坐标变换函数。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import ScaleLine from 'ol/control/ScaleLine.js';
import {defaults as defaultControls} from 'ol/control/defaults.js';
import TileLayer from 'ol/layer/Tile.js';
import {addCoordinateTransforms, addProjection, transform} from 'ol/proj.js';
import Projection from 'ol/proj/Projection.js';
import TileWMS from 'ol/source/TileWMS.js';
// 默认情况下 OpenLayers 并不知道 EPSG:21781(瑞士)投影。
// 因此我们为 EPSG:21781 创建一个投影实例,并把它传给
// ol/proj~addProjection,使该库能够通过其代号查找使用它。
const projection = new Projection({
code: 'EPSG:21781',
// 该范围用于确定缩放级别 0。投影有效范围的推荐值
// 可在 https://spatialreference.org/ 上找到。
extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
units: 'm',
});
addProjection(projection);
// 我们还声明了 EPSG:21781/EPSG:4326 的变换函数。在 ScaleLine 控件中
// 以及在调用 ol/proj~transform 设置视图初始中心时(见下文)
// 都需要这些函数。
addCoordinateTransforms(
'EPSG:4326',
projection,
function (coordinate) {
return [
WGStoCHy(coordinate[1], coordinate[0]),
WGStoCHx(coordinate[1], coordinate[0]),
];
},
function (coordinate) {
return [
CHtoWGSlng(coordinate[0], coordinate[1]),
CHtoWGSlat(coordinate[0], coordinate[1]),
];
},
);
const extent = [420000, 30000, 900000, 350000];
const layers = [
new TileLayer({
extent: extent,
source: new TileWMS({
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 TileLayer({
extent: extent,
source: new TileWMS({
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({
units: 'metric',
}),
]),
layers: layers,
target: 'map',
view: new View({
projection: projection,
center: transform([8.23, 46.86], 'EPSG:4326', 'EPSG:21781'),
extent: extent,
zoom: 2,
}),
});
/*
* 瑞士投影变换函数,下载自
* https://www.swisstopo.admin.ch/en/knowledge-facts/surveying-geodesy/reference-systems/map-projections.html
*/
// 将 WGS 纬度/经度(十进制度)转换为 CH y
function WGStoCHy(lat, lng) {
// 将十进制度转换为六十进制度
lat = DECtoSEX(lat);
lng = DECtoSEX(lng);
// 将度转换为秒(六十进制)
lat = DEGtoSEC(lat);
lng = DEGtoSEC(lng);
// 辅助值(以伯尔尼为基准)
const lat_aux = (lat - 169028.66) / 10000;
const lng_aux = (lng - 26782.5) / 10000;
// 计算 Y
const y =
600072.37 +
211455.93 * lng_aux -
10938.51 * lng_aux * lat_aux -
0.36 * lng_aux * Math.pow(lat_aux, 2) -
44.54 * Math.pow(lng_aux, 3);
return y;
}
// 将 WGS 纬度/经度(十进制度)转换为 CH x
function WGStoCHx(lat, lng) {
// 将十进制度转换为六十进制度
lat = DECtoSEX(lat);
lng = DECtoSEX(lng);
// 将度转换为秒(六十进制)
lat = DEGtoSEC(lat);
lng = DEGtoSEC(lng);
// 辅助值(以伯尔尼为基准)
const lat_aux = (lat - 169028.66) / 10000;
const lng_aux = (lng - 26782.5) / 10000;
// 计算 X
const x =
200147.07 +
308807.95 * lat_aux +
3745.25 * Math.pow(lng_aux, 2) +
76.63 * Math.pow(lat_aux, 2) -
194.56 * Math.pow(lng_aux, 2) * lat_aux +
119.79 * Math.pow(lat_aux, 3);
return x;
}
// 将 CH y/x 转换为 WGS 纬度
function CHtoWGSlat(y, x) {
// 将军用坐标转换为民用坐标,单位换算为 1000km
// 辅助值(以伯尔尼为基准)
const y_aux = (y - 600000) / 1000000;
const x_aux = (x - 200000) / 1000000;
// 计算纬度
let lat =
16.9023892 +
3.238272 * x_aux -
0.270978 * Math.pow(y_aux, 2) -
0.002528 * Math.pow(x_aux, 2) -
0.0447 * Math.pow(y_aux, 2) * x_aux -
0.014 * Math.pow(x_aux, 3);
// 单位从 10000" 换算为 1 ",并将秒转换为度(十进制)
lat = (lat * 100) / 36;
return lat;
}
// 将 CH y/x 转换为 WGS 经度
function CHtoWGSlng(y, x) {
// 将军用坐标转换为民用坐标,单位换算为 1000km
// 辅助值(以伯尔尼为基准)
const y_aux = (y - 600000) / 1000000;
const x_aux = (x - 200000) / 1000000;
// 计算经度
let lng =
2.6779094 +
4.728982 * y_aux +
0.791484 * y_aux * x_aux +
0.1306 * y_aux * Math.pow(x_aux, 2) -
0.0436 * Math.pow(y_aux, 3);
// 单位从 10000" 换算为 1 ",并将秒转换为度(十进制)
lng = (lng * 100) / 36;
return lng;
}
// 将十进制角度转换为六十进制 DMS
function DECtoSEX(angle) {
// 提取度分秒
const deg = parseInt(angle, 10);
const min = parseInt((angle - deg) * 60, 10);
const sec = ((angle - deg) * 60 - min) * 60;
// 以六十进制度表示的结果(dd.mmss)
return deg + min / 100 + sec / 10000;
}
// 将角度(度)转换为秒
function DEGtoSEC(angle) {
// 提取度分秒
const deg = parseInt(angle, 10);
let min = parseInt((angle - deg) * 100, 10);
let sec = ((angle - deg) * 100 - min) * 100;
// 避免秒数为 0 时的舍入问题
const parts = String(angle).split('.');
if (parts.length == 2 && parts[1].length == 2) {
min = Number(parts[1]);
sec = 0;
}
// 以六十进制度表示的结果(dd.mmss)
return sec + min * 60 + deg * 3600;
}index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自定义平铺 WMS</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>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "wms-custom-proj",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}