Skip to content
Snippets Groups Projects
Commit 882ae51b authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Enforce polygon minimum size at database level

parent c246f37a
No related branches found
No related tags found
1 merge request!1158Enforce polygon minimum size at database level
# Generated by Django 3.1.3 on 2021-01-04 11:04
from django.db import migrations, models
def remove_small_polygons(apps, schema_editor):
"""
Removes polygons with less than 4 points.
"""
Element = apps.get_model('documents', 'Element')
Zone = apps.get_model('images', 'Zone')
zone_ids = list(Zone.objects.using('default').filter(polygon__memsize__lt=96).values_list('id', flat=True))
if not zone_ids:
return
# Import the ElementQuerySet to access the trash() method
from arkindex.documents.managers import ElementQuerySet
ElementQuerySet(Element).filter(zone_id__in=zone_ids).trash()
Zone.objects.filter(id__in=zone_ids)._raw_delete(using='default')
class Migration(migrations.Migration):
dependencies = [
('images', '0005_polygon_index'),
]
atomic = False
operations = [
migrations.RemoveConstraint(
model_name='zone',
name='zone_polygon_size',
),
migrations.RunPython(
remove_small_polygons,
reverse_code=migrations.RunPython.noop,
elidable=True,
),
migrations.AddConstraint(
model_name='zone',
constraint=models.CheckConstraint(
check=models.Q(polygon__memsize__gte=96, polygon__memsize__lte=2664),
name='zone_polygon_size'
),
),
]
......@@ -379,9 +379,10 @@ class Zone(IndexableModel):
check=models.Q(polygon=SnapToGrid('polygon', 1)),
name='zone_polygon_integer_coordinates',
),
# Restrict to between 4 and 166 points (3+1 points for a triangle, and BTree indexes limit us to 2680 bits)
# Checking against the memory size is faster than checking the number of points
models.CheckConstraint(
check=models.Q(polygon__memsize__lte=2664),
check=models.Q(polygon__memsize__gte=96, polygon__memsize__lte=2664),
name='zone_polygon_size',
)
]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment