Skip to content

Commit

Permalink
Add geo shape query filter
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Mar 6, 2023
1 parent e3eef6b commit b6a8739
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
57 changes: 55 additions & 2 deletions public/model/geo/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import { LngLat } from 'maplibre-gl';
import { GeoBounds } from '../map/boundary';
import { FilterMeta, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common';
import { buildBBoxFilter } from './filter';
import { FilterMeta, FILTERS, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common';
import { buildBBoxFilter, buildSpatialGeometryFilter, GeoShapeFilter } from './filter';
import { Polygon } from 'geojson';

describe('test bounding box filter', function () {
it('should return valid bounding box', function () {
Expand Down Expand Up @@ -40,3 +41,55 @@ describe('test bounding box filter', function () {
expect(actualGeoBoundingBoxFilter.meta.params).toEqual(expectedBounds);
});
});

describe('test geo shape filter', function () {
it('should return valid geo shape query', function () {
const mockPolygon: Polygon = {
type: 'Polygon',
coordinates: [
[
[74.006, 40.7128],
[71.0589, 42.3601],
[73.7562, 42.6526],
[74.006, 40.7128],
],
],
};
const mockLabel: string = 'mypolygon';
const fieldName: string = 'location';

const geoShapeFilter: GeoShapeFilter = buildSpatialGeometryFilter(
fieldName,
mockPolygon,
mockLabel,
'INTERSECTS'
);
const expectedFilter: GeoShapeFilter = {
meta: {
alias: mockLabel,
disabled: false,
negate: false,
key: 'location',
type: FILTERS.SPATIAL_FILTER,
},
geo_shape: {
ignore_unmaped: true,
location: {
relation: 'INTERSECTS',
shape: {
type: 'Polygon',
coordinates: [
[
[74.006, 40.7128],
[71.0589, 42.3601],
[73.7562, 42.6526],
[74.006, 40.7128],
],
],
},
},
},
};
expect(geoShapeFilter).toEqual(expectedFilter);
});
});
41 changes: 40 additions & 1 deletion public/model/geo/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,22 @@
*/

import { LatLon } from '@opensearch-project/opensearch/api/types';
import { FilterMeta, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common';
import { Polygon } from 'geojson';
import {
Filter,
FilterMeta,
FILTERS,
GeoBoundingBoxFilter,
} 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 @@ -36,3 +49,29 @@ export const buildBBoxFilter = (
},
};
};

export const buildSpatialGeometryFilter = (
fieldName: string,
filterShape: Polygon,
filterLabel: string,
relation: FilterRelations
): GeoShapeFilter => {
const meta: FilterMeta = {
negate: false,
key: fieldName,
alias: filterLabel,
type: FILTERS.SPATIAL_FILTER,
disabled: false,
};

return {
meta,
geo_shape: {
ignore_unmaped: true,
[fieldName]: {
relation,
shape: filterShape,
},
},
};
};
2 changes: 2 additions & 0 deletions public/model/layerRenderController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isCompleteResponse,
TimeRange,
Query,
FILTERS,
} from '../../../../src/plugins/data/common';
import { layersFunctionMap } from './layersFunctions';
import { MapServices } from '../types';
Expand Down Expand Up @@ -116,6 +117,7 @@ export const handleDataLayerRender = (
alias: null,
disabled: !mapLayer.source.useGeoBoundingBoxFilter || geoFieldType !== 'geo_point',
negate: false,
type: FILTERS.GEO_BOUNDING_BOX,
};
const geoBoundingBoxFilter: GeoBoundingBoxFilter = buildBBoxFilter(geoField, mapBounds, meta);
filters.push(geoBoundingBoxFilter);
Expand Down

0 comments on commit b6a8739

Please sign in to comment.