diff --git a/.env b/.env index e93147cc9..819bf7959 100644 --- a/.env +++ b/.env @@ -14,7 +14,7 @@ LOGGING_LEVEL=WARNING # To prevent flooding the logging in local development. De SECRET_KEY_TOP_ZAKEN=SECRET_KEY_TOP_ZAKEN SECRET_KEY_TON_ZAKEN=SECRET_KEY_TON_ZAKEN BELASTING_API_URL=https://api-acc.belastingen.centric.eu/bel/inn/afne/vora/v1/vorderingenidentificatienummer/ -BAG_API_SEARCH_URL=https://api.data.amsterdam.nl/atlas/search/adres/ +BAG_API_SEARCH_URL=https://api.pdok.nl/bzk/locatieserver/search/v3_1/free BAG_API_NUMMERAANDUIDING_SEARCH_URL=https://api.data.amsterdam.nl/v1/bag/nummeraanduidingen/ BAG_API_BENKAGG_SEARCH_URL=https://api.data.amsterdam.nl/v1/benkagg/adresseerbareobjecten/ DECOS_JOIN_USERNAME=ZakenTop diff --git a/app/apps/addresses/models.py b/app/apps/addresses/models.py index 2c260b9c0..30edc1f90 100644 --- a/app/apps/addresses/models.py +++ b/app/apps/addresses/models.py @@ -81,8 +81,7 @@ def get_or_create_by_bag_id(bag_id): def get_bag_address_data(self): bag_search_response = do_bag_search_by_bag_id(self.bag_id) - bag_search_results = bag_search_response.get("results", []) - + bag_search_results = bag_search_response.get("response", {}).get("docs", []) if bag_search_results: # A BAG search will return an array with 1 or more results. # There could be a "Nevenadres" so check addresses for "Hoofdadres". @@ -98,12 +97,11 @@ def get_bag_address_data(self): self.postal_code = found_bag_data.get("postcode", "") self.street_name = found_bag_data.get("straatnaam", "") 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) + self.suffix_letter = found_bag_data.get("huisletter", "") + self.suffix = found_bag_data.get("huisnummertoevoeging", "") + self.nummeraanduiding_id = found_bag_data.get("nummeraanduiding_id", "") + centroid_string = found_bag_data.get("centroide_ll", None) + centroid = self._parse_centroid(centroid_string) if centroid: self.lng = centroid[0] self.lat = centroid[1] @@ -133,18 +131,7 @@ def get_bag_identificatie_and_stadsdeel(self): {}, ) - nummeraanduiding_id = found_bag_object.get("identificatie") - if nummeraanduiding_id: - self.nummeraanduiding_id = nummeraanduiding_id - district_name = found_bag_object.get("gebiedenStadsdeelNaam") - - if district_name: - self.district = District.objects.get_or_create(name=district_name)[0] - - # 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 @@ -168,3 +155,15 @@ def save(self, *args, **kwargs): if not self.bag_id or not self.nummeraanduiding_id: self.update_bag_data() return super().save(*args, **kwargs) + + def _parse_centroid(self, centroid): + # Check if the string starts with 'POINT(' and ends with ')' + if centroid.startswith("POINT(") and centroid.endswith(")"): + # Remove the 'POINT(' at the beginning and ')' at the end + coordinates_str = centroid[6:-1] + # Split the string by space to get the individual numbers + coordinates = coordinates_str.split() + # Convert the string numbers to float and return as a list + return [float(coordinates[0]), float(coordinates[1])] + else: + raise ValueError("Input string is not in the correct format.") diff --git a/app/apps/health/health_checks.py b/app/apps/health/health_checks.py index 59ba36427..280762b6d 100644 --- a/app/apps/health/health_checks.py +++ b/app/apps/health/health_checks.py @@ -76,7 +76,7 @@ class BAGAtlasServiceCheck(APIServiceCheckBackend): """ critical_service = True - api_url = settings.BAG_API_SEARCH_URL + api_url = f"{settings.BAG_API_SEARCH_URL}?q={settings.BAG_ID_AMSTEL_1}" verbose_name = "BAG Atlas" diff --git a/app/config/settings.py b/app/config/settings.py index c05f25298..94006647d 100644 --- a/app/config/settings.py +++ b/app/config/settings.py @@ -334,13 +334,9 @@ def filter_traces(envelope): # BAG Atlas BAG_API_SEARCH_URL = os.getenv( - "BAG_API_SEARCH_URL", "https://api.data.amsterdam.nl/atlas/search/adres/" -) -# BAG Nummeraanduidingen -BAG_API_NUMMERAANDUIDING_SEARCH_URL = os.getenv( - "BAG_API_NUMMERAANDUIDING_SEARCH_URL", - "https://api.data.amsterdam.nl/v1/bag/nummeraanduidingen/", + "BAG_API_SEARCH_URL", "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free" ) + # BAG benkagg for nummeraanduidingen and stadsdeel BAG_API_BENKAGG_SEARCH_URL = os.getenv( "BAG_API_BENKAGG_SEARCH_URL", diff --git a/app/utils/api_queries_bag.py b/app/utils/api_queries_bag.py index 687491d5e..3a99f041f 100644 --- a/app/utils/api_queries_bag.py +++ b/app/utils/api_queries_bag.py @@ -33,8 +33,14 @@ def do_bag_search_by_bag_id(bag_id): Search BAG using a BWV 'landelijk BAG ID' """ address_search = requests.get( - settings.BAG_API_SEARCH_URL, params={"q": bag_id}, timeout=5 + settings.BAG_API_SEARCH_URL, + params={ + "q": bag_id, + "fq": f"gemeentenaam:(amsterdam) AND (type:adres) AND (adresseerbaarobject_id: {bag_id}) AND (adrestype: hoofdadres)", + }, + timeout=5, ) + return address_search.json()