-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
23 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
x-pack/plugins/maps/public/layers/util/get_feature_collection_bounds.test.tsx
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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { getFeatureCollectionBounds } from './get_feature_collection_bounds'; | ||
import { FeatureCollection, Feature, Point } from 'geojson'; | ||
import { FEATURE_VISIBLE_PROPERTY_NAME } from '../../../common/constants'; | ||
|
||
const visibleFeature: Feature = { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [0, 0], | ||
} as Point, | ||
properties: { | ||
[FEATURE_VISIBLE_PROPERTY_NAME]: true, | ||
}, | ||
}; | ||
|
||
const nonVisibleFeature: Feature = { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [10, 0], | ||
} as Point, | ||
properties: { | ||
[FEATURE_VISIBLE_PROPERTY_NAME]: false, | ||
}, | ||
}; | ||
|
||
const featureWithoutVisibilityProp: Feature = { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'Point', | ||
coordinates: [-10, 0], | ||
} as Point, | ||
properties: {}, | ||
}; | ||
|
||
const featureCollection: FeatureCollection = { | ||
type: 'FeatureCollection', | ||
features: [visibleFeature, nonVisibleFeature, featureWithoutVisibilityProp], | ||
}; | ||
|
||
test('should return bounding box for visible features with join', () => { | ||
expect(getFeatureCollectionBounds(featureCollection, true)).toEqual({ | ||
maxLat: 0, | ||
maxLon: 0, | ||
minLat: 0, | ||
minLon: 0, | ||
}); | ||
}); | ||
|
||
test('should return bounding box for all features without join', () => { | ||
expect(getFeatureCollectionBounds(featureCollection, false)).toEqual({ | ||
maxLat: 0, | ||
maxLon: 10, | ||
minLat: 0, | ||
minLon: -10, | ||
}); | ||
}); | ||
|
||
test('should return null when there are no features', () => { | ||
const featureCollectionWithNoVisibileFeatures: FeatureCollection = { | ||
type: 'FeatureCollection', | ||
features: [nonVisibleFeature, featureWithoutVisibilityProp], | ||
}; | ||
expect(getFeatureCollectionBounds(featureCollectionWithNoVisibileFeatures, true)).toBeNull(); | ||
}); |
41 changes: 41 additions & 0 deletions
41
x-pack/plugins/maps/public/layers/util/get_feature_collection_bounds.ts
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,41 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
// @ts-ignore | ||
import turf from 'turf'; | ||
import { FeatureCollection } from 'geojson'; | ||
import { MapExtent } from '../../../common/descriptor_types'; | ||
import { FEATURE_VISIBLE_PROPERTY_NAME } from '../../../common/constants'; | ||
|
||
export function getFeatureCollectionBounds( | ||
featureCollection: FeatureCollection | null, | ||
hasJoins: boolean | ||
): MapExtent | null { | ||
if (!featureCollection) { | ||
return null; | ||
} | ||
|
||
const visibleFeatures = hasJoins | ||
? featureCollection.features.filter(feature => { | ||
return feature.properties && feature.properties[FEATURE_VISIBLE_PROPERTY_NAME]; | ||
}) | ||
: featureCollection.features; | ||
|
||
if (visibleFeatures.length === 0) { | ||
return null; | ||
} | ||
|
||
const bbox = turf.bbox({ | ||
type: 'FeatureCollection', | ||
features: visibleFeatures, | ||
}); | ||
return { | ||
minLon: bbox[0], | ||
minLat: bbox[1], | ||
maxLon: bbox[2], | ||
maxLat: bbox[3], | ||
}; | ||
} |
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