#132 Compensation states
* adds migration for ksp-KOM states (before+after) * adds migration for CEF/Coherence info
This commit is contained in:
parent
d21baf11f3
commit
8b4104c704
@ -1,7 +1,9 @@
|
|||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
from compensation.models import Compensation
|
from codelist.models import KonovaCode
|
||||||
|
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID
|
||||||
|
from compensation.models import Compensation, CompensationState
|
||||||
from intervention.models import Responsibility, Handler, Intervention
|
from intervention.models import Responsibility, Handler, Intervention
|
||||||
from konova.management.commands.kspMigrater.base_migrater import BaseMigrater
|
from konova.management.commands.kspMigrater.base_migrater import BaseMigrater
|
||||||
|
|
||||||
@ -55,14 +57,18 @@ class CompensationMigrater(BaseMigrater):
|
|||||||
compensation.comment = kom_comment
|
compensation.comment = kom_comment
|
||||||
compensation = self._migrate_geometry(compensation, kom)
|
compensation = self._migrate_geometry(compensation, kom)
|
||||||
compensation = self._migrate_responsibility(compensation, kom)
|
compensation = self._migrate_responsibility(compensation, kom)
|
||||||
|
compensation = self._migrate_compensation_type(compensation, kom)
|
||||||
|
compensation = self._migrate_states(compensation, kom)
|
||||||
try:
|
try:
|
||||||
compensation = self._migrate_interventions_reference(compensation, kom)
|
compensation = self._migrate_interventions_reference(compensation, kom)
|
||||||
compensation.save()
|
compensation.save()
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
|
compensation.delete()
|
||||||
unsuccessfull_compensations[kom_identifier] = "EIV does not exist"
|
unsuccessfull_compensations[kom_identifier] = "EIV does not exist"
|
||||||
print(f"{kom_identifier} does not have any intervention. ({len(unsuccessfull_compensations)} similar problems)")
|
|
||||||
num_processed += 1
|
num_processed += 1
|
||||||
print(f"The following KOMs could not be migrated: {unsuccessfull_compensations}")
|
print("The following KOMs could not be migrated: ")
|
||||||
|
for kom, val in unsuccessfull_compensations:
|
||||||
|
print(kom)
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
def _migrate_interventions_reference(self, compensation, kom):
|
def _migrate_interventions_reference(self, compensation, kom):
|
||||||
@ -96,3 +102,99 @@ class CompensationMigrater(BaseMigrater):
|
|||||||
def _migrate_responsibility(self, compensation, kom):
|
def _migrate_responsibility(self, compensation, kom):
|
||||||
compensation.responsible = compensation.responsible or Responsibility.objects.create()
|
compensation.responsible = compensation.responsible or Responsibility.objects.create()
|
||||||
return compensation
|
return compensation
|
||||||
|
|
||||||
|
def _migrate_states(self, compensation, kom):
|
||||||
|
kom_identifier = f"'{kom[0]}'"
|
||||||
|
before_states_sql = 'select ' \
|
||||||
|
'b."Biotoptyp", ' \
|
||||||
|
'b."Flaeche", ' \
|
||||||
|
'b."PKEY" ' \
|
||||||
|
'from "BtypHtyp" b ' \
|
||||||
|
'left join "OBJ_MASTER" om on om."GISPADID"=b."GISPADID" ' \
|
||||||
|
'where ' \
|
||||||
|
f'om."KENNUNG"={kom_identifier}'
|
||||||
|
|
||||||
|
after_states_sql = 'select ' \
|
||||||
|
'z."Zielbiotoptyp", ' \
|
||||||
|
'z."Flaeche", ' \
|
||||||
|
'z."PKEY" ' \
|
||||||
|
'from "Zielbiotoptypen" z ' \
|
||||||
|
'left join "OBJ_MASTER" om on om."GISPADID"=z."GISPADID" ' \
|
||||||
|
'where ' \
|
||||||
|
f'om."KENNUNG"={kom_identifier}'
|
||||||
|
z_code_sql = 'select z."Zusatzcode" from "Zusatzcodes" z where z."FKEY"={}'
|
||||||
|
|
||||||
|
compensation = self._process_state_migration(compensation, before_states_sql, z_code_sql, compensation.before_states)
|
||||||
|
compensation = self._process_state_migration(compensation, after_states_sql, z_code_sql, compensation.after_states)
|
||||||
|
return compensation
|
||||||
|
|
||||||
|
def _process_state_migration(self, compensation, sql, z_code_sql, state_manager):
|
||||||
|
tmp_cursor = self.db_connection.cursor()
|
||||||
|
tmp_cursor.execute(sql)
|
||||||
|
db_result = tmp_cursor.fetchall()
|
||||||
|
|
||||||
|
for result in db_result:
|
||||||
|
state_type = result[0]
|
||||||
|
state_surface = result[1] or 0.0
|
||||||
|
pkey_entry = f"'{result[2]}'"
|
||||||
|
state_code = KonovaCode.objects.get(
|
||||||
|
atom_id=state_type,
|
||||||
|
code_lists__in=[CODELIST_BIOTOPES_ID]
|
||||||
|
)
|
||||||
|
tmp_cursor_z_code = self.db_connection.cursor()
|
||||||
|
tmp_cursor_z_code.execute(z_code_sql.format(pkey_entry))
|
||||||
|
z_code_results = tmp_cursor_z_code.fetchall()
|
||||||
|
z_codes = []
|
||||||
|
|
||||||
|
for z_code_result in z_code_results:
|
||||||
|
z_code = KonovaCode.objects.filter(
|
||||||
|
atom_id=z_code_result[0],
|
||||||
|
code_lists__in=[CODELIST_BIOTOPES_EXTRA_CODES_ID],
|
||||||
|
)
|
||||||
|
z_codes += z_code
|
||||||
|
tmp_cursor_z_code.close()
|
||||||
|
|
||||||
|
try:
|
||||||
|
_state_obj = state_manager.get(
|
||||||
|
biotope_type=state_code,
|
||||||
|
surface=state_surface
|
||||||
|
)
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
_state_obj = CompensationState.objects.create(
|
||||||
|
biotope_type=state_code,
|
||||||
|
surface=state_surface,
|
||||||
|
)
|
||||||
|
state_manager.add(_state_obj)
|
||||||
|
|
||||||
|
_state_obj.biotope_type_details.set(z_codes)
|
||||||
|
|
||||||
|
tmp_cursor.close()
|
||||||
|
return compensation
|
||||||
|
|
||||||
|
def _migrate_compensation_type(self, compensation, kom):
|
||||||
|
kom_identifier = f"'{kom[0]}'"
|
||||||
|
tmp_cursor = self.db_connection.cursor()
|
||||||
|
tmp_cursor.execute(
|
||||||
|
'select '
|
||||||
|
'mt.typ '
|
||||||
|
'from "OBJ_MASTER" om '
|
||||||
|
'left join massnahmetyp mt on om."GISPADID"=mt.gispadid '
|
||||||
|
'where '
|
||||||
|
f'om."KENNUNG"={kom_identifier}'
|
||||||
|
)
|
||||||
|
db_result = tmp_cursor.fetchall()
|
||||||
|
if len(db_result) != 1:
|
||||||
|
raise AssertionError(f"{kom_identifier} has no specification on compensation type (CEF, ...)")
|
||||||
|
comp_type = db_result[0][0]
|
||||||
|
if comp_type == 705816:
|
||||||
|
# regular compensation, do nothing
|
||||||
|
pass
|
||||||
|
elif comp_type == 705815:
|
||||||
|
compensation.is_cef = True
|
||||||
|
elif comp_type == 705817:
|
||||||
|
compensation.is_coherence_keeping = True
|
||||||
|
elif comp_type == 154156555:
|
||||||
|
compensation.is_coherence_keeping = True
|
||||||
|
compensation.is_cef = True
|
||||||
|
tmp_cursor.close()
|
||||||
|
return compensation
|
Loading…
Reference in New Issue
Block a user