### ===================================================================================================================
### FUNCTION: Get address data for object
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from __future__ import annotations
from typing import TYPE_CHECKING, List, Tuple
import re
# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
from viiapackage.viiaStatus import ViiaProject
### ===================================================================================================================
### 2. Helper functions for address info
### ===================================================================================================================
def _sorting_house_numbers(s):
num, letters = re.match(r'(\d*)(.*)', s).groups()
return float(num or 'inf'), letters
### ===================================================================================================================
### 3. Function to collect the information for the report on addresses of the object
### ===================================================================================================================
[docs]def viia_get_address_info(
project: ViiaProject, myviia_data_object_address: dict) -> Tuple[str, List[str], List[int], str, str]:
"""
This function will generate the information about the addresses in the object that needs to be collected for the
reporting in a specific order. Data about the object addresses is to be retrieved from MYVIIA.
Input
- project (obj): VIIA project object containing collections of fem objects and project variables.
- myviia_data_object_address (dict): Dictionary with data on the object addresses from MYVIIA.
Output
- Returns a tuple with five variables, in the following order.
- String with contraction of all the addresses of the object, according specific rules on how to combine
different addresses (if required to combine), as string.
- List with the full address added separately, but ordered according requirements, as list of strings.
- List of the dossier numbers of NCG from MYVIIA, as list of integers.
- Zip-code for all the addresses, as string. If multiple a warning is thrown and the first one is used.
- Name of the municipality for all the addresses, as string. If multiple a warning is thrown and the first one
is used.
"""
dossier_nrs = []
addresses = {}
postcode_cities = {}
# Re-arrange the data per street name and per city name
for address in myviia_data_object_address:
if address['dossiernummer_og']:
if '/' in address['dossiernummer_og']:
dossier_nrs.append(int(address['dossiernummer_og'].split('/')[-1].replace(' ', '')))
else:
dossier_nrs.append(int(address['dossiernummer_og']))
if address['straatnaam'] not in addresses.keys():
addresses.update({address['straatnaam']: {
'plaatsnaam': address['plaatsnaam'],
'postcode': address['postcode'],
'nrs': []}})
addresses[address['straatnaam']]['nrs'].append(address['huisnummer'])
if address['plaatsnaam'] not in postcode_cities.keys():
postcode_cities.update(
{address['plaatsnaam']: {'postcode': []}})
postcode_cities[address['plaatsnaam']]['postcode'].append(address['postcode'])
# Compress the numbers for single line address
address_strings = []
address_list = []
for street, street_data in addresses.items():
sorted_nrs = sorted(street_data['nrs'], key=_sorting_house_numbers)
if len(sorted_nrs) > 1:
address_strings.append(f'{street} {sorted_nrs[0]}-{sorted_nrs[-1]}')
else:
address_strings.append(f'{street} {sorted_nrs[0]}')
for nr in sorted_nrs:
address_list.append(f"{street} {nr}, {street_data['postcode']} {street_data['plaatsnaam']}")
address_string = ', '.join(address_strings)
municipality = None
zip_code = None
if len(postcode_cities) > 1:
project.write_log(
f"WARNING: The object contains addresses in multiple cities. Reporting function currently "
f"cannot support this. Please fill in the addresses manually in the report.")
else:
for key, postcode_city in postcode_cities.items():
municipality = key
zip_code = set(postcode_city['postcode'])
# Combine postcodes if there are multiple
if len(zip_code) > 1:
zip_code = ', '.join(zip_code)
project.write_log(
f"WARNING: The object contains multiple postcodes. {zip_code} is used in the "
f"report. Please check in the report.")
else:
zip_code = list(zip_code)[0]
return address_string, address_list, dossier_nrs, zip_code, municipality
### ===================================================================================================================
### 4. End of script
### ===================================================================================================================