From fcacec06c16cd9bbf2778d6437450073f62e48c9 Mon Sep 17 00:00:00 2001 From: Vijayan Balasubramanian Date: Tue, 28 Mar 2023 01:01:23 +0530 Subject: [PATCH 1/2] add filter Signed-off-by: Vijayan Balasubramanian --- public/model/geo/filter.test.ts | 90 ++++++++++++++++++++++++++------- public/model/geo/filter.ts | 57 ++++++++++++--------- 2 files changed, 104 insertions(+), 43 deletions(-) diff --git a/public/model/geo/filter.test.ts b/public/model/geo/filter.test.ts index 212b58c1..49d21c6d 100644 --- a/public/model/geo/filter.test.ts +++ b/public/model/geo/filter.test.ts @@ -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 () { @@ -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: [ @@ -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); }); }); diff --git a/public/model/geo/filter.ts b/public/model/geo/filter.ts index ee6afdcf..340fbef6 100644 --- a/public/model/geo/filter.ts +++ b/public/model/geo/filter.ts @@ -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, @@ -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, }; }; From 25efc9ca8893a100eebbd18d47f75383886f9b23 Mon Sep 17 00:00:00 2001 From: Vijayan Balasubramanian Date: Tue, 28 Mar 2023 01:12:07 +0530 Subject: [PATCH 2/2] Add changelog Signed-off-by: Vijayan Balasubramanian --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1eaf3f96..42987f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) ### Bug Fixes * Fix property value undefined check ([#276](https://github.com/opensearch-project/dashboards-maps/pull/276))