/
region.ts
50 lines (47 loc) · 1.54 KB
/
region.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { assign, isArray, isString } from '@antv/util';
import { DataSet } from '../../data-set';
const { registerTransform } = DataSet;
import { getField } from '../../util/option-parser';
import { View } from '../../view';
const DEFAULT_OPTIONS: Partial<Options> = {
// field: 'name', // required
// geoView: view, // required
// geoDataView: view, // alias
as: ['_x', '_y'],
};
export interface Options {
field: string;
geoDataView: View | string;
as?: [string, string];
}
function transform(view: View, options: Options): void {
options = assign({} as Options, DEFAULT_OPTIONS, options);
const field = getField(options);
// @ts-ignore
let geoView = options.geoView || options.geoDataView; // alias
if (isString(geoView)) {
geoView = view.dataSet.getView(geoView);
}
if (!geoView || geoView.dataType !== 'geo') {
throw new TypeError('Invalid geoView: must be a DataView of GEO dataType!');
}
const as = options.as;
if (!isArray(as) || as.length !== 2) {
throw new TypeError('Invalid as: it must be an array with 2 strings (e.g. [ "x", "y" ])!');
}
const lonField = as[0];
const latField = as[1];
view.rows.forEach((row) => {
const feature = geoView.geoFeatureByName(row[field]);
if (feature) {
if (geoView._projectedAs) {
row[lonField] = feature[geoView._projectedAs[0]];
row[latField] = feature[geoView._projectedAs[1]];
} else {
row[lonField] = feature.longitude;
row[latField] = feature.latitude;
}
}
});
}
registerTransform('geo.region', transform);