This commit is contained in:
Xu Chang 2023-03-28 19:55:13 +08:00
parent 908e080977
commit a1c1fcdc0d
8 changed files with 147 additions and 0 deletions

12
lib/AspectDict.d.ts vendored
View File

@ -50,4 +50,16 @@ export declare type CommonAspectDict<ED extends EntityDict & BaseEntityDict, Cxt
getImportationTemplate: (params: {
id: string;
}, context: Cxt) => Promise<ArrayBuffer>;
searchPoi: (options: {
value: string;
areaCode?: string;
indexFrom?: number;
count?: number;
typeCode?: string;
}) => Promise<{
id: string;
areaId: string;
poiName: string;
coordinate: [number, number];
}[]>;
};

12
lib/geo.d.ts vendored Normal file
View File

@ -0,0 +1,12 @@
export declare function searchPoi(options: {
value: string;
areaCode?: string;
indexFrom?: number;
count?: number;
typeCode?: string;
}): Promise<{
id: string;
areaId: string;
poiName: string;
coordinate: [number, number];
}[]>;

41
lib/geo.js Normal file
View File

@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.searchPoi = void 0;
async function searchPoi(options) {
const { value, areaCode, indexFrom, count } = options;
const form = new FormData();
form.set('stName', value);
if (areaCode) {
form.set('code', areaCode);
}
if (indexFrom && count) {
form.set('page', `${indexFrom / count}`);
form.set('size', `${count}`);
}
const result = await fetch('https://dmfw.mca.gov.cn/9095/stname/listPub', {
method: 'post',
body: form,
});
const { records } = await result.json();
const pois = await Promise.all(records.map(async (ele) => {
let { area, standard_name, gdm, id } = ele;
// 对返回的area数据进行一些清洗不规范
if (area.length === 9) {
if (area.endsWith('999')) {
area = area.slice(0, 6);
}
}
if (area === '000000') {
// 搜索如长江这样的地理名称时会返回这样的数据,过滤掉
return undefined;
}
return {
id,
areaId: area,
poiName: standard_name,
coordinate: gdm.coordinates[0],
};
}));
return pois.filter(poi => !!poi);
}
exports.searchPoi = searchPoi;

2
lib/index.d.ts vendored
View File

@ -2,6 +2,7 @@ import { operate, select, fetchRows, count, aggregate } from './crud';
import { amap } from './amap';
import { getTranslations } from './locales';
import { registerPorts, clearPorts, importEntity, exportEntity, getImportationTemplate } from './port';
import { searchPoi } from './geo';
declare const aspectDict: {
operate: typeof operate;
select: typeof select;
@ -13,6 +14,7 @@ declare const aspectDict: {
importEntity: typeof importEntity;
exportEntity: typeof exportEntity;
getImportationTemplate: typeof getImportationTemplate;
searchPoi: typeof searchPoi;
};
export default aspectDict;
export * from './AspectDict';

View File

@ -8,6 +8,7 @@ const locales_1 = require("./locales");
const port_1 = require("./port");
Object.defineProperty(exports, "registerPorts", { enumerable: true, get: function () { return port_1.registerPorts; } });
Object.defineProperty(exports, "clearPorts", { enumerable: true, get: function () { return port_1.clearPorts; } });
const geo_1 = require("./geo");
const aspectDict = {
operate: crud_1.operate,
select: crud_1.select,
@ -19,6 +20,7 @@ const aspectDict = {
importEntity: port_1.importEntity,
exportEntity: port_1.exportEntity,
getImportationTemplate: port_1.getImportationTemplate,
searchPoi: geo_1.searchPoi,
};
exports.default = aspectDict;
tslib_1.__exportStar(require("./AspectDict"), exports);

View File

@ -68,4 +68,11 @@ export type CommonAspectDict<ED extends EntityDict & BaseEntityDict, Cxt extends
filter?: ED[T]['Selection']['filter'];
}, context: Cxt) => Promise<ArrayBuffer>;
getImportationTemplate: (params: { id: string }, context: Cxt) => Promise<ArrayBuffer>;
searchPoi: (options: {
value: string;
areaCode?: string;
indexFrom?: number;
count?: number;
typeCode?: string;
}) => Promise<{ id: string; areaId: string; poiName: string; coordinate: [number, number] }[]>;
};

69
src/geo.ts Normal file
View File

@ -0,0 +1,69 @@
import assert from 'assert';
/**
* poiName搜索地理位置
* @param options
* https://dmfw.mca.gov.cn/interface.html
*/
type DmfwPoi = {
id: string;
area: string;
standard_name: string;
gdm: {
coordinates: [[number, number]],
},
place_type: string;
place_code: string;
}
export async function searchPoi(options: {
value: string;
areaCode?: string;
indexFrom?: number;
count?: number;
typeCode?: string;
}): Promise<{ id: string; areaId: string; poiName: string; coordinate: [number, number] }[]> {
const { value, areaCode, indexFrom, count } = options;
const form = new FormData();
form.set('stName', value);
if (areaCode) {
form.set('code', areaCode);
}
if (indexFrom && count) {
form.set('page', `${indexFrom / count}`);
form.set('size', `${count}`);
}
const result = await fetch('https://dmfw.mca.gov.cn/9095/stname/listPub', {
method: 'post',
body: form,
});
const { records } = await result.json();
const pois = await Promise.all(
records.map(
async (ele: DmfwPoi) => {
let { area, standard_name, gdm, id } = ele;
// 对返回的area数据进行一些清洗不规范
if(area.length === 9) {
if (area.endsWith('999')) {
area = area.slice(0, 6);
}
}
if (area === '000000') {
// 搜索如长江这样的地理名称时会返回这样的数据,过滤掉
return undefined;
}
return {
id,
areaId: area,
poiName: standard_name,
coordinate: gdm.coordinates[0],
};
}
)
);
return pois.filter(
poi => !!poi
) as { id: string; areaId: string; poiName: string; coordinate: [number, number] }[];
}

View File

@ -2,6 +2,7 @@ import { operate, select, fetchRows, count, aggregate } from './crud';
import { amap } from './amap';
import { getTranslations } from './locales';
import { registerPorts, clearPorts, importEntity, exportEntity, getImportationTemplate } from './port';
import { searchPoi } from './geo';
const aspectDict = {
operate,
@ -14,6 +15,7 @@ const aspectDict = {
importEntity,
exportEntity,
getImportationTemplate,
searchPoi,
};
export default aspectDict;