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

[Maps] spatially filter by all geo fields #100735

Merged
merged 9 commits into from
Jun 2, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,61 @@ describe('mapSpatialFilter()', () => {
expect(result).toHaveProperty('type', FILTERS.SPATIAL_FILTER);
});

test('should return the key for matching multi field filter', async () => {
const filter = {
meta: {
alias: 'my spatial filter',
isMultiIndex: true,
type: FILTERS.SPATIAL_FILTER,
} as FilterMeta,
query: {
bool: {
should: [
{
bool: {
must: [
{
exists: {
field: 'geo.coordinates',
},
},
{
geo_distance: {
distance: '1000km',
'geo.coordinates': [120, 30],
},
},
],
},
},
{
bool: {
must: [
{
exists: {
field: 'location',
},
},
{
geo_distance: {
distance: '1000km',
location: [120, 30],
},
},
],
},
},
],
},
},
} as Filter;
const result = mapSpatialFilter(filter);

expect(result).toHaveProperty('key', 'query');
expect(result).toHaveProperty('value', '');
expect(result).toHaveProperty('type', FILTERS.SPATIAL_FILTER);
});

test('should return undefined for none matching', async (done) => {
const filter = {
meta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@ export const mapSpatialFilter = (filter: Filter) => {
value: '',
};
}

if (
filter.meta &&
filter.meta.type === FILTERS.SPATIAL_FILTER &&
filter.meta.isMultiIndex &&
filter.query?.bool?.should
) {
return {
key: 'query',
type: filter.meta.type,
value: '',
};
}
throw filter;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ReactNode } from 'react';
import { GeoJsonProperties } from 'geojson';
import { Geometry } from 'geojson';
import { Query } from '../../../../../src/plugins/data/common';
import { DRAW_TYPE, ES_GEO_FIELD_TYPE, ES_SPATIAL_RELATIONS } from '../constants';
import { DRAW_TYPE, ES_SPATIAL_RELATIONS } from '../constants';

export type MapExtent = {
minLon: number;
Expand Down Expand Up @@ -70,9 +70,6 @@ export type DrawState = {
actionId: string;
drawType: DRAW_TYPE;
filterLabel?: string; // point radius filter alias
geoFieldName?: string;
geoFieldType?: ES_GEO_FIELD_TYPE;
geometryLabel?: string;
indexPatternId?: string;
relation?: ES_SPATIAL_RELATIONS;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
hitsToGeoJson,
geoPointToGeometry,
geoShapeToGeometry,
createExtentFilter,
roundCoordinates,
extractFeaturesFromFilters,
makeESBbox,
scaleBounds,
} from './elasticsearch_geo_utils';
Expand Down Expand Up @@ -388,94 +386,6 @@ describe('geoShapeToGeometry', () => {
});
});

describe('createExtentFilter', () => {
it('should return elasticsearch geo_bounding_box filter', () => {
const mapExtent = {
maxLat: 39,
maxLon: -83,
minLat: 35,
minLon: -89,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-89, 39],
bottom_right: [-83, 35],
},
});
});

it('should clamp longitudes to -180 to 180 and latitudes to -90 to 90', () => {
const mapExtent = {
maxLat: 120,
maxLon: 200,
minLat: -100,
minLon: -190,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-180, 89],
bottom_right: [180, -89],
},
});
});

it('should make left longitude greater than right longitude when area crosses 180 meridian east to west', () => {
const mapExtent = {
maxLat: 39,
maxLon: 200,
minLat: 35,
minLon: 100,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
const leftLon = filter.geo_bounding_box.location.top_left[0];
const rightLon = filter.geo_bounding_box.location.bottom_right[0];
expect(leftLon).toBeGreaterThan(rightLon);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [100, 39],
bottom_right: [-160, 35],
},
});
});

it('should make left longitude greater than right longitude when area crosses 180 meridian west to east', () => {
const mapExtent = {
maxLat: 39,
maxLon: -100,
minLat: 35,
minLon: -200,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
const leftLon = filter.geo_bounding_box.location.top_left[0];
const rightLon = filter.geo_bounding_box.location.bottom_right[0];
expect(leftLon).toBeGreaterThan(rightLon);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [160, 39],
bottom_right: [-100, 35],
},
});
});

it('should clamp longitudes to -180 to 180 when longitude wraps globe', () => {
const mapExtent = {
maxLat: 39,
maxLon: 209,
minLat: 35,
minLon: -191,
};
const filter = createExtentFilter(mapExtent, [geoFieldName]);
expect(filter.geo_bounding_box).toEqual({
location: {
top_left: [-180, 39],
bottom_right: [180, 35],
},
});
});
});

describe('roundCoordinates', () => {
it('should set coordinates precision', () => {
const coordinates = [
Expand All @@ -492,134 +402,6 @@ describe('roundCoordinates', () => {
});
});

describe('extractFeaturesFromFilters', () => {
it('should ignore non-spatial filers', () => {
const phraseFilter = {
meta: {
alias: null,
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'machine.os',
negate: false,
params: {
query: 'ios',
},
type: 'phrase',
},
query: {
match_phrase: {
'machine.os': 'ios',
},
},
};
expect(extractFeaturesFromFilters([phraseFilter])).toEqual([]);
});

it('should convert geo_distance filter to feature', () => {
const spatialFilter = {
geo_distance: {
distance: '1096km',
'geo.coordinates': [-89.87125, 53.49454],
},
meta: {
alias: 'geo.coordinates within 1096km of -89.87125,53.49454',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

const features = extractFeaturesFromFilters([spatialFilter]);
expect(features[0].geometry.coordinates[0][0]).toEqual([-89.87125, 63.35109118642093]);
expect(features[0].properties).toEqual({
filter: 'geo.coordinates within 1096km of -89.87125,53.49454',
});
});

it('should convert geo_shape filter to feature', () => {
const spatialFilter = {
geo_shape: {
'geo.coordinates': {
relation: 'INTERSECTS',
shape: {
coordinates: [
[
[-101.21639, 48.1413],
[-101.21639, 41.84905],
[-90.95149, 41.84905],
[-90.95149, 48.1413],
[-101.21639, 48.1413],
],
],
type: 'Polygon',
},
},
ignore_unmapped: true,
},
meta: {
alias: 'geo.coordinates in bounds',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

expect(extractFeaturesFromFilters([spatialFilter])).toEqual([
{
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[-101.21639, 48.1413],
[-101.21639, 41.84905],
[-90.95149, 41.84905],
[-90.95149, 48.1413],
[-101.21639, 48.1413],
],
],
},
properties: {
filter: 'geo.coordinates in bounds',
},
},
]);
});

it('should ignore geo_shape filter with pre-index shape', () => {
const spatialFilter = {
geo_shape: {
'geo.coordinates': {
indexed_shape: {
id: 's5gldXEBkTB2HMwpC8y0',
index: 'world_countries_v1',
path: 'coordinates',
},
relation: 'INTERSECTS',
},
ignore_unmapped: true,
},
meta: {
alias: 'geo.coordinates in multipolygon',
disabled: false,
index: '90943e30-9a47-11e8-b64d-95841ca0b247',
key: 'geo.coordinates',
negate: false,
type: 'spatial_filter',
value: '',
},
};

expect(extractFeaturesFromFilters([spatialFilter])).toEqual([]);
});
});

describe('makeESBbox', () => {
it('Should invert Y-axis', () => {
const bbox = makeESBbox({
Expand Down
Loading