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

Fix using within expression with complex filter #9611

Merged
merged 3 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 14 additions & 4 deletions src/style-spec/feature_filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,13 @@ function createFilter(filter: any): FeatureFilter {
return {filter: () => true, needGeometry: false};
}

if (!isExpressionFilter(filter)) {
filter = convertFilter(filter);
}
filter = convertFilter(filter);

const compiled = createExpression(filter, filterSpec);
if (compiled.result === 'error') {
throw new Error(compiled.value.map(err => `${err.key}: ${err.message}`).join(', '));
} else {
const needGeometry = Array.isArray(filter) && filter.length !== 0 && filter[0] === 'within';
const needGeometry = geometryNeeded(filter);
return {filter: (globalProperties: GlobalProperties, feature: Feature, canonical?: CanonicalTileID) => compiled.value.evaluate(globalProperties, feature, {}, canonical),
needGeometry};
}
Expand All @@ -96,7 +94,19 @@ function compare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}

function geometryNeeded(filter) {
if (!Array.isArray(filter)) return false;
let needed = false;
for (let index = 0; index < filter.length; index++) {
const val = filter[index];
const op = Array.isArray(val) ? val[0] : val;
needed = needed || (op === 'within');
}
return needed;
arindam1993 marked this conversation as resolved.
Show resolved Hide resolved
}

function convertFilter(filter: ?Array<any>): mixed {
if (isExpressionFilter(filter)) return filter;
if (!filter) return true;
const op = filter[0];
if (filter.length <= 1) return (op !== 'any');
Expand Down
3 changes: 3 additions & 0 deletions src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,9 @@
},
"!has": {
"doc": "`[\"!has\", key]` `feature[key]` does not exist"
},
"within": {
"doc": "`[\"within\", object]` feature geometry is within object geometry"
}
},
"doc": "The filter operator."
Expand Down
10 changes: 8 additions & 2 deletions src/style-spec/validate/validate_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ function validateNonExpressionFilter(options) {
errors.push(new ValidationError(`${key}[1]`, value[1], `string expected, ${type} found`));
}
break;

case 'within':
type = getType(value[1]);
if (value.length !== 2) {
errors.push(new ValidationError(key, value, `filter array for "${value[0]}" operator must have 2 elements`));
} else if (type !== 'object') {
errors.push(new ValidationError(`${key}[1]`, value[1], `object expected, ${type} found`));
}
break;
}

return errors;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64
}
},
"zoom": 3,
"center": [2.5, 2.5],
"sources": {
"points": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "ABC"
},
"geometry": {
"type": "Point",
"coordinates": [
1.9775390625,
2.3284603685731593
]
}
},
{
"type": "Feature",
"properties": {
"name": "ABC"
},
"geometry": {
"type": "Point",
"coordinates": [
1.7138671875,
-1.7136116598836224
]
}
}
]
}
},
"polygon": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[[0, 0],
[0, 5],
[5, 5],
[5, 0],
[0, 0]]
]
}
}
]
}
}
},
"layers": [
{
"id": "border",
"type": "fill",
"source": "polygon",
"paint": {
"fill-color": "black",
"fill-opacity": 0.5
}
},
{
"id": "circle",
"type": "circle",
"source": "points",
"filter": ["all", ["in", "ABC", ["get", "name"]], ["within", {
"type": "Polygon",
"coordinates": [
[
[0, 0],
[0, 5],
[5, 5],
[5, 0],
[0, 0]
]
]
}]
],
"paint": {
"circle-radius": 5,
"circle-color": "red"
}
}
]
}