# Issue #381
* adds another validity check to SimpleGeomForm (is_size_valid) to make sure the area of the entered geometry is somehow rational (>= 1m²) * optimizes performance of django command sanitize_db * extends Geometry model with two new attributes, holding timestamps when a parcel calculation has been started and ended * finally drops unused update_parcel_wfs in favor of update_parcels in Geometry model * refactors update_parcel method * adds geometry buffer fallback in schneider/fetcher.py to avoid emptying of geometries when parcels shall be fetched * finally removes utils/wfs/spatial.py * extends GeomParcelsView according to #381 * updates translations * removes redundant psycopg2-binary requirement
This commit is contained in:
@@ -14,13 +14,24 @@ from django.utils import timezone
|
||||
from konova.models import BaseResource, UuidModel
|
||||
from konova.sub_settings.lanis_settings import DEFAULT_SRID_RLP
|
||||
from konova.utils.schneider.fetcher import ParcelFetcher
|
||||
from konova.utils.wfs.spatial import ParcelWFSFetcher
|
||||
|
||||
|
||||
class Geometry(BaseResource):
|
||||
"""
|
||||
Geometry model
|
||||
"""
|
||||
parcel_update_start = models.DateTimeField(
|
||||
blank=True,
|
||||
null=True,
|
||||
db_comment="When the last parcel calculation started",
|
||||
help_text="When the last parcel calculation started"
|
||||
)
|
||||
parcel_update_end = models.DateTimeField(
|
||||
blank=True,
|
||||
null=True,
|
||||
db_comment="When the last parcel calculation finished",
|
||||
help_text="When the last parcel calculation finished",
|
||||
)
|
||||
geom = MultiPolygonField(null=True, blank=True, srid=DEFAULT_SRID_RLP)
|
||||
|
||||
def __str__(self):
|
||||
@@ -109,82 +120,14 @@ class Geometry(BaseResource):
|
||||
objs += set_objs
|
||||
return objs
|
||||
|
||||
@transaction.atomic
|
||||
def update_parcels_wfs(self):
|
||||
""" Updates underlying parcel information using the WFS of LVermGeo
|
||||
|
||||
Returns:
|
||||
|
||||
def get_data_object(self):
|
||||
"""
|
||||
from konova.models import Parcel, District, ParcelIntersection, Municipal, ParcelGroup
|
||||
|
||||
if self.geom.empty:
|
||||
# Nothing to do
|
||||
return
|
||||
|
||||
parcel_fetcher = ParcelWFSFetcher(
|
||||
geometry_id=self.id,
|
||||
)
|
||||
typename = "ave:Flurstueck"
|
||||
fetched_parcels = parcel_fetcher.get_features(
|
||||
typename
|
||||
)
|
||||
_now = timezone.now()
|
||||
underlying_parcels = []
|
||||
for result in fetched_parcels:
|
||||
parcel_properties = result["properties"]
|
||||
# There could be parcels which include the word 'Flur',
|
||||
# which needs to be deleted and just keep the numerical values
|
||||
## THIS CAN BE REMOVED IN THE FUTURE, WHEN 'Flur' WON'T OCCUR ANYMORE!
|
||||
flr_val = parcel_properties["flur"].replace("Flur ", "")
|
||||
district = District.objects.get_or_create(
|
||||
key=parcel_properties["kreisschl"],
|
||||
name=parcel_properties["kreis"],
|
||||
)[0]
|
||||
municipal = Municipal.objects.get_or_create(
|
||||
key=parcel_properties["gmdschl"],
|
||||
name=parcel_properties["gemeinde"],
|
||||
district=district,
|
||||
)[0]
|
||||
parcel_group = ParcelGroup.objects.get_or_create(
|
||||
key=parcel_properties["gemaschl"],
|
||||
name=parcel_properties["gemarkung"],
|
||||
municipal=municipal,
|
||||
)[0]
|
||||
flrstck_nnr = parcel_properties['flstnrnen']
|
||||
if not flrstck_nnr:
|
||||
flrstck_nnr = None
|
||||
flrstck_zhlr = parcel_properties['flstnrzae']
|
||||
if not flrstck_zhlr:
|
||||
flrstck_zhlr = None
|
||||
parcel_obj = Parcel.objects.get_or_create(
|
||||
district=district,
|
||||
municipal=municipal,
|
||||
parcel_group=parcel_group,
|
||||
flr=flr_val,
|
||||
flrstck_nnr=flrstck_nnr,
|
||||
flrstck_zhlr=flrstck_zhlr,
|
||||
)[0]
|
||||
parcel_obj.district = district
|
||||
parcel_obj.updated_on = _now
|
||||
parcel_obj.save()
|
||||
underlying_parcels.append(parcel_obj)
|
||||
|
||||
# Update the linked parcels
|
||||
self.parcels.clear()
|
||||
self.parcels.set(underlying_parcels)
|
||||
|
||||
# Set the calculated_on intermediate field, so this related data will be found on lookups
|
||||
intersections_without_ts = self.parcelintersection_set.filter(
|
||||
parcel__in=self.parcels.all(),
|
||||
calculated_on__isnull=True,
|
||||
)
|
||||
for entry in intersections_without_ts:
|
||||
entry.calculated_on = _now
|
||||
ParcelIntersection.objects.bulk_update(
|
||||
intersections_without_ts,
|
||||
["calculated_on"]
|
||||
)
|
||||
Getter for the specific data object which is related to this geometry
|
||||
"""
|
||||
objs = self.get_data_objects()
|
||||
assert (len(objs) <= 1)
|
||||
result = objs.pop()
|
||||
return result
|
||||
|
||||
def update_parcels(self):
|
||||
""" Updates underlying parcel information
|
||||
@@ -192,55 +135,63 @@ class Geometry(BaseResource):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
from konova.models import Parcel, District, ParcelIntersection, Municipal, ParcelGroup
|
||||
|
||||
if self.geom.empty:
|
||||
# Nothing to do
|
||||
return
|
||||
|
||||
self._set_parcel_update_start_time()
|
||||
self._perform_parcel_update()
|
||||
self._set_parcel_update_end_time()
|
||||
|
||||
def _perform_parcel_update(self):
|
||||
"""
|
||||
Performs the main logic of parcel updating.
|
||||
"""
|
||||
from konova.models import Parcel, District, ParcelIntersection, Municipal, ParcelGroup
|
||||
|
||||
parcel_fetcher = ParcelFetcher(
|
||||
geometry=self
|
||||
)
|
||||
fetched_parcels = parcel_fetcher.get_parcels()
|
||||
|
||||
_now = timezone.now()
|
||||
underlying_parcels = []
|
||||
for result in fetched_parcels:
|
||||
# There could be parcels which include the word 'Flur',
|
||||
# which needs to be deleted and just keep the numerical values
|
||||
## THIS CAN BE REMOVED IN THE FUTURE, WHEN 'Flur' WON'T OCCUR ANYMORE!
|
||||
flr_val = result["flur"].replace("Flur ", "")
|
||||
district = District.objects.get_or_create(
|
||||
key=result["kreisschl"],
|
||||
name=result["kreis"],
|
||||
)[0]
|
||||
municipal = Municipal.objects.get_or_create(
|
||||
key=result["gmdschl"],
|
||||
name=result["gemeinde"],
|
||||
district=district,
|
||||
)[0]
|
||||
parcel_group = ParcelGroup.objects.get_or_create(
|
||||
key=result["gemaschl"],
|
||||
name=result["gemarkung"],
|
||||
municipal=municipal,
|
||||
)[0]
|
||||
flrstck_nnr = result['flstnrnen']
|
||||
if not flrstck_nnr:
|
||||
flrstck_nnr = None
|
||||
flrstck_zhlr = result['flstnrzae']
|
||||
if not flrstck_zhlr:
|
||||
flrstck_zhlr = None
|
||||
parcel_obj = Parcel.objects.get_or_create(
|
||||
district=district,
|
||||
municipal=municipal,
|
||||
parcel_group=parcel_group,
|
||||
flr=flr_val,
|
||||
flrstck_nnr=flrstck_nnr,
|
||||
flrstck_zhlr=flrstck_zhlr,
|
||||
)[0]
|
||||
parcel_obj.district = district
|
||||
parcel_obj.updated_on = _now
|
||||
parcel_obj.save()
|
||||
with transaction.atomic():
|
||||
# There could be parcels which include the word 'Flur',
|
||||
# which needs to be deleted and just keep the numerical values
|
||||
## THIS CAN BE REMOVED IN THE FUTURE, WHEN 'Flur' WON'T OCCUR ANYMORE!
|
||||
flr_val = result["flur"].replace("Flur ", "")
|
||||
district = District.objects.get_or_create(
|
||||
key=result["kreisschl"],
|
||||
name=result["kreis"],
|
||||
)[0]
|
||||
municipal = Municipal.objects.get_or_create(
|
||||
key=result["gmdschl"],
|
||||
name=result["gemeinde"],
|
||||
district=district,
|
||||
)[0]
|
||||
parcel_group = ParcelGroup.objects.get_or_create(
|
||||
key=result["gemaschl"],
|
||||
name=result["gemarkung"],
|
||||
municipal=municipal,
|
||||
)[0]
|
||||
flrstck_nnr = result['flstnrnen']
|
||||
if not flrstck_nnr:
|
||||
flrstck_nnr = None
|
||||
flrstck_zhlr = result['flstnrzae']
|
||||
if not flrstck_zhlr:
|
||||
flrstck_zhlr = None
|
||||
parcel_obj = Parcel.objects.get_or_create(
|
||||
district=district,
|
||||
municipal=municipal,
|
||||
parcel_group=parcel_group,
|
||||
flr=flr_val,
|
||||
flrstck_nnr=flrstck_nnr,
|
||||
flrstck_zhlr=flrstck_zhlr,
|
||||
)[0]
|
||||
parcel_obj.district = district
|
||||
parcel_obj.updated_on = _now
|
||||
parcel_obj.save()
|
||||
underlying_parcels.append(parcel_obj)
|
||||
|
||||
# Update the linked parcels
|
||||
@@ -259,6 +210,23 @@ class Geometry(BaseResource):
|
||||
["calculated_on"]
|
||||
)
|
||||
|
||||
@transaction.atomic
|
||||
def _set_parcel_update_start_time(self):
|
||||
"""
|
||||
Sets the current time for the parcel calculation begin
|
||||
"""
|
||||
self.parcel_update_start = timezone.now()
|
||||
self.parcel_update_end = None
|
||||
self.save()
|
||||
|
||||
@transaction.atomic
|
||||
def _set_parcel_update_end_time(self):
|
||||
"""
|
||||
Sets the current time for the parcel calculation end
|
||||
"""
|
||||
self.parcel_update_end = timezone.now()
|
||||
self.save()
|
||||
|
||||
def get_underlying_parcels(self):
|
||||
""" Getter for related parcels and their districts
|
||||
|
||||
|
||||
Reference in New Issue
Block a user