From d5cb80312c24a309f1bd15691a17ea8c3ff7fee5 Mon Sep 17 00:00:00 2001 From: Daniel da Silva Date: Mon, 31 Jul 2023 10:09:52 +0100 Subject: [PATCH] Center bounds only if no map position is set --- app/scripts/components/common/mapbox/index.tsx | 1 + .../common/mapbox/layers/raster-timeseries.tsx | 5 +++-- app/scripts/components/common/mapbox/layers/utils.ts | 8 +++++--- .../common/mapbox/layers/vector-timeseries.tsx | 11 ++++------- app/scripts/components/datasets/s-explore/index.tsx | 6 +++++- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/scripts/components/common/mapbox/index.tsx b/app/scripts/components/common/mapbox/index.tsx index 83aff43dc..beeedef51 100644 --- a/app/scripts/components/common/mapbox/index.tsx +++ b/app/scripts/components/common/mapbox/index.tsx @@ -419,6 +419,7 @@ function MapboxMapComponent( id={`base-${baseLayerResolvedData.id}`} stacCol={baseLayerResolvedData.stacCol} mapInstance={mapRef.current} + urlPosition={initialPosition} date={date} sourceParams={baseLayerResolvedData.sourceParams} zoomExtent={baseLayerResolvedData.zoomExtent} diff --git a/app/scripts/components/common/mapbox/layers/raster-timeseries.tsx b/app/scripts/components/common/mapbox/layers/raster-timeseries.tsx index 45ea69511..afc1e2061 100644 --- a/app/scripts/components/common/mapbox/layers/raster-timeseries.tsx +++ b/app/scripts/components/common/mapbox/layers/raster-timeseries.tsx @@ -75,7 +75,8 @@ export function MapLayerRasterTimeseries(props: MapLayerRasterTimeseriesProps) { bounds, onStatusChange, isHidden, - idSuffix = '' + idSuffix = '', + urlPosition } = props; const theme = useTheme(); @@ -471,7 +472,7 @@ export function MapLayerRasterTimeseries(props: MapLayerRasterTimeseriesProps) { () => (stacCollection.length ? getMergedBBox(stacCollection) : undefined), [stacCollection] ); - useFitBbox(mapInstance, bounds, layerBounds); + useFitBbox(mapInstance, urlPosition, bounds, layerBounds); return null; } diff --git a/app/scripts/components/common/mapbox/layers/utils.ts b/app/scripts/components/common/mapbox/layers/utils.ts index 31bc5c2de..fd7851ddd 100644 --- a/app/scripts/components/common/mapbox/layers/utils.ts +++ b/app/scripts/components/common/mapbox/layers/utils.ts @@ -133,7 +133,7 @@ export const getCompareLayerData = ( type: otherLayer.type, name: otherLayer.name, description: otherLayer.description, - legend: otherLayer.legend, + legend: otherLayer.legend, stacCol: otherLayer.stacCol, zoomExtent: zoomExtent ?? otherLayer.zoomExtent, sourceParams: defaultsDeep({}, sourceParams, otherLayer.sourceParams), @@ -432,7 +432,6 @@ export function useLayerInteraction({ }, [layerId, mapInstance, onClick]); } - type OptionalBbox = number[] | undefined | null; /** @@ -445,10 +444,13 @@ type OptionalBbox = number[] | undefined | null; */ export function useFitBbox( mapInstance: MapboxMap, + urlPosition: any, initialBbox: OptionalBbox, stacBbox: OptionalBbox ) { useEffect(() => { + if (urlPosition) return; + // Prefer layer defined bounds to STAC collection bounds. const bounds = (initialBbox ?? stacBbox) as | [number, number, number, number] @@ -457,5 +459,5 @@ export function useFitBbox( if (bounds?.length && checkFitBoundsFromLayer(bounds, mapInstance)) { mapInstance.fitBounds(bounds, { padding: FIT_BOUNDS_PADDING }); } - }, [mapInstance, initialBbox, stacBbox]); + }, [mapInstance, urlPosition, initialBbox, stacBbox]); } diff --git a/app/scripts/components/common/mapbox/layers/vector-timeseries.tsx b/app/scripts/components/common/mapbox/layers/vector-timeseries.tsx index 1a153214d..cf4ed58a7 100644 --- a/app/scripts/components/common/mapbox/layers/vector-timeseries.tsx +++ b/app/scripts/components/common/mapbox/layers/vector-timeseries.tsx @@ -12,11 +12,7 @@ import { Feature } from 'geojson'; import { endOfDay, startOfDay } from 'date-fns'; import centroid from '@turf/centroid'; -import { - requestQuickCache, - useFitBbox, - useLayerInteraction -} from './utils'; +import { requestQuickCache, useFitBbox, useLayerInteraction } from './utils'; import { useMapStyle } from './styles'; import { useCustomMarker } from './custom-marker'; @@ -47,7 +43,8 @@ export function MapLayerVectorTimeseries(props: MapLayerVectorTimeseriesProps) { bounds, onStatusChange, isHidden, - idSuffix = '' + idSuffix = '', + urlPosition } = props; const theme = useTheme(); @@ -288,7 +285,7 @@ export function MapLayerVectorTimeseries(props: MapLayerVectorTimeseriesProps) { // // FitBounds when needed // - useFitBbox(mapInstance, bounds, featuresBbox); + useFitBbox(mapInstance, urlPosition, bounds, featuresBbox); return null; } diff --git a/app/scripts/components/datasets/s-explore/index.tsx b/app/scripts/components/datasets/s-explore/index.tsx index 5f4d9be74..2a8556e15 100644 --- a/app/scripts/components/datasets/s-explore/index.tsx +++ b/app/scripts/components/datasets/s-explore/index.tsx @@ -569,7 +569,11 @@ function DatasetsExplore() { compareDate={selectedCompareDatetime ?? undefined} isComparing={isComparing} initialPosition={mapPosition ?? undefined} - onPositionChange={setMapPosition} + onPositionChange={(v) => { + if (v.userInitiated) { + setMapPosition(v); + } + }} projection={mapProjection ?? projectionDefault} onProjectionChange={setMapProjection} />