Data source/LV.CR/data model/record/address/mapping/CDQ/transformation

From CDQ
Jump to navigation Jump to search


Part of Hierarchical relation between two concepts of the CDQ Data Model. Data source LV.CR
Mapping source The source of a data model mapping. LV.CR addressTHOROUGHFARE_VALUE, THOROUGHFARE_NUMBER, PREMISE_VALUE, ADMINISTRATIVE_AREA_VALUE
Transformation type A type of a transformation applied to a data model mapping. CUSTOM_TRANSFORMATION"CUSTOM_TRANSFORMATION" is not in the list (UPPER_CASE, LOWER_CASE, CUSTOM) of allowed values for the "Has transformation type" property.
Transformation parameter A transformation parameter is an action proccessed by a data model transformation. n/a
Description Informal and comprehensive human-readable definition of a concept. n/a

Transformation code Any code (in Python, JAVA or other) that will transform values for a given data source attributes.

from builtins import len

ADMINISTRATIVE_AREA_0_PATH = 'addresses[0].administrativeAreas[0].value'

LOCALITY_PATH = 'addresses[0].localities[0].value'


def transform(raw_data):
    splitted_address = raw_data.split(",")
    if (len(splitted_address) == 2):
        result = {LOCALITY_PATH: splitted_address[0].strip()}
        fill_thoroughfare_information(result, splitted_address[1])
        return result
    if (len(splitted_address) == 3):
        result = {ADMINISTRATIVE_AREA_0_PATH:
                   splitted_address[0].strip(),
               LOCALITY_PATH:
                   splitted_address[1].strip()}
        fill_thoroughfare_information(result, splitted_address[2])
        return result
    if (len(splitted_address) == 4):
        result = {ADMINISTRATIVE_AREA_0_PATH:
                       splitted_address[0].strip(),
               'addresses[0].administrativeAreas[1].value':
                   splitted_address[1].strip(),
               LOCALITY_PATH:
                       splitted_address[2].strip()}
        fill_thoroughfare_information(result, splitted_address[3])
        return result
    return raw_data


def fill_thoroughfare_information(result, street):
    if (is_premise(street.strip())):
        result['addresses[0].premises[0].value'] = street.strip()
    else :
        result['addresses[0].thoroughfares[0].value'] = remove_house_number(street).strip()
        house_number = parse_house_number(street.strip())
        if (house_number != None):
            result['addresses[0].thoroughfares[0].number'] = house_number.strip()

def is_premise(street):
    return street.startswith("\"") & street.endswith("\"")

def remove_house_number(street):
    index = street.lower().find("iela")
    if (index == -1):
        return street
    return street[0:index + len("iela")]


def parse_house_number(street):
    index = street.lower().find("iela")
    if (index == -1):
        return None
    return street[index + len("iela"):len(street)]