mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
This commit is contained in:
parent
91fa01a173
commit
17d4b72ec1
2 changed files with 70 additions and 7 deletions
|
@ -19,17 +19,17 @@ import _ from 'lodash';
|
|||
export function hitsToGeoJson(hits, flattenHit, geoFieldName, geoFieldType) {
|
||||
const features = [];
|
||||
hits.forEach(hit => {
|
||||
const value = _.get(hit, `_source[${geoFieldName}]`);
|
||||
const properties = flattenHit(hit);
|
||||
|
||||
let geometries;
|
||||
if (geoFieldType === 'geo_point') {
|
||||
geometries = geoPointToGeometry(value);
|
||||
geometries = geoPointToGeometry(properties[geoFieldName]);
|
||||
} else if (geoFieldType === 'geo_shape') {
|
||||
geometries = geoShapeToGeometry(value);
|
||||
geometries = geoShapeToGeometry(properties[geoFieldName]);
|
||||
} else {
|
||||
throw new Error(`Unsupported field type, expected: geo_shape or geo_point, you provided: ${geoFieldType}`);
|
||||
}
|
||||
|
||||
const properties = flattenHit(hit);
|
||||
// don't include geometry field value in properties
|
||||
delete properties[geoFieldName];
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ import {
|
|||
convertMapExtentToPolygon,
|
||||
} from './elasticsearch_geo_utils';
|
||||
|
||||
import { IndexPatternsFlattenHitProvider } from 'ui/index_patterns/_flatten_hit';
|
||||
|
||||
const geoFieldName = 'location';
|
||||
const mapExtent = {
|
||||
maxLat: 39,
|
||||
|
@ -24,9 +26,7 @@ const flattenHitMock = hit => {
|
|||
const properties = {};
|
||||
for (const fieldName in hit._source) {
|
||||
if (hit._source.hasOwnProperty(fieldName)) {
|
||||
if (fieldName !== geoFieldName) {
|
||||
properties[fieldName] = hit._source[fieldName];
|
||||
}
|
||||
properties[fieldName] = hit._source[fieldName];
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
|
@ -129,6 +129,69 @@ describe('hitsToGeoJson', () => {
|
|||
type: 'Feature',
|
||||
});
|
||||
});
|
||||
|
||||
describe('dot in geoFieldName', () => {
|
||||
const configMock = {
|
||||
get: (key) => {
|
||||
if (key === 'metaFields') {
|
||||
return [];
|
||||
}
|
||||
throw new Error(`Unexpected config key: ${key}`);
|
||||
},
|
||||
watch: () => {}
|
||||
};
|
||||
const flattenHitWrapper = IndexPatternsFlattenHitProvider(configMock); // eslint-disable-line new-cap
|
||||
const indexPatternMock = {
|
||||
fields: {
|
||||
byName: {
|
||||
['my.location']: {
|
||||
type: 'geo_point'
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const indexPatternFlattenHit = flattenHitWrapper(indexPatternMock);
|
||||
|
||||
it('Should handle geoField being an object', () => {
|
||||
const hits = [
|
||||
{
|
||||
_source: {
|
||||
my: {
|
||||
location: { lat: 20, lon: 100 },
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
|
||||
expect(geojson.features[0]).toEqual({
|
||||
geometry: {
|
||||
coordinates: [100, 20],
|
||||
type: 'Point',
|
||||
},
|
||||
properties: {},
|
||||
type: 'Feature',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should handle geoField containing dot in the name', () => {
|
||||
const hits = [
|
||||
{
|
||||
_source: {
|
||||
['my.location']: { lat: 20, lon: 100 },
|
||||
}
|
||||
}
|
||||
];
|
||||
const geojson = hitsToGeoJson(hits, indexPatternFlattenHit, 'my.location', 'geo_point');
|
||||
expect(geojson.features[0]).toEqual({
|
||||
geometry: {
|
||||
coordinates: [100, 20],
|
||||
type: 'Point',
|
||||
},
|
||||
properties: {},
|
||||
type: 'Feature',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('geoPointToGeometry', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue