#49 Calculation implementation

* implements update routine for Geometry model
* reorganizes fields of Parcel and District
* adds tests
* simplifies usage of ParcelWFSFetcher
This commit is contained in:
2022-01-04 15:59:53 +01:00
parent b5cf18ac7d
commit 632fb0f48a
5 changed files with 95 additions and 35 deletions

View File

@@ -7,8 +7,10 @@ Created on: 15.11.21
"""
from django.contrib.gis.db.models import MultiPolygonField
from django.db import models
from django.utils import timezone
from konova.models import BaseResource, UuidModel
from konova.utils.wfs.spatial import ParcelWFSFetcher
class Geometry(BaseResource):
@@ -21,7 +23,6 @@ class Geometry(BaseResource):
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.check_for_conflicts()
self.update_parcels()
def check_for_conflicts(self):
""" Checks for new geometry overlaps
@@ -93,8 +94,38 @@ class Geometry(BaseResource):
return objs
def update_parcels(self):
# ToDo
pass
""" Updates underlying parcel information
Returns:
"""
from konova.models import Parcel, District
parcel_fetcher = ParcelWFSFetcher(
geometry_id=self.id,
)
typename = "ave:Flurstueck"
fetched_parcels = parcel_fetcher.get_features(
typename
)
underlying_parcels = []
for result in fetched_parcels:
fetched_parcel = result[typename]
parcel_obj = Parcel.objects.get_or_create(
flr=fetched_parcel["ave:flur"],
flrstck_nnr=fetched_parcel['ave:flstnrnen'],
flrstck_zhlr=fetched_parcel['ave:flstnrzae'],
)[0]
district = District.objects.get_or_create(
gmrkng=fetched_parcel["ave:gemarkung"],
gmnd=fetched_parcel["ave:gemeinde"],
krs=fetched_parcel["ave:kreis"],
)[0]
parcel_obj.district = district
parcel_obj.updated_on = timezone.now()
parcel_obj.save()
underlying_parcels.append(parcel_obj)
self.parcels.set(underlying_parcels)
class GeometryConflict(UuidModel):

View File

@@ -22,28 +22,30 @@ class Parcel(UuidModel):
To avoid conflicts due to german Umlaute, the field names are shortened and vocals are dropped.
"""
geometries = models.ManyToManyField("konova.Geometry", related_name="parcels", null=True, blank=True)
geometries = models.ManyToManyField("konova.Geometry", related_name="parcels", blank=True)
district = models.ForeignKey("konova.District", on_delete=models.SET_NULL, null=True, blank=True, related_name="parcels")
flrstck_nnr = models.CharField(
max_length=1000,
help_text="Flurstücksnenner"
help_text="Flurstücksnenner",
null=True,
blank=True,
)
flrstck_zhlr = models.CharField(
max_length=1000,
help_text="Flurstückszähler"
help_text="Flurstückszähler",
null=True,
blank=True,
)
flr = models.CharField(
max_length=1000,
help_text="Flur"
)
gmrkng = models.CharField(
max_length=1000,
help_text="Gemarkung"
help_text="Flur",
null=True,
blank=True,
)
updated_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.gmrkng} | {self.flr} | {self.flrstck_nnr} | {self.flrstck_zhlr}"
return f"{self.flr} | {self.flrstck_nnr} | {self.flrstck_zhlr}"
class District(UuidModel):
@@ -54,18 +56,24 @@ class District(UuidModel):
District.
"""
gmrkng = models.CharField(
max_length=1000,
help_text="Gemarkung",
null=True,
blank=True,
)
gmnd = models.CharField(
max_length=1000,
help_text="Gemeinde"
)
vg = models.CharField(
max_length=1000,
help_text="Verbandsgemeinde",
help_text="Gemeinde",
null=True,
blank=True,
)
krs = models.CharField(
max_length=1000,
help_text="Kreis"
help_text="Kreis",
null=True,
blank=True,
)
def __str__(self):
return f"{self.krs} | {self.vg} | {self.gmnd}"
return f"{self.gmrkng} | {self.gmnd} | {self.krs}"