From 68442ac9d3c28a0951e405c1e11f3aee788ba2db Mon Sep 17 00:00:00 2001 From: Remy van der Wereld Date: Thu, 18 Jul 2024 10:34:01 +0200 Subject: [PATCH] 116361 --- app/apps/addresses/models.py | 22 +++++++++++++++++++--- app/requirements.txt | 1 + app/utils/api_queries_bag.py | 8 ++++++-- app/utils/coordinates.py | 15 +++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 app/utils/coordinates.py diff --git a/app/apps/addresses/models.py b/app/apps/addresses/models.py index f62aa20cc..4da380d31 100644 --- a/app/apps/addresses/models.py +++ b/app/apps/addresses/models.py @@ -5,6 +5,7 @@ do_bag_search_benkagg_by_bag_id, do_bag_search_by_bag_id, ) +from utils.coordinates import convert_polygon_to_latlng logger = logging.getLogger(__name__) @@ -99,6 +100,8 @@ def get_bag_address_data(self): self.number = found_bag_data.get("huisnummer", "") self.suffix_letter = found_bag_data.get("bag_huisletter", "") self.suffix = found_bag_data.get("bag_toevoeging", "") + # Temporarily property for type. Could be verblijfsobject (huis) or standplaats (woonboot). + self.type = found_bag_data.get("type", "verblijfsobject") centroid = found_bag_data.get("centroid", None) if centroid: @@ -108,14 +111,17 @@ def get_bag_address_data(self): def get_bag_identificatie_and_stadsdeel(self): """ Retrieves the identificatie(nummeraanduiding_id) and stadsdeel of an address by bag_id. - nummeraanduiding_id is needed for BRP. - stadsdeel is needed for filtering. + nummeraanduiding_id is needed for BRP and stadsdeel is used for filtering. + If an address has an standplaats (woonboot) instead of verblijfsobject, the coordinates + will be calculated by a polygon. """ # When moving the import to the beginning of the file, a Django error follows: # ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed. from utils.exceptions import DistrictNotFoundError - response = do_bag_search_benkagg_by_bag_id(self.bag_id) + is_boat = self.type == "standplaats" + response = do_bag_search_benkagg_by_bag_id(self.bag_id, is_boat) + adresseerbareobjecten = response.get("_embedded", {}).get( "adresseerbareobjecten", [] ) @@ -129,17 +135,27 @@ def get_bag_identificatie_and_stadsdeel(self): ), {}, ) + nummeraanduiding_id = found_bag_object.get("identificatie") if nummeraanduiding_id: self.nummeraanduiding_id = nummeraanduiding_id # Add Stadsdeel to address. district_name = found_bag_object.get("gebiedenStadsdeelNaam") + if district_name: self.district = District.objects.get_or_create(name=district_name)[0] else: raise DistrictNotFoundError(f"API benkagg bag_id: {self.bag_id}") + # Get coordinates for standplaats (woonboot). + ligplaats_geometrie = found_bag_object.get("ligplaatsGeometrie") or {} + ligplaats_coordinates = ligplaats_geometrie.get("coordinates") + if ligplaats_coordinates: + (lat, lng) = convert_polygon_to_latlng(ligplaats_coordinates) + self.lng = lng + self.lat = lat + def update_bag_data(self): self.get_bag_address_data() # Prevent a nummeraanduiding_id error while creating a case. diff --git a/app/requirements.txt b/app/requirements.txt index 592e679a1..0994d8b31 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -58,6 +58,7 @@ pycparser==2.20 # pygraphviz==1.7 PyJWT pyOpenSSL==23.2.0 +pyproj==3.6.1 pyrsistent==0.18.0 python-crontab==2.5.1 python-dateutil==2.8.2 diff --git a/app/utils/api_queries_bag.py b/app/utils/api_queries_bag.py index 5f191549a..687491d5e 100644 --- a/app/utils/api_queries_bag.py +++ b/app/utils/api_queries_bag.py @@ -10,13 +10,17 @@ @retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR)) -def do_bag_search_benkagg_by_bag_id(bag_id): +def do_bag_search_benkagg_by_bag_id(bag_id, is_boat=False): """ Search BAG identificatie (nummeraanduiding_id) and stadsdeel using an adresseertVerblijfsobjectId """ + identification_type = "adresseertVerblijfsobjectIdentificatie" + if is_boat: + identification_type = "ligplaatsIdentificatie" + address_search = requests.get( settings.BAG_API_BENKAGG_SEARCH_URL, - params={"adresseertVerblijfsobjectIdentificatie": bag_id}, + params={identification_type: bag_id}, headers=headers, timeout=30, ) diff --git a/app/utils/coordinates.py b/app/utils/coordinates.py new file mode 100644 index 000000000..404fed694 --- /dev/null +++ b/app/utils/coordinates.py @@ -0,0 +1,15 @@ +from pyproj import Transformer + + +def convert_polygon_to_latlng(coordinates): + """ + Convert polygon cooordinates like this [[[121125.385, 488125.808],[121114.701, 488135.926]]] to a classic lat and lng. + Converting EPSG::28992 (Dutch RD New) to EPSG:4326 (WGS84) + """ + transformer = Transformer.from_crs("EPSG:28992", "EPSG:4326") + + x, y = coordinates[0][0] + + lat, lng = transformer.transform(x, y) + + return lat, lng