[Maps] Support geometry-collection (#87867)

This commit is contained in:
Thomas Neirynck 2021-01-12 13:49:59 -05:00 committed by GitHub
parent 33603e749d
commit d321950b93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 7 deletions

View file

@ -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 }) {

View file

@ -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 = [
{

View file

@ -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) {