Skip to content

Commit

Permalink
[Maps] use deep equals for mapbox filter expression equals check (#99673
Browse files Browse the repository at this point in the history
)

* [Maps] use deep equals for mapbox filter expression equals check

* cleanup
  • Loading branch information
nreese committed May 11, 2021
1 parent 126a83c commit 5dbc73f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer {
}

const filterExpr = getPointFilterExpression(this.hasJoins());
if (filterExpr !== mbMap.getFilter(pointLayerId)) {
if (!_.isEqual(filterExpr, mbMap.getFilter(pointLayerId))) {
mbMap.setFilter(pointLayerId, filterExpr);
mbMap.setFilter(textLayerId, filterExpr);
}
Expand Down Expand Up @@ -818,7 +818,7 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer {
}

const filterExpr = getPointFilterExpression(this.hasJoins());
if (filterExpr !== mbMap.getFilter(symbolLayerId)) {
if (!_.isEqual(filterExpr, mbMap.getFilter(symbolLayerId))) {
mbMap.setFilter(symbolLayerId, filterExpr);
}

Expand Down Expand Up @@ -900,14 +900,14 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer {
this.syncVisibilityWithMb(mbMap, fillLayerId);
mbMap.setLayerZoomRange(fillLayerId, this.getMinZoom(), this.getMaxZoom());
const fillFilterExpr = getFillFilterExpression(hasJoins);
if (fillFilterExpr !== mbMap.getFilter(fillLayerId)) {
if (!_.isEqual(fillFilterExpr, mbMap.getFilter(fillLayerId))) {
mbMap.setFilter(fillLayerId, fillFilterExpr);
}

this.syncVisibilityWithMb(mbMap, lineLayerId);
mbMap.setLayerZoomRange(lineLayerId, this.getMinZoom(), this.getMaxZoom());
const lineFilterExpr = getLineFilterExpression(hasJoins);
if (lineFilterExpr !== mbMap.getFilter(lineLayerId)) {
if (!_.isEqual(lineFilterExpr, mbMap.getFilter(lineLayerId))) {
mbMap.setFilter(lineLayerId, lineFilterExpr);
}

Expand All @@ -931,7 +931,7 @@ export class VectorLayer extends AbstractLayer implements IVectorLayer {
}

const filterExpr = getCentroidFilterExpression(this.hasJoins());
if (filterExpr !== mbMap.getFilter(centroidLayerId)) {
if (!_.isEqual(filterExpr, mbMap.getFilter(centroidLayerId))) {
mbMap.setFilter(centroidLayerId, filterExpr);
}

Expand Down
82 changes: 38 additions & 44 deletions x-pack/plugins/maps/public/classes/util/mb_filter_expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,55 @@ import {
export const EXCLUDE_TOO_MANY_FEATURES_BOX = ['!=', ['get', KBN_TOO_MANY_FEATURES_PROPERTY], true];
const EXCLUDE_CENTROID_FEATURES = ['!=', ['get', KBN_IS_CENTROID_FEATURE], true];

const VISIBILITY_FILTER_CLAUSE = ['all', ['==', ['get', FEATURE_VISIBLE_PROPERTY_NAME], true]];
// Kibana features are features added by kibana that do not exist in real data
const EXCLUDE_KBN_FEATURES = ['all', EXCLUDE_TOO_MANY_FEATURES_BOX, EXCLUDE_CENTROID_FEATURES];
function getFilterExpression(geometryFilter: unknown[], hasJoins: boolean) {
const filters: unknown[] = [
EXCLUDE_TOO_MANY_FEATURES_BOX,
EXCLUDE_CENTROID_FEATURES,
geometryFilter,
];

const CLOSED_SHAPE_MB_FILTER = [
...EXCLUDE_KBN_FEATURES,
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POLYGON],
],
];
if (hasJoins) {
filters.push(['==', ['get', FEATURE_VISIBLE_PROPERTY_NAME], true]);
}

const VISIBLE_CLOSED_SHAPE_MB_FILTER = [...VISIBILITY_FILTER_CLAUSE, CLOSED_SHAPE_MB_FILTER];

const ALL_SHAPE_MB_FILTER = [
...EXCLUDE_KBN_FEATURES,
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.LINE_STRING],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_LINE_STRING],
],
];

const VISIBLE_ALL_SHAPE_MB_FILTER = [...VISIBILITY_FILTER_CLAUSE, ALL_SHAPE_MB_FILTER];

const POINT_MB_FILTER = [
...EXCLUDE_KBN_FEATURES,
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POINT],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POINT],
],
];

const VISIBLE_POINT_MB_FILTER = [...VISIBILITY_FILTER_CLAUSE, POINT_MB_FILTER];

const CENTROID_MB_FILTER = ['all', ['==', ['get', KBN_IS_CENTROID_FEATURE], true]];

const VISIBLE_CENTROID_MB_FILTER = [...VISIBILITY_FILTER_CLAUSE, CENTROID_MB_FILTER];
return ['all', ...filters];
}

export function getFillFilterExpression(hasJoins: boolean): unknown[] {
return hasJoins ? VISIBLE_CLOSED_SHAPE_MB_FILTER : CLOSED_SHAPE_MB_FILTER;
return getFilterExpression(
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POLYGON],
],
hasJoins
);
}

export function getLineFilterExpression(hasJoins: boolean): unknown[] {
return hasJoins ? VISIBLE_ALL_SHAPE_MB_FILTER : ALL_SHAPE_MB_FILTER;
return getFilterExpression(
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POLYGON],
['==', ['geometry-type'], GEO_JSON_TYPE.LINE_STRING],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_LINE_STRING],
],
hasJoins
);
}

export function getPointFilterExpression(hasJoins: boolean): unknown[] {
return hasJoins ? VISIBLE_POINT_MB_FILTER : POINT_MB_FILTER;
return getFilterExpression(
[
'any',
['==', ['geometry-type'], GEO_JSON_TYPE.POINT],
['==', ['geometry-type'], GEO_JSON_TYPE.MULTI_POINT],
],
hasJoins
);
}

export function getCentroidFilterExpression(hasJoins: boolean): unknown[] {
return hasJoins ? VISIBLE_CENTROID_MB_FILTER : CENTROID_MB_FILTER;
return getFilterExpression(['==', ['get', KBN_IS_CENTROID_FEATURE], true], hasJoins);
}
41 changes: 14 additions & 27 deletions x-pack/test/functional/apps/maps/mapbox_styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@ export default function ({ getPageObjects, getService }) {
maxzoom: 24,
filter: [
'all',
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
['any', ['==', ['geometry-type'], 'Point'], ['==', ['geometry-type'], 'MultiPoint']],
['==', ['get', '__kbn_isvisibleduetojoin__'], true],
[
'all',
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
['any', ['==', ['geometry-type'], 'Point'], ['==', ['geometry-type'], 'MultiPoint']],
],
],
layout: { visibility: 'visible' },
paint: {
Expand Down Expand Up @@ -124,17 +121,10 @@ export default function ({ getPageObjects, getService }) {
maxzoom: 24,
filter: [
'all',
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
['any', ['==', ['geometry-type'], 'Polygon'], ['==', ['geometry-type'], 'MultiPolygon']],
['==', ['get', '__kbn_isvisibleduetojoin__'], true],
[
'all',
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
[
'any',
['==', ['geometry-type'], 'Polygon'],
['==', ['geometry-type'], 'MultiPolygon'],
],
],
],
layout: { visibility: 'visible' },
paint: {
Expand Down Expand Up @@ -208,19 +198,16 @@ export default function ({ getPageObjects, getService }) {
maxzoom: 24,
filter: [
'all',
['==', ['get', '__kbn_isvisibleduetojoin__'], true],
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
[
'all',
['!=', ['get', '__kbn_too_many_features__'], true],
['!=', ['get', '__kbn_is_centroid_feature__'], true],
[
'any',
['==', ['geometry-type'], 'Polygon'],
['==', ['geometry-type'], 'MultiPolygon'],
['==', ['geometry-type'], 'LineString'],
['==', ['geometry-type'], 'MultiLineString'],
],
'any',
['==', ['geometry-type'], 'Polygon'],
['==', ['geometry-type'], 'MultiPolygon'],
['==', ['geometry-type'], 'LineString'],
['==', ['geometry-type'], 'MultiLineString'],
],
['==', ['get', '__kbn_isvisibleduetojoin__'], true],
],
layout: { visibility: 'visible' },
paint: { 'line-color': '#41937c', 'line-opacity': 0.75, 'line-width': 1 },
Expand Down

0 comments on commit 5dbc73f

Please sign in to comment.