From 2200020d7786e8c3df01975b5f9623ddd53dc1ad Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Tue, 27 Aug 2024 17:44:30 +1000 Subject: [PATCH] omit a warning if maxzoom <= clusterMaxZoom for geojson sources (#4604) * omit a warning if maxzoom <= clusterMaxZoom for geojson sources * fix lint issues * CHANGELOG entry --- CHANGELOG.md | 1 + src/source/geojson_source.test.ts | 19 +++++++++++++++++++ src/source/geojson_source.ts | 6 +++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b857cb4f..c3031fccaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### ✨ Features and improvements - Support multiple layers in `map.on`, `map.once` and `map.off` methods ([#4279](https://github.com/maplibre/maplibre-gl-js/pull/4401)) +- Ensure GeoJSON cluster sources emit a console warning if `maxzoom` is less than or equal to `clusterMaxZoom` since in this case you may see unexpected results. ([#4604](https://github.com/maplibre/maplibre-gl-js/pull/4604)) - _...Add new stuff here..._ ### 🐞 Bug fixes diff --git a/src/source/geojson_source.test.ts b/src/source/geojson_source.test.ts index f5ef3f63b7..488275aff7 100644 --- a/src/source/geojson_source.test.ts +++ b/src/source/geojson_source.test.ts @@ -55,6 +55,25 @@ const hawkHill = { }] } as GeoJSON.GeoJSON; +describe('GeoJSONSource#constructor', () => { + const mapStub = { + _requestManager: { + transformRequest: (url) => { return {url}; } + } + } as any; + test('warn if maxzoom <= clusterMaxZoom', () => { + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + const source = new GeoJSONSource('id', {data: hawkHill, maxzoom: 4, clusterMaxZoom: 4} as GeoJSONSourceOptions, mockDispatcher, undefined); + source.map = mapStub; + source.load(); + + expect(warn).toHaveBeenCalledWith('The maxzoom value "4" is expected to be greater than the clusterMaxZoom value "4".'); + + warn.mockRestore(); + }); +}); + describe('GeoJSONSource#setData', () => { function createSource(opts?) { opts = opts || {}; diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index dc6da650f2..fabde168d9 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -1,6 +1,6 @@ import {Event, ErrorEvent, Evented} from '../util/evented'; -import {extend} from '../util/util'; +import {extend, warnOnce} from '../util/util'; import {EXTENT} from '../data/extent'; import {ResourceType} from '../util/request_manager'; import {browser} from '../util/browser'; @@ -158,6 +158,10 @@ export class GeoJSONSource extends Evented implements Source { const scale = EXTENT / this.tileSize; + if (options.clusterMaxZoom !== undefined && this.maxzoom <= options.clusterMaxZoom) { + warnOnce(`The maxzoom value "${this.maxzoom}" is expected to be greater than the clusterMaxZoom value "${options.clusterMaxZoom}".`); + } + // sent to the worker, along with `url: ...` or `data: literal geojson`, // so that it can load/parse/index the geojson data // extending with `options.workerOptions` helps to make it easy for