# Parcel duplicate repair
* adds mechanic to repair parcels in case of unwanted parcel duplicates * optimizes filtering of geometries for parcel recalculation
This commit is contained in:
@@ -8,11 +8,9 @@ Created on: 04.01.22
|
||||
import datetime
|
||||
|
||||
from django.contrib.gis.db.models.functions import Area
|
||||
from django.db.models import Q
|
||||
from django.utils.timezone import now
|
||||
|
||||
from konova.management.commands.setup import BaseKonovaCommand
|
||||
from konova.models import Geometry
|
||||
from konova.models import Geometry, ParcelIntersection
|
||||
|
||||
|
||||
class Command(BaseKonovaCommand):
|
||||
@@ -36,17 +34,21 @@ class Command(BaseKonovaCommand):
|
||||
def recalculate_parcels(self, options: dict):
|
||||
force_all = options.get("force_all", False)
|
||||
|
||||
if force_all:
|
||||
geometry_objects = Geometry.objects.all()
|
||||
else:
|
||||
_today = now().date()
|
||||
_date_threshold = _today - datetime.timedelta(days=1)
|
||||
geometry_objects = Geometry.objects.filter(
|
||||
Q(
|
||||
Q(parcel_update_start__date__lte=_date_threshold) |
|
||||
Q(parcel_update_start__isnull=True)
|
||||
),
|
||||
parcel_update_end__isnull=True
|
||||
geometry_objects = Geometry.objects.all()
|
||||
|
||||
if not force_all:
|
||||
# Fetch all intersections
|
||||
intersection_objs = ParcelIntersection.objects.filter(
|
||||
geometry__in=geometry_objects
|
||||
)
|
||||
# Just take the geometry ids, which seem to have intersections
|
||||
geom_with_intersection_ids = intersection_objs.values_list(
|
||||
"geometry__id",
|
||||
flat=True
|
||||
)
|
||||
# Filter those geometries out (they have intersections and do not need to be processed)
|
||||
geometry_objects = geometry_objects.exclude(
|
||||
id__in=geom_with_intersection_ids
|
||||
)
|
||||
|
||||
self._write_warning("=== Update parcels and districts ===")
|
||||
|
||||
Reference in New Issue
Block a user