Skip to content

Commit

Permalink
Add support to build GeoShapeFilterMeta and GeoShapeFilter (#360)
Browse files Browse the repository at this point in the history
Add support to build GeoShapeFilterMeta and GeoShapeFilter

Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB authored Mar 29, 2023
1 parent 2259b6f commit b7c1f9a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Add mapbox-gl draw mode ([#347](https://github.com/opensearch-project/dashboards-maps/pull/347))
* Add support to draw rectangle shape to filter documents ([#348](https://github.com/opensearch-project/dashboards-maps/pull/348))
* Avoid trigger tooltip from label ([#350](https://github.com/opensearch-project/dashboards-maps/pull/350))
* Add support to build GeoShapeFilterMeta and GeoShapeFilter ([#360](https://github.com/opensearch-project/dashboards-maps/pull/360))
* Remove cancel button on draw shape and use Escape to cancel draw ([#359](https://github.com/opensearch-project/dashboards-maps/pull/359))

### Bug Fixes
Expand Down
90 changes: 72 additions & 18 deletions public/model/geo/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@

import { LngLat } from 'maplibre-gl';
import { GeoBounds } from '../map/boundary';
import { FilterMeta, FILTERS, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common';
import { buildBBoxFilter, buildSpatialGeometryFilter, GeoShapeFilter } from './filter';
import {
FilterMeta,
FILTERS,
GeoBoundingBoxFilter,
GeoShapeFilterMeta,
GeoShapeFilter,
ShapeFilter,
FilterStateStore,
} from '../../../../../src/plugins/data/common';
import { buildBBoxFilter, buildGeoShapeFilter, buildGeoShapeFilterMeta } from './filter';
import { Polygon } from 'geojson';
import { GeoShapeRelation } from '@opensearch-project/opensearch/api/types';

describe('test bounding box filter', function () {
it('should return valid bounding box', function () {
Expand Down Expand Up @@ -57,25 +66,31 @@ describe('test geo shape filter', function () {
};
const mockLabel: string = 'mypolygon';
const fieldName: string = 'location';
const expectedParams: {
shape: ShapeFilter;
relation: GeoShapeRelation;
} = {
shape: mockPolygon,
relation: 'intersects',
};
const mockFilterMeta: GeoShapeFilterMeta = {
alias: mockLabel,
disabled: false,
negate: false,
type: FILTERS.GEO_SHAPE,
params: expectedParams,
};
const geoShapeFilter: GeoShapeFilter = buildGeoShapeFilter(fieldName, mockFilterMeta);

const geoShapeFilter: GeoShapeFilter = buildSpatialGeometryFilter(
fieldName,
mockPolygon,
mockLabel,
'INTERSECTS'
);
const expectedFilter: GeoShapeFilter = {
meta: {
alias: mockLabel,
disabled: false,
negate: false,
key: 'location',
type: FILTERS.SPATIAL_FILTER,
},
const expectedFilterMeta: GeoShapeFilterMeta = {
...mockFilterMeta,
key: fieldName,
};
const expectedFilter = {
geo_shape: {
ignore_unmapped: true,
location: {
relation: 'INTERSECTS',
relation: 'intersects',
shape: {
type: 'Polygon',
coordinates: [
Expand All @@ -90,6 +105,45 @@ describe('test geo shape filter', function () {
},
},
};
expect(geoShapeFilter).toEqual(expectedFilter);
expect(geoShapeFilter.geo_shape).toEqual(expectedFilter.geo_shape);
expect(geoShapeFilter.meta).toEqual(expectedFilterMeta);
expect(geoShapeFilter.$state?.store).toEqual(FilterStateStore.APP_STATE);
});
});

describe('build GeoShapeFilterMeta', function () {
it('should return valid filter meta', function () {

const mockPolygon: Polygon = {
type: 'Polygon',
coordinates: [
[
[74.006, 40.7128],
[71.0589, 42.3601],
[73.7562, 42.6526],
[74.006, 40.7128],
],
],
};
const actualFilter: GeoShapeFilterMeta = buildGeoShapeFilterMeta(
'label',
mockPolygon,
'intersects'
);
const expectedParams: {
shape: ShapeFilter;
relation: GeoShapeRelation;
} = {
shape: mockPolygon,
relation: 'intersects',
};
const expectedFilter: GeoShapeFilterMeta = {
disabled: false,
negate: false,
alias: 'label',
type: FILTERS.GEO_SHAPE,
params: expectedParams,
};
expect(actualFilter).toEqual(expectedFilter);
});
});
57 changes: 32 additions & 25 deletions public/model/geo/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { LatLon } from '@opensearch-project/opensearch/api/types';
import { Polygon } from 'geojson';
import { GeoShapeRelation, LatLon } from '@opensearch-project/opensearch/api/types';
import {
Filter,
FilterMeta,
FILTERS,
FilterState,
FilterStateStore,
GeoBoundingBoxFilter,
GeoShapeFilter,
GeoShapeFilterMeta,
ShapeFilter,
} from '../../../../../src/plugins/data/common';
import { GeoBounds } from '../map/boundary';

export type FilterRelations = 'INTERSECTS' | 'DISJOINT' | 'WITHIN';

export type GeoShapeFilter = Filter & {
meta: FilterMeta;
geo_shape: any;
};

export const buildBBoxFilter = (
fieldName: string,
mapBounds: GeoBounds,
Expand Down Expand Up @@ -50,28 +46,39 @@ export const buildBBoxFilter = (
};
};

export const buildSpatialGeometryFilter = (
fieldName: string,
filterShape: Polygon,
filterLabel: string,
relation: FilterRelations
): GeoShapeFilter => {
const meta: FilterMeta = {
negate: false,
key: fieldName,
export const buildGeoShapeFilterMeta = (
filterLabel: string | null,
filterShape: ShapeFilter,
relation: GeoShapeRelation
): GeoShapeFilterMeta => {
return {
type: FILTERS.GEO_SHAPE,
alias: filterLabel,
type: FILTERS.SPATIAL_FILTER,
disabled: false,
params: {
relation,
shape: filterShape,
},
negate: false,
};
};

export const buildGeoShapeFilter = (
fieldName: string,
filterMeta: GeoShapeFilterMeta
): GeoShapeFilter => {
const $state: FilterState = {
store: FilterStateStore.APP_STATE,
};
return {
meta,
meta: {
...filterMeta,
key: fieldName,
},
geo_shape: {
ignore_unmapped: true,
[fieldName]: {
relation,
shape: filterShape,
},
[fieldName]: filterMeta.params,
},
$state,
};
};

0 comments on commit b7c1f9a

Please sign in to comment.