Utils
SDK Utilities
Core
debounce
The debounce
function ensures that the given function fn
is only called once within the specified delay
period, even if it is triggered multiple times. This is useful for scenarios where you want to limit the rate at which a function can be executed, such as handling user input or resize events.
Parameters
-
fn: Function
The function to debounce. This is the function that will be executed after the delay period has elapsed since the last time the debounced function was called. -
delay: number
The delay in milliseconds. This specifies how long to wait after the last invocation before executing the function.
Returns
Function
The debounced function. This returned function can be called multiple times, but it will only execute the original functionfn
after the specifieddelay
has passed since the last call.
throttle
The throttle
function ensures that the given function fn
is only called at most once within the specified delay period, even if it is triggered multiple times. This is useful for scenarios where you want to limit the rate at which a function can be executed, such as handling window resize or scroll events.
Parameters
-
fn: Function
The function to throttle. This is the function that will be executed at most once within the specifieddelay
period. -
delay: number
The delay in milliseconds. This specifies the minimum time interval between consecutive calls to the functionfn
.
Returns
Function
The throttled function. This returned function can be called multiple times, but it will only execute the original functionfn
at most once everydelay
milliseconds.
mergeByProperty
The mergeByProperty
function merges two arrays of objects based on a specified property. It updates objects in the target array with matching objects from the source array by the specified property and adds non-matching objects from the source array to the target array.
Parameters
-
target: object[]
The target array of objects. This array will be merged with objects from the source array based on the specified property. -
source: object[]
The source array of objects. Objects from this array will be used to update or add to the target array based on the specified property. -
propName: string
The property name to merge by. This specifies which property of the objects to use for matching and merging.
Returns
object[]
The merged array of objects. This array contains all objects from the target array updated with matching objects from the source array and any additional non-matching objects from the source array.
Geographic
getHexagonsFeatureCollection
The getHexagonsFeatureCollection
function generates a GeoJSON FeatureCollection from an array of H3 hexagon IDs. It adjusts coordinates to account for antimeridian crossing and ensures valid longitude and latitude ranges, without mutating the original data.
Parameters
hexagons: Array<{hexId: string}>
An array of objects, each containing ahexId
representing an H3 hexagon. The default value is an empty array if no input is provided.
Returns
Object
A GeoJSON FeatureCollection representing the hexagons. If the input array is empty or not provided, the function returnsundefined
.
getHexIdsByMapBoundsAndResolution
The getHexIdsByMapBoundsAndResolution
function calculates the H3 hexagon IDs that intersect with the given map bounds at a specified resolution. This function is useful for identifying the hexagon cells that fall within a specific geographic area defined by the map bounds.
Parameters
-
mapBounds: Array<number>
The bounding box of the map. This is an object representing the geographic area of interest. -
resolution: number
The H3 resolution to use. This specifies the resolution level of the H3 hexagons. The default value is0
.
Returns
Array<string>
An array of H3 hexagon IDs that intersect with the given map bounds.
getHexIdsFromMapboxMap
The getHexIdsFromMapboxMap
function retrieves the H3 hexagon IDs that intersect with the current viewport of a Mapbox map instance at a specified resolution. This function is useful for obtaining hexagon IDs that cover the visible area of a Mapbox map.
Parameters
-
map: object
The Mapbox map instance. This is the Mapbox GL JS map object from which the viewport bounds will be retrieved. -
resolution: number
(optional)
The H3 resolution to use. This specifies the resolution level of the H3 hexagons. If not provided, a default resolution will be used.
Returns
Array<string>
An array of H3 hexagon IDs that intersect with the current viewport of the Mapbox map. If the map instance is not provided, the function returnsundefined
.
getMapPolygon
The getMapPolygon
function retrieves the current viewport bounds of a Mapbox map instance and returns them as a polygon in latitude and longitude coordinates. This polygon represents the geographic area currently visible in the map viewport.
Parameters
map: object
The Mapbox map instance. This is the Mapbox GL JS map object from which the viewport bounds will be retrieved.
Returns
Array<Array<number>>
A polygon represented as an array of latitude and longitude coordinate pairs. This polygon outlines the current viewport bounds of the Mapbox map.
Features
Nowcasting
getNowcastingDataSlice
The getNowcastingDataSlice
function filters nowcasting data based on specified filter criteria. It returns a slice of the data that matches the provided forecast accuracy, severity, and altitude levels.
Parameters
-
props: Object
The props object containing data and filters.-
props.data: NowcastingData
The nowcasting data to slice. This is an object where keys are cell identifiers and values are arrays ofNowcastingItem
. -
props.filters: FilterCriteria
The filters to apply. This is an object containingforecast
,severity
, andaltitude
filters.
-
Returns
NowcastingData
The sliced/filtered nowcasting data. This is an object where keys are cell identifiers and values are arrays ofNowcastingItem
that match the filter criteria.
prepareNowcastingDataForMapHexagons
The prepareNowcastingDataForMapHexagons
function prepares nowcasting data for display on a map. It converts the hexagon IDs in the nowcasting data to the specified resolution and returns an array of objects containing the hexagon ID and other nowcasting attributes.
Parameters
-
props: Object
The props object containing data and resolution.-
props.data: NowcastingData
The nowcasting data to prepare. This is an object where keys are cell identifiers and values are arrays ofNowcastingItem
. -
props.resolution: number
The resolution of the map display. If not provided, a default resolution is used.
-
Returns
Array<{alt: number, sev: number, forecast: number, hexId: string}>
An array of objects containing the nowcasting data prepared for the map hexagons. Each object includes the altitude, severity, forecast accuracy, and hexagon ID.