#86 Parcel-Geometry improvement
* improves the way parcel-geometry relations are stored on the DB
* instead of a numerical sequence we switched to UUID, so no sequence will run out at anytime (new model: ParcelIntersection)
* instead of dropping all M2M relations between parcel and geometry on each calculation, we keep the ones that still exist, drop the ones that do not exist and add new ones (if new ones exist)
This commit is contained in:
54
konova/migrations/0003_auto_20220208_1801.py
Normal file
54
konova/migrations/0003_auto_20220208_1801.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# Generated by Django 3.1.3 on 2022-02-08 17:01
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
def migrate_parcels(apps, schema_editor):
|
||||
Geometry = apps.get_model('konova', 'Geometry')
|
||||
SpatialIntersection = apps.get_model('konova', 'SpatialIntersection')
|
||||
|
||||
all_geoms = Geometry.objects.all()
|
||||
for geom in all_geoms:
|
||||
SpatialIntersection.objects.bulk_create([
|
||||
SpatialIntersection(geometry=geom, parcel=parcel)
|
||||
for parcel in geom.parcels.all()
|
||||
])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('konova', '0002_auto_20220114_0936'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SpatialIntersection',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('calculated_on', models.DateTimeField(auto_now_add=True, null=True)),
|
||||
('geometry', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='konova.geometry')),
|
||||
('parcel', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='konova.parcel')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.RunPython(migrate_parcels),
|
||||
migrations.AddField(
|
||||
model_name='parcel',
|
||||
name='geometries_tmp',
|
||||
field=models.ManyToManyField(blank=True, related_name='parcels', through='konova.SpatialIntersection', to='konova.Geometry'),
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='parcel',
|
||||
name='geometries',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='parcel',
|
||||
old_name='geometries_tmp',
|
||||
new_name='geometries',
|
||||
),
|
||||
]
|
||||
17
konova/migrations/0004_auto_20220209_0839.py
Normal file
17
konova/migrations/0004_auto_20220209_0839.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 3.1.3 on 2022-02-09 07:39
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('konova', '0003_auto_20220208_1801'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameModel(
|
||||
old_name='SpatialIntersection',
|
||||
new_name='ParcelIntersection',
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user