Skip to content

Commit

Permalink
[Maps] Support GeometryCollections in GeoJson upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Mar 3, 2021
1 parent c42b994 commit cea9b1e
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ const FEATURE_COLLECTION = {
],
};

const GEOMETRY_COLLECTION_FEATURE = {
type: 'Feature',
properties: {
population: 200,
},
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
coordinates: [100.0, 0.0],
},
{
type: 'LineString',
coordinates: [
[101.0, 0.0],
[102.0, 1.0],
],
},
],
},
};

describe('previewFile', () => {
const FILE_WITH_FEATURE_COLLECTION = new File(
[JSON.stringify(FEATURE_COLLECTION)],
Expand Down Expand Up @@ -60,6 +83,27 @@ describe('previewFile', () => {
});
});

test('should read GeometryCollection feature', async () => {
const fileWithGeometryCollectionFeature = new File(
[
JSON.stringify({
type: 'FeatureCollection',
features: [GEOMETRY_COLLECTION_FEATURE],
}),
],
'testfile.json',
{ type: 'text/json' }
);
const importer = new GeoJsonImporter(fileWithGeometryCollectionFeature);
const results = await importer.previewFile();
expect(results).toEqual({
previewCoverage: 100,
hasPoints: false,
hasShapes: true,
features: [GEOMETRY_COLLECTION_FEATURE],
});
});

test('should remove features without geometry', async () => {
const fileWithFeaturesWithoutGeometry = new File(
[
Expand Down Expand Up @@ -193,11 +237,36 @@ describe('toEsDocs', () => {
expect(esDocs).toEqual([
{
coordinates: {
type: 'point',
type: 'Point',
coordinates: [-112.0372, 46.608058],
},
population: 200,
},
]);
});

test('should convert GeometryCollection feature to geo_shape ES documents', () => {
const esDocs = toEsDocs([GEOMETRY_COLLECTION_FEATURE], ES_FIELD_TYPES.GEO_SHAPE);
expect(esDocs).toEqual([
{
coordinates: {
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
coordinates: [100.0, 0.0],
},
{
type: 'LineString',
coordinates: [
[101.0, 0.0],
[102.0, 1.0],
],
},
],
},
population: 200,
},
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@
* 2.0.
*/

import {
Feature,
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
} from 'geojson';
import { Feature, Point } from 'geojson';
import { i18n } from '@kbn/i18n';
// @ts-expect-error
import { JSONLoader, loadInBatches } from './loaders';
Expand Down Expand Up @@ -71,7 +63,8 @@ export class GeoJsonImporter extends Importer {
this._geometryTypesMap.has('LineString') ||
this._geometryTypesMap.has('MultiLineString') ||
this._geometryTypesMap.has('Polygon') ||
this._geometryTypesMap.has('MultiPolygon'),
this._geometryTypesMap.has('MultiPolygon') ||
this._geometryTypesMap.has('GeometryCollection'),
};
}

Expand Down Expand Up @@ -266,23 +259,12 @@ export function toEsDocs(
const esDocs = [];
for (let i = 0; i < features.length; i++) {
const feature = features[i];
const geometry = feature.geometry as
| Point
| MultiPoint
| LineString
| MultiLineString
| Polygon
| MultiPolygon;
const coordinates =
geoFieldType === ES_FIELD_TYPES.GEO_SHAPE
? {
type: geometry.type.toLowerCase(),
coordinates: geometry.coordinates,
}
: geometry.coordinates;
const properties = feature.properties ? feature.properties : {};
esDocs.push({
coordinates,
coordinates:
geoFieldType === ES_FIELD_TYPES.GEO_SHAPE
? feature.geometry
: (feature.geometry as Point).coordinates,
...properties,
});
}
Expand Down
127 changes: 127 additions & 0 deletions x-pack/plugins/maps/common/get_centroid_features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,130 @@ test('should create centroid feature for multi polygon', () => {
},
});
});

test('should create centroid feature for GeometryCollection with Point', () => {
const featureCollection: FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
coordinates: [100.0, 0.0],
},
],
},
properties: {
prop0: 'value0',
prop1: 0.0,
},
},
],
};
const centroidFeatures = getCentroidFeatures(featureCollection);
expect(centroidFeatures.length).toBe(1);
expect(centroidFeatures[0]).toEqual({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [100.0, 0.0],
},
properties: {
__kbn_is_centroid_feature__: true,
prop0: 'value0',
prop1: 0.0,
},
});
});

test('should create centroid feature for GeometryCollection with MultiPoint', () => {
const featureCollection: FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'MultiPoint',
coordinates: [
[10, 40],
[40, 30],
[20, 20],
[30, 10],
],
},
],
},
properties: {
prop0: 'value0',
prop1: 0.0,
},
},
],
};
const centroidFeatures = getCentroidFeatures(featureCollection);
expect(centroidFeatures.length).toBe(1);
expect(centroidFeatures[0]).toEqual({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [10, 40],
},
properties: {
__kbn_is_centroid_feature__: true,
prop0: 'value0',
prop1: 0.0,
},
});
});

test('should create centroid feature for GeometryCollection with Polygon', () => {
const featureCollection: FeatureCollection = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'GeometryCollection',
geometries: [
{
type: 'Polygon',
coordinates: [
[
[35, 10],
[45, 45],
[15, 40],
[10, 20],
[35, 10],
],
],
},
],
},
properties: {
prop0: 'value0',
prop1: 0.0,
},
},
],
};
const centroidFeatures = getCentroidFeatures(featureCollection);
expect(centroidFeatures.length).toBe(1);
expect(centroidFeatures[0]).toEqual({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [27.526881720430108, 28.70967741935484],
},
properties: {
__kbn_is_centroid_feature__: true,
prop0: 'value0',
prop1: 0.0,
},
});
});
Loading

0 comments on commit cea9b1e

Please sign in to comment.