Skip to content

Commit

Permalink
default to selecting geo_shape when file contains both points and shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Mar 2, 2021
1 parent e80285a commit eedb74e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@
*/

import React, { Component } from 'react';
import { Feature } from 'geojson';
import { EuiFilePicker, EuiFormRow } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { MB } from '../../common';
import { getMaxBytesFormatted } from '../get_max_bytes';
import { validateFile } from '../importer';
import { GeoJsonImporter, GEOJSON_FILE_TYPES } from '../importer/geojson_importer';
import { GeoJsonImporter, GeoJsonPreview, GEOJSON_FILE_TYPES } from '../importer/geojson_importer';

interface Props {
onSelect: ({
features,
geoFieldTypes,
hasPoints,
hasShapes,
importer,
indexName,
previewCoverage,
}: {
features: Feature[];
}: GeoJsonPreview & {
indexName: string;
importer: GeoJsonImporter;
geoFieldTypes: string[];
previewCoverage: number;
}) => void;
onClear: () => void;
}
Expand Down Expand Up @@ -73,11 +70,7 @@ export class GeoJsonFilePicker extends Component<Props, State> {

let importer: GeoJsonImporter | null = null;
let previewError: string | null = null;
let preview: {
features: Feature[];
geoFieldTypes: string[];
previewCoverage: number;
} | null = null;
let preview: GeoJsonPreview | null = null;
try {
validateFile(file, GEOJSON_FILE_TYPES);
importer = new GeoJsonImporter(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class IndexSettings extends Component {
text: indexType,
value: indexType,
}))}
value={this.props.selectedIndexType}
onChange={({ target }) => setSelectedIndexType(target.value)}
/>
</EuiFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IndexSettings } from './index_settings';
import { getIndexPatternService } from '../kibana_services';
import { GeoJsonFilePicker } from './geojson_file_picker';
import { ImportCompleteView } from './import_complete_view';
import { ES_FIELD_TYPES } from '../../../../../src/plugins/data/public';

const PHASE = {
CONFIGURE: 'CONFIGURE',
Expand Down Expand Up @@ -201,16 +202,21 @@ export class JsonUploadAndParse extends Component {
});
};

_onFileSelect = ({ features, geoFieldTypes, importer, indexName, previewCoverage }) => {
_onFileSelect = ({ features, hasPoints, hasShapes, importer, indexName, previewCoverage }) => {
this._geojsonImporter = importer;

const geoFieldTypes = hasPoints
? [ES_FIELD_TYPES.GEO_POINT, ES_FIELD_TYPES.GEO_SHAPE]
: [ES_FIELD_TYPES.GEO_SHAPE];

const newState = {
indexTypes: geoFieldTypes,
indexName,
};
if (!this.state.selectedIndexType && geoFieldTypes.length) {
if (!this.state.selectedIndexType) {
// auto select index type
newState.selectedIndexType = geoFieldTypes[0];
newState.selectedIndexType =
hasPoints && !hasShapes ? ES_FIELD_TYPES.GEO_POINT : ES_FIELD_TYPES.GEO_SHAPE;
} else if (
this.state.selectedIndexType &&
!geoFieldTypes.includes(this.state.selectedIndexType)
Expand Down Expand Up @@ -274,6 +280,7 @@ export class JsonUploadAndParse extends Component {
indexName={this.state.indexName}
setIndexName={(indexName) => this.setState({ indexName })}
indexTypes={this.state.indexTypes}
selectedIndexType={this.state.selectedIndexType}
setSelectedIndexType={(selectedIndexType) => this.setState({ selectedIndexType })}
setHasIndexErrors={(hasIndexErrors) => this.setState({ hasIndexErrors })}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe('previewFile', () => {
expect(results).toEqual({
features: [],
previewCoverage: 0,
geoFieldTypes: ['geo_shape'],
hasPoints: false,
hasShapes: false,
});
});

Expand All @@ -53,7 +54,8 @@ describe('previewFile', () => {
const results = await importer.previewFile();
expect(results).toEqual({
previewCoverage: 100,
geoFieldTypes: ['geo_point', 'geo_shape'],
hasPoints: true,
hasShapes: false,
features: FEATURE_COLLECTION.features,
});
});
Expand Down Expand Up @@ -88,7 +90,8 @@ describe('previewFile', () => {

expect(results).toEqual({
previewCoverage: 100,
geoFieldTypes: ['geo_point', 'geo_shape'],
hasPoints: true,
hasShapes: false,
features: FEATURE_COLLECTION.features,
});
});
Expand Down Expand Up @@ -116,7 +119,8 @@ describe('previewFile', () => {

expect(results).toEqual({
previewCoverage: 100,
geoFieldTypes: ['geo_point', 'geo_shape'],
hasPoints: true,
hasShapes: false,
features: FEATURE_COLLECTION.features,
});
});
Expand All @@ -138,7 +142,8 @@ describe('previewFile', () => {

expect(results).toEqual({
previewCoverage: 100,
geoFieldTypes: ['geo_shape'],
hasPoints: false,
hasShapes: false,
features: [],
});
});
Expand All @@ -165,7 +170,8 @@ describe('previewFile', () => {

expect(results).toEqual({
previewCoverage: 100,
geoFieldTypes: ['geo_shape'],
hasPoints: false,
hasShapes: false,
features: [],
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ import { ImportFailure, ImportResponse, MB } from '../../../common';
const IMPORT_CHUNK_SIZE_MB = 10 * MB;
export const GEOJSON_FILE_TYPES = ['.json', '.geojson'];

export interface GeoJsonPreview {
features: Feature[];
hasPoints: boolean;
hasShapes: boolean;
previewCoverage: number;
}

export class GeoJsonImporter extends Importer {
private _file: File;
private _isActive = true;
Expand All @@ -52,20 +59,19 @@ export class GeoJsonImporter extends Importer {
this._isActive = false;
}

public async previewFile(
rowLimit?: number,
sizeLimit?: number
): Promise<{ features: Feature[]; geoFieldTypes: string[]; previewCoverage: number }> {
public async previewFile(rowLimit?: number, sizeLimit?: number): Promise<GeoJsonPreview> {
await this._readUntil(rowLimit, sizeLimit);
return {
features: [...this._features],
previewCoverage: this._hasNext
? Math.round((this._unimportedBytesProcessed / this._file.size) * 100)
: 100,
geoFieldTypes:
this._geometryTypesMap.has('Point') || this._geometryTypesMap.has('MultiPoint')
? [ES_FIELD_TYPES.GEO_POINT, ES_FIELD_TYPES.GEO_SHAPE]
: [ES_FIELD_TYPES.GEO_SHAPE],
hasPoints: this._geometryTypesMap.has('Point') || this._geometryTypesMap.has('MultiPoint'),
hasShapes:
this._geometryTypesMap.has('LineString') ||
this._geometryTypesMap.has('MultiLineString') ||
this._geometryTypesMap.has('Polygon') ||
this._geometryTypesMap.has('MultiPolygon'),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

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

0 comments on commit eedb74e

Please sign in to comment.