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

[Maps] fix fit to data for Point to Point layer #73563

Merged
merged 3 commits into from
Jul 29, 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
10 changes: 2 additions & 8 deletions x-pack/plugins/maps/public/actions/data_request_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { ILayer } from '../classes/layers/layer';
import { IVectorLayer } from '../classes/layers/vector_layer/vector_layer';
import { DataMeta, MapExtent, MapFilters } from '../../common/descriptor_types';
import { DataRequestAbortError } from '../classes/util/data_request';
import { scaleBounds } from '../elasticsearch_geo_utils';
import { scaleBounds, turfBboxToBounds } from '../elasticsearch_geo_utils';

const FIT_TO_BOUNDS_SCALE_FACTOR = 0.1;

Expand Down Expand Up @@ -368,13 +368,7 @@ export function fitToDataBounds() {
return;
}

const turfUnionBbox = turf.bbox(turf.multiPoint(corners));
const dataBounds = {
minLon: turfUnionBbox[0],
minLat: turfUnionBbox[1],
maxLon: turfUnionBbox[2],
maxLat: turfUnionBbox[3],
};
const dataBounds = turfBboxToBounds(turf.bbox(turf.multiPoint(corners)));

dispatch(setGotoWithBounds(scaleBounds(dataBounds, FIT_TO_BOUNDS_SCALE_FACTOR)));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

import React from 'react';
import uuid from 'uuid/v4';
import turf from 'turf';

import { UpdateSourceEditor } from './update_source_editor';
import { i18n } from '@kbn/i18n';
import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { convertToLines } from './convert_to_lines';
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
import { indexPatterns } from '../../../../../../../src/plugins/data/public';
import { registerSource } from '../source_registry';
import { turfBboxToBounds } from '../../../elasticsearch_geo_utils';
import { DataRequestAbortError } from '../../util/data_request';

const MAX_GEOTILE_LEVEL = 29;

Expand Down Expand Up @@ -158,19 +160,63 @@ export class ESPewPewSource extends AbstractESAggSource {
};
}

async _getGeoField() {
const indexPattern = await this.getIndexPattern();
const field = indexPattern.fields.getByName(this._descriptor.destGeoField);
const geoField = indexPatterns.isNestedField(field) ? undefined : field;
if (!geoField) {
throw new Error(
i18n.translate('xpack.maps.source.esSource.noGeoFieldErrorMessage', {
defaultMessage: `Index pattern {indexPatternTitle} no longer contains the geo field {geoField}`,
values: { indexPatternTitle: indexPattern.title, geoField: this._descriptor.geoField },
})
);
getGeoFieldName() {
return this._descriptor.destGeoField;
}

async getBoundsForFilters(boundsFilters, registerCancelCallback) {
const searchSource = await this.makeSearchSource(boundsFilters, 0);
searchSource.setField('aggs', {
destFitToBounds: {
geo_bounds: {
field: this._descriptor.destGeoField,
},
},
sourceFitToBounds: {
geo_bounds: {
field: this._descriptor.sourceGeoField,
},
},
});

const corners = [];
try {
const abortController = new AbortController();
registerCancelCallback(() => abortController.abort());
const esResp = await searchSource.fetch({ abortSignal: abortController.signal });
if (esResp.aggregations.destFitToBounds.bounds) {
corners.push([
esResp.aggregations.destFitToBounds.bounds.top_left.lon,
esResp.aggregations.destFitToBounds.bounds.top_left.lat,
]);
corners.push([
esResp.aggregations.destFitToBounds.bounds.bottom_right.lon,
esResp.aggregations.destFitToBounds.bounds.bottom_right.lat,
]);
}
if (esResp.aggregations.sourceFitToBounds.bounds) {
corners.push([
esResp.aggregations.sourceFitToBounds.bounds.top_left.lon,
esResp.aggregations.sourceFitToBounds.bounds.top_left.lat,
]);
corners.push([
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lon,
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lat,
]);
}
} catch (error) {
if (error.name === 'AbortError') {
throw new DataRequestAbortError();
}

return null;
}

if (corners.length === 0) {
return null;
}
return geoField;

return turfBboxToBounds(turf.bbox(turf.multiPoint(corners)));
}

canFormatFeatureProperties() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class AbstractESSource extends AbstractVectorSource {
searchSource.setField('aggs', {
fitToBounds: {
geo_bounds: {
field: this._descriptor.geoField,
field: this.getGeoFieldName(),
},
},
});
Expand Down Expand Up @@ -230,12 +230,12 @@ export class AbstractESSource extends AbstractVectorSource {

async _getGeoField() {
const indexPattern = await this.getIndexPattern();
const geoField = indexPattern.fields.getByName(this._descriptor.geoField);
const geoField = indexPattern.fields.getByName(this.getGeoFieldName());
if (!geoField) {
throw new Error(
i18n.translate('xpack.maps.source.esSource.noGeoFieldErrorMessage', {
defaultMessage: `Index pattern {indexPatternTitle} no longer contains the geo field {geoField}`,
values: { indexPatternTitle: indexPattern.title, geoField: this._descriptor.geoField },
values: { indexPatternTitle: indexPattern.title, geoField: this.getGeoFieldName() },
})
);
}
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/maps/public/elasticsearch_geo_utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
import { MapExtent } from '../common/descriptor_types';

export function scaleBounds(bounds: MapExtent, scaleFactor: number): MapExtent;

export function turfBboxToBounds(turfBbox: unknown): MapExtent;
9 changes: 9 additions & 0 deletions x-pack/plugins/maps/public/elasticsearch_geo_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,12 @@ export function scaleBounds(bounds, scaleFactor) {
maxLat: bounds.maxLat + height * scaleFactor,
};
}

export function turfBboxToBounds(turfBbox) {
return {
minLon: turfBbox[0],
minLat: turfBbox[1],
maxLon: turfBbox[2],
maxLat: turfBbox[3],
};
}
9 changes: 9 additions & 0 deletions x-pack/test/functional/apps/maps/es_pew_pew_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ export default function ({ getPageObjects, getService }) {
expect(features.length).to.equal(2);
expect(features[0].geometry.type).to.equal('LineString');
});

it('should fit to bounds', async () => {
// Set view to other side of world so no matching results
await PageObjects.maps.setView(-70, 0, 6);
await PageObjects.maps.clickFitToBounds('connections');
const { lat, lon } = await PageObjects.maps.getView();
expect(Math.round(lat)).to.equal(41);
expect(Math.round(lon)).to.equal(-70);
});
});
}