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

Support expressions in filters #5193

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion bench/benchmarks/filter_evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = class FilterEvaluate extends Benchmark {
for (const layer of this.layers) {
for (const filter of layer.filters) {
for (const feature of layer.features) {
if (typeof filter(feature) !== 'boolean') {
if (typeof filter({zoom: 0}, feature) !== 'boolean') {
assert(false, 'Expected boolean result from filter');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/circle_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CircleBucket implements Bucket {

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
for (const {feature, index, sourceLayerIndex} of features) {
if (this.layers[0].filter(feature)) {
if (this.layers[0]._featureFilter({zoom: this.zoom}, feature)) {
const geometry = loadGeometry(feature);
this.addFeature(feature, geometry);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/fill_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FillBucket implements Bucket {

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
for (const {feature, index, sourceLayerIndex} of features) {
if (this.layers[0].filter(feature)) {
if (this.layers[0]._featureFilter({zoom: this.zoom}, feature)) {
const geometry = loadGeometry(feature);
this.addFeature(feature, geometry);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/fill_extrusion_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class FillExtrusionBucket implements Bucket {

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
for (const {feature, index, sourceLayerIndex} of features) {
if (this.layers[0].filter(feature)) {
if (this.layers[0]._featureFilter({zoom: this.zoom}, feature)) {
const geometry = loadGeometry(feature);
this.addFeature(feature, geometry);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class LineBucket implements Bucket {

populate(features: Array<IndexedFeature>, options: PopulateParameters) {
for (const {feature, index, sourceLayerIndex} of features) {
if (this.layers[0].filter(feature)) {
if (this.layers[0]._featureFilter({zoom: this.zoom}, feature)) {
const geometry = loadGeometry(feature);
this.addFeature(feature, geometry);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
Expand Down
2 changes: 1 addition & 1 deletion src/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class SymbolBucket implements Bucket {
const globalProperties = {zoom: this.zoom};

for (const {feature, index, sourceLayerIndex} of features) {
if (!layer.filter(feature)) {
if (!layer._featureFilter(globalProperties, feature)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions src/data/feature_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type CollisionTile from '../symbol/collision_tile';
import type TileCoord from '../source/tile_coord';
import type StyleLayer from '../style/style_layer';
import type {SerializedStructArray} from '../util/struct_array';
import type {FeatureFilter} from '../style-spec/feature_filter';

const FeatureIndexArray = createStructArrayType({
members: [
Expand Down Expand Up @@ -178,7 +179,7 @@ class FeatureIndex {
matching: Array<any>,
array: any,
queryGeometry: Array<Array<Point>>,
filter: any,
filter: FeatureFilter,
filterLayerIDs: Array<string>,
styleLayers: {[string]: StyleLayer},
bearing: number,
Expand All @@ -201,7 +202,7 @@ class FeatureIndex {
const sourceLayer = this.vtLayers[sourceLayerName];
const feature = sourceLayer.feature(match.featureIndex);

if (!filter(feature)) continue;
if (!filter({zoom: this.coord.z}, feature)) continue;

let geometry = null;

Expand Down
2 changes: 1 addition & 1 deletion src/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ class Tile {

for (let i = 0; i < layer.length; i++) {
const feature = layer.feature(i);
if (filter(feature)) {
if (filter({zoom: this.coord.z}, feature)) {
const geojsonFeature = new GeoJSONFeature(feature, this.coord.z, this.coord.x, this.coord.y);
(geojsonFeature: any).tile = coord;
result.push(geojsonFeature);
Expand Down
10 changes: 3 additions & 7 deletions src/style-spec/expression/compound_expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import type { Value } from './values';

type Varargs = {| type: Type |};
type Signature = Array<Type> | Varargs;
type Evaluate = (EvaluationContext, Array<Expression>) => Value;
type Evaluate = (EvaluationContext) => Value;
type Definition = [Type, Signature, Evaluate] |
{|type: Type, overloads: Array<[Signature, Evaluate]>|};

class CompoundExpression implements Expression {
key: string;
name: string;
type: Type;
_evaluate: Evaluate;
evaluate: (ctx: EvaluationContext) => any;
args: Array<Expression>;

static definitions: { [string]: Definition };
Expand All @@ -28,14 +28,10 @@ class CompoundExpression implements Expression {
this.key = key;
this.name = name;
this.type = type;
this._evaluate = evaluate;
this.evaluate = evaluate;
this.args = args;
}

evaluate(ctx: EvaluationContext) {
return this._evaluate(ctx, this.args);
}

serialize() {
const name = this.name;
const args = this.args.map(e => e.serialize());
Expand Down
Loading