Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/map/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -284,6 +307,7 @@ export {
ensureViewIntegrity, //check use in ptr-state
mergeViews,
getBoundingBoxFromViewForEpsg3857,
getViewFromBoundingBoxForEpsg3857,
getBoxRangeFromZoomLevel,
getNearestZoomLevelBoxRange,
getMapViewportRange,
Expand Down
69 changes: 69 additions & 0 deletions tests/map/view/getViewFromBoundingBoxForEpsg3857-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});