Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

116361 #1241

Merged
merged 1 commit into from
Jul 18, 2024
Merged

116361 #1241

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions app/apps/addresses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand All @@ -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", []
)
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/utils/api_queries_bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
15 changes: 15 additions & 0 deletions app/utils/coordinates.py
Original file line number Diff line number Diff line change
@@ -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
Loading