[Maps] Support geometry-collection (#87867)
This commit is contained in:
parent
33603e749d
commit
d321950b93
|
@ -172,8 +172,15 @@ export function convertESShapeToGeojsonGeometry(value) {
|
|||
geoJson.type = GEO_JSON_TYPE.MULTI_POLYGON;
|
||||
break;
|
||||
case 'geometrycollection':
|
||||
geoJson.type = GEO_JSON_TYPE.GEOMETRY_COLLECTION;
|
||||
break;
|
||||
case GEO_JSON_TYPE.GEOMETRY_COLLECTION:
|
||||
// PEBKAC - geometry-collections need to be unrolled to their individual geometries first.
|
||||
const invalidGeometrycollectionError = i18n.translate(
|
||||
'xpack.maps.es_geo_utils.convert.invalidGeometryCollectionErrorMessage',
|
||||
{
|
||||
defaultMessage: `Should not pass GeometryCollection to convertESShapeToGeojsonGeometry`,
|
||||
}
|
||||
);
|
||||
throw new Error(invalidGeometrycollectionError);
|
||||
case 'envelope':
|
||||
// format defined here https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#_envelope
|
||||
const polygon = formatEnvelopeAsPolygon({
|
||||
|
@ -227,14 +234,21 @@ export function geoShapeToGeometry(value, accumulator) {
|
|||
return;
|
||||
}
|
||||
|
||||
let geoJson;
|
||||
if (typeof value === 'string') {
|
||||
geoJson = convertWKTStringToGeojson(value);
|
||||
const geoJson = convertWKTStringToGeojson(value);
|
||||
accumulator.push(geoJson);
|
||||
} else if (
|
||||
// Needs to deal with possible inconsistencies in capitalization
|
||||
value.type === GEO_JSON_TYPE.GEOMETRY_COLLECTION ||
|
||||
value.type === 'geometrycollection'
|
||||
) {
|
||||
for (let i = 0; i < value.geometries.length; i++) {
|
||||
geoShapeToGeometry(value.geometries[i], accumulator);
|
||||
}
|
||||
} else {
|
||||
geoJson = convertESShapeToGeojsonGeometry(value);
|
||||
const geoJson = convertESShapeToGeojsonGeometry(value);
|
||||
accumulator.push(geoJson);
|
||||
}
|
||||
|
||||
accumulator.push(geoJson);
|
||||
}
|
||||
|
||||
export function makeESBbox({ maxLat, maxLon, minLat, minLon }) {
|
||||
|
|
|
@ -147,6 +147,71 @@ describe('hitsToGeoJson', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('Should create feature per item when geometry value is a geometry-collection', () => {
|
||||
const hits = [
|
||||
{
|
||||
_id: 'doc1',
|
||||
_index: 'index1',
|
||||
_source: {
|
||||
[geoFieldName]: {
|
||||
type: 'GeometryCollection',
|
||||
geometries: [
|
||||
{
|
||||
type: 'geometrycollection', //explicitly test coercion to proper GeoJson type value
|
||||
geometries: [
|
||||
{
|
||||
type: 'point', //explicitly test coercion to proper GeoJson type value
|
||||
coordinates: [0, 0],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'LineString',
|
||||
coordinates: [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
myField: 8,
|
||||
},
|
||||
},
|
||||
];
|
||||
const geojson = hitsToGeoJson(hits, flattenHitMock, geoFieldName, 'geo_shape', []);
|
||||
expect(geojson.type).toBe('FeatureCollection');
|
||||
expect(geojson.features.length).toBe(2);
|
||||
expect(geojson.features[0]).toEqual({
|
||||
geometry: {
|
||||
coordinates: [0, 0],
|
||||
type: 'Point',
|
||||
},
|
||||
id: 'index1:doc1:0',
|
||||
properties: {
|
||||
_id: 'doc1',
|
||||
_index: 'index1',
|
||||
myField: 8,
|
||||
},
|
||||
type: 'Feature',
|
||||
});
|
||||
expect(geojson.features[1]).toEqual({
|
||||
geometry: {
|
||||
coordinates: [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
],
|
||||
type: 'LineString',
|
||||
},
|
||||
id: 'index1:doc1:1',
|
||||
properties: {
|
||||
_id: 'doc1',
|
||||
_index: 'index1',
|
||||
myField: 8,
|
||||
},
|
||||
type: 'Feature',
|
||||
});
|
||||
});
|
||||
|
||||
it('Should convert epoch_millis value from string to integer', () => {
|
||||
const hits = [
|
||||
{
|
||||
|
|
|
@ -65,6 +65,8 @@ export function getCentroidFeatures(featureCollection: FeatureCollection): Featu
|
|||
}
|
||||
}
|
||||
centroidGeometry = turfCenterOfMass(polygon(largestPolygon)).geometry;
|
||||
} else if (feature.geometry.type === GEO_JSON_TYPE.GEOMETRY_COLLECTION) {
|
||||
throw new Error('Should not have features with geometrycollection');
|
||||
}
|
||||
|
||||
if (centroidGeometry) {
|
||||
|
|
Loading…
Reference in a new issue