Skip to content

Commit 1b3b284

Browse files
committed
Improve efficiency of getMapFitBounds
1 parent 7e48373 commit 1b3b284

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

src/plots/map/get_map_fit_bounds.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
import { getFitboundsLonRange } from '../../lib/geo_location_utils';
42
import type { MapLayout, ScattermapData } from '../../types/generated/schema';
53

@@ -30,6 +28,8 @@ interface FitBoundsTrace extends Pick<ScattermapData, 'subplot' | 'visible'> {
3028
*/
3129
export function getMapFitBounds(fullData: FitBoundsTrace[], subplotId: string): LonLatBox | null {
3230
const validLons: number[] = [];
31+
let minLon = Infinity;
32+
let maxLon = -Infinity;
3333
let minLat = Infinity;
3434
let maxLat = -Infinity;
3535

@@ -49,6 +49,10 @@ export function getMapFitBounds(fullData: FitBoundsTrace[], subplotId: string):
4949
const la = lat[j];
5050
if (Number.isFinite(lo) && Number.isFinite(la)) {
5151
validLons.push(lo);
52+
// Track longitude bounds
53+
if (lo < minLon) minLon = lo;
54+
if (lo > maxLon) maxLon = lo;
55+
// Track latitude bounds
5256
if (la < minLat) minLat = la;
5357
if (la > maxLat) maxLat = la;
5458
}
@@ -57,18 +61,14 @@ export function getMapFitBounds(fullData: FitBoundsTrace[], subplotId: string):
5761

5862
if (!validLons.length) return null;
5963

60-
let west: number;
61-
let east: number;
62-
const lonRange = getFitboundsLonRange(validLons);
63-
if (lonRange) {
64-
west = lonRange[0];
65-
east = lonRange[1];
66-
} else {
67-
west = Infinity;
68-
east = -Infinity;
69-
for (const lon of validLons) {
70-
if (lon < west) west = lon;
71-
if (lon > east) east = lon;
64+
let west = minLon;
65+
let east = maxLon;
66+
// Only handle antimeridian if it actually gets crossed
67+
if (maxLon - minLon > 180) {
68+
const lonRange = getFitboundsLonRange(validLons);
69+
if (lonRange) {
70+
west = lonRange[0];
71+
east = lonRange[1];
7272
}
7373
}
7474

0 commit comments

Comments
 (0)