Skip to content

Commit

Permalink
chore: 从数据中计算经纬度成对列字段
Browse files Browse the repository at this point in the history
  • Loading branch information
lvisei committed Oct 19, 2023
1 parent 2378f46 commit 524cde4
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
15 changes: 5 additions & 10 deletions packages/li-editor/src/services/editor-dataset-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import {
isRemoteDatasetSchema,
} from '@antv/li-sdk';
import type { FieldPair } from '../types';
import { getPointFieldPairs } from '../utils/dataset';
import type AppService from './app-service';

const getPointFieldPairs = (fields: DatasetField[]) => {
const fieldPairs: FieldPair[] = [];

return fieldPairs;
};

/**
* 单个数据集的数据状态
*/
Expand All @@ -24,8 +19,8 @@ export class EditorDataset {
public data: Record<string, any>[] = [];
/** 列字段 */
public columns: DatasetField[] = [];
/** 成对列字段 */
public fieldPairs: FieldPair[] = [];
/** 经纬度成对列字段 */
public pointFieldPairs: FieldPair[] = [];
/** 数据集是否请求中,动态数据源类型情况 */
public loading = false;

Expand All @@ -34,7 +29,7 @@ export class EditorDataset {
if (isLocalDatasetSchema(schema)) {
this.data = schema.data;
this.columns = schema.columns;
this.fieldPairs = getPointFieldPairs(this.columns);
this.pointFieldPairs = getPointFieldPairs(this.columns);
} else if (isRemoteDatasetSchema(schema)) {
//
}
Expand Down Expand Up @@ -66,7 +61,7 @@ export class EditorDataset {
public updateData(data: Record<string, any>[]) {
this.data = data;
this.columns = data.length ? getDatasetColumns(data) : [];
this.fieldPairs = getPointFieldPairs(this.columns);
this.pointFieldPairs = getPointFieldPairs(this.columns);
}

/**
Expand Down
86 changes: 86 additions & 0 deletions packages/li-editor/src/utils/dataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type { DatasetField } from '@antv/li-sdk';
import type { FieldPair } from '../types';

const POINT_FIELDS: [string, string][] = [
['lat', 'lng'],
['lat', 'lon'],
['lat', 'long'],
['latitude', 'longitude'],
['纬度', '经度'],
];
const ALTITUDE_FIELDS = ['alt', 'altitude', '海拔'];

const SpecialCharacterSet = `[#_&@\\.\\-\\ ]`;

function foundMatchingFields(re: RegExp, suffixPair: [string, string], allNames: string[], fieldName: string) {
const lngName = fieldName.replace(re, (match) => match.replace(suffixPair[0], suffixPair[1]));
const lngIdx = allNames.findIndex((d) => d === lngName);

let altIdx = -1;

// 如果纬度找到,继续查找高度字段
if (lngIdx !== -1) {
ALTITUDE_FIELDS.some((alt) => {
const altName = fieldName.replace(re, (match) => match.replace(suffixPair[0], alt));
altIdx = allNames.findIndex((d) => d === altName);
return altIdx > -1;
});
}
return { lngIdx, altIdx };
}

/**
* 从字段中查找经纬度点数据的字段
* @param fields DatasetField[]
* @returns fieldPairs
*/
export const getPointFieldPairs = (fields: DatasetField[]) => {
const allFieldNames = fields.map((item) => item.name.toLowerCase());
const fieldPairs: FieldPair[] = [];

for (let idx = 0; idx < allFieldNames.length; idx++) {
const fieldName = allFieldNames[idx];

for (let index = 0; index < POINT_FIELDS.length; index++) {
const suffixPair = POINT_FIELDS[index];

// copy form https://github.com/keplergl/kepler.gl/blob/master/src/table/src/kepler-table.ts#L520
// 先匹配第一个字段
// (^|[#_&@\.\-\ ])lat([#_&@\.\-\ ]|$)
const re = new RegExp(`(^|${SpecialCharacterSet})${suffixPair[0]}(${SpecialCharacterSet}|$)`);

if (!re.test(fieldName)) continue;

const { lngIdx, altIdx } = foundMatchingFields(re, suffixPair, allFieldNames, fieldName);

if (lngIdx === -1) continue;

const trimName = fieldName.replace(re, '').trim();

fieldPairs.push({
defaultName: trimName || 'point',
pair: {
lat: {
fieldIdx: idx,
value: fields[idx].name,
},
lng: {
fieldIdx: lngIdx,
value: fields[lngIdx].name,
},
...(altIdx !== -1
? {
alt: {
fieldIdx: altIdx,
value: fields[altIdx].name,
},
}
: {}),
},
suffix: suffixPair,
});
}
}

return fieldPairs;
};

0 comments on commit 524cde4

Please sign in to comment.