Improve the calculation of projection extent

Add show graticule option
This commit is contained in:
mike-000
2020-03-09 15:24:01 +00:00
committed by GitHub
parent 6019a61cca
commit 230c6b011e

View File

@@ -1,19 +1,34 @@
import Map from '../src/ol/Map.js'; import Map from '../src/ol/Map.js';
import View from '../src/ol/View.js'; import View from '../src/ol/View.js';
import {applyTransform} from '../src/ol/extent.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 TileLayer from '../src/ol/layer/Tile.js';
import {get as getProjection, getTransform} from '../src/ol/proj.js'; import {get as getProjection, getTransform} from '../src/ol/proj.js';
import {register} from '../src/ol/proj/proj4.js'; import {register} from '../src/ol/proj/proj4.js';
import OSM from '../src/ol/source/OSM.js'; import OSM from '../src/ol/source/OSM.js';
import TileImage from '../src/ol/source/TileImage.js'; import TileImage from '../src/ol/source/TileImage.js';
import Stroke from '../src/ol/style/Stroke.js';
import proj4 from 'proj4'; 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({ const map = new Map({
layers: [ layers: [
new TileLayer({ new TileLayer({
source: new OSM() source: new OSM()
}) }),
graticule
], ],
target: 'map', target: 'map',
view: new View({ view: new View({
@@ -28,6 +43,7 @@ const queryInput = document.getElementById('epsg-query');
const searchButton = document.getElementById('epsg-search'); const searchButton = document.getElementById('epsg-search');
const resultSpan = document.getElementById('epsg-result'); const resultSpan = document.getElementById('epsg-result');
const renderEdgesCheckbox = document.getElementById('render-edges'); const renderEdgesCheckbox = document.getElementById('render-edges');
const showGraticuleCheckbox = document.getElementById('show-graticule');
function setProjection(code, name, proj4def, bbox) { function setProjection(code, name, proj4def, bbox) {
if (code === null || name === null || proj4def === null || bbox === null) { if (code === null || name === null || proj4def === null || bbox === null) {
@@ -48,9 +64,15 @@ function setProjection(code, name, proj4def, bbox) {
const newProj = getProjection(newProjCode); const newProj = getProjection(newProjCode);
const fromLonLat = getTransform('EPSG:4326', newProj); const fromLonLat = getTransform('EPSG:4326', newProj);
// very approximate calculation of projection extent let worldExtent = [bbox[1], bbox[2], bbox[3], bbox[0]];
const extent = applyTransform( newProj.setWorldExtent(worldExtent);
[bbox[1], bbox[2], bbox[3], bbox[0]], fromLonLat);
// 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); newProj.setExtent(extent);
const newView = new View({ const newView = new View({
projection: newProj projection: newProj
@@ -98,7 +120,7 @@ searchButton.onclick = function(event) {
/** /**
* Handle change event. * Handle checkbox change event.
*/ */
renderEdgesCheckbox.onchange = function() { renderEdgesCheckbox.onchange = function() {
map.getLayers().forEach(function(layer) { map.getLayers().forEach(function(layer) {
@@ -110,3 +132,10 @@ renderEdgesCheckbox.onchange = function() {
} }
}); });
}; };
/**
* Handle checkbox change event.
*/
showGraticuleCheckbox.onchange = function() {
graticule.setVisible(showGraticuleCheckbox.checked);
};