Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Map] return bounding box for static feature collection without joins #66607

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import turf from 'turf';
import React from 'react';
import { AbstractLayer } from '../layer';
import { VectorStyle } from '../../styles/vector/vector_style';
Expand All @@ -30,6 +29,7 @@ import {
canSkipFormattersUpdate,
} from '../../util/can_skip_fetch';
import { assignFeatureIds } from '../../util/assign_feature_ids';
import { getFeatureCollectionBounds } from '../../util/get_feature_collection_bounds';
import {
getFillFilterExpression,
getLineFilterExpression,
Expand Down Expand Up @@ -153,31 +153,10 @@ export class VectorLayer extends AbstractLayer {
return this.getCurrentStyle().renderLegendDetails();
}

_getBoundsBasedOnData() {
const featureCollection = this._getSourceFeatureCollection();
if (!featureCollection) {
return null;
}

const visibleFeatures = featureCollection.features.filter(
feature => feature.properties[FEATURE_VISIBLE_PROPERTY_NAME]
);
const bbox = turf.bbox({
type: 'FeatureCollection',
features: visibleFeatures,
});
return {
minLon: bbox[0],
minLat: bbox[1],
maxLon: bbox[2],
maxLat: bbox[3],
};
}

async getBounds(dataFilters) {
const isStaticLayer = !this.getSource().isBoundsAware();
if (isStaticLayer) {
return this._getBoundsBasedOnData();
return getFeatureCollectionBounds(this._getSourceFeatureCollection(), this._hasJoins());
}

const searchFilters = this._getSearchFilters(
Expand Down
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();
});
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],
};
}