diff --git a/src/utils/map.utils.ts b/src/utils/map.utils.ts index ae5792ed..85629eb6 100644 --- a/src/utils/map.utils.ts +++ b/src/utils/map.utils.ts @@ -16,23 +16,26 @@ export function uniquePoints(points: IPoint[]) { } export function aggregatePoints(points: IPointAccident[]) : IAggregatePointAccident[] { - const sameLocationPointMap : Map = new Map(); + const sameLocationPointMap : Map = new Map(); // fill only unique points (not yet included in uniqueSet) points.reduce(( map, currentPoint) => { const key = JSON.stringify({latitude : currentPoint.latitude ,longitude : currentPoint.longitude }); - const label = map.get(key); + const mapValue = map.get(key); - if(map.get(key)){ - label?.push(currentPoint.accident_timestamp); - //map.set(key, `${label},${currentPoint.accident_timestamp}`) + if(mapValue){ + mapValue?.dates.push(currentPoint.accident_timestamp); + if(mapValue.severity !== "fatal" ){ + mapValue.severity = currentPoint.accident_severity ; + } } else{ - map.set(key, [currentPoint.accident_timestamp]); + map.set(key, {severity : currentPoint.accident_severity ,dates : [currentPoint.accident_timestamp]}); } return map; },sameLocationPointMap); - return Array.from(sameLocationPointMap.entries()).map(([key, value]) => { + // convert map to array of IAggregatePointAccident + return Array.from(sameLocationPointMap.entries()).map(([key, locationData]) => { const point = JSON.parse(key) as IPoint; - return {latitude: point.latitude, longitude: point.longitude, accident_timestamp: value, accident_severity: "severe"}; + return {latitude: point.latitude, longitude: point.longitude, accident_timestamp: locationData.dates, accident_severity: locationData.severity}; }) }