1 | import mapboxgl from 'mapbox-gl';
|
2 | import turfHelpers from '@turf/helpers';
|
3 | import turfBboxPolygon from '@turf/bbox-polygon';
|
4 |
|
5 | const featureCollection = turfHelpers.featureCollection;
|
6 |
|
7 | export function getBounds(bbox) {
|
8 | var left = +bbox.left,
|
9 | right = +bbox.right,
|
10 | top = +bbox.top,
|
11 | bottom = +bbox.bottom;
|
12 |
|
13 | return new mapboxgl.LngLatBounds(
|
14 | new mapboxgl.LngLat(left, bottom),
|
15 | new mapboxgl.LngLat(right, top)
|
16 | );
|
17 | }
|
18 |
|
19 | export function getBoundingBox(bounds) {
|
20 | var left = bounds.getWest(),
|
21 | right = bounds.getEast(),
|
22 | top = bounds.getNorth(),
|
23 | bottom = bounds.getSouth();
|
24 |
|
25 | var padX = 0;
|
26 | var padY = 0;
|
27 | if (!(left === -180 && right === 180 && top === 90 && bottom === -90)) {
|
28 | padX = Math.max((right - left) / 5, 0.0001);
|
29 | padY = Math.max((top - bottom) / 5, 0.0001);
|
30 | }
|
31 |
|
32 | var bboxPolygon = turfBboxPolygon([
|
33 | left - padX,
|
34 | bottom - padY,
|
35 | right + padX,
|
36 | top + padY
|
37 | ]);
|
38 |
|
39 | return featureCollection([bboxPolygon]);
|
40 | }
|