Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove check map bbox, add bbox validation #662

Merged
merged 3 commits into from
Sep 21, 2023
Merged
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
50 changes: 23 additions & 27 deletions app/scripts/components/common/mapbox/layers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FunctionComponent, useEffect } from 'react';
import { Feature } from 'geojson';
import { Map as MapboxMap } from 'mapbox-gl';
import { defaultsDeep } from 'lodash';
import { defaultsDeep, clamp } from 'lodash';
import axios, { Method } from 'axios';
import {
eachDayOfInterval,
Expand Down Expand Up @@ -372,30 +372,6 @@ export function getMergedBBox(features: StacFeature[]) {

export const FIT_BOUNDS_PADDING = 32;

export function checkFitBoundsFromLayer(
layerBounds?: [number, number, number, number],
mapInstance?: MapboxMap
) {
if (!layerBounds || !mapInstance) return false;

const [minXLayer, minYLayer, maxXLayer, maxYLayer] = layerBounds;
const [[minXMap, minYMap], [maxXMap, maxYMap]] = mapInstance
.getBounds()
.toArray();
const isOutside =
maxXLayer < minXMap ||
minXLayer > maxXMap ||
maxYLayer < minYMap ||
minYLayer > maxYMap;
const layerExtentSmaller =
maxXLayer - minXLayer < maxXMap - minXMap &&
maxYLayer - minYLayer < maxYMap - minYMap;

// only fitBounds if layer extent is smaller than viewport extent (ie zoom to area of interest),
// or if layer extent does not overlap at all with viewport extent (ie pan to area of interest)
return layerExtentSmaller || isOutside;
}

interface LayerInteractionHookOptions {
layerId: string;
mapInstance: MapboxMap;
Expand Down Expand Up @@ -434,6 +410,25 @@ export function useLayerInteraction({

type OptionalBbox = number[] | undefined | null;

/**
* MapboxGL requires maxX value to be 180, minX -180, maxY 90, minY -90
* While some of the datasets having bbox going above it.
* @param bounds Bounding box to fit layer
* @returns Boolean
*/

function clampBbox(
bounds: [number, number, number, number]
): [number, number, number, number] {
const [minX, minY, maxX, maxY] = bounds;
return [
clamp(minX, -180, 180),
clamp(minY, -90, 90),
clamp(maxX, -180, 180),
clamp(maxY, -90, 90)
];
}

/**
* Centers on the given bounds if the current position is not within the bounds,
* and there's no user defined position (via user initiated map movement). Gives
Expand All @@ -458,8 +453,9 @@ export function useFitBbox(
| [number, number, number, number]
| undefined;

if (bounds?.length && checkFitBoundsFromLayer(bounds, mapInstance)) {
mapInstance.fitBounds(bounds, { padding: FIT_BOUNDS_PADDING });
if (bounds?.length) {
const clampedBbox = clampBbox(bounds);
mapInstance.fitBounds(clampedBbox, { padding: FIT_BOUNDS_PADDING });
}
}, [mapInstance, isUserPositionSet, initialBbox, stacBbox]);
}
Loading