Skip to content

Commit a939b9c

Browse files
[7.9] [Maps] fix fit to data for Point to Point layer (#73563) (#73706)
* merge conflicts * tslint cleanup Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent f0faf7e commit a939b9c

File tree

5 files changed

+83
-23
lines changed

5 files changed

+83
-23
lines changed

x-pack/plugins/maps/public/actions/data_request_actions.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
import { ILayer } from '../classes/layers/layer';
4040
import { DataMeta, MapExtent, MapFilters } from '../../common/descriptor_types';
4141
import { DataRequestAbortError } from '../classes/util/data_request';
42+
// @ts-expect-error
43+
import { turfBboxToBounds } from '../elasticsearch_geo_utils';
4244

4345
export type DataRequestContext = {
4446
startLoading(dataId: string, requestToken: symbol, meta: DataMeta): void;
@@ -351,13 +353,7 @@ export function fitToDataBounds() {
351353
return;
352354
}
353355

354-
const turfUnionBbox = turf.bbox(turf.multiPoint(corners));
355-
const dataBounds = {
356-
minLon: turfUnionBbox[0],
357-
minLat: turfUnionBbox[1],
358-
maxLon: turfUnionBbox[2],
359-
maxLat: turfUnionBbox[3],
360-
};
356+
const dataBounds = turfBboxToBounds(turf.bbox(turf.multiPoint(corners)));
361357

362358
dispatch(setGotoWithBounds(dataBounds));
363359
};

x-pack/plugins/maps/public/classes/sources/es_pew_pew_source/es_pew_pew_source.js

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66

77
import React from 'react';
88
import uuid from 'uuid/v4';
9+
import turf from 'turf';
910

1011
import { UpdateSourceEditor } from './update_source_editor';
1112
import { i18n } from '@kbn/i18n';
1213
import { SOURCE_TYPES, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
1314
import { getDataSourceLabel } from '../../../../common/i18n_getters';
1415
import { convertToLines } from './convert_to_lines';
1516
import { AbstractESAggSource, DEFAULT_METRIC } from '../es_agg_source';
16-
import { indexPatterns } from '../../../../../../../src/plugins/data/public';
1717
import { registerSource } from '../source_registry';
18+
import { turfBboxToBounds } from '../../../elasticsearch_geo_utils';
19+
import { DataRequestAbortError } from '../../util/data_request';
1820

1921
const MAX_GEOTILE_LEVEL = 29;
2022

@@ -158,19 +160,63 @@ export class ESPewPewSource extends AbstractESAggSource {
158160
};
159161
}
160162

161-
async _getGeoField() {
162-
const indexPattern = await this.getIndexPattern();
163-
const field = indexPattern.fields.getByName(this._descriptor.destGeoField);
164-
const geoField = indexPatterns.isNestedField(field) ? undefined : field;
165-
if (!geoField) {
166-
throw new Error(
167-
i18n.translate('xpack.maps.source.esSource.noGeoFieldErrorMessage', {
168-
defaultMessage: `Index pattern {indexPatternTitle} no longer contains the geo field {geoField}`,
169-
values: { indexPatternTitle: indexPattern.title, geoField: this._descriptor.geoField },
170-
})
171-
);
163+
getGeoFieldName() {
164+
return this._descriptor.destGeoField;
165+
}
166+
167+
async getBoundsForFilters(boundsFilters, registerCancelCallback) {
168+
const searchSource = await this.makeSearchSource(boundsFilters, 0);
169+
searchSource.setField('aggs', {
170+
destFitToBounds: {
171+
geo_bounds: {
172+
field: this._descriptor.destGeoField,
173+
},
174+
},
175+
sourceFitToBounds: {
176+
geo_bounds: {
177+
field: this._descriptor.sourceGeoField,
178+
},
179+
},
180+
});
181+
182+
const corners = [];
183+
try {
184+
const abortController = new AbortController();
185+
registerCancelCallback(() => abortController.abort());
186+
const esResp = await searchSource.fetch({ abortSignal: abortController.signal });
187+
if (esResp.aggregations.destFitToBounds.bounds) {
188+
corners.push([
189+
esResp.aggregations.destFitToBounds.bounds.top_left.lon,
190+
esResp.aggregations.destFitToBounds.bounds.top_left.lat,
191+
]);
192+
corners.push([
193+
esResp.aggregations.destFitToBounds.bounds.bottom_right.lon,
194+
esResp.aggregations.destFitToBounds.bounds.bottom_right.lat,
195+
]);
196+
}
197+
if (esResp.aggregations.sourceFitToBounds.bounds) {
198+
corners.push([
199+
esResp.aggregations.sourceFitToBounds.bounds.top_left.lon,
200+
esResp.aggregations.sourceFitToBounds.bounds.top_left.lat,
201+
]);
202+
corners.push([
203+
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lon,
204+
esResp.aggregations.sourceFitToBounds.bounds.bottom_right.lat,
205+
]);
206+
}
207+
} catch (error) {
208+
if (error.name === 'AbortError') {
209+
throw new DataRequestAbortError();
210+
}
211+
212+
return null;
213+
}
214+
215+
if (corners.length === 0) {
216+
return null;
172217
}
173-
return geoField;
218+
219+
return turfBboxToBounds(turf.bbox(turf.multiPoint(corners)));
174220
}
175221

176222
canFormatFeatureProperties() {

x-pack/plugins/maps/public/classes/sources/es_source/es_source.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class AbstractESSource extends AbstractVectorSource {
150150
searchSource.setField('aggs', {
151151
fitToBounds: {
152152
geo_bounds: {
153-
field: this._descriptor.geoField,
153+
field: this.getGeoFieldName(),
154154
},
155155
},
156156
});
@@ -230,12 +230,12 @@ export class AbstractESSource extends AbstractVectorSource {
230230

231231
async _getGeoField() {
232232
const indexPattern = await this.getIndexPattern();
233-
const geoField = indexPattern.fields.getByName(this._descriptor.geoField);
233+
const geoField = indexPattern.fields.getByName(this.getGeoFieldName());
234234
if (!geoField) {
235235
throw new Error(
236236
i18n.translate('xpack.maps.source.esSource.noGeoFieldErrorMessage', {
237237
defaultMessage: `Index pattern {indexPatternTitle} no longer contains the geo field {geoField}`,
238-
values: { indexPatternTitle: indexPattern.title, geoField: this._descriptor.geoField },
238+
values: { indexPatternTitle: indexPattern.title, geoField: this.getGeoFieldName() },
239239
})
240240
);
241241
}

x-pack/plugins/maps/public/elasticsearch_geo_utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,12 @@ export function extractFeaturesFromFilters(filters) {
469469

470470
return features;
471471
}
472+
473+
export function turfBboxToBounds(turfBbox) {
474+
return {
475+
minLon: turfBbox[0],
476+
minLat: turfBbox[1],
477+
maxLon: turfBbox[2],
478+
maxLat: turfBbox[3],
479+
};
480+
}

x-pack/test/functional/apps/maps/es_pew_pew_source.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,14 @@ export default function ({ getPageObjects, getService }) {
3535
expect(features.length).to.equal(2);
3636
expect(features[0].geometry.type).to.equal('LineString');
3737
});
38+
39+
it('should fit to bounds', async () => {
40+
// Set view to other side of world so no matching results
41+
await PageObjects.maps.setView(-70, 0, 6);
42+
await PageObjects.maps.clickFitToBounds('connections');
43+
const { lat, lon } = await PageObjects.maps.getView();
44+
expect(Math.round(lat)).to.equal(41);
45+
expect(Math.round(lon)).to.equal(-70);
46+
});
3847
});
3948
}

0 commit comments

Comments
 (0)