本体地图Integrate data for the map地图搜索周围函数

注意:以下翻译的准确性尚未经过验证。这是使用 AIP ↗ 从原始英文文本进行的机器翻译。

地图搜索周围函数

您可以通过编写地图搜索周围函数为地图创建强大的地图搜索周围功能。这允许您编写TypeScript函数,这些函数会接收选定的对象并遍历Ontology,以带回与正在进行的特定分析相关或有用的所有对象。

地图搜索周围函数返回的数据结构可以包括:

  • objectRids:Object,将被添加到地图中。
  • edges:Edges,包括源对象、目标对象和非必填的中介对象。源对象和目标对象将被添加到地图中,并在它们之间绘制一条弧线,中介对象将在选择弧线时列出。
  • measures:时间序列度量,将被添加到系列面板中。

实现地图搜索周围函数

地图搜索周围函数在TypeScript函数库中开发。有关更多信息,请参阅函数文档

返回类型

地图搜索周围函数必须声明一个返回类型为Promise<IMapSearchAroundResults>。地图应用程序将使用其返回类型的名称和结构来发现搜索周围函数,因此必须精确地声明返回类型,如下所示:

Copied!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 export interface IMapSearchAroundResults { objectRids?: string[]; // 可选属性,表示对象的唯一标识符数组 edges?: IMapSearchAroundEdge[]; // 可选属性,表示边的数组 measures?: IMapSearchAroundMeasure[]; // 可选属性,表示测量的数组 } export interface IMapSearchAroundEdge { sourceObjectRid: string; // 源对象的唯一标识符 targetObjectRid: string; // 目标对象的唯一标识符 intermediaryObjectRids?: string[]; // 可选属性,中介对象的唯一标识符数组 } export interface IMapSearchAroundMeasure { objectRids: string[]; // 对象的唯一标识符数组 measureId: string; // 测量的唯一标识符 }

参数

地图环绕搜索函数必须包含一个(且仅一个)对象参数,该参数可以是以下之一:

  • 单个Object: 当选择指定类型的单个Object时,此环绕搜索函数将在环绕搜索菜单中可用。例如:
Copied!
1 2 3 4 5 public exampleSearchAround(object: ExampleObjectType) { // 此方法用于在某个范围内执行搜索操作 // 参数 object 为 ExampleObjectType 类型的对象 ... }
  • 一个对象数组: 当选择任意数量的指定类型的对象时,此环绕搜索函数将在环绕搜索菜单中可用。例如:
Copied!
1 2 3 4 public exampleSearchAround(objects: ExampleObjectType[]) { // 该方法用于在给定的对象数组中进行某种类型的搜索 ... }
  • 对象集: 当选择了任意数量的指定类型的Objects时,此附近搜索功能将在附近搜索菜单中可用。例如:
Copied!
1 2 public exampleSearchAround(objectSet: ObjectSet<ExampleObjectType>) { ... // 这是一个公共方法,名为 exampleSearchAround,参数是一个 ExampleObjectType 类型对象的集合 objectSet

地图搜索周围函数可以选择性地包括任意数量的这些标量类型的附加参数:stringbooleanIntegerLongFloatDoubleLocalDateTimestamp(有关详细信息,请参见标量类型)。当用户执行带有附加参数的搜索周围函数时,用户将被提示输入这些参数的值。例如:

Copied!
1 2 3 4 5 6 public exampleSearchAround(objectSet: ObjectSet<ExampleObjectType>, stringParameter: string, timestampParameter: Timestamp) { // objectSet: 包含ExampleObjectType对象的集合 // stringParameter: 字符串参数,可能用于过滤或匹配 // timestampParameter: 时间戳参数,可能用于时间相关的过滤操作 ... }

提示和故障排除

  • 为了最大化性能,所有代码应尽可能是异步的。在您的函数代码中,加载对象时始终使用 allAsync()getAsync() 而不是 all()get(),并尽可能少地使用 await 语句。
  • 地图应用程序将使用函数的最新发布版本。要发布您的函数,您需要为要标记的分支/提交附上一个兼容语义版本的版本号,例如:1.0.0。
  • 您的存储库需要访问您希望在函数中使用的所有Ontology对象和链接。这可以在存储库的设置Ontology部分进行配置。
  • 如果对象类型及其支持的数据集在与存储库不同的项目中定义,则包含您的存储库的项目将需要引用这些支持的数据集和那些对象类型。

示例

此示例代码包含三个围绕搜索的函数,基于Foundry Reference Project示例数据:

  • airportsRelatedObjects:返回与一组机场相关的各种对象。可以在机场分析的地图模板中使用。
  • nearbyAirports:执行地理空间搜索以查找给定距离内的其他机场。该函数接受一个非必填距离参数,允许用户在执行函数时提供距离。
  • routesBetweenAirports:给定一组机场,返回仅在这些机场之间的所有航线。
Copied!
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 import { Distance, Function, Integer, Filters } from "@foundry/functions-api"; import { ObjectSet, Objects, ExampleDataAirport } from "@foundry/ontology-api"; export interface IMapSearchAroundResults { objectRids?: string[]; edges?: IMapSearchAroundEdge[]; measures?: IMapSearchAroundMeasure[]; } export interface IMapSearchAroundEdge { sourceObjectRid: string; targetObjectRid: string; intermediaryObjectRids?: string[]; } export interface IMapSearchAroundMeasure { objectRids: string[]; measureId: string; } export class MapSearchAroundFunctions { /** * 返回与机场相关的对象:跑道,航线 * 该函数异步获取与指定机场集合相关的跑道和航线对象 */ @Function() public async airportsRelatedObjects(airportSet: ObjectSet<ExampleDataAirport>): Promise<IMapSearchAroundResults> { const relatedObjects = (await Promise.all([ airportSet.searchAroundExampleDataRunway().allAsync(), airportSet.searchAroundRoutes().allAsync(), ])).flat(); const objectRids = relatedObjects.map(o => o.rid!); return { objectRids, }; } /** * 返回距离选定机场特定公里数内的所有机场(默认为50公里) * 如果未指定距离,默认搜索半径为50公里 */ @Function() public async nearbyAirports(airport: ExampleDataAirport, distanceKm?: Integer): Promise<IMapSearchAroundResults> { const point = airport.airportLocation; const distance = Distance.ofKilometers(distanceKm ?? 50); if (point === undefined) { return {}; } const nearbyAirports = await Objects.search() .exampleDataAirport() .filter(airportFilter => airportFilter.airportLocation.withinDistanceOf(point, distance)) .allAsync(); const objectRids = nearbyAirports.map(o => o.rid!); return { objectRids, }; } /** * 返回仅从选定机场出发并到达的航线 * 此函数搜索出发地和目的地均为指定机场集合的航线 */ @Function() public async routesBetweenAirports(airportSet: ObjectSet<ExampleDataAirport>): Promise<IMapSearchAroundResults> { const airports = await airportSet.allAsync(); const airportCodes = airports.map(airport => airport.airport); const airportsByCodes = new Map(Array.from(airports, a => [a.airport, a])); const routes = await Objects.search() .exampleDataRoute() .filter(route => Filters.and( route.origin.exactMatch(...airportCodes), route.dest.exactMatch(...airportCodes), )) .allAsync(); const edges = routes.map(route => ({ sourceObjectRid: airportsByCodes.get(route.origin!)!.rid!, targetObjectRid: airportsByCodes.get(route.dest!)!.rid!, intermediaryObjectRids: [route.rid!], })); return { edges }; } }