blob: ca8f494b7e1d6368aba09031a1f07381ddde132c [file] [log] [blame]
import type { FeatureCollection } from 'geojson';
import { feature } from 'topojson-client';
import { get } from 'lodash/fp';
import { RawMapMetadata } from '../types';
import Projection from './Projection';
export default class MapMetadata {
config: RawMapMetadata;
keyAccessor: (...args: unknown[]) => string;
constructor(metadata: RawMapMetadata) {
const { keyField } = metadata;
this.config = metadata;
this.keyAccessor = get(keyField);
}
loadMap(): Promise<FeatureCollection> {
const { key } = this.config;
return this.config.type === 'topojson'
? this.config.load().then(map => feature(map, map.objects[key]) as FeatureCollection)
: this.config.load();
}
createProjection() {
const { projection = 'Mercator', rotate } = this.config;
const projectionFn = Projection[projection]();
if (rotate) {
projectionFn.rotate(rotate);
}
return projectionFn;
}
}