Fetch geojson using ems-client (#97908) (#98547)

* Fetch geojson using ems-client

* Review feedback
This commit is contained in:
Nick Peihl 2021-04-27 15:26:06 -07:00 committed by GitHub
parent d773e4f78a
commit 899fd68e47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 64 deletions

View file

@ -377,7 +377,7 @@
"tar": "4.4.13",
"tinycolor2": "1.4.1",
"tinygradient": "0.4.3",
"topojson-client": "3.0.0",
"topojson-client": "3.1.0",
"tree-kill": "^1.2.2",
"ts-easing": "^0.2.0",
"tslib": "^2.0.0",

View file

@ -12,13 +12,8 @@ import { Adapters } from 'src/plugins/inspector/public';
import { FileLayer } from '@elastic/ems-client';
import { Attribution, ImmutableSourceProperty, SourceEditorArgs } from '../source';
import { AbstractVectorSource, GeoJsonWithMeta, IVectorSource } from '../vector_source';
import {
SOURCE_TYPES,
FIELD_ORIGIN,
VECTOR_SHAPE_TYPE,
FORMAT_TYPE,
} from '../../../../common/constants';
import { fetchGeoJson, getEmsFileLayers } from '../../../util';
import { SOURCE_TYPES, FIELD_ORIGIN, VECTOR_SHAPE_TYPE } from '../../../../common/constants';
import { getEmsFileLayers } from '../../../util';
import { getDataSourceLabel } from '../../../../common/i18n_getters';
import { UpdateSourceEditor } from './update_source_editor';
import { EMSFileField } from '../../fields/ems_file_field';
@ -122,24 +117,26 @@ export class EMSFileSource extends AbstractVectorSource implements IEmsFileSourc
}
async getGeoJsonWithMeta(): Promise<GeoJsonWithMeta> {
const emsFileLayer = await this.getEMSFileLayer();
const featureCollection = await fetchGeoJson(
emsFileLayer.getDefaultFormatUrl(),
emsFileLayer.getDefaultFormatType() as FORMAT_TYPE,
'data'
);
try {
const emsFileLayer = await this.getEMSFileLayer();
const featureCollection = await emsFileLayer.getGeoJson();
const emsIdField = emsFileLayer.getFields().find((field) => {
return field.type === 'id';
});
featureCollection.features.forEach((feature: Feature, index: number) => {
feature.id = emsIdField ? feature!.properties![emsIdField.id] : index;
});
if (!featureCollection) throw new Error('No features found');
return {
data: featureCollection,
meta: {},
};
const emsIdField = emsFileLayer.getFields().find((field) => {
return field.type === 'id';
});
featureCollection.features.forEach((feature: Feature, index: number) => {
feature.id = emsIdField ? feature!.properties![emsIdField.id] : index;
});
return {
data: featureCollection,
meta: {},
};
} catch (error) {
throw new Error(`${getErrorInfo(this._descriptor.id)} - ${error.message}`);
}
}
async getImmutableProperties(): Promise<ImmutableSourceProperty[]> {

View file

@ -6,7 +6,6 @@
*/
import { suggestEMSTermJoinConfig } from './ems_autosuggest';
import { FORMAT_TYPE } from '../../common';
import { FeatureCollection } from 'geojson';
class MockFileLayer {
@ -19,16 +18,28 @@ class MockFileLayer {
this._id = url;
this._fields = fields;
}
getDefaultFormatUrl() {
return this._url;
}
getFields() {
return this._fields;
}
getDefaultFormatType() {
return FORMAT_TYPE.GEOJSON;
getGeoJson() {
if (this._url === 'world_countries') {
return ({
type: 'FeatureCollection',
features: [
{ properties: { iso2: 'CA', iso3: 'CAN' } },
{ properties: { iso2: 'US', iso3: 'USA' } },
],
} as unknown) as FeatureCollection;
} else if (this._url === 'zips') {
return ({
type: 'FeatureCollection',
features: [{ properties: { zip: '40204' } }, { properties: { zip: '40205' } }],
} as unknown) as FeatureCollection;
} else {
throw new Error(`unrecognized mock url ${this._url}`);
}
}
hasId(id: string) {
@ -44,24 +55,6 @@ jest.mock('../util', () => {
new MockFileLayer('zips', [{ id: 'zip' }]),
];
},
async fetchGeoJson(url: string): Promise<FeatureCollection> {
if (url === 'world_countries') {
return ({
type: 'FeatureCollection',
features: [
{ properties: { iso2: 'CA', iso3: 'CAN' } },
{ properties: { iso2: 'US', iso3: 'USA' } },
],
} as unknown) as FeatureCollection;
} else if (url === 'zips') {
return ({
type: 'FeatureCollection',
features: [{ properties: { zip: '40204' } }, { properties: { zip: '40205' } }],
} as unknown) as FeatureCollection;
} else {
throw new Error(`unrecognized mock url ${url}`);
}
},
};
});

View file

@ -6,8 +6,8 @@
*/
import type { FileLayer } from '@elastic/ems-client';
import { getEmsFileLayers, fetchGeoJson } from '../util';
import { FORMAT_TYPE, emsWorldLayerId, emsRegionLayerId, emsUsaZipLayerId } from '../../common';
import { getEmsFileLayers } from '../util';
import { emsWorldLayerId, emsRegionLayerId, emsUsaZipLayerId } from '../../common';
export interface SampleValuesConfig {
emsLayerIds?: string[];
@ -165,14 +165,9 @@ async function getMatchesForEMSLayer(
}
const emsFields = emsFileLayer.getFields();
const url = emsFileLayer.getDefaultFormatUrl();
try {
const emsJson = await fetchGeoJson(
url,
emsFileLayer.getDefaultFormatType() as FORMAT_TYPE,
'data'
);
const emsJson = await emsFileLayer.getGeoJson();
const matches: EMSTermJoinConfig[] = [];
for (let f = 0; f < emsFields.length; f++) {
if (matchesEmsField(emsJson, emsFields[f].id, sampleValues)) {

View file

@ -26843,14 +26843,7 @@ topo@3.x.x:
dependencies:
hoek "5.x.x"
topojson-client@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/topojson-client/-/topojson-client-3.0.0.tgz#1f99293a77ef42a448d032a81aa982b73f360d2f"
integrity sha1-H5kpOnfvQqRI0DKoGqmCtz82DS8=
dependencies:
commander "2"
topojson-client@^3.1.0:
topojson-client@3.1.0, topojson-client@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/topojson-client/-/topojson-client-3.1.0.tgz#22e8b1ed08a2b922feeb4af6f53b6ef09a467b99"
integrity sha512-605uxS6bcYxGXw9qi62XyrV6Q3xwbndjachmNxu8HWTtVPxZfEJN9fd/SZS1Q54Sn2y0TMyMxFj/cJINqGHrKw==