From 903ab73788ea823fb60b1d59454f008616344298 Mon Sep 17 00:00:00 2001 From: lub Date: Sun, 14 Jan 2024 19:41:20 +0100 Subject: [PATCH] implement municipal election results --- streets.ps1 | 80 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/streets.ps1 b/streets.ps1 index 89d079f..af383a2 100644 --- a/streets.ps1 +++ b/streets.ps1 @@ -1,8 +1,15 @@ -$partyMapping = @{ - ARD = 'Blue' - ZDF = 'Orange' - 'C&A' = 'Red' -} +[CmdletBinding()] +param ( + [string] + $DataSource = './Gesamtergebnis-Gemeinderatswahl-2019_stripped.csv' +) + + +$data = Import-Csv $DataSource + +$greenParty = ($data[0].PSObject.Properties.Name -like '*Grüne*')[0] + +$partyMapping = $data | Where-Object Description -eq 'Color' # nested array instead of hashtable, because hashtable keys are case-insensitive $replacementList = @( @@ -16,13 +23,53 @@ $replacementList = @( ,('Elstarweg ','Elstarweg') ) -$featureList = Import-Csv ./data.csv +$featureList = Import-Csv ./polling_stations.csv | Sort-Object -Unique Strassenname -| Group-Object {$_.Wahllokal1,$_.Wahllokal2,$_.Wahllokal3 -join ' '} +| Group-Object Concat | ForEach-Object { $pollingStation = $_ - $party = ('ARD','ZDF','C&A' | Get-Random) + $electionResultObject = $data | Where-Object Description -eq $pollingStation.Name + if ($electionResultObject) { + # form into an array for easier handling and to preserve sorting + $electionResultArray = $electionResultObject.PSObject.Properties | ForEach-Object { + if ($_.Name -ne 'Description') { + [pscustomobject]@{ + Party = $_.Name + ResultValue = [decimal](($_.Value -eq '-' ? 0 : $_.Value) -replace '%$') + Formatted = $_.Value + } + } + } | Sort-Object -Descending ResultValue + + Write-Host $pollingStation.Name + $electionResultString = ($electionResultArray | ForEach-Object { + $_.Formatted + "`t" + $_.Party + }) -join "`n" + + $greenResult = ($electionResultArray | Where-Object Party -eq $greenParty).ResultValue + + $opacity = ($greenResult - 20) / 5 * 0.6 + 0.4 + $opacity = $opacity -gt 1.0 ? 1.0 : $opacity + $opacity = $opacity -lt 0.4 ? 0.4 : $opacity + + $weight = ($greenResult - 20) / 5 * 10 + 5 + $weight = $weight -gt 15 ? 15 : $weight + $weight = $weight -lt 5 ? 5 : $weight + + $party = $electionResultArray[0].Party + } else { + $electionResultString = 'no data available' + $opacity = 0.5 + $party = $null + } + + $umapOptiones = [PSCustomObject]@{ + #color = ($using:partyMapping).$party + color = $partyMapping.$party ?? '#ff00ff' + weight = $weight + opacity = $opacity + } $pollingStation.Group | ForEach-Object { $street = $_.Strassenname @@ -30,7 +77,11 @@ $featureList = Import-Csv ./data.csv $street = $street.Replace($replacement[0], $replacement[1]) } - $query = ' + $description = $street + $description += "`n`n" + $description += $electionResultString + + $query = @' @@ -44,7 +95,8 @@ $featureList = Import-Csv ./data.csv -' -f $street + +'@ -f $street $overpassSplat = @{ Method = 'Post' @@ -70,13 +122,9 @@ $featureList = Import-Csv ./data.csv [PSCustomObject]@{ type = 'Feature' properties = [PSCustomObject]@{ - _umap_options = [PSCustomObject]@{ - #color = ($using:partyMapping).$party - color = $partyMapping.$party - weight = 10 - } + _umap_options = $umapOptiones name = $pollingStation.Name - description = $street + description = $description } geometry = [PSCustomObject]@{ type = 'LineString'