Skip to content

Commit 7dfd543

Browse files
lazergCommanderStormHarelM
authored
feat: add getClusterOptions getter to GeoJSONSource (#7948)
* feat: add getClusterOptions getter to GeoJSONSource * test: reuse a single options object for setClusterOptions and its expectation --------- Co-authored-by: Frank Elsinga <frank@elsinga.de> Co-authored-by: Harel M <harel.mazor@gmail.com>
1 parent 02e7b0f commit 7dfd543

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
## main
22
### ✨ Features and improvements
3+
- Add `GeoJSONSource.getClusterOptions` to get a source's current cluster options (`cluster`, `clusterMaxZoom`, `clusterRadius`) ([#7948](https://github.com/maplibre/maplibre-gl-js/pull/7948)) (by [@lazerg](https://github.com/lazerg))
34
- Support `global-state` expressions in `light.*` properties ([#7966](https://github.com/maplibre/maplibre-gl-js/pull/7966)) (by [@CommanderStorm](https://github.com/CommanderStorm))
45
- Add `MapOptions.rotateSpeed` and `MapOptions.pitchSpeed`, the degrees the bearing/pitch change per pixel dragged ([#7949](https://github.com/maplibre/maplibre-gl-js/pull/7949)) (by [@clement-igonet](https://github.com/clement-igonet))
56
- _...Add new stuff here..._
67

78
### 🐞 Bug fixes
89
- Fix an error thrown when a paint property transitions between arrays of different length ([#6606](https://github.com/maplibre/maplibre-gl-js/issues/6606)) (by [@HarelM](https://github.com/HarelM))
9-
- Fix renderer crash when `RasterTileSource#setTiles`/`setUrl` is called while the source contains errored tiles ([#7911](https://github.com/maplibre/maplibre-gl-js/pull/7911)) (by [@lazerg](https://github.com/lazerg))
10+
- Fix renderer crash when `RasterTileSource.setTiles`/`setUrl` is called while the source contains errored tiles ([#7911](https://github.com/maplibre/maplibre-gl-js/pull/7911)) (by [@lazerg](https://github.com/lazerg))
1011
- Fix globe latitude precision on some GPUs (e.g. Mali) by reformulating the mercator-to-sphere Y coordinate algebraically (`exp` + rational arithmetic instead of `atan`/`sin`/`cos`), avoiding float32 cancellation and imprecise hardware transcendentals near the equator; the runtime GPU `atan`-error measurement/correction this superseded has also been removed ([#7419](https://github.com/maplibre/maplibre-gl-js/issues/7419)) (by [@clement-igonet](https://github.com/clement-igonet))
1112
- _...Add new stuff here..._
1213

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {now, setNow, restoreNow, isTimeFrozen} from './util/time_control.ts';
2222
import {WorkerPool} from './util/worker_pool.ts';
2323
import {prewarm, clearPrewarmedResources} from './util/global_worker_pool.ts';
2424
import {AJAXError, type ExpiryData, type GetResourceResponse, type RequestParameters} from './util/ajax.ts';
25-
import {GeoJSONSource, type SetClusterOptions} from './source/geojson_source.ts';
25+
import {GeoJSONSource, type GetClusterOptions, type SetClusterOptions} from './source/geojson_source.ts';
2626
import {CanvasSource, type CanvasSourceSpecification} from './source/canvas_source.ts';
2727
import {type CanonicalTileRange, type Coordinates, ImageSource, type UpdateImageOptions} from './source/image_source.ts';
2828
import {RasterDEMTileSource} from './source/raster_dem_tile_source.ts';
@@ -301,6 +301,7 @@ export {
301301
type UpdateImageOptions,
302302
type DragPanOptions,
303303
type FullscreenControlOptions,
304+
type GetClusterOptions,
304305
type SetClusterOptions,
305306
type GeoJSONSourceDiff,
306307
type GeolocateControlOptions,

src/source/geojson_source.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,3 +1312,30 @@ describe('GeoJSONSource.getClusterLeaves', () => {
13121312
vi.resetAllMocks();
13131313
});
13141314
});
1315+
1316+
describe('GeoJSONSource.getClusterOptions', () => {
1317+
test('returns the cluster options configured on the source', () => {
1318+
const source = new GeoJSONSource('id', {
1319+
type: 'geojson',
1320+
data: {} as GeoJSON.GeoJSON,
1321+
cluster: true,
1322+
clusterMaxZoom: 12,
1323+
clusterRadius: 80
1324+
}, mockDispatcher, undefined);
1325+
1326+
expect(source.getClusterOptions()).toEqual({cluster: true, clusterMaxZoom: 12, clusterRadius: 80});
1327+
});
1328+
1329+
test('reflects options updated via setClusterOptions', async () => {
1330+
const source = new GeoJSONSource('id', {
1331+
type: 'geojson',
1332+
data: {} as GeoJSON.GeoJSON,
1333+
cluster: false
1334+
}, mockDispatcher, undefined);
1335+
1336+
const options = {cluster: true, clusterMaxZoom: 9, clusterRadius: 40};
1337+
await source.setClusterOptions(options);
1338+
1339+
expect(source.getClusterOptions()).toEqual(options);
1340+
});
1341+
});

src/source/geojson_source.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ export type SetClusterOptions = {
6969
clusterRadius?: number;
7070
};
7171

72+
/**
73+
* The cluster options currently configured on a source, as returned by `getClusterOptions`
74+
*/
75+
export type GetClusterOptions = {
76+
/**
77+
* Whether or not the source is clustered
78+
*/
79+
cluster: boolean;
80+
/**
81+
* The cluster's max zoom
82+
*/
83+
clusterMaxZoom: number;
84+
/**
85+
* The cluster's radius, in pixels
86+
*/
87+
clusterRadius: number;
88+
};
89+
7290
/**
7391
* A source containing GeoJSON.
7492
* (See the [Style Specification](https://maplibre.org/maplibre-style-spec/#sources-geojson) for detailed documentation of options.)
@@ -226,6 +244,10 @@ export class GeoJSONSource extends Evented<SourceEventType> implements Source {
226244
return pixelValue * (EXTENT / this.tileSize);
227245
}
228246

247+
private _tileUnitsToPixels(tileUnitValue: number): number {
248+
return tileUnitValue / (EXTENT / this.tileSize);
249+
}
250+
229251
private _getClusterMaxZoom(clusterMaxZoom: number): number {
230252
const effectiveClusterMaxZoom = clusterMaxZoom ? Math.round(clusterMaxZoom) : this.maxzoom - 1;
231253
if (!(Number.isInteger(clusterMaxZoom) || clusterMaxZoom === undefined)) {
@@ -321,6 +343,25 @@ export class GeoJSONSource extends Evented<SourceEventType> implements Source {
321343
return this._updateWorkerData();
322344
}
323345

346+
/**
347+
* Gets the cluster options currently configured on the source.
348+
* The returned values mirror the options accepted by `setClusterOptions`.
349+
*
350+
* @returns the source's current cluster options
351+
* @example
352+
* ```ts
353+
* const {cluster, clusterMaxZoom, clusterRadius} = map.getSource('some id').getClusterOptions();
354+
* ```
355+
*/
356+
getClusterOptions(): GetClusterOptions {
357+
const {cluster, clusterOptions} = this.workerOptions.geojsonVtOptions;
358+
return {
359+
cluster,
360+
clusterMaxZoom: clusterOptions.maxZoom,
361+
clusterRadius: this._tileUnitsToPixels(clusterOptions.radius)
362+
};
363+
}
364+
324365
/**
325366
* For clustered sources, fetches the zoom at which the given cluster expands.
326367
*

0 commit comments

Comments
 (0)