Skip to content

Commit 0473347

Browse files
authored
[fix] fit to bounds - fix initial basemap and deck projections mismatch (#3155)
* [fix] fit to bounds - fix initial basemap and deck projections mismatch Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> * fix tests Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com> --------- Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
1 parent d43e8bb commit 0473347

5 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/reducers/src/map-state-updaters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ function updateViewportBasedOnBounds(state: MapState, newMapState: MapState) {
422422
if (!booleanWithin(viewportBoundsPolygon, maxBoundsPolygon)) {
423423
const {latitude, longitude, zoom} = fitBounds({
424424
width: newMapState.width,
425-
height: newMapState.width,
425+
height: newMapState.height,
426426
bounds: [
427427
[newStateMaxBounds[0], newStateMaxBounds[1]],
428428
[newStateMaxBounds[2], newStateMaxBounds[3]]

src/utils/src/projection-utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,27 @@ export function getCenterAndZoomFromBounds(bounds, {width, height}) {
4141
}
4242

4343
// viewport(bounds, dimensions, minzoom, maxzoom, tileSize, allowFloat)
44-
const {zoom} = geoViewport.viewport(
44+
let {zoom} = geoViewport.viewport(
4545
bounds,
4646
[width, height],
4747
undefined,
4848
undefined,
49-
MAPBOX_TILE_SIZE
49+
MAPBOX_TILE_SIZE,
50+
true
5051
);
5152
// center being calculated by geo-vieweport.viewport has a complex logic that
5253
// projects and then unprojects the coordinates to determine the center
5354
// Calculating a simple average instead as that is the expected behavior in most of cases
5455
const center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2];
5556

57+
// NOTE: this logic is used in deck.gl normalizeViewportProps
58+
// This is required in order to prevent projection matrix mismatch between basemap and layers
59+
const minZoom = Math.log2(height / MAPBOX_TILE_SIZE);
60+
if (zoom <= minZoom) {
61+
zoom = minZoom;
62+
center[1] = 0;
63+
}
64+
5665
return {zoom, center};
5766
}
5867

test/browser/components/geocoder-panel-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ test('GeocoderPanel - render', t => {
190190

191191
t.deepEqual(
192192
{latitude: newVP.latitude, longitude: newVP.longitude, zoom: newVP.zoom},
193-
{latitude: 57.5, longitude: 1.5, zoom: 4},
193+
{latitude: 57.5, longitude: 1.5, zoom: 4.307606395110668},
194194
'Should call updateMap action on onSelected w/ new viewport'
195195
);
196196

@@ -201,7 +201,7 @@ test('GeocoderPanel - render', t => {
201201
const newVP2 = updateMap.args[1][0];
202202
t.deepEqual(
203203
{latitude: newVP2.latitude, longitude: newVP2.longitude, zoom: newVP2.zoom},
204-
{latitude: 55, longitude: 1, zoom: 11},
204+
{latitude: 55, longitude: 1, zoom: 11.655698543267773},
205205
'Should call updateMapaction on onSelected w/o bbox'
206206
);
207207

test/browser/components/plot-container-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ test('PlotContainer -> mount -> imageSize', t => {
9090
-45.57105275929253,
9191
'should set longitude when center: true'
9292
);
93-
t.equal(map.props.mapState.zoom, 1, 'should set zoom when center: true');
93+
t.equal(map.props.mapState.zoom, 1.8721094288367688, 'should set zoom when center: true');
9494
t.end();
9595
});

test/node/reducers/map-state-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ test('#mapStateReducer -> FIT_BOUNDS', t => {
346346

347347
const expected = {
348348
center: [5.7604079999999955, 45.189756500000016],
349-
zoom: 10
349+
zoom: 10.569800116329509
350350
};
351351

352352
const stateWidthMapDimension = reducer(undefined, updateMap(mapUpdate, 0));

0 commit comments

Comments
 (0)