Edit on GitHub

aprs.decimaldegrees

PyDecimalDegrees - geographic coordinates conversion utility.

Copyright (C) 2006-2013 by Mateusz Łoskot mateusz@loskot.net Copyright (C) 2010-2013 by Evan Wheeler ewheeler@unicef.org

This file is part of PyDecimalDegrees module.

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

DESCRIPTION

DecimalDegrees module provides functions to convert between degrees/minutes/seconds and decimal degrees.

Original source distribution: http://mateusz.loskot.net/software/gis/pydecimaldegrees/

Inspired by Walter Mankowski's Geo::Coordinates::DecimalDegrees module for Perl, originally located in CPAN Archives: http://search.cpan.org/~waltman/Geo-Coordinates-DecimalDegrees-0.05/

doctest examples are based following coordinates: DMS: 121 8' 6" DM: 121 8.1' DD: 121.135

To run doctest units just execut this module script as follows (-v instructs Python to run script in verbose mode):

$ python decimaldegrees.py [-v]

  1"""
  2PyDecimalDegrees - geographic coordinates conversion utility.
  3
  4Copyright (C) 2006-2013 by Mateusz Łoskot <mateusz@loskot.net>
  5Copyright (C) 2010-2013 by Evan Wheeler <ewheeler@unicef.org>
  6
  7This file is part of PyDecimalDegrees module.
  8
  9This software is provided 'as-is', without any express or implied warranty.
 10In no event will the authors be held liable for any damages arising from
 11the use of this software.
 12
 13Permission is granted to anyone to use this software for any purpose,
 14including commercial applications, and to alter it and redistribute it freely,
 15subject to the following restrictions:
 161. The origin of this software must not be misrepresented; you must not
 17   claim that you wrote the original software. If you use this software
 18   in a product, an acknowledgment in the product documentation would be
 19   appreciated but is not required.
 202. Altered source versions must be plainly marked as such, and must not be
 21   misrepresented as being the original software.
 223. This notice may not be removed or altered from any source distribution.
 23
 24DESCRIPTION
 25
 26DecimalDegrees module provides functions to convert between
 27degrees/minutes/seconds and decimal degrees.
 28
 29Original source distribution:
 30http://mateusz.loskot.net/software/gis/pydecimaldegrees/
 31
 32Inspired by Walter Mankowski's Geo::Coordinates::DecimalDegrees module
 33for Perl, originally located in CPAN Archives:
 34http://search.cpan.org/~waltman/Geo-Coordinates-DecimalDegrees-0.05/
 35
 36doctest examples are based following coordinates:
 37DMS: 121 8' 6"
 38DM: 121 8.1'
 39DD: 121.135
 40
 41To run doctest units just execut this module script as follows
 42(-v instructs Python to run script in verbose mode):
 43
 44$ python decimaldegrees.py [-v]
 45
 46"""
 47
 48import decimal as libdecimal
 49from decimal import Decimal as D
 50
 51__revision__ = "$Revision: 1.1 $"
 52
 53
 54def decimal2dms(decimal_degrees):
 55    """Converts a floating point number of degrees to the equivalent
 56    number of degrees, minutes, and seconds, which are returned
 57    as a 3-element tuple of decimals. If 'decimal_degrees' is negative,
 58    only degrees (1st element of returned tuple) will be negative,
 59    minutes (2nd element) and seconds (3rd element) will always be positive.
 60
 61    Example:
 62
 63        >>> decimal2dms(121.135)
 64        (Decimal('121'), Decimal('8'), Decimal('6.000'))
 65        >>> decimal2dms(-121.135)
 66        (Decimal('-121'), Decimal('8'), Decimal('6.000'))
 67
 68    """
 69
 70    degrees = D(int(decimal_degrees))
 71    decimal_minutes = libdecimal.getcontext().multiply(
 72        (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)
 73    )
 74    minutes = D(int(decimal_minutes))
 75    seconds = libdecimal.getcontext().multiply((decimal_minutes - minutes), D(60))
 76    return (degrees, minutes, seconds)
 77
 78
 79def decimal2dm(decimal_degrees):
 80    """
 81    Converts a floating point number of degrees to the degress & minutes.
 82
 83    Returns a 2-element tuple of decimals.
 84
 85    If 'decimal_degrees' is negative, only degrees (1st element of returned
 86    tuple) will be negative, minutes (2nd element) will always be positive.
 87
 88    Example:
 89
 90        >>> decimal2dm(121.135)
 91        (Decimal('121'), Decimal('8.100'))
 92        >>> decimal2dm(-121.135)
 93        (Decimal('-121'), Decimal('8.100'))
 94
 95    """
 96    degrees = D(int(decimal_degrees))
 97
 98    minutes = libdecimal.getcontext().multiply(
 99        (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)
100    )
101
102    return (degrees, minutes)
103
104
105def dms2decimal(degrees, minutes, seconds):
106    """Converts degrees, minutes, and seconds to the equivalent
107    number of decimal degrees. If parameter 'degrees' is negative,
108    then returned decimal-degrees will also be negative.
109
110    NOTE: this method returns a decimal.Decimal
111
112    Example:
113
114        >>> dms2decimal(121, 8, 6)
115        Decimal('121.135')
116        >>> dms2decimal(-121, 8, 6)
117        Decimal('-121.135')
118
119    """
120    decimal = D(0)
121    degs = D(str(degrees))
122    mins = libdecimal.getcontext().divide(D(str(minutes)), D(60))
123    secs = libdecimal.getcontext().divide(D(str(seconds)), D(3600))
124
125    if degrees >= D(0):
126        decimal = degs + mins + secs
127    else:
128        decimal = degs - mins - secs
129
130    return libdecimal.getcontext().normalize(decimal)
131
132
133def dm2decimal(degrees, minutes):
134    """Converts degrees and minutes to the equivalent number of decimal
135    degrees. If parameter 'degrees' is negative, then returned decimal-degrees
136    will also be negative.
137
138    Example:
139
140        >>> dm2decimal(121, 8.1)
141        Decimal('121.135')
142        >>> dm2decimal(-121, 8.1)
143        Decimal('-121.135')
144
145    """
146    return dms2decimal(degrees, minutes, 0)
147
148
149def run_doctest():  # pragma: no cover
150    """Runs doctests for this module."""
151    import doctest  # pylint: disable=import-outside-toplevel
152
153    return doctest.testmod()
154
155
156if __name__ == "__main__":
157    run_doctest()  # pragma: no cover
def decimal2dms(decimal_degrees):
55def decimal2dms(decimal_degrees):
56    """Converts a floating point number of degrees to the equivalent
57    number of degrees, minutes, and seconds, which are returned
58    as a 3-element tuple of decimals. If 'decimal_degrees' is negative,
59    only degrees (1st element of returned tuple) will be negative,
60    minutes (2nd element) and seconds (3rd element) will always be positive.
61
62    Example:
63
64        >>> decimal2dms(121.135)
65        (Decimal('121'), Decimal('8'), Decimal('6.000'))
66        >>> decimal2dms(-121.135)
67        (Decimal('-121'), Decimal('8'), Decimal('6.000'))
68
69    """
70
71    degrees = D(int(decimal_degrees))
72    decimal_minutes = libdecimal.getcontext().multiply(
73        (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)
74    )
75    minutes = D(int(decimal_minutes))
76    seconds = libdecimal.getcontext().multiply((decimal_minutes - minutes), D(60))
77    return (degrees, minutes, seconds)

Converts a floating point number of degrees to the equivalent number of degrees, minutes, and seconds, which are returned as a 3-element tuple of decimals. If 'decimal_degrees' is negative, only degrees (1st element of returned tuple) will be negative, minutes (2nd element) and seconds (3rd element) will always be positive.

Example:

>>> decimal2dms(121.135)
(Decimal('121'), Decimal('8'), Decimal('6.000'))
>>> decimal2dms(-121.135)
(Decimal('-121'), Decimal('8'), Decimal('6.000'))
def decimal2dm(decimal_degrees):
 80def decimal2dm(decimal_degrees):
 81    """
 82    Converts a floating point number of degrees to the degress & minutes.
 83
 84    Returns a 2-element tuple of decimals.
 85
 86    If 'decimal_degrees' is negative, only degrees (1st element of returned
 87    tuple) will be negative, minutes (2nd element) will always be positive.
 88
 89    Example:
 90
 91        >>> decimal2dm(121.135)
 92        (Decimal('121'), Decimal('8.100'))
 93        >>> decimal2dm(-121.135)
 94        (Decimal('-121'), Decimal('8.100'))
 95
 96    """
 97    degrees = D(int(decimal_degrees))
 98
 99    minutes = libdecimal.getcontext().multiply(
100        (D(str(decimal_degrees)) - degrees).copy_abs(), D(60)
101    )
102
103    return (degrees, minutes)

Converts a floating point number of degrees to the degress & minutes.

Returns a 2-element tuple of decimals.

If 'decimal_degrees' is negative, only degrees (1st element of returned tuple) will be negative, minutes (2nd element) will always be positive.

Example:

>>> decimal2dm(121.135)
(Decimal('121'), Decimal('8.100'))
>>> decimal2dm(-121.135)
(Decimal('-121'), Decimal('8.100'))
def dms2decimal(degrees, minutes, seconds):
106def dms2decimal(degrees, minutes, seconds):
107    """Converts degrees, minutes, and seconds to the equivalent
108    number of decimal degrees. If parameter 'degrees' is negative,
109    then returned decimal-degrees will also be negative.
110
111    NOTE: this method returns a decimal.Decimal
112
113    Example:
114
115        >>> dms2decimal(121, 8, 6)
116        Decimal('121.135')
117        >>> dms2decimal(-121, 8, 6)
118        Decimal('-121.135')
119
120    """
121    decimal = D(0)
122    degs = D(str(degrees))
123    mins = libdecimal.getcontext().divide(D(str(minutes)), D(60))
124    secs = libdecimal.getcontext().divide(D(str(seconds)), D(3600))
125
126    if degrees >= D(0):
127        decimal = degs + mins + secs
128    else:
129        decimal = degs - mins - secs
130
131    return libdecimal.getcontext().normalize(decimal)

Converts degrees, minutes, and seconds to the equivalent number of decimal degrees. If parameter 'degrees' is negative, then returned decimal-degrees will also be negative.

NOTE: this method returns a decimal.Decimal

Example:

>>> dms2decimal(121, 8, 6)
Decimal('121.135')
>>> dms2decimal(-121, 8, 6)
Decimal('-121.135')
def dm2decimal(degrees, minutes):
134def dm2decimal(degrees, minutes):
135    """Converts degrees and minutes to the equivalent number of decimal
136    degrees. If parameter 'degrees' is negative, then returned decimal-degrees
137    will also be negative.
138
139    Example:
140
141        >>> dm2decimal(121, 8.1)
142        Decimal('121.135')
143        >>> dm2decimal(-121, 8.1)
144        Decimal('-121.135')
145
146    """
147    return dms2decimal(degrees, minutes, 0)

Converts degrees and minutes to the equivalent number of decimal degrees. If parameter 'degrees' is negative, then returned decimal-degrees will also be negative.

Example:

>>> dm2decimal(121, 8.1)
Decimal('121.135')
>>> dm2decimal(-121, 8.1)
Decimal('-121.135')
def run_doctest():
150def run_doctest():  # pragma: no cover
151    """Runs doctests for this module."""
152    import doctest  # pylint: disable=import-outside-toplevel
153
154    return doctest.testmod()

Runs doctests for this module.