Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: more fixes to WMS layer (#3603)
Igor Dykhta800083e0498e
diff --git a/src/deckgl-layers/src/wms/wms-layer.ts b/src/deckgl-layers/src/wms/wms-layer.tsindex 85a4c65f7..6201cf9e3 100644--- a/src/deckgl-layers/src/wms/wms-layer.ts+++ b/src/deckgl-layers/src/wms/wms-layer.ts@@ -85,6 +85,14 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {width: number;height: number;};+ inFlightRequestParameters?: {+ bbox: [number, number, number, number];+ boundingBox: [[number, number], [number, number]];+ layers: string[];+ srs: 'EPSG:4326' | 'EPSG:3857';+ width: number;+ height: number;+ };lastRequestId: number;_nextRequestId: number;/** TODO: Change any => setTimeout return type. Different between Node and browser... */@@ -220,6 +228,19 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {? getGlobeVisibleBounds(viewport): viewport.getBounds();const {width, height} = viewport;++ // Edge case: when bounds are perfectly symmetric around 0° (both longitude and latitude),+ // it can cause rendering artifacts with EPSG:4326 on the globe.+ // Add asymmetry to avoid this issue.+ if (viewport.resolution && bounds) {+ const minLat = bounds[1];+ const maxLat = bounds[3];+ // Check if latitude bounds are symmetric around 0° (within 0.1° tolerance)+ if (Math.abs(minLat + maxLat) < 0.1 && Math.abs(minLat) > 1) {+ bounds[3] += 0.1;+ }+ }+let {srs} = this.props;if (srs === 'auto') {// BitmapLayer only supports LNGLAT or CARTESIAN (Web-Mercator)@@ -249,16 +270,16 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {requestParams.bbox = [minX, minY, maxX, maxY];}- // Skip request if parameters haven't meaningfully changed+ // Skip request if parameters haven't meaningfully changed from an in-flight requestif (- this.state.lastRequestParameters &&- this._areRequestParamsEqual(this.state.lastRequestParameters, requestParams)+ this.state.inFlightRequestParameters &&+ this._areRequestParamsEqual(this.state.inFlightRequestParameters, requestParams)) {return;}- // Skip request if the new view is already fully covered by the last requested- // image at comparable resolution. This is very common while panning (and small+ // Skip request if the new view is already fully covered by the last COMPLETED request+ // at comparable resolution. This is very common while panning (and small// zoom-ins) within a larger, recently fetched extent — e.g. on the globe a// single request can cover the whole visible hemisphere, so rotating around it// shouldn't hit the network again. The existing image is simply repositioned by@@ -273,25 +294,46 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {// During video export, throttle zoom-in requests to only fire on zoom level crossings.// The existing image is stretched by the BitmapLayer until the next level is reached.+ // However, always allow requests if the new view isn't fully covered by the last image.const gl = this.context.gl;const isExporting = gl?.getContextAttributes?.()?.preserveDrawingBuffer;+if (isExporting && this.state._lastRequestZoom >= 0) {const currentZoom = viewport.zoom;const lastZoom = this.state._lastRequestZoom;const isZoomingIn = currentZoom > lastZoom;- if (isZoomingIn && Math.floor(currentZoom) === Math.floor(lastZoom)) {++ // Check if the new view is fully covered by the last loaded image+ const lastBbox = this.state.lastRequestParameters?.bbox;+ const currentBbox = requestParams.bbox;+ let fullyContained = false;++ if (lastBbox && currentBbox) {+ // New bbox must be fully inside the previous bbox+ fullyContained =+ currentBbox[0] >= lastBbox[0] &&+ currentBbox[1] >= lastBbox[1] &&+ currentBbox[2] <= lastBbox[2] &&+ currentBbox[3] <= lastBbox[3];+ }++ // Only throttle if:+ // 1. Zooming in (not zooming out or panning at same zoom)+ // 2. Same zoom floor (haven't crossed an integer zoom level)+ // 3. New view is fully contained in the last loaded image (no uncovered areas)+ if (isZoomingIn && Math.floor(currentZoom) === Math.floor(lastZoom) && fullyContained) {return;}}- // Update lastRequestParameters BEFORE making the request to prevent race conditions- // where multiple debounced calls see the same old parameters and all proceed- this.state.lastRequestParameters = requestParams;- this.state._lastRequestZoom = viewport.zoom;+ // Mark request as in-flight to prevent duplicate requests+ this.state.inFlightRequestParameters = requestParams;const requestId = this.getRequestId();try {this.state.loadCounter++;+ // Trigger a redraw to update the UI loading indicator+ this.setNeedsRedraw();this.props.onImageLoadStart(requestId);const image = await this.state.imageSource.getImage(requestParams);@@ -299,6 +341,10 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {// If a request takes a long time, later requests may have already loaded.if (this.state.lastRequestId < requestId) {this.getCurrentLayer()?.props.onImageLoad(requestId);+ // Update lastRequestParameters only after successful load+ // This ensures coverage checks only use successfully loaded images+ this.state.lastRequestParameters = requestParams;+ this.state._lastRequestZoom = viewport.zoom;// Not type safe...this.setState({image,@@ -310,7 +356,11 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {this.context.onError?.(error as Error, this);this.getCurrentLayer()?.props.onImageLoadError(requestId, error as Error);} finally {+ // Clear in-flight marker when request completes (success or error)+ this.state.inFlightRequestParameters = undefined;this.state.loadCounter--;+ // Trigger a redraw to update the UI loading indicator+ this.setNeedsRedraw();}}@@ -352,6 +402,7 @@ export default class WMSLayer extends CompositeLayer<Required<_WMSLayerProps>> {// Is the new bbox fully contained within the previously requested bbox?const contained = n[0] >= p[0] && n[1] >= p[1] && n[2] <= p[2] && n[3] <= p[3];+if (!contained) {return false;}diff --git a/src/layers/src/wms-layer/wms-layer.ts b/src/layers/src/wms-layer/wms-layer.tsindex 96d626090..638955b1f 100644--- a/src/layers/src/wms-layer/wms-layer.ts+++ b/src/layers/src/wms-layer/wms-layer.ts@@ -82,6 +82,7 @@ export default class WMSLayer extends AbstractTileLayer<WMSTile, any[]> {const defaultWmsLayer = props.layers?.[0] ?? null;this.registerVisConfig(wmsTileVisConfigs);+this.updateLayerVisConfig({opacity: 0.8, // Default opacitywmsLayer: defaultWmsLayer,diff --git a/src/reducers/src/map-state-updaters.ts b/src/reducers/src/map-state-updaters.tsindex 7e7f51f46..486099424 100644--- a/src/reducers/src/map-state-updaters.ts+++ b/src/reducers/src/map-state-updaters.ts@@ -187,13 +187,21 @@ export const fitBoundsUpdater = (return state;}+ // In globe mode, clamp zoom to globe's allowed range+ const isGlobeMode = Boolean(state.globe?.enabled);+ const zoom = Number.isFinite(centerAndZoom.zoom)+ ? isGlobeMode+ ? Math.min(Math.max(centerAndZoom.zoom, GLOBE_MIN_ZOOM), GLOBE_MAX_ZOOM)+ : centerAndZoom.zoom+ : undefined;+const newState = {...state,latitude: centerAndZoom.center[1],longitude: centerAndZoom.center[0],// For marginal or invalid bounds, zoom may be NaN. Make sure to provide a valid value in order// to avoid corrupt state and potential crashes as zoom is expected to be a number- ...(Number.isFinite(centerAndZoom.zoom) ? {zoom: centerAndZoom.zoom} : {})+ ...(zoom !== undefined ? {zoom} : {})};// if fitting to bounds while split and unsynceddiff --git a/test/node/reducers/map-state-test.js b/test/node/reducers/map-state-test.jsindex 0f1ca4e2d..1b8501b13 100644--- a/test/node/reducers/map-state-test.js+++ b/test/node/reducers/map-state-test.js@@ -438,6 +438,62 @@ test('#mapStateReducer -> FIT_BOUNDS.invalid', t => {t.end();});+test('#mapStateReducer -> FIT_BOUNDS - globe mode clamps zoom to globe range', t => {+ // When in globe mode, fitBounds should respect GLOBE_MIN_ZOOM and GLOBE_MAX_ZOOM++ const mapUpdate = {+ width: 640,+ height: 480+ };++ // World-wide bounds that would normally zoom out very far (below GLOBE_MIN_ZOOM)+ const worldWideBounds = [-180, -85, 180, 85];++ // First set up the map with dimensions+ let state = reducer(undefined, updateMap(mapUpdate, 0));++ // Enter globe mode+ state = reducer(state, setMapViewMode(MapViewMode.MODE_GLOBE));+ t.equal(state.globe.enabled, true, 'sanity: globe should be enabled');++ // Now fit to world-wide bounds (which would zoom out beyond globe's minimum)+ state = reducer(state, fitBounds(worldWideBounds));++ t.ok(+ state.zoom >= GLOBE_MIN_ZOOM,+ `zoom should not go below GLOBE_MIN_ZOOM (${GLOBE_MIN_ZOOM}), got ${state.zoom}`+ );+ t.ok(+ state.zoom <= GLOBE_MAX_ZOOM,+ `zoom should not exceed GLOBE_MAX_ZOOM (${GLOBE_MAX_ZOOM}), got ${state.zoom}`+ );++ // Test with very small bounds that would zoom in very far (above GLOBE_MAX_ZOOM)+ const smallBounds = [5.668, 45.111, 5.670, 45.113];+ state = reducer(state, fitBounds(smallBounds));++ t.ok(+ state.zoom >= GLOBE_MIN_ZOOM,+ `zoom should not go below GLOBE_MIN_ZOOM (${GLOBE_MIN_ZOOM}), got ${state.zoom}`+ );+ t.ok(+ state.zoom <= GLOBE_MAX_ZOOM,+ `zoom should not exceed GLOBE_MAX_ZOOM (${GLOBE_MAX_ZOOM}), got ${state.zoom}`+ );++ // In non-globe mode (2D), fitBounds should not be constrained by globe limits+ state = reducer(state, setMapViewMode(MapViewMode.MODE_2D));+ const prevZoom = state.zoom;+ state = reducer(state, fitBounds(smallBounds));+ // The zoom should be able to go higher than GLOBE_MAX_ZOOM when not in globe mode+ t.ok(+ state.zoom > GLOBE_MAX_ZOOM || state.zoom === prevZoom,+ 'in 2D mode, zoom should not be limited by GLOBE_MAX_ZOOM'+ );++ t.end();+});+test('#mapStateReducer -> SPLIT_MAP: toggle', t => {let newState = reducer(INITIAL_MAP_STATE, toggleSplitMap());