-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
move map bounds to model and added unit test. Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
- Loading branch information
1 parent
c662b14
commit 7ce84a2
Showing
8 changed files
with
196 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { LngLat } from 'maplibre-gl'; | ||
import { GeoBounds } from '../map/boundary'; | ||
import { FilterMeta, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common'; | ||
import { buildBBoxFilter } from './filter'; | ||
|
||
describe('test bounding box filter', function () { | ||
it('should return valid bounding box', function () { | ||
const mockBounds: GeoBounds = { | ||
bottomRight: new LngLat(-2.340000000000032, 27.67), | ||
topLeft: new LngLat(-135.18, 71.01), | ||
}; | ||
const mockFilterMeta: FilterMeta = { | ||
alias: null, | ||
disabled: true, | ||
negate: false, | ||
}; | ||
const actualGeoBoundingBoxFilter: GeoBoundingBoxFilter = buildBBoxFilter( | ||
'field-name', | ||
mockBounds, | ||
mockFilterMeta | ||
); | ||
const expectedBounds = { | ||
bottom_right: { | ||
lat: mockBounds.bottomRight.lat, | ||
lon: mockBounds.bottomRight.lng, | ||
}, | ||
top_left: { | ||
lat: mockBounds.topLeft.lat, | ||
lon: mockBounds.topLeft.lng, | ||
}, | ||
}; | ||
expect(actualGeoBoundingBoxFilter.geo_bounding_box).toEqual({ | ||
['field-name']: expectedBounds, | ||
}); | ||
expect(actualGeoBoundingBoxFilter.meta.params).toEqual(expectedBounds); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { LatLon } from '@opensearch-project/opensearch/api/types'; | ||
import { FilterMeta, GeoBoundingBoxFilter } from '../../../../../src/plugins/data/common'; | ||
import { GeoBounds } from '../map/boundary'; | ||
|
||
export const buildBBoxFilter = ( | ||
fieldName: string, | ||
mapBounds: GeoBounds, | ||
filterMeta: FilterMeta | ||
): GeoBoundingBoxFilter => { | ||
const bottomRight: LatLon = { | ||
lon: mapBounds.bottomRight.lng, | ||
lat: mapBounds.bottomRight.lat, | ||
}; | ||
|
||
const topLeft: LatLon = { | ||
lon: mapBounds.topLeft.lng, | ||
lat: mapBounds.topLeft.lat, | ||
}; | ||
|
||
const boundingBox = { | ||
bottom_right: bottomRight, | ||
top_left: topLeft, | ||
}; | ||
return { | ||
meta: { | ||
...filterMeta, | ||
params: boundingBox, | ||
}, | ||
geo_bounding_box: { | ||
[fieldName]: boundingBox, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { LngLat, LngLatBounds, Map as Maplibre } from 'maplibre-gl'; | ||
import { MockMaplibreMap } from './__mocks__/map'; | ||
import { GeoBounds, getBounds } from './boundary'; | ||
|
||
describe('verify get bounds', function () { | ||
it('should cover complete map if more than on copy is visible', function () { | ||
const ne: LngLat = new LngLat(333.811, 82.8); | ||
const sw: LngLat = new LngLat(-248.8, -79.75); | ||
const mockMap = new MockMaplibreMap([], new LngLatBounds(sw, ne)); | ||
const expectedBounds: GeoBounds = { | ||
bottomRight: new LngLat(180, -79.75), | ||
topLeft: new LngLat(-180, 82.8), | ||
}; | ||
expect(getBounds((mockMap as unknown) as Maplibre)).toEqual(expectedBounds); | ||
}); | ||
|
||
it('should wrap if map crosses international date line', function () { | ||
const ne: LngLat = new LngLat(11.56, 80.85); | ||
const sw: LngLat = new LngLat(-220.77, 21.52); | ||
const mockMap = new MockMaplibreMap([], new LngLatBounds(sw, ne)); | ||
const expectedBounds: GeoBounds = { | ||
bottomRight: new LngLat(11.559999999999945, 21.52), | ||
topLeft: new LngLat(139.23000000000002, 80.85), | ||
}; | ||
expect(getBounds((mockMap as unknown) as Maplibre)).toEqual(expectedBounds); | ||
}); | ||
it('should give same value as map bounds for other cases', function () { | ||
const sw: LngLat = new LngLat(-135.18, 27.67); | ||
const ne: LngLat = new LngLat(-2.34, 71.01); | ||
const mockMap = new MockMaplibreMap([], new LngLatBounds(sw, ne)); | ||
const expectedBounds: GeoBounds = { | ||
bottomRight: new LngLat(-2.340000000000032, 27.67), | ||
topLeft: new LngLat(-135.18, 71.01), | ||
}; | ||
expect(getBounds((mockMap as unknown) as Maplibre)).toEqual(expectedBounds); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { LngLat, LngLatBounds, Map as Maplibre } from 'maplibre-gl'; | ||
import { MAX_LONGITUDE, MIN_LONGITUDE } from '../../../common'; | ||
|
||
export interface GeoBounds { | ||
bottomRight: LngLat; | ||
topLeft: LngLat; | ||
} | ||
|
||
// calculate lng limits based on map bounds | ||
// maps can render more than 1 copies of map at lower zoom level and displays | ||
// one side from 1 copy and other side from other copy at higher zoom level if | ||
// screen crosses internation dateline | ||
export function getBounds(map: Maplibre): GeoBounds { | ||
const mapBounds: LngLatBounds = map.getBounds(); | ||
const boundsMinLng: number = mapBounds.getNorthWest().lng; | ||
const boundsMaxLng: number = mapBounds.getSouthEast().lng; | ||
|
||
let right: number; | ||
let left: number; | ||
// if bounds expands more than 360 then, consider complete globe is visible | ||
if (boundsMaxLng - boundsMinLng >= MAX_LONGITUDE - MIN_LONGITUDE) { | ||
right = MAX_LONGITUDE; | ||
left = MIN_LONGITUDE; | ||
} else { | ||
// wrap bounds if only portion of globe is visible | ||
// wrap() returns a new LngLat object whose longitude is | ||
// wrapped to the range (-180, 180). | ||
right = mapBounds.getSouthEast().wrap().lng; | ||
left = mapBounds.getNorthWest().wrap().lng; | ||
} | ||
return { | ||
bottomRight: new LngLat(right, mapBounds.getSouthEast().lat), | ||
topLeft: new LngLat(left, mapBounds.getNorthWest().lat), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters