Skip to content

Commit

Permalink
severity logic
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsh28 committed Oct 6, 2024
1 parent cd6b7f2 commit 7b999ae
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/utils/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ export function uniquePoints(points: IPoint[]) {
}

export function aggregatePoints(points: IPointAccident[]) : IAggregatePointAccident[] {
const sameLocationPointMap : Map<string,string[]> = new Map();
const sameLocationPointMap : Map<string,{dates : string[], severity : IAggregatePointAccident['accident_severity'] }> = 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};
})
}

0 comments on commit 7b999ae

Please sign in to comment.