#132 WIP Old entries
* WIP: implements raw migration from ksp to konova
This commit is contained in:
parent
caa840849e
commit
228333d509
79
konova/management/commands/migrate_ksp_konova.py
Normal file
79
konova/management/commands/migrate_ksp_konova.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import psycopg2
|
||||||
|
from django.contrib.gis.geos import MultiPolygon, GEOSException, MultiPoint, MultiLineString, Polygon
|
||||||
|
|
||||||
|
from intervention.models import Intervention, Legal, Responsibility
|
||||||
|
from konova.management.commands.setup import BaseKonovaCommand
|
||||||
|
from konova.models import Geometry
|
||||||
|
from konova.tasks import celery_update_parcels
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseKonovaCommand):
|
||||||
|
help = "Migrates from KSP database to Konova"
|
||||||
|
db_connection = None
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('--host', type=str)
|
||||||
|
parser.add_argument('--db-user', type=str)
|
||||||
|
parser.add_argument('--db-pw', type=str)
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
try:
|
||||||
|
self.connect_db(options)
|
||||||
|
self.migrate_interventions()
|
||||||
|
|
||||||
|
if self.db_connection is not None:
|
||||||
|
self.db_connection.close()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self._break_line()
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
def connect_db(self, options):
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
host=options["host"],
|
||||||
|
database="ksp",
|
||||||
|
user=options["db_user"],
|
||||||
|
password=options["db_pw"],
|
||||||
|
)
|
||||||
|
self._write_success("Connected to ksp db...")
|
||||||
|
self.db_connection = conn
|
||||||
|
|
||||||
|
def migrate_interventions(self):
|
||||||
|
cursor = self.db_connection.cursor()
|
||||||
|
cursor.execute(
|
||||||
|
'select om."KENNUNG", linf."OBJBEZ", ST_AsEWKT(ST_Transform(ST_MakeValid(geomf.the_geom),4326)) as geomf, ST_AsEWKT(ST_Transform(ST_MakeValid(geoml.the_geom),4326)) as geoml, ST_AsEWKT(ST_Transform(ST_MakeValid(geomp.the_geom),4326)) as geomp from "OBJ_MASTER" om left join "LINFOS" linf on om."GISPADID"=linf."GISPADID" left join geometry_f geomf on om."GISPADID"=geomf.gispadid left join geometry_l geoml on om."GISPADID"=geoml.gispadid left join geometry_p geomp on om."GISPADID"=geomp.gispadid where om."OKL"=7730085 and om.archiv=false;'
|
||||||
|
)
|
||||||
|
all_eivs = cursor.fetchall()
|
||||||
|
for eiv in all_eivs:
|
||||||
|
intervention = Intervention.objects.get_or_create(
|
||||||
|
identifier=eiv[0]
|
||||||
|
)[0]
|
||||||
|
intervention.title = eiv[1]
|
||||||
|
try:
|
||||||
|
# eiv[2] == polygon, eiv[3] == line, eiv[4] == point
|
||||||
|
if eiv[2] is not None:
|
||||||
|
eiv_geom = MultiPolygon.from_ewkt(eiv[2])
|
||||||
|
elif eiv[3] is not None:
|
||||||
|
eiv_geom = MultiPoint.from_ewkt(eiv[3])
|
||||||
|
eiv_geom = eiv_geom.buffer(0.00001, 1)
|
||||||
|
if isinstance(eiv_geom, Polygon):
|
||||||
|
eiv_geom = MultiPolygon(eiv_geom)
|
||||||
|
elif eiv[4] is not None:
|
||||||
|
eiv_geom = MultiLineString.from_ewkt(eiv[4])
|
||||||
|
eiv_geom = eiv_geom.buffer(0.00001, 1)
|
||||||
|
if isinstance(eiv_geom, Polygon):
|
||||||
|
eiv_geom = MultiPolygon(eiv_geom)
|
||||||
|
else:
|
||||||
|
eiv_geom = None
|
||||||
|
except GEOSException:
|
||||||
|
self._write_error(f"Malicious geometry on {eiv}")
|
||||||
|
return
|
||||||
|
geom = intervention.geometry or Geometry()
|
||||||
|
geom.geom = eiv_geom
|
||||||
|
geom.save()
|
||||||
|
#celery_update_parcels.delay(geom.id)
|
||||||
|
|
||||||
|
intervention.geometry = geom
|
||||||
|
intervention.legal = intervention.legal or Legal.objects.create()
|
||||||
|
intervention.responsible = intervention.responsible or Responsibility.objects.create()
|
||||||
|
intervention.save()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user