Files
konova/konova/utils/schneider/fetcher.py
T
mpeltriaux 4c7c667bdb # Error fetching
* adds proper fetching of GDAL Exceptions in case of further problems in the future
2026-09-13 11:36:28 +02:00

71 lines
2.2 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 14.12.22
"""
import json
from json import JSONDecodeError
import requests
from django.contrib.gis import gdal
from konova.sub_settings import schneider_settings
from konova.sub_settings.lanis_settings import DEFAULT_SRID
from konova.sub_settings.proxy_settings import PROXIES
class ParcelFetcher:
base_url = schneider_settings.base_url
auth_header = schneider_settings.auth_header
auth_header_token = schneider_settings.auth_header_token
geometry = None
geojson = None
results = None
def __init__(self, geometry):
if geometry is None:
raise AssertionError("Geometry must not be none")
self.geometry = geometry
# Reduce size of geometry to avoid "intersections" because of exact border matching
buffer_threshold = 0.001
geom = geometry.geom.buffer(-buffer_threshold)
if geom.area < buffer_threshold:
# Fallback for malicious geometries which are way too small and would disappear on negative buffering
geom = geometry.geom
try:
geom.transform(DEFAULT_SRID)
except gdal.GDALException as e:
raise ValueError(f"{str(e)}\nCould not transform geometry (id: {geometry.id}) to {DEFAULT_SRID}:\n {geom.geojson}")
self.geojson = geom.ewkt
self.results = []
def get_parcels(self, url: str = None):
post_url = url
if post_url is None:
post_url = f"{self.base_url}/parcel/intersect/"
response = requests.post(
url=post_url,
proxies=PROXIES,
data=self.geojson,
headers={
self.auth_header: self.auth_header_token
}
)
try:
content = json.loads(response.content.decode("utf-8"))
except JSONDecodeError:
content = {}
_next = content.get("next", None)
fetched_parcels = content.get("results", [])
self.results += fetched_parcels
if _next:
self.get_parcels(_next)
return self.results