Functions can be used to create derived properties on objects. These can be displayed in the Selection panel and used to color objects on the map, as part of value-based styling.
Any function that takes in an array of objects and returns a FunctionsMap
from object to a primitive can be used as a derived property. For example:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import { Function, FunctionsMap, Double } from "@foundry/functions-api"; import { ExampleDataRoute } from "@foundry/ontology-api"; export class DerivedPropertyFunctions { @Function() public async flightCancellationPercentage(routes: ExampleDataRoute[]): Promise<FunctionsMap<ExampleDataRoute, Double>> { const routeMap = new FunctionsMap<ExampleDataRoute, Double>(); const allFlights = await Promise.all(routes.map(route => route.flights.allAsync())); for (let i = 0; i < routes.length; i++) { const route = routes[i]; const flights = allFlights[i]; const cancelledFlights = flights.filter(flight => flight.cancelled); const cancellationPercentage = (cancelledFlights.length / flights.length) * 100; routeMap.set(route, cancellationPercentage); } return routeMap; } }
Although your function must consume an array of objects, there is no guarantee that all objects in a layer will be passed to the function at once. The Map application will create and pass batches of objects to derived property functions, in order to help avoid your function invocation from timing out. Your function should compute a derived property for an object based solely on the information available from that object itself.