import _ from 'lodash' ; import { FeatureCollection , Feature } from 'geojson' ; export const GEOJSON_FEATURE_ID_PROPERTY_NAME = '__kbn__feature_id__' ; let idCounter = 0 ; function generateNumericalId ( ) { const newId = idCounter < Number . MAX_SAFE_INTEGER ? idCounter : 0 ; idCounter = newId + 1 ; return newId ; } export function assignFeatureIds ( featureCollection ) { const ids = [ ] ; for ( let i = 0 ; i < featureCollection . features . length ; i ++ ) { const id = generateNumericalId ( ) ; ids . push ( id ) ; } const randomizedIds = _ . shuffle ( ids ) ; const features = [ ] ; for ( let i = 0 ; i < featureCollection . features . length ; i ++ ) { const numericId = randomizedIds [ i ] ; const feature = featureCollection . features [ i ] ; features . push ( { type : 'Feature' , geometry : feature . geometry , properties : { [ GEOJSON_FEATURE_ID_PROPERTY_NAME ] : feature . id == null ? numericId : feature . id , ... feature . properties , } , id : numericId , } ) ; } return { type : 'FeatureCollection' , features , } ; }