原文链接及内容

运行界面

这个例子展示了在客户端单个图像数据源的重投影。

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
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import Static from 'ol/source/ImageStatic.js';
import View from 'ol/View.js';
import proj4 from 'proj4';
import {Image as ImageLayer, Tile as TileLayer} from 'ol/layer.js';
import {getCenter} from 'ol/extent.js';
import {register} from 'ol/proj/proj4.js';
import {transform} from 'ol/proj.js';

proj4.defs(
'EPSG:27700',
'+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 ' +
'+x_0=400000 +y_0=-100000 +ellps=airy ' +
'+towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 ' +
'+units=m +no_defs'
);
register(proj4);

const imageExtent = [0, 0, 700000, 1300000];
const imageLayer = new ImageLayer();

const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
imageLayer,
],
target: 'map',
view: new View({
center: transform(getCenter(imageExtent), 'EPSG:27700', 'EPSG:3857'),
zoom: 4,
}),
});

const interpolate = document.getElementById('interpolate');

function setSource() {
const source = new Static({
url:
'https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/' +
'British_National_Grid.svg/2000px-British_National_Grid.svg.png',
crossOrigin: '',
projection: 'EPSG:27700',
imageExtent: imageExtent,
interpolate: interpolate.checked,
});
imageLayer.setSource(source);
}
setSource();

interpolate.addEventListener('change', setSource);

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Reprojection</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>
<div>
<input type="checkbox" id="interpolate" checked />
<label for="interpolate">Interpolate</label>
</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>