diff --git a/src/map/view.js b/src/map/view.js index 8f0cc49..f6ca1ef 100644 --- a/src/map/view.js +++ b/src/map/view.js @@ -266,10 +266,33 @@ function getBoundingBoxFromViewForEpsg3857( }; } +function getViewFromBoundingBoxForEpsg3857(bbox) { + const {minLat, maxLat, minLon, maxLon} = bbox; + const bottomLeftEpsg3857 = projections.coordinates4326To3857({ + lat: minLat, + lon: minLon, + }); + const topRightEpsg3857 = projections.coordinates4326To3857({ + lat: maxLat, + lon: maxLon, + }); + const {x: xMin, y: yMin} = bottomLeftEpsg3857; + const {x: xMax, y: yMax} = topRightEpsg3857; + + return { + center: { + lat: (maxLat + minLat) / 2, + lon: (maxLon + minLon) / 2, + }, + boxRange: Math.max(xMax - xMin, yMax - yMin), + }; +} + export default { ensureViewIntegrity, mergeViews, getBoundingBoxFromViewForEpsg3857, + getViewFromBoundingBoxForEpsg3857, getBoxRangeFromZoomLevel, getNearestZoomLevelBoxRange, getMapViewportRange, @@ -284,6 +307,7 @@ export { ensureViewIntegrity, //check use in ptr-state mergeViews, getBoundingBoxFromViewForEpsg3857, + getViewFromBoundingBoxForEpsg3857, getBoxRangeFromZoomLevel, getNearestZoomLevelBoxRange, getMapViewportRange, diff --git a/tests/map/view/getViewFromBoundingBoxForEpsg3857-test.js b/tests/map/view/getViewFromBoundingBoxForEpsg3857-test.js new file mode 100644 index 0000000..45d8d59 --- /dev/null +++ b/tests/map/view/getViewFromBoundingBoxForEpsg3857-test.js @@ -0,0 +1,69 @@ +import {assert} from 'chai'; +import view, {getViewFromBoundingBoxForEpsg3857} from '../../../src/map/view'; + +describe('getViewFromBoundingBoxForEpsg3857', function () { + // Expected values calculated here https://epsg.io/transform#s_srs=3857&t_srs=4326 + + it('returns expected view approximately', function () { + // given params + const bbox = { + minLat: 0, + maxLat: 90, + minLon: 0, + maxLon: 180, + }; + + // expected output params + const expectedCenter = {lat: 45, lon: 90}; + const expectedBoxRange = 20000000; // 10000 km + + // no optLat given + const output = view.getViewFromBoundingBoxForEpsg3857(bbox); + + assert.equal(output.center.lat, expectedCenter.lat); + assert.equal(output.center.lon, expectedCenter.lon); + assert.approximately(output.boxRange, expectedBoxRange, 100000); + }); + + it('returns expected view approximately 2', function () { + // given params + const bbox = { + minLat: 50, + maxLat: 51, + minLon: 15, + maxLon: 16, + }; + + // expected output params + const expectedCenter = {lat: 50.5, lon: 15.5}; + const expectedBoxRange = 180000; // 111km + + // no optLat given + const output = view.getViewFromBoundingBoxForEpsg3857(bbox); + + assert.equal(output.center.lat, expectedCenter.lat); + assert.equal(output.center.lon, expectedCenter.lon); + assert.approximately(output.boxRange, expectedBoxRange, 10000); + }); + + it('returns expected view approximately 3', function () { + // given params + const bbox = { + minLat: 50.01, + maxLat: 50.02, + minLon: 15.01, + maxLon: 15.02, + }; + + // expected output params + const expectedCenter = {lat: 50.015, lon: 15.015}; + const expectedBoxRange = 1700; // 111km + + // no optLat given + const output = view.getViewFromBoundingBoxForEpsg3857(bbox); + + assert.equal(output.center.lat, expectedCenter.lat); + assert.equal(output.center.lon, expectedCenter.lon); + assert.approximately(output.boxRange, expectedBoxRange, 100); + }); +});