Merge pull request #10787 from mike-000/patch-7

Improved projection extent in the "Reprojection with EPSG.io Search" example
This commit is contained in:
Tim Schaub
2020-03-10 16:56:08 -06:00
committed by GitHub
2 changed files with 40 additions and 7 deletions

View File

@@ -6,12 +6,12 @@ docs: >
This example shows client-side raster reprojection capabilities from
OSM (EPSG:3857) to arbitrary projection by searching
in <a href="https://epsg.io/">EPSG.io</a> database.
tags: "reprojection, projection, proj4js, epsg.io"
tags: "reprojection, projection, proj4js, epsg.io, graticule"
---
<div id="map" class="map"></div>
<form class="form-inline">
<label for="epsg-query">Search projection:</label>
<input type="text" id="epsg-query" placeholder="4326, 27700, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
<input type="text" id="epsg-query" placeholder="4326, 27700, 3031, US National Atlas, Swiss, France, ..." class="form-control" size="50" />
<button id="epsg-search" class="btn">Search</button>
<span id="epsg-result"></span>
<div>
@@ -19,5 +19,9 @@ tags: "reprojection, projection, proj4js, epsg.io"
Render reprojection edges
<input type="checkbox" id="render-edges">
</label>
<label for="show-graticule">
&nbsp;&nbsp;&nbsp;Show graticule
<input type="checkbox" id="show-graticule" />
</label>
</div>
</form>

View File

@@ -1,19 +1,34 @@
import Map from '../src/ol/Map.js';
import View from '../src/ol/View.js';
import {applyTransform} from '../src/ol/extent.js';
import Graticule from '../src/ol/layer/Graticule.js';
import TileLayer from '../src/ol/layer/Tile.js';
import {get as getProjection, getTransform} from '../src/ol/proj.js';
import {register} from '../src/ol/proj/proj4.js';
import OSM from '../src/ol/source/OSM.js';
import TileImage from '../src/ol/source/TileImage.js';
import Stroke from '../src/ol/style/Stroke.js';
import proj4 from 'proj4';
const graticule = new Graticule({
// the style to use for the lines, optional.
strokeStyle: new Stroke({
color: 'rgba(255,120,0,0.9)',
width: 2,
lineDash: [0.5, 4]
}),
showLabels: true,
visible: false,
wrapX: false
});
const map = new Map({
layers: [
new TileLayer({
source: new OSM()
})
}),
graticule
],
target: 'map',
view: new View({
@@ -28,6 +43,7 @@ const queryInput = document.getElementById('epsg-query');
const searchButton = document.getElementById('epsg-search');
const resultSpan = document.getElementById('epsg-result');
const renderEdgesCheckbox = document.getElementById('render-edges');
const showGraticuleCheckbox = document.getElementById('show-graticule');
function setProjection(code, name, proj4def, bbox) {
if (code === null || name === null || proj4def === null || bbox === null) {
@@ -48,9 +64,15 @@ function setProjection(code, name, proj4def, bbox) {
const newProj = getProjection(newProjCode);
const fromLonLat = getTransform('EPSG:4326', newProj);
// very approximate calculation of projection extent
const extent = applyTransform(
[bbox[1], bbox[2], bbox[3], bbox[0]], fromLonLat);
let worldExtent = [bbox[1], bbox[2], bbox[3], bbox[0]];
newProj.setWorldExtent(worldExtent);
// approximate calculation of projection extent,
// checking if the world extent crosses the dateline
if (bbox[1] > bbox[3]) {
worldExtent = [bbox[1], bbox[2], bbox[3] + 360, bbox[0]];
}
const extent = applyTransform(worldExtent, fromLonLat, undefined, 8);
newProj.setExtent(extent);
const newView = new View({
projection: newProj
@@ -98,7 +120,7 @@ searchButton.onclick = function(event) {
/**
* Handle change event.
* Handle checkbox change event.
*/
renderEdgesCheckbox.onchange = function() {
map.getLayers().forEach(function(layer) {
@@ -110,3 +132,10 @@ renderEdgesCheckbox.onchange = function() {
}
});
};
/**
* Handle checkbox change event.
*/
showGraticuleCheckbox.onchange = function() {
graticule.setVisible(showGraticuleCheckbox.checked);
};