Tuesday 17 September 2019

Dealing with obstacles data stored in CSV files (2) - validating parsed data

Data should be checked - e. g. coordinates have 'sense' before inserting it into database.
In this post I am  going to write method which checks data in terms of constraint imposed by our model of obstacle database.

Code is quite simple - if ... else statements to check if each value for given attribute meets its constraints and rules which results from the adopted model of the obstacle database.

I use CoordinatesPair class from previous posts to check if coordinates are valid.

Method is called check_paresed_data and returns tuple that contains two items:

  • result: might be True or False, depends on the result of validation
  • err_msg: string with reason why attribute or attributes did not pass the validation

Here is the check_parsed_data mtheod code:


def check_parsed_data(self):
    """ Checks if parsed data is correct """
    result = True
    err_msg = ''

    # Check if AMSL and AGL are numbers or is not an empty string
    if self._obstacle_data['AMSL'].strip() == '':
        result = False
        err_msg += 'AMSL is required'
    else:
        try:
            float(self._obstacle_data['AMSL'])
        except ValueError:
            result = False
            err_msg += 'AMSL value must be a number'

    if self._obstacle_data['AGL'].strip() == '':
        result = False
        err_msg += 'AGL is required'
    else:
        try:
            float(self._obstacle_data['AGL'])
        except ValueError:
            result = False
            err_msg += 'AGL value must be a number'

    # Check if obstacle type is given
    if self._obstacle_data['TYPE'] == '':
        result = False
        err_msg += 'Obstacle type can not be empty'

    # Check if latitude and longitude are correct
    coord = CoordinatesPair(self._obstacle_data['LAT_SRC'],
                            self._obstacle_data['LON_SRC'])

    if coord.is_valid:
        self._obstacle_data['LAT_DD'] = coord.lat_dd
        self._obstacle_data['LON_DD'] = coord.lon_dd
    else:
        result = False
        err_msg += coord.err_msg
    return result, err_msg



No comments:

Post a Comment