1. ===================
    
  2. Measurement Objects
    
  3. ===================
    
  4. 
    
  5. .. module:: django.contrib.gis.measure
    
  6.     :synopsis: GeoDjango's distance and area measurement objects.
    
  7. 
    
  8. The :mod:`django.contrib.gis.measure` module contains objects that allow
    
  9. for convenient representation of distance and area units of measure. [#]_
    
  10. Specifically, it implements two objects, :class:`Distance` and
    
  11. :class:`Area` -- both of which may be accessed via the
    
  12. :class:`D` and :class:`A` convenience aliases, respectively.
    
  13. 
    
  14. Example
    
  15. =======
    
  16. 
    
  17. :class:`Distance` objects may be instantiated using a keyword argument indicating the
    
  18. context of the units.  In the example below, two different distance objects are
    
  19. instantiated in units of kilometers (``km``) and miles (``mi``)::
    
  20. 
    
  21.     >>> from django.contrib.gis.measure import D, Distance
    
  22.     >>> d1 = Distance(km=5)
    
  23.     >>> print(d1)
    
  24.     5.0 km
    
  25.     >>> d2 = D(mi=5) # `D` is an alias for `Distance`
    
  26.     >>> print(d2)
    
  27.     5.0 mi
    
  28. 
    
  29. For conversions, access the preferred unit attribute to get a converted
    
  30. distance quantity::
    
  31. 
    
  32.     >>> print(d1.mi) # Converting 5 kilometers to miles
    
  33.     3.10685596119
    
  34.     >>> print(d2.km) # Converting 5 miles to kilometers
    
  35.     8.04672
    
  36. 
    
  37. Moreover, arithmetic operations may be performed between the distance
    
  38. objects::
    
  39. 
    
  40.     >>> print(d1 + d2) # Adding 5 miles to 5 kilometers
    
  41.     13.04672 km
    
  42.     >>> print(d2 - d1) # Subtracting 5 kilometers from 5 miles
    
  43.     1.89314403881 mi
    
  44. 
    
  45. Two :class:`Distance` objects multiplied together will yield an :class:`Area`
    
  46. object, which uses squared units of measure::
    
  47. 
    
  48.     >>> a = d1 * d2 # Returns an Area object.
    
  49.     >>> print(a)
    
  50.     40.2336 sq_km
    
  51. 
    
  52. To determine what the attribute abbreviation of a unit is, the ``unit_attname``
    
  53. class method may be used::
    
  54. 
    
  55.     >>> print(Distance.unit_attname('US Survey Foot'))
    
  56.     survey_ft
    
  57.     >>> print(Distance.unit_attname('centimeter'))
    
  58.     cm
    
  59. 
    
  60. .. _supported_units:
    
  61. 
    
  62. Supported units
    
  63. ===============
    
  64. 
    
  65. =================================  ========================================
    
  66. Unit Attribute                     Full name or alias(es)
    
  67. =================================  ========================================
    
  68. ``km``                             Kilometre, Kilometer
    
  69. ``mi``                             Mile
    
  70. ``m``                              Meter, Metre
    
  71. ``yd``                             Yard
    
  72. ``ft``                             Foot, Foot (International)
    
  73. ``survey_ft``                      U.S. Foot, US survey foot
    
  74. ``inch``                           Inches
    
  75. ``cm``                             Centimeter
    
  76. ``mm``                             Millimetre, Millimeter
    
  77. ``um``                             Micrometer, Micrometre
    
  78. ``british_ft``                     British foot (Sears 1922)
    
  79. ``british_yd``                     British yard (Sears 1922)
    
  80. ``british_chain_sears``            British chain (Sears 1922)
    
  81. ``indian_yd``                      Indian yard, Yard (Indian)
    
  82. ``sears_yd``                       Yard (Sears)
    
  83. ``clarke_ft``                      Clarke's Foot
    
  84. ``chain``                          Chain
    
  85. ``chain_benoit``                   Chain (Benoit)
    
  86. ``chain_sears``                    Chain (Sears)
    
  87. ``british_chain_benoit``           British chain (Benoit 1895 B)
    
  88. ``british_chain_sears_truncated``  British chain (Sears 1922 truncated)
    
  89. ``gold_coast_ft``                  Gold Coast foot
    
  90. ``link``                           Link
    
  91. ``link_benoit``                    Link (Benoit)
    
  92. ``link_sears``                     Link (Sears)
    
  93. ``clarke_link``                    Clarke's link
    
  94. ``fathom``                         Fathom
    
  95. ``rod``                            Rod
    
  96. ``furlong``                        Furlong, Furrow Long
    
  97. ``nm``                             Nautical Mile
    
  98. ``nm_uk``                          Nautical Mile (UK)
    
  99. ``german_m``                       German legal metre
    
  100. =================================  ========================================
    
  101. 
    
  102. .. note::
    
  103. 
    
  104.     :class:`Area` attributes are the same as :class:`Distance` attributes,
    
  105.     except they are prefixed with ``sq_`` (area units are square in nature).
    
  106.     For example, ``Area(sq_m=2)`` creates an :class:`Area` object
    
  107.     representing two square meters.
    
  108. 
    
  109. Measurement API
    
  110. ===============
    
  111. 
    
  112. ``Distance``
    
  113. ------------
    
  114. 
    
  115. .. class:: Distance(**kwargs)
    
  116. 
    
  117.     To initialize a distance object, pass in a keyword corresponding to the
    
  118.     desired :ref:`unit attribute name <supported_units>` set with desired
    
  119.     value. For example, the following creates a distance object representing 5
    
  120.     miles::
    
  121. 
    
  122.         >>> dist = Distance(mi=5)
    
  123. 
    
  124.     .. method:: __getattr__(unit_att)
    
  125. 
    
  126.     Returns the distance value in units corresponding to the given unit
    
  127.     attribute. For example::
    
  128. 
    
  129.         >>> print(dist.km)
    
  130.         8.04672
    
  131. 
    
  132.     .. classmethod:: unit_attname(unit_name)
    
  133. 
    
  134.     Returns the distance unit attribute name for the given full unit name. For
    
  135.     example::
    
  136. 
    
  137.         >>> Distance.unit_attname('Mile')
    
  138.         'mi'
    
  139. 
    
  140. .. class:: D
    
  141. 
    
  142.     Alias for :class:`Distance` class.
    
  143. 
    
  144. ``Area``
    
  145. --------
    
  146. 
    
  147. .. class:: Area(**kwargs)
    
  148. 
    
  149.     To initialize an area object, pass in a keyword corresponding to the
    
  150.     desired :ref:`unit attribute name <supported_units>` set with desired
    
  151.     value. For example, the following creates an area object representing 5
    
  152.     square miles::
    
  153. 
    
  154.         >>> a = Area(sq_mi=5)
    
  155. 
    
  156.     .. method:: __getattr__(unit_att)
    
  157. 
    
  158.     Returns the area value in units corresponding to the given unit attribute.
    
  159.     For example::
    
  160. 
    
  161.         >>> print(a.sq_km)
    
  162.         12.949940551680001
    
  163. 
    
  164.     .. classmethod:: unit_attname(unit_name)
    
  165. 
    
  166.     Returns the area unit attribute name for the given full unit name. For
    
  167.     example::
    
  168. 
    
  169.         >>> Area.unit_attname('Kilometer')
    
  170.         'sq_km'
    
  171. 
    
  172. .. class:: A
    
  173. 
    
  174.     Alias for :class:`Area` class.
    
  175. 
    
  176. .. rubric:: Footnotes
    
  177. .. [#] `Robert Coup <https://koordinates.com/>`_ is the initial author of the measure objects,
    
  178.        and was inspired by Brian Beck's work in `geopy <https://github.com/geopy/geopy/>`_
    
  179.        and Geoff Biggs' PhD work on dimensioned units for robotics.