-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: old version * fix: use predefined colors * fix: add type param to generic type * fix: requested code style changes * feat: add legend to conflict layer
- Loading branch information
Showing
21 changed files
with
277 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useSidebar } from '@/domain/contexts/SidebarContext'; | ||
import { AlertType } from '@/domain/enums/AlertType'; | ||
|
||
import { ConflictLayer } from './ConflictLayer'; | ||
|
||
export function AlertContainer() { | ||
const { selectedAlert } = useSidebar(); | ||
|
||
switch (selectedAlert) { | ||
case AlertType.CONFLICTS: | ||
return <ConflictLayer />; | ||
default: | ||
return null; // TODO: hazard layers | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useMemo } from 'react'; | ||
import { CircleMarker } from 'react-leaflet'; | ||
import MarkerClusterGroup from 'react-leaflet-cluster'; | ||
|
||
import LegendContainer from '@/components/Legend/LegendContainer'; | ||
import { MAP_MAX_ZOOM } from '@/domain/constant/Map'; | ||
import { ConflictType } from '@/domain/enums/ConflictType'; | ||
import { useConflictQuery } from '@/domain/hooks/alertHooks'; | ||
import ConflictOperations from '@/operations/ConflictOperations'; | ||
import GeometryOperations from '@/operations/GeometryOperations'; | ||
import { getTailwindColor } from '@/utils/tailwind-util'; | ||
|
||
export function ConflictLayer() { | ||
const { data, isPending } = useConflictQuery(); | ||
const conflictsByType = useMemo(() => ConflictOperations.sortConflictsByType(data), [data]); | ||
|
||
return ( | ||
<> | ||
<div className="absolute bottom-6 right-8 z-9999"> | ||
<LegendContainer loading={isPending || !data} items={ConflictOperations.generateConflictLegend()} /> | ||
</div> | ||
{!isPending && | ||
data && | ||
(Object.keys(conflictsByType) as ConflictType[]).map((conflictType) => ( | ||
<MarkerClusterGroup | ||
key={conflictType} | ||
iconCreateFunction={(cluster) => ConflictOperations.createClusterCustomIcon(cluster, conflictType)} | ||
showCoverageOnHover={false} | ||
spiderLegPolylineOptions={{ weight: 0 }} | ||
disableClusteringAtZoom={MAP_MAX_ZOOM} | ||
zoomToBoundsOnClick={false} | ||
maxClusterRadius={60} | ||
spiderfyOnMaxZoom={false} | ||
> | ||
{conflictsByType[conflictType].map((marker) => ( | ||
<CircleMarker | ||
radius={3} | ||
color="white" | ||
fillColor={getTailwindColor(`--nextui-${ConflictOperations.getMarkerColor(conflictType)}`)} | ||
weight={1} | ||
fillOpacity={1} | ||
key={marker.geometry.coordinates.toString()} | ||
center={GeometryOperations.swapCoords(marker.geometry.coordinates)} | ||
/> | ||
))} | ||
</MarkerClusterGroup> | ||
))} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const MAP_MAX_ZOOM = 8; | ||
export const MAP_MIN_ZOOM = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
import { LatLngExpression } from 'leaflet'; | ||
|
||
import { ConflictType } from '@/domain/enums/ConflictType'; | ||
|
||
import { Feature } from '../common/Feature'; | ||
|
||
export type Conflict = Feature<{ | ||
count: number; | ||
event_type: ConflictType; | ||
}>; | ||
export type Conflict = Feature< | ||
{ | ||
count: number; | ||
event_type: ConflictType; | ||
}, | ||
LatLngExpression | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ConflictType } from '@/domain/enums/ConflictType'; | ||
|
||
import { Conflict } from './Conflict'; | ||
|
||
export type ConflictTypeMap = { [K in ConflictType]: Conflict[] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { LatLngExpression } from 'leaflet'; | ||
|
||
import { Geometry } from './Geometry'; | ||
|
||
export interface Feature<T> { | ||
export interface Feature<T, U = LatLngExpression[][][]> { | ||
type: string; | ||
geometry: Geometry; | ||
geometry: Geometry<U>; | ||
properties: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
import { LatLngExpression } from 'leaflet'; | ||
|
||
export interface Geometry { | ||
export interface Geometry<T> { | ||
type: string; | ||
coordinates: LatLngExpression[][][]; // Maybe a common type is not best idea here, the coordinate arrays seem to have different dephts. | ||
coordinates: T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
export enum AlertType { | ||
HUNGER = 'hunger', | ||
CONFLICTS = 'conflicts', | ||
CONFLICT1 = 'conflict1', | ||
CONFLICT2 = 'conflict2', | ||
HAZARDS = 'hazards', | ||
COVID19 = 'covid19', | ||
FLOODS = 'floods', | ||
DROUGHTS = 'droughts', | ||
EARTHQUAKES = 'earthquakes', | ||
CYCLONES = 'cyclones', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import L, { MarkerCluster } from 'leaflet'; | ||
|
||
import { Conflict } from '@/domain/entities/alerts/Conflict'; | ||
import { ConflictTypeMap } from '@/domain/entities/alerts/ConflictTypeMap'; | ||
import { ConflictType } from '@/domain/enums/ConflictType'; | ||
import PointLegendContainerItem from '@/domain/props/PointLegendContainerItem'; | ||
|
||
export default class ConflictOperations { | ||
static getMarkerColor(conflictType: ConflictType): string { | ||
switch (conflictType) { | ||
case ConflictType.PROTESTS: | ||
return 'conflictProtest'; | ||
case ConflictType.RIOTS: | ||
return 'conflictRiot'; | ||
case ConflictType.BATTLES: | ||
return 'conflictBattle'; | ||
case ConflictType.CIVIL_VIOLENCE: | ||
return 'conflictCivil'; | ||
case ConflictType.EXPLOSIONS: | ||
return 'conflictExplosion'; | ||
default: | ||
return 'conflictStrategic'; | ||
} | ||
} | ||
|
||
static sortConflictsByType(data?: Conflict[]): ConflictTypeMap { | ||
const result: ConflictTypeMap = { | ||
[ConflictType.BATTLES]: [], | ||
[ConflictType.PROTESTS]: [], | ||
[ConflictType.RIOTS]: [], | ||
[ConflictType.CIVIL_VIOLENCE]: [], | ||
[ConflictType.EXPLOSIONS]: [], | ||
[ConflictType.STRATEGIC]: [], | ||
}; | ||
data?.forEach((conflict) => { | ||
result[conflict.properties.event_type].push(conflict); | ||
}); | ||
return result; | ||
} | ||
|
||
static createClusterCustomIcon(cluster: MarkerCluster, conflictType: ConflictType): L.DivIcon { | ||
return L.divIcon({ | ||
html: `<span | ||
style=" | ||
width: ${Math.min(Math.floor(cluster.getChildCount() / 5) + 20, 40)}px; | ||
height: ${Math.min(Math.floor(cluster.getChildCount() / 5) + 20, 40)}px; | ||
" | ||
class="bg-${ConflictOperations.getMarkerColor(conflictType)} flex items-center justify-center rounded-full border-white border-1 text-white font-bold" | ||
>${cluster.getChildCount()}</span>`, | ||
className: '', | ||
iconSize: L.point(40, 40, true), | ||
}); | ||
} | ||
|
||
static generateConflictLegend(): PointLegendContainerItem[] { | ||
return [ | ||
{ | ||
title: 'Types of conflict', | ||
tooltipInfo: | ||
'All reported violence and conflicts across Africa, the Middle East, South and South East Asia, Eastern and Southeastern Europe and the Balkans.', | ||
records: [ | ||
{ label: ConflictType.BATTLES, color: 'conflictBattle' }, | ||
{ label: ConflictType.CIVIL_VIOLENCE, color: 'conflictCivil' }, | ||
{ label: ConflictType.EXPLOSIONS, color: 'conflictExplosion' }, | ||
{ label: ConflictType.RIOTS, color: 'conflictRiot' }, | ||
{ label: ConflictType.PROTESTS, color: 'conflictProtest' }, | ||
{ label: ConflictType.STRATEGIC, color: 'conflictStrategic' }, | ||
], | ||
}, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { LatLngExpression, LatLngTuple } from 'leaflet'; | ||
|
||
export default class GeometryOperations { | ||
static swapCoords(coords: LatLngExpression): LatLngExpression { | ||
if (!GeometryOperations.isLatLngTuple(coords)) { | ||
throw Error('Invlaid coordinate array'); | ||
} | ||
|
||
return [coords[1], coords[0]]; | ||
} | ||
|
||
static isLatLngTuple(value: LatLngExpression): value is LatLngTuple { | ||
return Array.isArray(value) && value.length === 2 && typeof value[0] === 'number' && typeof value[1] === 'number'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.