Skip to content

Commit ca1bfe0

Browse files
committed
Add custom bounding box helpers
These work with anti-meridian unlike turf.js.
1 parent 6896aaa commit ca1bfe0

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/plots/map/layout_defaults.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,51 @@ function handleDefaults(containerIn, containerOut, coerce, opts) {
4747
containerOut._input = containerIn;
4848
}
4949

50+
function getMinBoundLon(lon) {
51+
if (!lon.length) return { minLon: 0, maxLon: 0 };
52+
53+
// normalize to [0, 360)
54+
const norm = lon.map(to360).sort((a, b) => a - b);
55+
56+
let maxGap = -1;
57+
let gapIndex = 0;
58+
59+
// find largest gap
60+
for (let i = 0; i < norm.length; i++) {
61+
const curr = norm[i];
62+
const next = norm[(i + 1) % norm.length];
63+
const gap = (next - curr + 360) % 360;
64+
65+
if (gap > maxGap) {
66+
maxGap = gap;
67+
gapIndex = i;
68+
}
69+
}
70+
71+
// take complement of largest gap
72+
let minLon = norm[(gapIndex + 1) % norm.length];
73+
let maxLon = norm[gapIndex];
74+
minLon = to180(minLon)
75+
maxLon = to180(maxLon)
76+
77+
return { minLon, maxLon };
78+
79+
// https://gis.stackexchange.com/questions/201789/verifying-formula-that-will-convert-longitude-0-360-to-180-to-180
80+
function to180(deg) {
81+
return ((deg + 180) % 360) - 180
82+
}
83+
function to360(deg) {
84+
return ((deg % 360) + 360) % 360;
85+
}
86+
}
87+
88+
function getMinBoundLat(lat) {
89+
return {
90+
minLat: Math.min(...lat),
91+
maxLat: Math.max(...lat)
92+
};
93+
}
94+
5095
function handleLayerDefaults(layerIn, layerOut) {
5196
function coerce(attr, dflt) {
5297
return Lib.coerce(layerIn, layerOut, layoutAttributes.layers, attr, dflt);

0 commit comments

Comments
 (0)