Demonstrates client-side reprojection of single image source.
This example shows client-side reprojection of single image source.
import 'ol/ol.css';
import Map from 'ol/Map';
import OSM from 'ol/source/OSM';
import Static from 'ol/source/ImageStatic';
import View from 'ol/View';
import proj4 from 'proj4';
import {Image as ImageLayer, Tile as TileLayer} from 'ol/layer';
import {getCenter} from 'ol/extent';
import {register} from 'ol/proj/proj4';
import {transform} from 'ol/proj';
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 imageSmoothing = document.getElementById('imageSmoothing');
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,
imageSmoothing: imageSmoothing.checked,
});
imageLayer.setSource(source);
}
setSource();
imageSmoothing.addEventListener('change', setSource);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Reprojection</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL,TextDecoder,Number.isInteger"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<div>
<input type="checkbox" id="imageSmoothing" checked />
<label for="imageSmoothing">Image smoothing</label>
</div>
<script src="main.js"></script>
</body>
</html>
{
"name": "reprojection-image",
"dependencies": {
"ol": "6.6.1",
"proj4": "2.7.4"
},
"devDependencies": {
"parcel": "^2.0.0-beta.1"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}