Skip to content

Commit

Permalink
add fields implementation for geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Jan 20, 2021
1 parent 83cba9b commit 90e0db7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,13 @@ export type TiledSingleLayerVectorSourceDescriptor = AbstractSourceDescriptor &
tooltipProperties: string[];
};

export type GeoJsonFileFieldDescriptor = {
name: string;
type: 'string' | 'number';
};

export type GeojsonFileSourceDescriptor = {
__fields?: GeoJsonFileFieldDescriptor[];
__featureCollection: FeatureCollection;
name: string;
type: string;
Expand Down
35 changes: 35 additions & 0 deletions x-pack/plugins/maps/public/classes/fields/geojson_file_field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FIELD_ORIGIN } from '../../../common/constants';
import { IField, AbstractField } from './field';
import { IVectorSource } from '../sources/vector_source';
import { GeoJsonFileSource } from '../sources/geojson_file_source';

export class GeoJsonFileField extends AbstractField implements IField {
private readonly _source: GeoJsonFileSource;

constructor({
fieldName,
source,
origin,
}: {
fieldName: string;
source: GeoJsonFileSource;
origin: FIELD_ORIGIN;
}) {
super({ fieldName, origin });
this._source = source;
}

getSource(): IVectorSource {
return this._source;
}

async getLabel(): Promise<string> {
return this.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
SCALING_TYPES,
} from '../../../../common/constants';
import { getFileUploadComponent } from '../../../kibana_services';
import { GeojsonFileSource } from '../../sources/geojson_file_source';
import { GeoJsonFileSource } from '../../sources/geojson_file_source';
import { VectorLayer } from '../../layers/vector_layer/vector_layer';
import { createDefaultLayerDescriptor } from '../../sources/es_search_source';
import { RenderWizardArguments } from '../../layers/layer_wizard_registry';
Expand Down Expand Up @@ -79,7 +79,7 @@ export class ClientFileCreateSourceEditor extends Component<RenderWizardArgument
return;
}

const sourceDescriptor = GeojsonFileSource.createDescriptor(geojsonFile, name);
const sourceDescriptor = GeoJsonFileSource.createDescriptor(geojsonFile, name);
const layerDescriptor = VectorLayer.createDescriptor(
{ sourceDescriptor },
this.props.mapColors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

import { Feature, FeatureCollection } from 'geojson';
import { AbstractVectorSource, ESGlobalFilters, GeoJsonWithMeta } from '../vector_source';
import { EMPTY_FEATURE_COLLECTION, SOURCE_TYPES } from '../../../../common/constants';
import { GeojsonFileSourceDescriptor, MapExtent } from '../../../../common/descriptor_types';
import { EMPTY_FEATURE_COLLECTION, FIELD_ORIGIN, SOURCE_TYPES } from '../../../../common/constants';
import {
GeoJsonFileFieldDescriptor,
GeojsonFileSourceDescriptor,
MapExtent,
} from '../../../../common/descriptor_types';
import { registerSource } from '../source_registry';
import { IField } from '../../fields/field';
import { getFeatureCollectionBounds } from '../../util/get_feature_collection_bounds';
import { GeoJsonFileField } from '../../fields/geojson_file_field';

function getFeatureCollection(geoJson: Feature | FeatureCollection | null): FeatureCollection {
if (!geoJson) {
Expand All @@ -31,18 +36,56 @@ function getFeatureCollection(geoJson: Feature | FeatureCollection | null): Feat
return EMPTY_FEATURE_COLLECTION;
}

export class GeojsonFileSource extends AbstractVectorSource {
export class GeoJsonFileSource extends AbstractVectorSource {
static createDescriptor(
geoJson: Feature | FeatureCollection | null,
name: string
name: string,
fields?: GeoJsonFileFieldDescriptor[]
): GeojsonFileSourceDescriptor {
return {
type: SOURCE_TYPES.GEOJSON_FILE,
__featureCollection: getFeatureCollection(geoJson),
__fields: fields || [],
name,
};
}

_getFields(): GeoJsonFileFieldDescriptor[] {
const fields = (this._descriptor as GeojsonFileSourceDescriptor).__fields;
return fields ? fields : [];
}

createField({ fieldName }: { fieldName: string }): IField {
const fields = this._getFields();
const descriptor: GeoJsonFileFieldDescriptor | undefined = fields.find((field) => {
return field.name === fieldName;
});

if (!descriptor) {
throw new Error(
`Cannot find corresponding field ${fieldName} in __fields array ${JSON.stringify(
this._getFields()
)} `
);
}
return new GeoJsonFileField({
fieldName: descriptor.name,
source: this,
origin: FIELD_ORIGIN.SOURCE,
});
}

async getFields(): Promise<IField[]> {
const fields = this._getFields();
return fields.map((field: GeoJsonFileFieldDescriptor) => {
return new GeoJsonFileField({
fieldName: field.name,
source: this,
origin: FIELD_ORIGIN.SOURCE,
});
});
}

isBoundsAware(): boolean {
return true;
}
Expand All @@ -62,10 +105,6 @@ export class GeojsonFileSource extends AbstractVectorSource {
};
}

createField({ fieldName }: { fieldName: string }): IField {
throw new Error('Not implemented');
}

async getDisplayName() {
return (this._descriptor as GeojsonFileSourceDescriptor).name;
}
Expand All @@ -76,6 +115,6 @@ export class GeojsonFileSource extends AbstractVectorSource {
}

registerSource({
ConstructorFunction: GeojsonFileSource,
ConstructorFunction: GeoJsonFileSource,
type: SOURCE_TYPES.GEOJSON_FILE,
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { GeojsonFileSource } from './geojson_file_source';
export { GeoJsonFileSource } from './geojson_file_source';
6 changes: 3 additions & 3 deletions x-pack/plugins/maps/public/selectors/map_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { TiledVectorLayer } from '../classes/layers/tiled_vector_layer/tiled_vec
import { copyPersistentState, TRACKED_LAYER_DESCRIPTOR } from '../reducers/util';
import { InnerJoin } from '../classes/joins/inner_join';
import { getSourceByType } from '../classes/sources/source_registry';
import { GeojsonFileSource } from '../classes/sources/geojson_file_source';
import { GeoJsonFileSource } from '../classes/sources/geojson_file_source';
import {
SOURCE_DATA_REQUEST_ID,
STYLE_TYPE,
Expand Down Expand Up @@ -241,7 +241,7 @@ export const getSpatialFiltersLayer = createSelector(
type: 'FeatureCollection',
features: extractFeaturesFromFilters(filters),
};
const geoJsonSourceDescriptor = GeojsonFileSource.createDescriptor(
const geoJsonSourceDescriptor = GeoJsonFileSource.createDescriptor(
featureCollection,
'spatialFilters'
);
Expand Down Expand Up @@ -272,7 +272,7 @@ export const getSpatialFiltersLayer = createSelector(
},
}),
}),
source: new GeojsonFileSource(geoJsonSourceDescriptor),
source: new GeoJsonFileSource(geoJsonSourceDescriptor),
});
}
);
Expand Down

0 comments on commit 90e0db7

Please sign in to comment.