Browse Source

comments and structure

master
Walter Hupfeld 2 years ago
parent
commit
bab4f36431
  1. 140
      backend/calculation.py
  2. 129
      backend/models.py
  3. 20
      backend/views.py
  4. 13969
      data/12411-01-01-5.csv
  5. 3
      frontend/views.py

140
backend/calculation.py

@ -0,0 +1,140 @@
""" Klasse Calc - Berechnung aller Werte für den Klimaplan
benutzt die Tabellen County und Factsheet
"""
import datetime
import math
from .models import Factsheet
from .models import County
class Calc:
""" Klasse zur Berechnung der Werte für Orte und Zieljahr der Klimaneutralität """
def __init__(self, gemeindeId="05915", targetyear=2030):
self.gemeindeId=gemeindeId
self.zieljahr=targetyear
def get(self,code):
__model=Factsheet
return __model.objects.get(code=code).value
def round(self, number):
""" Rundungsfunktion """
if number>1e6:
t1 = ("%s%s" % ( round(number/1e6,2) , " Mio"))
return t1.replace(".",",")+"."
elif number>1000:
t1 = math.trunc(number/1000)
t2 = round(number-(t1*1000))
return ("%s.%s" % ( t1, t2))
elif number>0:
return round(number)
elif number<0:
return "-"+self.round(-number)
else:
return number
def calculate(self):
now = datetime.datetime.now()
kalkulationszeitraum = self.zieljahr-now.year
#Debugging: print(self.gemeindeId)
populationDE = County.objects.get(gemeindeId="DG").Anzahl
populationDistrict = County.objects.get(gemeindeId=self.gemeindeId).Anzahl
nameDistrict = County.objects.get(gemeindeId=self.gemeindeId).Name
nameShort = nameDistrict.split(",")[0]
budgetKommune2016 = self.get('CO2bud2016') * populationDistrict / self.get('population_world')
# Verteilung auf die Sektoren
bilanzEW = self.get('CO2e2018_ew') * populationDistrict / populationDE
bilanzGB = self.get('CO2e2018_gb') * populationDistrict / populationDE
bilanzVK = self.get('CO2e2018_vk') * populationDistrict / populationDE
bilanzLW = self.get('CO2e2018_lw') * populationDistrict / populationDE
bilanzSO = self.get('CO2e2018_so') * populationDistrict / populationDE
bilanzIN = self.get('CO2e2018_in') * populationDistrict / populationDE
bilanzGES = bilanzEW + bilanzGB + bilanzVK + bilanzLW + bilanzSO + bilanzIN
# Pfad für die Emission der Kommune
emissionKomm = {
"2016" : bilanzGES * self.get('CO2emissionDE2016') / self.get('CO2emissionDE2018'),
"2017" : bilanzGES * self.get('CO2emissionDE2017') / self.get('CO2emissionDE2018'),
"2018" : bilanzGES,
"2019" : bilanzGES * self.get('CO2emissionDE2019') / self.get('CO2emissionDE2018'),
"2020" : bilanzGES * self.get('CO2emissionDE2020') / self.get('CO2emissionDE2018'),
"2021" : bilanzGES * self.get('CO2emissionDE2021') / self.get('CO2emissionDE2018'),
}
for year in range(2022, self.zieljahr+2):
yearStr = str(year)
emissionKomm[yearStr] = emissionKomm["2021"]-emissionKomm["2021"]/(kalkulationszeitraum+1)*(year-2021)
gesamtEmission = 0
for value in emissionKomm:
gesamtEmission = gesamtEmission + emissionKomm[value]
# Übergabe nicht gerundeter Werte für die Erzeugung von Grafiken
emissionKommData = {}
for value in emissionKomm:
emissionKommData[value] = round(emissionKomm[value])
# Übergabe gerundeter Werte für die Darstellung im Test
for value in emissionKomm:
emissionKomm[value] = self.round(emissionKomm[value])
# Kosten
kostenGES = self.get('kosten_gesamt')*populationDistrict
kostenAntKommune = self.get('kosten_komm')*populationDistrict
kostenProJahr = kostenGES/kalkulationszeitraum
kostenAnteilKommuneJahr = kostenAntKommune/kalkulationszeitraum
# Arbeitsplätze
arbeitsplGES = self.get('arbeitsplaetze')*populationDistrict/1000000
arbeitsplAntKommune = self.get('arbeitsplaetze_komm')*populationDistrict/1000000
context = {
'populationDistrict' : self.round(populationDistrict),
'nameDistrict' : nameDistrict,
'nameShort' : nameShort,
'gemeindeId' : self.gemeindeId,
'zieljahr' : self.zieljahr,
'kalkulationszeitraum' : kalkulationszeitraum,
'budgetKommune2016': self.round(budgetKommune2016),
'gesamtBisKN': self.round(gesamtEmission),
'verbleibendesBudget': self.round(budgetKommune2016 - gesamtEmission),
'emissionKommune': emissionKomm,
'emissionKommuneData': emissionKommData,
'bilanzGES' :self.round(bilanzGES),
'bilanzEW' :self.round(bilanzEW),
'bilanzGB' :self.round(bilanzGB),
'bilanzVK' :self.round(bilanzVK),
'bilanzLW' :self.round(bilanzLW),
'bilanzSO' :self.round(bilanzSO),
'bilanzIN' :self.round(bilanzIN),
'bilanzGESdata' :bilanzGES,
'bilanzEWdata' :bilanzEW,
'bilanzGBdata' :bilanzGB,
'bilanzVKdata' :bilanzVK,
'bilanzLWdata' :bilanzLW,
'bilanzSOdata' :bilanzSO,
'bilanzINdata' :bilanzIN,
'kostenGES' : self.round(kostenGES),
'kostenAntKommune' : self.round(kostenAntKommune),
'kostenProJahr' : self.round(kostenProJahr),
'kostenAnteilKommuneJahr' : self.round(kostenAnteilKommuneJahr),
'arbeitsplGES': self.round(arbeitsplGES),
'arbeitsplAntKommune':self.round(arbeitsplAntKommune),
}
return context

129
backend/models.py

@ -1,7 +1,6 @@
from django.db import models
from django.urls import reverse
import datetime
import math
# Create your models here.
@ -58,129 +57,3 @@ class Factsheet(models.Model):
class Calc:
""" Klasse zur Berechnung der Werte für Orte und Zieljahr der Klimaneutralität """
def __init__(self, gemeindeId="05915", targetyear=2030):
self.gemeindeId=gemeindeId
self.zieljahr=targetyear
def get(self,code):
model=Factsheet
return model.objects.get(code=code).value
def round(self, number):
""" Rundungsfunktion """
if number>1e6:
t1 = ("%s%s" % ( round(number/1e6,2) , " Mio"))
return t1.replace(".",",")+"."
elif number>1000:
t1 = math.trunc(number/1000)
t2 = round(number-(t1*1000))
return ("%s.%s" % ( t1, t2))
elif number>0:
return round(number)
elif number<0:
return "-"+self.round(-number)
else:
return number
def calculate(self):
now = datetime.datetime.now()
kalkulationszeitraum = self.zieljahr-now.year
print(self.gemeindeId)
populationDE = County.objects.get(gemeindeId="DG").Anzahl
populationDistrict = County.objects.get(gemeindeId=self.gemeindeId).Anzahl
nameDistrict = County.objects.get(gemeindeId=self.gemeindeId).Name
nameShort = nameDistrict.split(",")[0]
budgetKommune2016 = self.get('CO2bud2016') * populationDistrict / self.get('population_world')
bilanzEW = self.get('CO2e2018_ew') * populationDistrict / populationDE
bilanzGB = self.get('CO2e2018_gb') * populationDistrict / populationDE
bilanzVK = self.get('CO2e2018_vk') * populationDistrict / populationDE
bilanzLW = self.get('CO2e2018_lw') * populationDistrict / populationDE
bilanzSO = self.get('CO2e2018_so') * populationDistrict / populationDE
bilanzIN = self.get('CO2e2018_in') * populationDistrict / populationDE
bilanzGES = bilanzEW + bilanzGB + bilanzVK + bilanzLW + bilanzSO + bilanzIN
emissionKomm = {
"2016" : bilanzGES * self.get('CO2emissionDE2016') / self.get('CO2emissionDE2018'),
"2017" : bilanzGES * self.get('CO2emissionDE2017') / self.get('CO2emissionDE2018'),
"2018" : bilanzGES,
"2019" : bilanzGES * self.get('CO2emissionDE2019') / self.get('CO2emissionDE2018'),
"2020" : bilanzGES * self.get('CO2emissionDE2020') / self.get('CO2emissionDE2018'),
"2021" : bilanzGES * self.get('CO2emissionDE2021') / self.get('CO2emissionDE2018'),
}
for year in range(2022, self.zieljahr+2):
yearStr = str(year)
emissionKomm[yearStr] = emissionKomm["2021"]-emissionKomm["2021"]/(kalkulationszeitraum+1)*(year-2021)
gesamtEmission = 0
for value in emissionKomm:
gesamtEmission = gesamtEmission + emissionKomm[value]
kostenGES = self.get('kosten_gesamt')*populationDistrict
kostenAntKommune = self.get('kosten_komm')*populationDistrict
kostenProJahr = kostenGES/kalkulationszeitraum
kostenAnteilKommuneJahr = kostenAntKommune/kalkulationszeitraum
arbeitsplGES = self.get('arbeitsplaetze')*populationDistrict/1000000
arbeitsplAntKommune = self.get('arbeitsplaetze_komm')*populationDistrict/1000000
emissionKommData = {}
for value in emissionKomm:
emissionKommData[value] = round(emissionKomm[value])
for value in emissionKomm:
emissionKomm[value] = self.round(emissionKomm[value])
context = {
'populationDistrict' : self.round(populationDistrict),
'nameDistrict' : nameDistrict,
'nameShort' : nameShort,
'gemeindeId' : self.gemeindeId,
'zieljahr' : self.zieljahr,
'kalkulationszeitraum' : kalkulationszeitraum,
'budgetKommune2016': self.round(budgetKommune2016),
'gesamtBisKN': self.round(gesamtEmission),
'verbleibendesBudget': self.round(budgetKommune2016 - gesamtEmission),
'emissionKommune': emissionKomm,
'emissionKommuneData': emissionKommData,
'bilanzGES' :self.round(bilanzGES),
'bilanzEW' :self.round(bilanzEW),
'bilanzGB' :self.round(bilanzGB),
'bilanzVK' :self.round(bilanzVK),
'bilanzLW' :self.round(bilanzLW),
'bilanzSO' :self.round(bilanzSO),
'bilanzIN' :self.round(bilanzIN),
'bilanzGESdata' :bilanzGES,
'bilanzEWdata' :bilanzEW,
'bilanzGBdata' :bilanzGB,
'bilanzVKdata' :bilanzVK,
'bilanzLWdata' :bilanzLW,
'bilanzSOdata' :bilanzSO,
'bilanzINdata' :bilanzIN,
'kostenGES' : self.round(kostenGES),
'kostenAntKommune' : self.round(kostenAntKommune),
'kostenProJahr' : self.round(kostenProJahr),
'kostenAnteilKommuneJahr' : self.round(kostenAnteilKommuneJahr),
'arbeitsplGES': self.round(arbeitsplGES),
'arbeitsplAntKommune':self.round(arbeitsplAntKommune),
}
return context

20
backend/views.py

@ -17,7 +17,7 @@ from django.db.models import Q
from .models import County
from .models import Factsheet
from .models import Calc
from .calculation import Calc
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
@ -111,17 +111,24 @@ def details(request,id):
# Create your views here.# one parameter named request
@login_required
def population_upload(request): # declaring template
""" Upload der CSV-Datei """
""" Upload der CSV-Datei für die Einwohnerzahlen
funktionionert nicht fehlerfrei ohne vorherige Anpassung der CSV-Datei.
Evtl. müssen Punkte aus den Ortsbezeichnungen entfernt werden.
Quelle der CSV: www.gemeindezahlen.de ???
"""
template = "backend/counties_upload.html"
startline=6
data = County.objects.all()# prompt is a context variable that can have different values depending on their context
startline=6 # Starte ab Zeile 6
data = County.objects.all()
# prompt is a context variable that can have different values depending on their context
prompt = {
'order': 'Reihenfolge der CSV sollte seien: gemeindeId, Name, Anzahl, männlich, weiblich',
'profiles': data
}
# GET request returns the value of the data with the specified key.
if request.method == "GET":
return render(request, template, prompt)
return render(request, template, prompt)
# CSV lesen
csv_file = request.FILES['file']
if not csv_file.name.endswith('.csv'):
messages.error(request, 'Das ist keine CSV-Datei')
@ -129,8 +136,9 @@ def population_upload(request): # declaring template
io_string = io.StringIO(data_set)
next(io_string)
line_count=0
County.objects.all().delete()
County.objects.all().delete() # Delete table
for column in csv.reader(io_string, delimiter=';', quotechar="|"):
#print (column)
if line_count>startline:

13969
data/12411-01-01-5.csv

File diff suppressed because it is too large

3
frontend/views.py

@ -1,7 +1,8 @@
from django.shortcuts import render
from backend.models import Calc
from backend.models import County
from backend.calculation import Calc
from django.db.models import Q
from django.http import JsonResponse

Loading…
Cancel
Save