Skip to content

Commit eedb74e

Browse files
committed
default to selecting geo_shape when file contains both points and shapes
1 parent e80285a commit eedb74e

6 files changed

Lines changed: 43 additions & 30 deletions

File tree

x-pack/plugins/file_upload/public/components/geojson_file_picker.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@
66
*/
77

88
import React, { Component } from 'react';
9-
import { Feature } from 'geojson';
109
import { EuiFilePicker, EuiFormRow } from '@elastic/eui';
1110
import { i18n } from '@kbn/i18n';
1211
import { MB } from '../../common';
1312
import { getMaxBytesFormatted } from '../get_max_bytes';
1413
import { validateFile } from '../importer';
15-
import { GeoJsonImporter, GEOJSON_FILE_TYPES } from '../importer/geojson_importer';
14+
import { GeoJsonImporter, GeoJsonPreview, GEOJSON_FILE_TYPES } from '../importer/geojson_importer';
1615

1716
interface Props {
1817
onSelect: ({
1918
features,
20-
geoFieldTypes,
19+
hasPoints,
20+
hasShapes,
2121
importer,
2222
indexName,
2323
previewCoverage,
24-
}: {
25-
features: Feature[];
24+
}: GeoJsonPreview & {
2625
indexName: string;
2726
importer: GeoJsonImporter;
28-
geoFieldTypes: string[];
29-
previewCoverage: number;
3027
}) => void;
3128
onClear: () => void;
3229
}
@@ -73,11 +70,7 @@ export class GeoJsonFilePicker extends Component<Props, State> {
7370

7471
let importer: GeoJsonImporter | null = null;
7572
let previewError: string | null = null;
76-
let preview: {
77-
features: Feature[];
78-
geoFieldTypes: string[];
79-
previewCoverage: number;
80-
} | null = null;
73+
let preview: GeoJsonPreview | null = null;
8174
try {
8275
validateFile(file, GEOJSON_FILE_TYPES);
8376
importer = new GeoJsonImporter(file);

x-pack/plugins/file_upload/public/components/index_settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class IndexSettings extends Component {
118118
text: indexType,
119119
value: indexType,
120120
}))}
121+
value={this.props.selectedIndexType}
121122
onChange={({ target }) => setSelectedIndexType(target.value)}
122123
/>
123124
</EuiFormRow>

x-pack/plugins/file_upload/public/components/json_upload_and_parse.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { IndexSettings } from './index_settings';
1313
import { getIndexPatternService } from '../kibana_services';
1414
import { GeoJsonFilePicker } from './geojson_file_picker';
1515
import { ImportCompleteView } from './import_complete_view';
16+
import { ES_FIELD_TYPES } from '../../../../../src/plugins/data/public';
1617

1718
const PHASE = {
1819
CONFIGURE: 'CONFIGURE',
@@ -201,16 +202,21 @@ export class JsonUploadAndParse extends Component {
201202
});
202203
};
203204

204-
_onFileSelect = ({ features, geoFieldTypes, importer, indexName, previewCoverage }) => {
205+
_onFileSelect = ({ features, hasPoints, hasShapes, importer, indexName, previewCoverage }) => {
205206
this._geojsonImporter = importer;
206207

208+
const geoFieldTypes = hasPoints
209+
? [ES_FIELD_TYPES.GEO_POINT, ES_FIELD_TYPES.GEO_SHAPE]
210+
: [ES_FIELD_TYPES.GEO_SHAPE];
211+
207212
const newState = {
208213
indexTypes: geoFieldTypes,
209214
indexName,
210215
};
211-
if (!this.state.selectedIndexType && geoFieldTypes.length) {
216+
if (!this.state.selectedIndexType) {
212217
// auto select index type
213-
newState.selectedIndexType = geoFieldTypes[0];
218+
newState.selectedIndexType =
219+
hasPoints && !hasShapes ? ES_FIELD_TYPES.GEO_POINT : ES_FIELD_TYPES.GEO_SHAPE;
214220
} else if (
215221
this.state.selectedIndexType &&
216222
!geoFieldTypes.includes(this.state.selectedIndexType)
@@ -274,6 +280,7 @@ export class JsonUploadAndParse extends Component {
274280
indexName={this.state.indexName}
275281
setIndexName={(indexName) => this.setState({ indexName })}
276282
indexTypes={this.state.indexTypes}
283+
selectedIndexType={this.state.selectedIndexType}
277284
setSelectedIndexType={(selectedIndexType) => this.setState({ selectedIndexType })}
278285
setHasIndexErrors={(hasIndexErrors) => this.setState({ hasIndexErrors })}
279286
/>

x-pack/plugins/file_upload/public/importer/geojson_importer/geojson_importer.test.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ describe('previewFile', () => {
4444
expect(results).toEqual({
4545
features: [],
4646
previewCoverage: 0,
47-
geoFieldTypes: ['geo_shape'],
47+
hasPoints: false,
48+
hasShapes: false,
4849
});
4950
});
5051

@@ -53,7 +54,8 @@ describe('previewFile', () => {
5354
const results = await importer.previewFile();
5455
expect(results).toEqual({
5556
previewCoverage: 100,
56-
geoFieldTypes: ['geo_point', 'geo_shape'],
57+
hasPoints: true,
58+
hasShapes: false,
5759
features: FEATURE_COLLECTION.features,
5860
});
5961
});
@@ -88,7 +90,8 @@ describe('previewFile', () => {
8890

8991
expect(results).toEqual({
9092
previewCoverage: 100,
91-
geoFieldTypes: ['geo_point', 'geo_shape'],
93+
hasPoints: true,
94+
hasShapes: false,
9295
features: FEATURE_COLLECTION.features,
9396
});
9497
});
@@ -116,7 +119,8 @@ describe('previewFile', () => {
116119

117120
expect(results).toEqual({
118121
previewCoverage: 100,
119-
geoFieldTypes: ['geo_point', 'geo_shape'],
122+
hasPoints: true,
123+
hasShapes: false,
120124
features: FEATURE_COLLECTION.features,
121125
});
122126
});
@@ -138,7 +142,8 @@ describe('previewFile', () => {
138142

139143
expect(results).toEqual({
140144
previewCoverage: 100,
141-
geoFieldTypes: ['geo_shape'],
145+
hasPoints: false,
146+
hasShapes: false,
142147
features: [],
143148
});
144149
});
@@ -165,7 +170,8 @@ describe('previewFile', () => {
165170

166171
expect(results).toEqual({
167172
previewCoverage: 100,
168-
geoFieldTypes: ['geo_shape'],
173+
hasPoints: false,
174+
hasShapes: false,
169175
features: [],
170176
});
171177
});

x-pack/plugins/file_upload/public/importer/geojson_importer/geojson_importer.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ import { ImportFailure, ImportResponse, MB } from '../../../common';
2727
const IMPORT_CHUNK_SIZE_MB = 10 * MB;
2828
export const GEOJSON_FILE_TYPES = ['.json', '.geojson'];
2929

30+
export interface GeoJsonPreview {
31+
features: Feature[];
32+
hasPoints: boolean;
33+
hasShapes: boolean;
34+
previewCoverage: number;
35+
}
36+
3037
export class GeoJsonImporter extends Importer {
3138
private _file: File;
3239
private _isActive = true;
@@ -52,20 +59,19 @@ export class GeoJsonImporter extends Importer {
5259
this._isActive = false;
5360
}
5461

55-
public async previewFile(
56-
rowLimit?: number,
57-
sizeLimit?: number
58-
): Promise<{ features: Feature[]; geoFieldTypes: string[]; previewCoverage: number }> {
62+
public async previewFile(rowLimit?: number, sizeLimit?: number): Promise<GeoJsonPreview> {
5963
await this._readUntil(rowLimit, sizeLimit);
6064
return {
6165
features: [...this._features],
6266
previewCoverage: this._hasNext
6367
? Math.round((this._unimportedBytesProcessed / this._file.size) * 100)
6468
: 100,
65-
geoFieldTypes:
66-
this._geometryTypesMap.has('Point') || this._geometryTypesMap.has('MultiPoint')
67-
? [ES_FIELD_TYPES.GEO_POINT, ES_FIELD_TYPES.GEO_SHAPE]
68-
: [ES_FIELD_TYPES.GEO_SHAPE],
69+
hasPoints: this._geometryTypesMap.has('Point') || this._geometryTypesMap.has('MultiPoint'),
70+
hasShapes:
71+
this._geometryTypesMap.has('LineString') ||
72+
this._geometryTypesMap.has('MultiLineString') ||
73+
this._geometryTypesMap.has('Polygon') ||
74+
this._geometryTypesMap.has('MultiPolygon'),
6975
};
7076
}
7177

x-pack/plugins/file_upload/public/importer/geojson_importer/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* 2.0.
66
*/
77

8-
export { GeoJsonImporter, GEOJSON_FILE_TYPES } from './geojson_importer';
8+
export { GeoJsonImporter, GeoJsonPreview, GEOJSON_FILE_TYPES } from './geojson_importer';

0 commit comments

Comments
 (0)