Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Filter method for GeoJSON Source #13023

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/source/geojson_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -253,6 +253,48 @@ class GeoJSONSource extends Evented<SourceEvents> implements ISource {
return this;
}

/**
* Sets filter for the GeoJSON data source and re-renders the map.
*
* @param {FilterSpecification} filter A FilterSpecification type 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?: FilterSpecification): this {
this.workerOptions = extend({filter}, this.workerOptions);
this._updateWorkerData();
return this;
}

/**
* For clustered sources, fetches the zoom at which the given cluster expands.
*
Expand Down
Loading