Monday 12 November 2018

aviation_gis_tools: dealing with distance

Let's start building aviation tools from something which is quite simple and very basic: dealing with distances.

There are many units of measure for distance that are used in aviation industry: meters, feet, nautical miles and statue miles. Definition of unit of measure that will be used in our tools:
UOM_M = 'M'  # meters
UOM_KM = 'KM'  # kilometers
UOM_NM = 'NM'  # nautical miles
UOM_FEET = 'FEET'  # feet
UOM_SM = 'SM'  # statue miles
I also introduce two constants:

VALID = 'VALID'
NOT_VALID = 'NOT_VALID'
That will be used to keep information if result of some function is acceptable or not. I don't use False, or True values because result of some function might be 0. 
Before we are going to do anything with value that will be passed into script or QGIS plugin we have to check if this value is correct, distance in this case.

def check_distance2(d):
    """ Distance validation. Uses float() function to check if parameters is a number
    :param d: string, distance to validate
    :return is_valid: True if distance is valid,
                     constant NOT_VALID if distance is not valid (e.g distance is less than 0)
    """
    try:
        dist = float(d)
        if dist < 0:  # We assume that distance is >= 0
            dist = NOT_VALID
    except ValueError:
        dist = NOT_VALID
    return dist

Of course one of the basic issue is conversion distance among various units of measure. The problem of converting from any unit to any other one (e. g. meters to feet, statue miles to meters) has been solved in the following way:
  • first, we need function which converts distance from given unit to meters
def to_meters(d, from_unit):
    """ Converts distance given in specified unit to distance in meters
    :param d: float, distance in unit specified by parameter from_unit
    :param from_unit: constant unit of measure, unit of measure parameter d_unit
    :return float, distance in unit specified by parameter to_unit
    """
    if from_unit == UOM_M:
        return d
    elif from_unit == UOM_KM:
        return d * 1000
    elif from_unit == UOM_NM:
        return d * 1852
    elif from_unit == UOM_FEET:
        return d * 0.3048
    elif from_unit == UOM_SM:
        return d * 1609.344
    else:
        return NOT_VALID
  • next, we need function that converts distance from meters into specified unit of measure:
def from_meters(d, to_unit):
    """ Converts distance given in meters to distance in specified unit
    :param d: float, distance in meters
    :param to_unit: constant unit of measurement
    :return float, distance in unit specified by parameter to_unit
    """
    if to_unit == UOM_M:
        return d
    elif to_unit == UOM_KM:
        return d / 1000
    elif to_unit == UOM_NM:
        return d / 1852
    elif to_unit == UOM_FEET:
        return d / 0.3048
    elif to_unit == UOM_SM:
        return d / 1609.344
    else:
        return NOT_VALID
  • and finally function that converts distance between two units of measure
def convert_distance(d, from_unit, to_unit):
    """ Convert distance between various units
    :param d: float, distance in units specified by parameter from_unit
    :param from_unit: constant measure of units
    :param to_unit: constant measure of unit
    :return float, distance in units specified by parameter to_unit
    """
    if from_unit == to_unit:
        return d
    else:
        d_m = to_meters(d, from_unit)  # Convert to meters
        return from_meters(d_m, to_unit)  # Convert from meters

In module you can also find small, simple functions that converts from one specified unit of measure into another one. Here is an example, function that converts kilometers into nautical miles:

def km2nautical_mile(km):
    """ Converts kilometers to nautical miles
    :param km: float, value in kilometers
    :return: value in nautical miles
    """
    return km / 1.852

Source code for module aviation_distance_tools is available at:https://github.com/strpaw/aviation_gis_tools

No comments:

Post a Comment