From 92ae3d93a44d7801ba7709feb145215abc4ca356 Mon Sep 17 00:00:00 2001 From: Luca Ruschioni <44363237+Ruschio@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:14:53 +0100 Subject: [PATCH 1/5] Set Filter method for GeoJSON Source Adding setFilter method for GeoJSON source, to allow filtering data without passing new ones with setData function --- src/source/geojson_source.ts | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index a7aa4c1bd85..eea65d5478f 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -253,6 +253,48 @@ class GeoJSONSource extends Evented implements ISource { return this; } + /** + * Sets filter for the GeoJSON data source and re-renders the map. + * + * @param {Array | string} filter An array for the filter expression. + * @returns {GeoJSONSource} Returns itself to allow for method chaining. + * @example + * map.addSource('source_id', { + * type: 'geojson', + * data: { + * "type": "FeatureCollection", + * "features": [{ + * "type": "Feature", + * "properties": {"name": "Null Island"}, + * "geometry": { + * "type": "Point", + * "coordinates": [ 0, 0 ] + * } + * }, + * { + * "type": "Feature", + * "properties": {"name": "Another Island"}, + * "geometry": { + * "type": "Point", + * "coordinates": [ 1, 1 ] + * } + * }] + * } + * }); + * const geojsonSource = map.getSource('source_id'); + * // Update the filter after the GeoJSON source was created + * geojsonSource.setFilter([ + * "==", + * ["get", "name"], + * "Another Island" + * ]); + */ + setFilter(filter: Array | string): this { + this.workerOptions = extend$1({ filter: filter }, this.workerOptions); + this._updateWorkerData(); + return this; + } + /** * For clustered sources, fetches the zoom at which the given cluster expands. * From 8673c7e2341df1bf5334d41cc980ddc35dd888be Mon Sep 17 00:00:00 2001 From: Luca Ruschioni <44363237+Ruschio@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:29:09 +0100 Subject: [PATCH 2/5] Fix mistakes --- src/source/geojson_source.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index eea65d5478f..9a118e75574 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -256,7 +256,7 @@ class GeoJSONSource extends Evented implements ISource { /** * Sets filter for the GeoJSON data source and re-renders the map. * - * @param {Array | string} filter An array for the filter expression. + * @param {Array} filter An array for the filter expression. * @returns {GeoJSONSource} Returns itself to allow for method chaining. * @example * map.addSource('source_id', { @@ -289,8 +289,8 @@ class GeoJSONSource extends Evented implements ISource { * "Another Island" * ]); */ - setFilter(filter: Array | string): this { - this.workerOptions = extend$1({ filter: filter }, this.workerOptions); + setFilter(filter: Array): this { + this.workerOptions = extend({filter: filter}, this.workerOptions); this._updateWorkerData(); return this; } From ab05d71bccbbeb718e406a033697637ebb07a31e Mon Sep 17 00:00:00 2001 From: Luca Ruschioni <44363237+Ruschio@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:37:28 +0100 Subject: [PATCH 3/5] Fix object-shorthand lint error Fix object-shorthand lint error --- src/source/geojson_source.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index 9a118e75574..5f2b8bdfe9a 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -290,7 +290,7 @@ class GeoJSONSource extends Evented implements ISource { * ]); */ setFilter(filter: Array): this { - this.workerOptions = extend({filter: filter}, this.workerOptions); + this.workerOptions = extend({filter}, this.workerOptions); this._updateWorkerData(); return this; } From 74424d2cd44f39e9c106c145f4d2ecd134da459e Mon Sep 17 00:00:00 2001 From: Luca Ruschioni <44363237+Ruschio@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:57:37 +0100 Subject: [PATCH 4/5] Add FilterSpecification type --- src/source/geojson_source.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index 5f2b8bdfe9a..d647d5a0eb0 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -11,7 +11,7 @@ import type Tile from './tile'; import type Actor from '../util/actor'; import type {Callback} from '../types/callback'; import type {GeoJSONWorkerOptions} from './geojson_worker_source'; -import type {GeoJSONSourceSpecification, PromoteIdSpecification} from '../style-spec/types'; +import type {GeoJSONSourceSpecification, PromoteIdSpecification, FilterSpecification} from '../style-spec/types'; import type {Cancelable} from '../types/cancelable'; /** @@ -256,7 +256,7 @@ class GeoJSONSource extends Evented implements ISource { /** * Sets filter for the GeoJSON data source and re-renders the map. * - * @param {Array} filter An array for the filter expression. + * @param {FilterSpecification} filter A FilterSpecification type for the filter expression. * @returns {GeoJSONSource} Returns itself to allow for method chaining. * @example * map.addSource('source_id', { @@ -289,7 +289,7 @@ class GeoJSONSource extends Evented implements ISource { * "Another Island" * ]); */ - setFilter(filter: Array): this { + setFilter(filter: ?FilterSpecification): this { this.workerOptions = extend({filter}, this.workerOptions); this._updateWorkerData(); return this; From 64ddbfd9d5f55a8d6615fa2011f8849d2b600787 Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Wed, 18 Sep 2024 13:38:03 +0300 Subject: [PATCH 5/5] fixup --- src/source/geojson_source.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index d647d5a0eb0..b77069be00d 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -289,7 +289,7 @@ class GeoJSONSource extends Evented implements ISource { * "Another Island" * ]); */ - setFilter(filter: ?FilterSpecification): this { + setFilter(filter?: FilterSpecification): this { this.workerOptions = extend({filter}, this.workerOptions); this._updateWorkerData(); return this;